Skip to content
Myr

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 file

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

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

line 3why, one step back
3import pytest

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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:

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"])

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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:

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"])

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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:

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"])

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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:

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"])

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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 )

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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")

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"])

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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"

Remove leftover print function in test used for previous debugging (#876)Jon Dufresnebefore adoptiond97c93f

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–275why, one step back
268
269270def test_exit_not_standalone():271 @click.command()272 @click.pass_context273 def cli(ctx):274 ctx.exit(1)275

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 277–282why, one step back
277
278 @click.command()279 @click.pass_context280 def cli(ctx):281 ctx.exit(0)282

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 284why, one step back
284

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

lines 285–286why, one step back
285
286def test_parameter_source_default(runner):

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

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

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

line 292why, one step back
292

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 295why, one step back
295

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

lines 296–297why, one step back
296
297def test_parameter_source_default_map(runner):

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

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

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

line 303why, one step back
303

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 306why, one step back
306

Merge branch '7.x'David Lordbefore adoption7239794

line 307why, one step back
307

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

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

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

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

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

line 314why, one step back
314

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 319why, one step back
319

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

line 320why, one step back
320

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

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

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

line 327why, one step back
327

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 330why, one step back
330

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

line 331why, one step back
331

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 338why, one step back
338

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 341–344why, one step back
341
342343def test_validate_parameter_source():344 with pytest.raises(ValueError):

Merge branch '7.x'David Lordbefore adoption7239794

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