test_context.py
This file
Path
tests/test_context.py
170 lines · 3.6 KiB · Python
Last changed by
More defensive for scope depth managementArmin Ronacherbefore adoptionfe2f525
History of this fileTake it
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]113114115def 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 None126127128def 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)145146147def 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):161 # Internal162 assert ctx._depth == 2163164 assert rv == []165166 with ctx.scope():167 # Internal168 assert ctx._depth == 1169170 assert rv == [42]