Skip to content
Myr

test_context.py

This file

Path

tests/test_context.py

359 lines · 8.1 KiB · Python

Last changed by

Merge pull request #1566 from achimnol/fix/show-default-propagationDavid Lordbefore adoption4ae204e

History of this file

Take 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 back
1from contextlib import contextmanager
2

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

line 3why, one step back
3import pytest

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

line 4why, one step back
4

Merge branch '7.x'David Lordbefore adoption7239794

lines 5–8why, one step back
5import click
6from click.core import ParameterSource78

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

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

line 10why, one step back
10 class Foo:

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 13–25why, one step back
13
14 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)25

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 29–31why, one step back
29
3031def test_get_context_objects(runner):

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

line 32why, one step back
32 class Foo:

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 35–41why, one step back
35
36 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 back
42 ctx.obj.title = "test"

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 43–48why, one step back
43
44 @cli.command()45 @pass_foo46 def test(foo):47 click.echo(foo.title)48

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 52–54why, one step back
52
5354def test_get_context_objects_no_ensuring(runner):

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

line 55why, one step back
55 class Foo:

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 58–64why, one step back
58
59 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 back
65 ctx.obj.title = "test"

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 66–71why, one step back
66
67 @cli.command()68 @pass_foo69 def test(foo):70 click.echo(foo.title)71

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 75–77why, one step back
75
7677def test_get_context_objects_missing(runner):

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

line 78why, one step back
78 class Foo:

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 79–92why, one step back
79 pass
8081 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)92

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 96–99why, one step back
96 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 back
100
101102def 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 back
110

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 137–141why, one step back
137
138 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 back
142 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 back
143
144 @click.command()145 @click.pass_context146 def cli(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 147–149why, one step back
147 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 back
150
151 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 back
184 ctx.obj = "test"

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 185–190why, one step back
185
186 @cli.command()187 @click.pass_obj188 def test(obj):189 click.echo(obj)190

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 194–201why, one step back
194
195196def 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 back
202 ctx.obj = "test"

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

line 203why, one step back
203

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

lines 207–208why, one step back
207 called.append(True)
208

Merge branch '7.x'David Lordbefore adoption7239794

lines 209–210why, one step back
209 click.echo("aha!")
210

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #973 from SWu/patch-1David Lordbefore adoptiona43b947

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

Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7

lines 215–230why, one step back
215
216217def 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 back
231
232233def 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 back
238

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

line 239why, one step back
239 class Foo:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

line 264why, one step back
264

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #787 from ericfrederich/fix_make_pass_decoratorDavid Lordbefore adoption56314db

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 268–269why, one step back
268
269

Merge pull request #1566 from achimnol/fix/show-default-propagationDavid Lordbefore adoption4ae204e

lines 270–283why, one step back
270def 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.output282283

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

lines 284–289why, one step back
284def test_exit_not_standalone():
285 @click.command()286 @click.pass_context287 def cli(ctx):288 ctx.exit(1)289

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 291–296why, one step back
291
292 @click.command()293 @click.pass_context294 def cli(ctx):295 ctx.exit(0)296

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 298why, one step back
298

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

lines 299–300why, one step back
299
300def test_parameter_source_default(runner):

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

lines 301–304why, one step back
301 @click.command()
302 @click.pass_context303 @click.option("-o", "--option", default=1)304 def cli(ctx, option):

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

line 305why, one step back
305 click.echo(ctx.get_parameter_source("option"))

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

line 306why, one step back
306

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 310–311why, one step back
310
311def test_parameter_source_default_map(runner):

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

lines 312–315why, one step back
312 @click.command()
313 @click.pass_context314 @click.option("-o", "--option", default=1)315 def cli(ctx, option):

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

line 316why, one step back
316 click.echo(ctx.get_parameter_source("option"))

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

line 317why, one step back
317

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 320why, one step back
320

Merge branch '7.x'David Lordbefore adoption7239794

line 321why, one step back
321

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

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

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

lines 323–326why, one step back
323 @click.command()
324 @click.pass_context325 @click.option("-o", "--option", default=1)326 def cli(ctx, option):

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

line 327why, one step back
327 click.echo(ctx.get_parameter_source("option"))

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

line 328why, one step back
328

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 333why, one step back
333

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

line 334why, one step back
334

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

lines 335–339why, one step back
335def test_parameter_source_environment(runner):
336 @click.command()337 @click.pass_context338 @click.option("-o", "--option", default=1)339 def cli(ctx, option):

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

line 340why, one step back
340 click.echo(ctx.get_parameter_source("option"))

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

line 341why, one step back
341

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 344why, one step back
344

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

line 345why, one step back
345

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

lines 346–350why, one step back
346def test_parameter_source_environment_variable_specified(runner):
347 @click.command()348 @click.pass_context349 @click.option("-o", "--option", default=1, envvar="NAME")350 def cli(ctx, option):

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

line 351why, one step back
351 click.echo(ctx.get_parameter_source("option"))

Merge branch '7.x'David Lordbefore adoption7239794

line 352why, one step back
352

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 355–358why, one step back
355
356357def test_validate_parameter_source():358 with pytest.raises(ValueError):

Merge branch '7.x'David Lordbefore adoption7239794

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