test_context.py
This file
Path
tests/test_context.py
344 lines · 7.6 KiB · Python
Last changed by
Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
History of this fileTake it
The walk stopped after 200 commits: lines older than a43b947 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 pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 4why, one step back4Merge branch '7.x'David Lordbefore adoption7239794
lines 5–8why, one step back5import click6from click.core import ParameterSource78Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 9why, one step back9def test_ensure_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 10why, one step back10 class Foo:Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 11why, one step back11 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 12why, one step back12 self.title = "default"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 13–25why, one step back1314 pass_foo = click.make_pass_decorator(Foo, ensure=True)1516 @click.group()17 @pass_foo18 def cli(foo):19 pass2021 @cli.command()22 @pass_foo23 def test(foo):24 click.echo(foo.title)25Merge branch '7.x'David Lordbefore adoption7239794
line 26why, one step back26 result = runner.invoke(cli, ["test"])Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 27why, one step back27 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 28why, one step back28 assert result.output == "default\n"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 29–31why, one step back293031def test_get_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 32why, one step back32 class Foo:Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 33why, one step back33 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 34why, one step back34 self.title = "default"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 35–41why, one step back3536 pass_foo = click.make_pass_decorator(Foo, ensure=True)3738 @click.group()39 @click.pass_context40 def cli(ctx):41 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 42why, one step back42 ctx.obj.title = "test"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 43–48why, one step back4344 @cli.command()45 @pass_foo46 def test(foo):47 click.echo(foo.title)48Merge branch '7.x'David Lordbefore adoption7239794
line 49why, one step back49 result = runner.invoke(cli, ["test"])Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 50why, one step back50 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 51why, one step back51 assert result.output == "test\n"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 52–54why, one step back525354def test_get_context_objects_no_ensuring(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 55why, one step back55 class Foo:Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 56why, one step back56 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 57why, one step back57 self.title = "default"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 58–64why, one step back5859 pass_foo = click.make_pass_decorator(Foo)6061 @click.group()62 @click.pass_context63 def cli(ctx):64 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 65why, one step back65 ctx.obj.title = "test"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 66–71why, one step back6667 @cli.command()68 @pass_foo69 def test(foo):70 click.echo(foo.title)71Merge branch '7.x'David Lordbefore adoption7239794
line 72why, one step back72 result = runner.invoke(cli, ["test"])Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 73why, one step back73 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 74why, one step back74 assert result.output == "test\n"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 75–77why, one step back757677def test_get_context_objects_missing(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 78why, one step back78 class Foo:Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 79–92why, one step back79 pass8081 pass_foo = click.make_pass_decorator(Foo)8283 @click.group()84 @click.pass_context85 def cli(ctx):86 pass8788 @cli.command()89 @pass_foo90 def test(foo):91 click.echo(foo.title)92Merge branch '7.x'David Lordbefore adoption7239794
line 93why, one step back93 result = runner.invoke(cli, ["test"])Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 94–95why, one step back94 assert result.exception is not None95 assert isinstance(result.exception, RuntimeError)Merge branch '7.x'David Lordbefore adoption7239794
lines 96–99why, one step back96 assert (97 "Managed to invoke callback without a context object of type"98 " 'Foo' existing" in str(result.exception)99 )Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 100–109why, one step back100101102def test_multi_enter(runner):103 called = []104105 @click.command()106 @click.pass_context107 def cli(ctx):108 def callback():109 called.append(True)Merge branch '7.x'David Lordbefore adoption7239794
line 110why, one step back110Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 111–126why, one step back111 ctx.call_on_close(callback)112113 with ctx:114 pass115 assert not called116117 result = runner.invoke(cli, [])118 assert result.exception is None119 assert called == [True]120121122def test_global_context_object(runner):123 @click.command()124 @click.pass_context125 def cli(ctx):126 assert click.get_current_context() is ctxMerge branch '7.x'David Lordbefore adoption7239794
lines 127–128why, one step back127 ctx.obj = "FOOBAR"128 assert click.get_current_context().obj == "FOOBAR"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 129–135why, one step back129130 assert click.get_current_context(silent=True) is None131 runner.invoke(cli, [], catch_exceptions=False)132 assert click.get_current_context(silent=True) is None133134135def test_context_meta(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 136why, one step back136 LANG_KEY = f"{__name__}.lang"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 137–141why, one step back137138 def set_language(value):139 click.get_current_context().meta[LANG_KEY] = value140141 def get_language():Merge branch '7.x'David Lordbefore adoption7239794
line 142why, one step back142 return click.get_current_context().meta.get(LANG_KEY, "en_US")Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 143–146why, one step back143144 @click.command()145 @click.pass_context146 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 147–149why, one step back147 assert get_language() == "en_US"148 set_language("de_DE")149 assert get_language() == "de_DE"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 150–183why, one step back150151 runner.invoke(cli, [], catch_exceptions=False)152153154def test_context_pushing():155 rv = []156157 @click.command()158 def cli():159 pass160161 ctx = click.Context(cli)162163 @ctx.call_on_close164 def test_callback():165 rv.append(42)166167 with ctx.scope(cleanup=False):168 # Internal169 assert ctx._depth == 2170171 assert rv == []172173 with ctx.scope():174 # Internal175 assert ctx._depth == 1176177 assert rv == [42]178179180def test_pass_obj(runner):181 @click.group()182 @click.pass_context183 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 184why, one step back184 ctx.obj = "test"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 185–190why, one step back185186 @cli.command()187 @click.pass_obj188 def test(obj):189 click.echo(obj)190Merge branch '7.x'David Lordbefore adoption7239794
line 191why, one step back191 result = runner.invoke(cli, ["test"])Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 192why, one step back192 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 193why, one step back193 assert result.output == "test\n"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 194–201why, one step back194195196def test_close_before_pop(runner):197 called = []198199 @click.command()200 @click.pass_context201 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 202why, one step back202 ctx.obj = "test"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 203why, one step back203Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 204–205why, one step back204 @ctx.call_on_close205 def foo():Merge branch '7.x'David Lordbefore adoption7239794
line 206why, one step back206 assert click.get_current_context().obj == "test"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 207–208why, one step back207 called.append(True)208Merge branch '7.x'David Lordbefore adoption7239794
lines 209–210why, one step back209 click.echo("aha!")210Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
lines 211–212why, one step back211 result = runner.invoke(cli, [])212 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 213why, one step back213 assert result.output == "aha!\n"Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947
line 214why, one step back214 assert called == [True]Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7
lines 215–230why, one step back215216217def test_with_resource():218 @contextmanager219 def manager():220 val = [1]221 yield val222 val[0] = 0223224 ctx = click.Context(click.Command("test"))225226 with ctx.scope():227 rv = ctx.with_resource(manager())228 assert rv[0] == 1229230 assert rv == [0]Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
lines 231–237why, one step back231232233def test_make_pass_decorator_args(runner):234 """235 Test to check that make_pass_decorator doesn't consume arguments based on236 invocation order.237 """Merge branch '7.x'David Lordbefore adoption7239794
line 238why, one step back238Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 239why, one step back239 class Foo:Merge branch '7.x'David Lordbefore adoption7239794
line 240why, one step back240 title = "foocmd"Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
lines 241–260why, one step back241242 pass_foo = click.make_pass_decorator(Foo)243244 @click.group()245 @click.pass_context246 def cli(ctx):247 ctx.obj = Foo()248249 @cli.command()250 @click.pass_context251 @pass_foo252 def test1(foo, ctx):253 click.echo(foo.title)254255 @cli.command()256 @pass_foo257 @click.pass_context258 def test2(ctx, foo):259 click.echo(foo.title)260Merge branch '7.x'David Lordbefore adoption7239794
line 261why, one step back261 result = runner.invoke(cli, ["test1"])Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 262why, one step back262 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 263why, one step back263 assert result.output == "foocmd\n"Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 264why, one step back264Merge branch '7.x'David Lordbefore adoption7239794
line 265why, one step back265 result = runner.invoke(cli, ["test2"])Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 266why, one step back266 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 267why, one step back267 assert result.output == "foocmd\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 268–269why, one step back268269Merge pull request #1566 from achimnol/fix/show-default-propagationDavid Lordbefore adoption4ae204e
lines 270–283why, one step back270def test_propagate_show_default_setting(runner):271 """A context's ``show_default`` setting defaults to the value from272 the parent context.273 """274 group = click.Group(275 commands={276 "sub": click.Command("sub", params=[click.Option(["-a"], default="a")]),277 },278 context_settings={"show_default": True},279 )280 result = runner.invoke(group, ["sub", "--help"])281 assert "[default: a]" in result.output282283Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 284–289why, one step back284def test_exit_not_standalone():285 @click.command()286 @click.pass_context287 def cli(ctx):288 ctx.exit(1)289Merge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 291–296why, one step back291292 @click.command()293 @click.pass_context294 def cli(ctx):295 ctx.exit(0)296Merge branch '7.x'David Lordbefore adoption7239794
line 297why, one step back297 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 298why, one step back298Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 299why, one step back299Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
lines 300–336why, one step back300@pytest.mark.parametrize(301 ("option_args", "invoke_args", "expect"),302 [303 pytest.param({}, {}, ParameterSource.DEFAULT, id="default"),304 pytest.param(305 {},306 {"default_map": {"option": 1}},307 ParameterSource.DEFAULT_MAP,308 id="default_map",309 ),310 pytest.param(311 {},312 {"args": ["-o", "1"]},313 ParameterSource.COMMANDLINE,314 id="commandline short",315 ),316 pytest.param(317 {},318 {"args": ["--option", "1"]},319 ParameterSource.COMMANDLINE,320 id="commandline long",321 ),322 pytest.param(323 {},324 {"auto_envvar_prefix": "TEST", "env": {"TEST_OPTION": "1"}},325 ParameterSource.ENVIRONMENT,326 id="environment auto",327 ),328 pytest.param(329 {"envvar": "NAME"},330 {"env": {"NAME": "1"}},331 ParameterSource.ENVIRONMENT,332 id="environment manual",333 ),334 ],335)336def test_parameter_source(runner, option_args, invoke_args, expect):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 337–338why, one step back337 @click.command()338 @click.pass_contextMerge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
line 339why, one step back339 @click.option("-o", "--option", default=1, **option_args)Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 340why, one step back340 def cli(ctx, option):Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
line 341why, one step back341 return ctx.get_parameter_source("option")Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 342why, one step back342Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047
lines 343–344why, one step back343 rv = runner.invoke(cli, standalone_mode=False, **invoke_args)344 assert rv.return_value == expect