test_context.py
This file
Path
tests/test_context.py
367 lines · 8.2 KiB · Python
Last changed by
Merge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31
History of this fileTake it
The walk stopped after 200 commits: lines older than 8bcd79a point at it.
Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7
lines 1–2why, one step back1from contextlib import contextmanager2Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 3why, one step back3import pytestMerge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 4why, one step back4Merge branch '7.x'David Lordbefore adoption7239794
lines 5–6why, one step back5import click6from click.core import ParameterSourceMerge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31
line 7why, one step back7from click.decorators import pass_meta_keyMerge branch '7.x'David Lordbefore adoption7239794
lines 8–9why, one step back89Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 10why, one step back10def test_ensure_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 11why, one step back11 class Foo:Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 12why, one step back12 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 13why, one step back13 self.title = "default"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 14–26why, one step back1415 pass_foo = click.make_pass_decorator(Foo, ensure=True)1617 @click.group()18 @pass_foo19 def cli(foo):20 pass2122 @cli.command()23 @pass_foo24 def test(foo):25 click.echo(foo.title)26Merge branch '7.x'David Lordbefore adoption7239794
line 27why, one step back27 result = runner.invoke(cli, ["test"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 28why, one step back28 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 29why, one step back29 assert result.output == "default\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 30–32why, one step back303132def test_get_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 33why, one step back33 class Foo:Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 34why, one step back34 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 35why, one step back35 self.title = "default"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 36–42why, one step back3637 pass_foo = click.make_pass_decorator(Foo, ensure=True)3839 @click.group()40 @click.pass_context41 def cli(ctx):42 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 43why, one step back43 ctx.obj.title = "test"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 44–49why, one step back4445 @cli.command()46 @pass_foo47 def test(foo):48 click.echo(foo.title)49Merge branch '7.x'David Lordbefore adoption7239794
line 50why, one step back50 result = runner.invoke(cli, ["test"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 51why, one step back51 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 52why, one step back52 assert result.output == "test\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 53–55why, one step back535455def test_get_context_objects_no_ensuring(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 56why, one step back56 class Foo:Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 57why, one step back57 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 58why, one step back58 self.title = "default"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 59–65why, one step back5960 pass_foo = click.make_pass_decorator(Foo)6162 @click.group()63 @click.pass_context64 def cli(ctx):65 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 66why, one step back66 ctx.obj.title = "test"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 67–72why, one step back6768 @cli.command()69 @pass_foo70 def test(foo):71 click.echo(foo.title)72Merge branch '7.x'David Lordbefore adoption7239794
line 73why, one step back73 result = runner.invoke(cli, ["test"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 74why, one step back74 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 75why, one step back75 assert result.output == "test\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 76–78why, one step back767778def test_get_context_objects_missing(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 79why, one step back79 class Foo:Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 80–93why, one step back80 pass8182 pass_foo = click.make_pass_decorator(Foo)8384 @click.group()85 @click.pass_context86 def cli(ctx):87 pass8889 @cli.command()90 @pass_foo91 def test(foo):92 click.echo(foo.title)93Merge branch '7.x'David Lordbefore adoption7239794
line 94why, one step back94 result = runner.invoke(cli, ["test"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 95–96why, one step back95 assert result.exception is not None96 assert isinstance(result.exception, RuntimeError)Merge branch '7.x'David Lordbefore adoption7239794
lines 97–100why, one step back97 assert (98 "Managed to invoke callback without a context object of type"99 " 'Foo' existing" in str(result.exception)100 )Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 101–110why, one step back101102103def test_multi_enter(runner):104 called = []105106 @click.command()107 @click.pass_context108 def cli(ctx):109 def callback():110 called.append(True)Merge branch '7.x'David Lordbefore adoption7239794
line 111why, one step back111Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 112–127why, one step back112 ctx.call_on_close(callback)113114 with ctx:115 pass116 assert not called117118 result = runner.invoke(cli, [])119 assert result.exception is None120 assert called == [True]121122123def test_global_context_object(runner):124 @click.command()125 @click.pass_context126 def cli(ctx):127 assert click.get_current_context() is ctxMerge branch '7.x'David Lordbefore adoption7239794
lines 128–129why, one step back128 ctx.obj = "FOOBAR"129 assert click.get_current_context().obj == "FOOBAR"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 130–136why, one step back130131 assert click.get_current_context(silent=True) is None132 runner.invoke(cli, [], catch_exceptions=False)133 assert click.get_current_context(silent=True) is None134135136def test_context_meta(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 137why, one step back137 LANG_KEY = f"{__name__}.lang"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 138–142why, one step back138139 def set_language(value):140 click.get_current_context().meta[LANG_KEY] = value141142 def get_language():Merge branch '7.x'David Lordbefore adoption7239794
line 143why, one step back143 return click.get_current_context().meta.get(LANG_KEY, "en_US")Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 144–147why, one step back144145 @click.command()146 @click.pass_context147 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 148–150why, one step back148 assert get_language() == "en_US"149 set_language("de_DE")150 assert get_language() == "de_DE"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 151–152why, one step back151152 runner.invoke(cli, [], catch_exceptions=False)Merge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31
lines 153–174why, one step back153154155def test_make_pass_meta_decorator(runner):156 @click.group()157 @click.pass_context158 def cli(ctx):159 ctx.meta["value"] = "good"160161 @cli.command()162 @pass_meta_key("value")163 def show(value):164 return value165166 result = runner.invoke(cli, ["show"], standalone_mode=False)167 assert result.return_value == "good"168169170def test_make_pass_meta_decorator_doc():171 pass_value = pass_meta_key("value")172 assert "the 'value' key from :attr:`click.Context.meta`" in pass_value.__doc__173 pass_value = pass_meta_key("value", doc_description="the test value")174 assert "passes the test value" in pass_value.__doc__Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 175–206why, one step back175176177def test_context_pushing():178 rv = []179180 @click.command()181 def cli():182 pass183184 ctx = click.Context(cli)185186 @ctx.call_on_close187 def test_callback():188 rv.append(42)189190 with ctx.scope(cleanup=False):191 # Internal192 assert ctx._depth == 2193194 assert rv == []195196 with ctx.scope():197 # Internal198 assert ctx._depth == 1199200 assert rv == [42]201202203def test_pass_obj(runner):204 @click.group()205 @click.pass_context206 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 207why, one step back207 ctx.obj = "test"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 208–213why, one step back208209 @cli.command()210 @click.pass_obj211 def test(obj):212 click.echo(obj)213Merge branch '7.x'David Lordbefore adoption7239794
line 214why, one step back214 result = runner.invoke(cli, ["test"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 215why, one step back215 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 216why, one step back216 assert result.output == "test\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 217–224why, one step back217218219def test_close_before_pop(runner):220 called = []221222 @click.command()223 @click.pass_context224 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 225why, one step back225 ctx.obj = "test"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 226–228why, one step back226227 @ctx.call_on_close228 def foo():Merge branch '7.x'David Lordbefore adoption7239794
line 229why, one step back229 assert click.get_current_context().obj == "test"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 230–231why, one step back230 called.append(True)231Merge branch '7.x'David Lordbefore adoption7239794
lines 232–233why, one step back232 click.echo("aha!")233Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 234–235why, one step back234 result = runner.invoke(cli, [])235 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 236why, one step back236 assert result.output == "aha!\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 237why, one step back237 assert called == [True]Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7
lines 238–253why, one step back238239240def test_with_resource():241 @contextmanager242 def manager():243 val = [1]244 yield val245 val[0] = 0246247 ctx = click.Context(click.Command("test"))248249 with ctx.scope():250 rv = ctx.with_resource(manager())251 assert rv[0] == 1252253 assert rv == [0]Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 254–260why, one step back254255256def test_make_pass_decorator_args(runner):257 """258 Test to check that make_pass_decorator doesn't consume arguments based on259 invocation order.260 """Merge branch '7.x'David Lordbefore adoption7239794
line 261why, one step back261Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 262why, one step back262 class Foo:Merge branch '7.x'David Lordbefore adoption7239794
line 263why, one step back263 title = "foocmd"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 264–283why, one step back264265 pass_foo = click.make_pass_decorator(Foo)266267 @click.group()268 @click.pass_context269 def cli(ctx):270 ctx.obj = Foo()271272 @cli.command()273 @click.pass_context274 @pass_foo275 def test1(foo, ctx):276 click.echo(foo.title)277278 @cli.command()279 @pass_foo280 @click.pass_context281 def test2(ctx, foo):282 click.echo(foo.title)283Merge branch '7.x'David Lordbefore adoption7239794
line 284why, one step back284 result = runner.invoke(cli, ["test1"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 285why, one step back285 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 286why, one step back286 assert result.output == "foocmd\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 287why, one step back287Merge branch '7.x'David Lordbefore adoption7239794
line 288why, one step back288 result = runner.invoke(cli, ["test2"])Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
line 289why, one step back289 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 assert result.output == "foocmd\n"Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 291–292why, one step back291292Merge pull request #1566 from achimnol/fix/show-default-propagationDavid Lordbefore adoption4ae204e
lines 293–306why, one step back293def test_propagate_show_default_setting(runner):294 """A context's ``show_default`` setting defaults to the value from295 the parent context.296 """297 group = click.Group(298 commands={299 "sub": click.Command("sub", params=[click.Option(["-a"], default="a")]),300 },301 context_settings={"show_default": True},302 )303 result = runner.invoke(group, ["sub", "--help"])304 assert "[default: a]" in result.output305306Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 307–312why, one step back307def test_exit_not_standalone():308 @click.command()309 @click.pass_context310 def cli(ctx):311 ctx.exit(1)312Merge branch '7.x'David Lordbefore adoption7239794
line 313why, one step back313 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1Merge remote-tracking branch 'origin/6.x-maintenance'David Lordbefore adoption8bcd79a
lines 314–319why, one step back314315 @click.command()316 @click.pass_context317 def cli(ctx):318 ctx.exit(0)319Merge branch '7.x'David Lordbefore adoption7239794
line 320why, one step back320 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 321why, one step back321Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 322why, one step back322Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
lines 323–359why, one step back323@pytest.mark.parametrize(324 ("option_args", "invoke_args", "expect"),325 [326 pytest.param({}, {}, ParameterSource.DEFAULT, id="default"),327 pytest.param(328 {},329 {"default_map": {"option": 1}},330 ParameterSource.DEFAULT_MAP,331 id="default_map",332 ),333 pytest.param(334 {},335 {"args": ["-o", "1"]},336 ParameterSource.COMMANDLINE,337 id="commandline short",338 ),339 pytest.param(340 {},341 {"args": ["--option", "1"]},342 ParameterSource.COMMANDLINE,343 id="commandline long",344 ),345 pytest.param(346 {},347 {"auto_envvar_prefix": "TEST", "env": {"TEST_OPTION": "1"}},348 ParameterSource.ENVIRONMENT,349 id="environment auto",350 ),351 pytest.param(352 {"envvar": "NAME"},353 {"env": {"NAME": "1"}},354 ParameterSource.ENVIRONMENT,355 id="environment manual",356 ),357 ],358)359def test_parameter_source(runner, option_args, invoke_args, expect):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 360–361why, one step back360 @click.command()361 @click.pass_contextMerge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
line 362why, one step back362 @click.option("-o", "--option", default=1, **option_args)Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 363why, one step back363 def cli(ctx, option):Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
line 364why, one step back364 return ctx.get_parameter_source("option")Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 365why, one step back365Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
lines 366–367why, one step back366 rv = runner.invoke(cli, standalone_mode=False, **invoke_args)367 assert rv.return_value == expect