Skip to content
Myr

test_context.py

This file

Path

tests/test_context.py

258 lines · 5.5 KiB · Python

Last changed by

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

History of this file

Take it

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 1–195why, 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]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]171172173def 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'187188189def test_close_before_pop(runner):190 called = []191192 @click.command()193 @click.pass_context194 def cli(ctx):195 ctx.obj = 'test'

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 196why, one step back
196

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 197–206why, one step back
197 @ctx.call_on_close
198 def foo():199 assert click.get_current_context().obj == 'test'200 called.append(True)201 click.echo('aha!')202203 result = runner.invoke(cli, [])204 assert not result.exception205 assert result.output == 'aha!\n'206 assert called == [True]

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

lines 207–242why, one step back
207
208209def test_make_pass_decorator_args(runner):210 """211 Test to check that make_pass_decorator doesn't consume arguments based on212 invocation order.213 """214 class Foo(object):215 title = 'foocmd'216217 pass_foo = click.make_pass_decorator(Foo)218219 @click.group()220 @click.pass_context221 def cli(ctx):222 ctx.obj = Foo()223224 @cli.command()225 @click.pass_context226 @pass_foo227 def test1(foo, ctx):228 click.echo(foo.title)229230 @cli.command()231 @pass_foo232 @click.pass_context233 def test2(ctx, foo):234 click.echo(foo.title)235236 result = runner.invoke(cli, ['test1'])237 assert not result.exception238 assert result.output == 'foocmd\n'239240 result = runner.invoke(cli, ['test2'])241 assert not result.exception242 assert result.output == 'foocmd\n'

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 243–258why, one step back
243
244245def test_exit_not_standalone():246 @click.command()247 @click.pass_context248 def cli(ctx):249 ctx.exit(1)250251 assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 1252253 @click.command()254 @click.pass_context255 def cli(ctx):256 ctx.exit(0)257258 assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 0