test_context.py
This file
Path
tests/test_context.py
327 lines · 7.4 KiB · Python
Last changed by
Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
History of this fileTake it
The walk stopped after 200 commits: lines older than 5210849 point at it.
Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 1why, one step back1import pytestMerge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 2why, one step back2Merge branch '7.x'David Lordbefore adoption7239794
lines 3–6why, one step back3import click4from click.core import ParameterSource56Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 7why, one step back7def test_ensure_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 8why, one step back8 class Foo:Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 9why, one step back9 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 10why, one step back10 self.title = "default"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 11–23why, one step back1112 pass_foo = click.make_pass_decorator(Foo, ensure=True)1314 @click.group()15 @pass_foo16 def cli(foo):17 pass1819 @cli.command()20 @pass_foo21 def test(foo):22 click.echo(foo.title)23Merge branch '7.x'David Lordbefore adoption7239794
line 24why, one step back24 result = runner.invoke(cli, ["test"])Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 25why, one step back25 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 26why, one step back26 assert result.output == "default\n"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 27–29why, one step back272829def test_get_context_objects(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 30why, one step back30 class Foo:Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 31why, one step back31 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 32why, one step back32 self.title = "default"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 33–39why, one step back3334 pass_foo = click.make_pass_decorator(Foo, ensure=True)3536 @click.group()37 @click.pass_context38 def cli(ctx):39 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 40why, one step back40 ctx.obj.title = "test"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 41–46why, one step back4142 @cli.command()43 @pass_foo44 def test(foo):45 click.echo(foo.title)46Merge branch '7.x'David Lordbefore adoption7239794
line 47why, one step back47 result = runner.invoke(cli, ["test"])Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 48why, one step back48 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 49why, one step back49 assert result.output == "test\n"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 50–52why, one step back505152def test_get_context_objects_no_ensuring(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 53why, one step back53 class Foo:Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 54why, one step back54 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 55why, one step back55 self.title = "default"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 56–62why, one step back5657 pass_foo = click.make_pass_decorator(Foo)5859 @click.group()60 @click.pass_context61 def cli(ctx):62 ctx.obj = Foo()Merge branch '7.x'David Lordbefore adoption7239794
line 63why, one step back63 ctx.obj.title = "test"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 64–69why, one step back6465 @cli.command()66 @pass_foo67 def test(foo):68 click.echo(foo.title)69Merge branch '7.x'David Lordbefore adoption7239794
line 70why, one step back70 result = runner.invoke(cli, ["test"])Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 71why, one step back71 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 72why, one step back72 assert result.output == "test\n"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 73–75why, one step back737475def test_get_context_objects_missing(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 76why, one step back76 class Foo:Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 77–90why, one step back77 pass7879 pass_foo = click.make_pass_decorator(Foo)8081 @click.group()82 @click.pass_context83 def cli(ctx):84 pass8586 @cli.command()87 @pass_foo88 def test(foo):89 click.echo(foo.title)90Merge branch '7.x'David Lordbefore adoption7239794
line 91why, one step back91 result = runner.invoke(cli, ["test"])Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 92–93why, one step back92 assert result.exception is not None93 assert isinstance(result.exception, RuntimeError)Merge branch '7.x'David Lordbefore adoption7239794
lines 94–97why, one step back94 assert (95 "Managed to invoke callback without a context object of type"96 " 'Foo' existing" in str(result.exception)97 )Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 98–107why, one step back9899100def test_multi_enter(runner):101 called = []102103 @click.command()104 @click.pass_context105 def cli(ctx):106 def callback():107 called.append(True)Merge branch '7.x'David Lordbefore adoption7239794
line 108why, one step back108Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 109–124why, one step back109 ctx.call_on_close(callback)110111 with ctx:112 pass113 assert not called114115 result = runner.invoke(cli, [])116 assert result.exception is None117 assert called == [True]118119120def test_global_context_object(runner):121 @click.command()122 @click.pass_context123 def cli(ctx):124 assert click.get_current_context() is ctxMerge branch '7.x'David Lordbefore adoption7239794
lines 125–126why, one step back125 ctx.obj = "FOOBAR"126 assert click.get_current_context().obj == "FOOBAR"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 127–133why, one step back127128 assert click.get_current_context(silent=True) is None129 runner.invoke(cli, [], catch_exceptions=False)130 assert click.get_current_context(silent=True) is None131132133def test_context_meta(runner):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 134why, one step back134 LANG_KEY = f"{__name__}.lang"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 135–139why, one step back135136 def set_language(value):137 click.get_current_context().meta[LANG_KEY] = value138139 def get_language():Merge branch '7.x'David Lordbefore adoption7239794
line 140why, one step back140 return click.get_current_context().meta.get(LANG_KEY, "en_US")Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 141–144why, one step back141142 @click.command()143 @click.pass_context144 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 145–147why, one step back145 assert get_language() == "en_US"146 set_language("de_DE")147 assert get_language() == "de_DE"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 148–181why, one step back148149 runner.invoke(cli, [], catch_exceptions=False)150151152def test_context_pushing():153 rv = []154155 @click.command()156 def cli():157 pass158159 ctx = click.Context(cli)160161 @ctx.call_on_close162 def test_callback():163 rv.append(42)164165 with ctx.scope(cleanup=False):166 # Internal167 assert ctx._depth == 2168169 assert rv == []170171 with ctx.scope():172 # Internal173 assert ctx._depth == 1174175 assert rv == [42]176177178def test_pass_obj(runner):179 @click.group()180 @click.pass_context181 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 182why, one step back182 ctx.obj = "test"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 183–188why, one step back183184 @cli.command()185 @click.pass_obj186 def test(obj):187 click.echo(obj)188Merge branch '7.x'David Lordbefore adoption7239794
line 189why, one step back189 result = runner.invoke(cli, ["test"])Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 190why, one step back190 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 191why, one step back191 assert result.output == "test\n"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 192–199why, one step back192193194def test_close_before_pop(runner):195 called = []196197 @click.command()198 @click.pass_context199 def cli(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 200why, one step back200 ctx.obj = "test"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 201why, one step back201Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 202–203why, one step back202 @ctx.call_on_close203 def foo():Merge branch '7.x'David Lordbefore adoption7239794
line 204why, one step back204 assert click.get_current_context().obj == "test"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 205–206why, one step back205 called.append(True)206Merge branch '7.x'David Lordbefore adoption7239794
lines 207–208why, one step back207 click.echo("aha!")208Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
lines 209–210why, one step back209 result = runner.invoke(cli, [])210 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 211why, one step back211 assert result.output == "aha!\n"Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 212why, one step back212 assert called == [True]Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
lines 213–219why, one step back213214215def test_make_pass_decorator_args(runner):216 """217 Test to check that make_pass_decorator doesn't consume arguments based on218 invocation order.219 """Merge branch '7.x'David Lordbefore adoption7239794
line 220why, one step back220Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 221why, one step back221 class Foo:Merge branch '7.x'David Lordbefore adoption7239794
line 222why, one step back222 title = "foocmd"Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
lines 223–242why, one step back223224 pass_foo = click.make_pass_decorator(Foo)225226 @click.group()227 @click.pass_context228 def cli(ctx):229 ctx.obj = Foo()230231 @cli.command()232 @click.pass_context233 @pass_foo234 def test1(foo, ctx):235 click.echo(foo.title)236237 @cli.command()238 @pass_foo239 @click.pass_context240 def test2(ctx, foo):241 click.echo(foo.title)242Merge branch '7.x'David Lordbefore adoption7239794
line 243why, one step back243 result = runner.invoke(cli, ["test1"])Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 244why, one step back244 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 245why, one step back245 assert result.output == "foocmd\n"Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 246why, one step back246Merge branch '7.x'David Lordbefore adoption7239794
line 247why, one step back247 result = runner.invoke(cli, ["test2"])Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db
line 248why, one step back248 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 249why, one step back249 assert result.output == "foocmd\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 250–257why, one step back250251252def test_exit_not_standalone():253 @click.command()254 @click.pass_context255 def cli(ctx):256 ctx.exit(1)257Merge branch '7.x'David Lordbefore adoption7239794
line 258why, one step back258 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 259–264why, one step back259260 @click.command()261 @click.pass_context262 def cli(ctx):263 ctx.exit(0)264Merge branch '7.x'David Lordbefore adoption7239794
line 265why, one step back265 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 266why, one step back266Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
lines 267–268why, one step back267268def test_parameter_source_default(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 269–272why, one step back269 @click.command()270 @click.pass_context271 @click.option("-o", "--option", default=1)272 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 273why, one step back273 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 274why, one step back274Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 275why, one step back275 rv = runner.invoke(cli)Merge branch '7.x'David Lordbefore adoption7239794
line 276why, one step back276 assert rv.output.rstrip() == ParameterSource.DEFAULTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 277why, one step back277Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
lines 278–279why, one step back278279def test_parameter_source_default_map(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 280–283why, one step back280 @click.command()281 @click.pass_context282 @click.option("-o", "--option", default=1)283 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 284why, one step back284 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 285why, one step back285Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 286why, one step back286 rv = runner.invoke(cli, default_map={"option": 1})Merge branch '7.x'David Lordbefore adoption7239794
line 287why, one step back287 assert rv.output.rstrip() == ParameterSource.DEFAULT_MAPMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 288why, one step back288Merge branch '7.x'David Lordbefore adoption7239794
line 289why, one step back289Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 290why, one step back290def test_parameter_source_commandline(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 291–294why, one step back291 @click.command()292 @click.pass_context293 @click.option("-o", "--option", default=1)294 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 295why, one step back295 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 296why, one step back296Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 297why, one step back297 rv = runner.invoke(cli, ["-o", "1"])Merge branch '7.x'David Lordbefore adoption7239794
line 298why, one step back298 assert rv.output.rstrip() == ParameterSource.COMMANDLINEMerge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 299why, one step back299 rv = runner.invoke(cli, ["--option", "1"])Merge branch '7.x'David Lordbefore adoption7239794
line 300why, one step back300 assert rv.output.rstrip() == ParameterSource.COMMANDLINEMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 301why, one step back301Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 302why, one step back302Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 303–307why, one step back303def test_parameter_source_environment(runner):304 @click.command()305 @click.pass_context306 @click.option("-o", "--option", default=1)307 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 308why, one step back308 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 309why, one step back309Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 310why, one step back310 rv = runner.invoke(cli, auto_envvar_prefix="TEST", env={"TEST_OPTION": "1"})Merge branch '7.x'David Lordbefore adoption7239794
line 311why, one step back311 assert rv.output.rstrip() == ParameterSource.ENVIRONMENTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 312why, one step back312Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 313why, one step back313Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 314–318why, one step back314def test_parameter_source_environment_variable_specified(runner):315 @click.command()316 @click.pass_context317 @click.option("-o", "--option", default=1, envvar="NAME")318 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 319why, one step back319 click.echo(ctx.get_parameter_source("option"))Merge branch '7.x'David Lordbefore adoption7239794
line 320why, one step back320Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 321why, one step back321 rv = runner.invoke(cli, env={"NAME": "1"})Merge branch '7.x'David Lordbefore adoption7239794
line 322why, one step back322 assert rv.output.rstrip() == ParameterSource.ENVIRONMENTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 323–326why, one step back323324325def test_validate_parameter_source():326 with pytest.raises(ValueError):Merge branch '7.x'David Lordbefore adoption7239794
line 327why, one step back327 ParameterSource.validate("NOT_A_VALID_PARAMETER_SOURCE")