Skip to content
Myr

test_context.py

This file

Path

tests/test_context.py

205 lines · 4.3 KiB · Python

Last changed by

Pop the context after closing and not before. This fixes #412Armin Ronacherbefore adoption851ce7f

History of this file

Take it

The walk stopped after 200 commits: lines older than da0132f point at it.

Added a test for ignoring of unknown options.Armin Ronacherbefore adoptionda0132f

lines 1–112why, one step back
1# -*- coding: utf-8 -*-
2import click345def test_ensure_context_objects(runner):6 class Foo(object):7 def __init__(self):8 self.title = 'default'910 pass_foo = click.make_pass_decorator(Foo, ensure=True)1112 @click.group()13 @pass_foo14 def cli(foo):15 pass1617 @cli.command()18 @pass_foo19 def test(foo):20 click.echo(foo.title)2122 result = runner.invoke(cli, ['test'])23 assert not result.exception24 assert result.output == 'default\n'252627def test_get_context_objects(runner):28 class Foo(object):29 def __init__(self):30 self.title = 'default'3132 pass_foo = click.make_pass_decorator(Foo, ensure=True)3334 @click.group()35 @click.pass_context36 def cli(ctx):37 ctx.obj = Foo()38 ctx.obj.title = 'test'3940 @cli.command()41 @pass_foo42 def test(foo):43 click.echo(foo.title)4445 result = runner.invoke(cli, ['test'])46 assert not result.exception47 assert result.output == 'test\n'484950def test_get_context_objects_no_ensuring(runner):51 class Foo(object):52 def __init__(self):53 self.title = 'default'5455 pass_foo = click.make_pass_decorator(Foo)5657 @click.group()58 @click.pass_context59 def cli(ctx):60 ctx.obj = Foo()61 ctx.obj.title = 'test'6263 @cli.command()64 @pass_foo65 def test(foo):66 click.echo(foo.title)6768 result = runner.invoke(cli, ['test'])69 assert not result.exception70 assert result.output == 'test\n'717273def test_get_context_objects_missing(runner):74 class Foo(object):75 pass7677 pass_foo = click.make_pass_decorator(Foo)7879 @click.group()80 @click.pass_context81 def cli(ctx):82 pass8384 @cli.command()85 @pass_foo86 def test(foo):87 click.echo(foo.title)8889 result = runner.invoke(cli, ['test'])90 assert result.exception is not None91 assert isinstance(result.exception, RuntimeError)92 assert "Managed to invoke callback without a context object " \93 "of type 'Foo' existing" in str(result.exception)949596def test_multi_enter(runner):97 called = []9899 @click.command()100 @click.pass_context101 def cli(ctx):102 def callback():103 called.append(True)104 ctx.call_on_close(callback)105106 with ctx:107 pass108 assert not called109110 result = runner.invoke(cli, [])111 assert result.exception is None112 assert called == [True]

Merge pull request #379 from mitsuhiko/feature/global-ctxArmin Ronacherbefore adoption022fc73

lines 113–125why, one step back
113
114115def test_global_context_object(runner):116 @click.command()117 @click.pass_context118 def cli(ctx):119 assert click.get_current_context() is ctx120 ctx.obj = 'FOOBAR'121 assert click.get_current_context().obj == 'FOOBAR'122123 assert click.get_current_context(silent=True) is None124 runner.invoke(cli, [], catch_exceptions=False)125 assert click.get_current_context(silent=True) is None

Added Context.metaArmin Ronacherbefore adoption86facdb

lines 126–144why, one step back
126
127128def test_context_meta(runner):129 LANG_KEY = __name__ + '.lang'130131 def set_language(value):132 click.get_current_context().meta[LANG_KEY] = value133134 def get_language():135 return click.get_current_context().meta.get(LANG_KEY, 'en_US')136137 @click.command()138 @click.pass_context139 def cli(ctx):140 assert get_language() == 'en_US'141 set_language('de_DE')142 assert get_language() == 'de_DE'143144 runner.invoke(cli, [], catch_exceptions=False)

Added Context.scopeArmin Ronacherbefore adoption8e31125

lines 145–160why, one step back
145
146147def test_context_pushing():148 rv = []149150 @click.command()151 def cli():152 pass153154 ctx = click.Context(cli)155156 @ctx.call_on_close157 def test_callback():158 rv.append(42)159160 with ctx.scope(cleanup=False):

More defensive for scope depth managementArmin Ronacherbefore adoptionfe2f525

lines 161–162why, one step back
161 # Internal
162 assert ctx._depth == 2

Added Context.scopeArmin Ronacherbefore adoption8e31125

lines 163–166why, one step back
163
164 assert rv == []165166 with ctx.scope():

More defensive for scope depth managementArmin Ronacherbefore adoptionfe2f525

lines 167–168why, one step back
167 # Internal
168 assert ctx._depth == 1

Added Context.scopeArmin Ronacherbefore adoption8e31125

lines 169–170why, one step back
169
170 assert rv == [42]

Merge branch '5.x-maintenance'Markus Unterwaditzerbefore adoption41cb9c9

lines 171–186why, one step back
171
172173def test_pass_obj(runner):174 @click.group()175 @click.pass_context176 def cli(ctx):177 ctx.obj = 'test'178179 @cli.command()180 @click.pass_obj181 def test(obj):182 click.echo(obj)183184 result = runner.invoke(cli, ['test'])185 assert not result.exception186 assert result.output == 'test\n'

Pop the context after closing and not before. This fixes #412Armin Ronacherbefore adoption851ce7f

lines 187–205why, one step back
187
188189def test_close_before_pop(runner):190 called = []191192 @click.command()193 @click.pass_context194 def cli(ctx):195 ctx.obj = 'test'196 @ctx.call_on_close197 def foo():198 assert click.get_current_context().obj == 'test'199 called.append(True)200 click.echo('aha!')201202 result = runner.invoke(cli, [])203 assert not result.exception204 assert result.output == 'aha!\n'205 assert called == [True]