Skip to content
Myr

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 file

Take 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 back
1import pytest

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 2why, one step back
2

Merge branch '7.x'David Lordbefore adoption7239794

lines 3–6why, one step back
3import click
4from click.core import ParameterSource56

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 7why, one step back
7def test_ensure_context_objects(runner):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 8why, one step back
8 class Foo:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 9why, one step back
9 def __init__(self):

Merge branch '7.x'David Lordbefore adoption7239794

line 10why, one step back
10 self.title = "default"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 11–23why, one step back
11
12 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)23

Merge branch '7.x'David Lordbefore adoption7239794

line 24why, one step back
24 result = runner.invoke(cli, ["test"])

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 25why, one step back
25 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 26why, one step back
26 assert result.output == "default\n"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 27–29why, one step back
27
2829def test_get_context_objects(runner):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 30why, one step back
30 class Foo:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 31why, one step back
31 def __init__(self):

Merge branch '7.x'David Lordbefore adoption7239794

line 32why, one step back
32 self.title = "default"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 33–39why, one step back
33
34 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 back
40 ctx.obj.title = "test"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 41–46why, one step back
41
42 @cli.command()43 @pass_foo44 def test(foo):45 click.echo(foo.title)46

Merge branch '7.x'David Lordbefore adoption7239794

line 47why, one step back
47 result = runner.invoke(cli, ["test"])

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 48why, one step back
48 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 49why, one step back
49 assert result.output == "test\n"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 50–52why, one step back
50
5152def test_get_context_objects_no_ensuring(runner):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 53why, one step back
53 class Foo:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 54why, one step back
54 def __init__(self):

Merge branch '7.x'David Lordbefore adoption7239794

line 55why, one step back
55 self.title = "default"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 56–62why, one step back
56
57 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 back
63 ctx.obj.title = "test"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 64–69why, one step back
64
65 @cli.command()66 @pass_foo67 def test(foo):68 click.echo(foo.title)69

Merge branch '7.x'David Lordbefore adoption7239794

line 70why, one step back
70 result = runner.invoke(cli, ["test"])

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 71why, one step back
71 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 72why, one step back
72 assert result.output == "test\n"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 73–75why, one step back
73
7475def test_get_context_objects_missing(runner):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 76why, one step back
76 class Foo:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 77–90why, one step back
77 pass
7879 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)90

Merge branch '7.x'David Lordbefore adoption7239794

line 91why, one step back
91 result = runner.invoke(cli, ["test"])

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 92–93why, one step back
92 assert result.exception is not None
93 assert isinstance(result.exception, RuntimeError)

Merge branch '7.x'David Lordbefore adoption7239794

lines 94–97why, one step back
94 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 back
98
99100def 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 back
108

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 109–124why, one step back
109 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 ctx

Merge branch '7.x'David Lordbefore adoption7239794

lines 125–126why, one step back
125 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 back
127
128 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 back
134 LANG_KEY = f"{__name__}.lang"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 135–139why, one step back
135
136 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 back
140 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 back
141
142 @click.command()143 @click.pass_context144 def cli(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 145–147why, one step back
145 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 back
148
149 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 back
182 ctx.obj = "test"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 183–188why, one step back
183
184 @cli.command()185 @click.pass_obj186 def test(obj):187 click.echo(obj)188

Merge branch '7.x'David Lordbefore adoption7239794

line 189why, one step back
189 result = runner.invoke(cli, ["test"])

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 190why, one step back
190 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 191why, one step back
191 assert result.output == "test\n"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 192–199why, one step back
192
193194def 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 back
200 ctx.obj = "test"

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

line 201why, one step back
201

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 202–203why, one step back
202 @ctx.call_on_close
203 def foo():

Merge branch '7.x'David Lordbefore adoption7239794

line 204why, one step back
204 assert click.get_current_context().obj == "test"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 205–206why, one step back
205 called.append(True)
206

Merge branch '7.x'David Lordbefore adoption7239794

lines 207–208why, one step back
207 click.echo("aha!")
208

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 209–210why, one step back
209 result = runner.invoke(cli, [])
210 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 211why, one step back
211 assert result.output == "aha!\n"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 212why, one step back
212 assert called == [True]

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

lines 213–219why, one step back
213
214215def 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 back
220

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 221why, one step back
221 class Foo:

Merge branch '7.x'David Lordbefore adoption7239794

line 222why, one step back
222 title = "foocmd"

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

lines 223–242why, one step back
223
224 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)242

Merge branch '7.x'David Lordbefore adoption7239794

line 243why, one step back
243 result = runner.invoke(cli, ["test1"])

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

line 244why, one step back
244 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 245why, one step back
245 assert result.output == "foocmd\n"

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

line 246why, one step back
246

Merge branch '7.x'David Lordbefore adoption7239794

line 247why, one step back
247 result = runner.invoke(cli, ["test2"])

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

line 248why, one step back
248 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 249why, one step back
249 assert result.output == "foocmd\n"

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

lines 250–257why, one step back
250
251252def test_exit_not_standalone():253 @click.command()254 @click.pass_context255 def cli(ctx):256 ctx.exit(1)257

Merge branch '7.x'David Lordbefore adoption7239794

line 258why, one step back
258 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1

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

lines 259–264why, one step back
259
260 @click.command()261 @click.pass_context262 def cli(ctx):263 ctx.exit(0)264

Merge branch '7.x'David Lordbefore adoption7239794

line 265why, one step back
265 assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 266why, one step back
266

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

lines 267–268why, one step back
267
268def test_parameter_source_default(runner):

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 269–272why, one step back
269 @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 back
273 click.echo(ctx.get_parameter_source("option"))

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 274why, one step back
274

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 275why, one step back
275 rv = runner.invoke(cli)

Merge branch '7.x'David Lordbefore adoption7239794

line 276why, one step back
276 assert rv.output.rstrip() == ParameterSource.DEFAULT

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 277why, one step back
277

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

lines 278–279why, one step back
278
279def test_parameter_source_default_map(runner):

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 280–283why, one step back
280 @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 back
284 click.echo(ctx.get_parameter_source("option"))

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 285why, one step back
285

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 286why, one step back
286 rv = runner.invoke(cli, default_map={"option": 1})

Merge branch '7.x'David Lordbefore adoption7239794

line 287why, one step back
287 assert rv.output.rstrip() == ParameterSource.DEFAULT_MAP

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 288why, one step back
288

Merge branch '7.x'David Lordbefore adoption7239794

line 289why, one step back
289

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 290why, one step back
290def test_parameter_source_commandline(runner):

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 291–294why, one step back
291 @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 back
295 click.echo(ctx.get_parameter_source("option"))

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 296why, one step back
296

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 297why, one step back
297 rv = runner.invoke(cli, ["-o", "1"])

Merge branch '7.x'David Lordbefore adoption7239794

line 298why, one step back
298 assert rv.output.rstrip() == ParameterSource.COMMANDLINE

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 299why, one step back
299 rv = runner.invoke(cli, ["--option", "1"])

Merge branch '7.x'David Lordbefore adoption7239794

line 300why, one step back
300 assert rv.output.rstrip() == ParameterSource.COMMANDLINE

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 301why, one step back
301

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 302why, one step back
302

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 303–307why, one step back
303def 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 back
308 click.echo(ctx.get_parameter_source("option"))

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 309why, one step back
309

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 310why, one step back
310 rv = runner.invoke(cli, auto_envvar_prefix="TEST", env={"TEST_OPTION": "1"})

Merge branch '7.x'David Lordbefore adoption7239794

line 311why, one step back
311 assert rv.output.rstrip() == ParameterSource.ENVIRONMENT

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

line 312why, one step back
312

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 313why, one step back
313

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 314–318why, one step back
314def 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 back
319 click.echo(ctx.get_parameter_source("option"))

Merge branch '7.x'David Lordbefore adoption7239794

line 320why, one step back
320

Merge pull request #1417 from cclauss/patch-2David Lordbefore adoption2f39830

line 321why, one step back
321 rv = runner.invoke(cli, env={"NAME": "1"})

Merge branch '7.x'David Lordbefore adoption7239794

line 322why, one step back
322 assert rv.output.rstrip() == ParameterSource.ENVIRONMENT

Merge pull request #1329 from joshuastorck/PR-1264David Lordbefore adoption91e5bc3

lines 323–326why, one step back
323
324325def test_validate_parameter_source():326 with pytest.raises(ValueError):

Merge branch '7.x'David Lordbefore adoption7239794

line 327why, one step back
327 ParameterSource.validate("NOT_A_VALID_PARAMETER_SOURCE")