test_context.py
This file
Path
tests/test_context.py
345 lines · 7.7 KiB · Python
Last changed by
Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7
History of this fileTake it
The walk stopped after 200 commits: lines older than d97c93f 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 pytestRemove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
line 4why, one step back4Merge branch '7.x'David Lordbefore adoption7239794
lines 5–8why, one step back5import click6from click.core import ParameterSource78Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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:Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
line 11why, one step back11 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 12why, one step back12 self.title = "default"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"])Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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:Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
line 33why, one step back33 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 34why, one step back34 self.title = "default"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"])Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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:Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
line 56why, one step back56 def __init__(self):Merge branch '7.x'David Lordbefore adoption7239794
line 57why, one step back57 self.title = "default"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"])Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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:Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"])Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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 )Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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 back110Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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")Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"])Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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 back203Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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!")210Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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"Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f
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–275why, one step back268269270def test_exit_not_standalone():271 @click.command()272 @click.pass_context273 def cli(ctx):274 ctx.exit(1)275Merge branch '7.x'David Lordbefore adoption7239794
line 276why, one step back276 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 277–282why, one step back277278 @click.command()279 @click.pass_context280 def cli(ctx):281 ctx.exit(0)282Merge branch '7.x'David Lordbefore adoption7239794
line 283why, one step back283 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 284why, one step back284Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
lines 285–286why, one step back285286def test_parameter_source_default(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 287–290why, one step back287 @click.command()288 @click.pass_context289 @click.option("-o", "--option", default=1)290 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 291why, one step back291 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 292why, one step back292Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 293why, one step back293 rv = runner.invoke(cli)Merge branch '7.x'David Lordbefore adoption7239794
line 294why, one step back294 assert rv.output.rstrip() == ParameterSource.DEFAULTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 295why, one step back295Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
lines 296–297why, one step back296297def test_parameter_source_default_map(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 298–301why, one step back298 @click.command()299 @click.pass_context300 @click.option("-o", "--option", default=1)301 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 302why, one step back302 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 303why, one step back303Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 304why, one step back304 rv = runner.invoke(cli, default_map={"option": 1})Merge branch '7.x'David Lordbefore adoption7239794
line 305why, one step back305 assert rv.output.rstrip() == ParameterSource.DEFAULT_MAPMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 306why, one step back306Merge branch '7.x'David Lordbefore adoption7239794
line 307why, one step back307Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 308why, one step back308def test_parameter_source_commandline(runner):Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 309–312why, one step back309 @click.command()310 @click.pass_context311 @click.option("-o", "--option", default=1)312 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 313why, one step back313 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 314why, one step back314Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 315why, one step back315 rv = runner.invoke(cli, ["-o", "1"])Merge branch '7.x'David Lordbefore adoption7239794
line 316why, one step back316 assert rv.output.rstrip() == ParameterSource.COMMANDLINEMerge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 317why, one step back317 rv = runner.invoke(cli, ["--option", "1"])Merge branch '7.x'David Lordbefore adoption7239794
line 318why, one step back318 assert rv.output.rstrip() == ParameterSource.COMMANDLINEMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 319why, one step back319Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 320why, one step back320Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 321–325why, one step back321def test_parameter_source_environment(runner):322 @click.command()323 @click.pass_context324 @click.option("-o", "--option", default=1)325 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 326why, one step back326 click.echo(ctx.get_parameter_source("option"))Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 327why, one step back327Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 328why, one step back328 rv = runner.invoke(cli, auto_envvar_prefix="TEST", env={"TEST_OPTION": "1"})Merge branch '7.x'David Lordbefore adoption7239794
line 329why, one step back329 assert rv.output.rstrip() == ParameterSource.ENVIRONMENTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
line 330why, one step back330Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 331why, one step back331Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 332–336why, one step back332def test_parameter_source_environment_variable_specified(runner):333 @click.command()334 @click.pass_context335 @click.option("-o", "--option", default=1, envvar="NAME")336 def cli(ctx, option):Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 337why, one step back337 click.echo(ctx.get_parameter_source("option"))Merge branch '7.x'David Lordbefore adoption7239794
line 338why, one step back338Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830
line 339why, one step back339 rv = runner.invoke(cli, env={"NAME": "1"})Merge branch '7.x'David Lordbefore adoption7239794
line 340why, one step back340 assert rv.output.rstrip() == ParameterSource.ENVIRONMENTMerge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3
lines 341–344why, one step back341342343def test_validate_parameter_source():344 with pytest.raises(ValueError):Merge branch '7.x'David Lordbefore adoption7239794
line 345why, one step back345 ParameterSource.validate("NOT_A_VALID_PARAMETER_SOURCE")