Skip to content
Myr

test_context.py

This file

Path

tests/test_context.py

367 lines · 8.2 KiB · Python

Last changed by

Merge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31

History of this file

Take it

The walk stopped after 200 commits: lines older than 91e5bc3 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

lines 3–4why, one step back
3import pytest
4

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31

line 7why, one step back
7from click.decorators import pass_meta_key

Merge branch '7.x'David Lordbefore adoption7239794

lines 8–9why, one step back
8
9

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

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

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

line 11why, one step back
11 class Foo:

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 14–26why, one step back
14
15 pass_foo = click.make_pass_decorator(Foo, ensure=True)1617 @click.group()18 @pass_foo19 def cli(foo):20 pass2122 @cli.command()23 @pass_foo24 def test(foo):25 click.echo(foo.title)26

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 30–32why, one step back
30
3132def test_get_context_objects(runner):

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

line 33why, one step back
33 class Foo:

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 36–42why, one step back
36
37 pass_foo = click.make_pass_decorator(Foo, ensure=True)3839 @click.group()40 @click.pass_context41 def cli(ctx):42 ctx.obj = Foo()

Merge branch '7.x'David Lordbefore adoption7239794

line 43why, one step back
43 ctx.obj.title = "test"

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

lines 44–49why, one step back
44
45 @cli.command()46 @pass_foo47 def test(foo):48 click.echo(foo.title)49

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 53–55why, one step back
53
5455def test_get_context_objects_no_ensuring(runner):

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

line 56why, one step back
56 class Foo:

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 59–65why, one step back
59
60 pass_foo = click.make_pass_decorator(Foo)6162 @click.group()63 @click.pass_context64 def cli(ctx):65 ctx.obj = Foo()

Merge branch '7.x'David Lordbefore adoption7239794

line 66why, one step back
66 ctx.obj.title = "test"

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

lines 67–72why, one step back
67
68 @cli.command()69 @pass_foo70 def test(foo):71 click.echo(foo.title)72

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 76–78why, one step back
76
7778def test_get_context_objects_missing(runner):

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

line 79why, one step back
79 class Foo:

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

lines 80–93why, one step back
80 pass
8182 pass_foo = click.make_pass_decorator(Foo)8384 @click.group()85 @click.pass_context86 def cli(ctx):87 pass8889 @cli.command()90 @pass_foo91 def test(foo):92 click.echo(foo.title)93

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 97–100why, one step back
97 assert (
98 "Managed to invoke callback without a context object of type"99 " 'Foo' existing" in str(result.exception)100 )

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

lines 101–110why, one step back
101
102103def test_multi_enter(runner):104 called = []105106 @click.command()107 @click.pass_context108 def cli(ctx):109 def callback():110 called.append(True)

Merge branch '7.x'David Lordbefore adoption7239794

line 111why, one step back
111

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

lines 112–127why, one step back
112 ctx.call_on_close(callback)
113114 with ctx:115 pass116 assert not called117118 result = runner.invoke(cli, [])119 assert result.exception is None120 assert called == [True]121122123def test_global_context_object(runner):124 @click.command()125 @click.pass_context126 def cli(ctx):127 assert click.get_current_context() is ctx

Merge branch '7.x'David Lordbefore adoption7239794

lines 128–129why, one step back
128 ctx.obj = "FOOBAR"
129 assert click.get_current_context().obj == "FOOBAR"

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

lines 130–136why, one step back
130
131 assert click.get_current_context(silent=True) is None132 runner.invoke(cli, [], catch_exceptions=False)133 assert click.get_current_context(silent=True) is None134135136def test_context_meta(runner):

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

line 137why, one step back
137 LANG_KEY = f"{__name__}.lang"

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

lines 138–142why, one step back
138
139 def set_language(value):140 click.get_current_context().meta[LANG_KEY] = value141142 def get_language():

Merge branch '7.x'David Lordbefore adoption7239794

line 143why, one step back
143 return click.get_current_context().meta.get(LANG_KEY, "en_US")

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

lines 144–147why, one step back
144
145 @click.command()146 @click.pass_context147 def cli(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 148–150why, one step back
148 assert get_language() == "en_US"
149 set_language("de_DE")150 assert get_language() == "de_DE"

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

lines 151–152why, one step back
151
152 runner.invoke(cli, [], catch_exceptions=False)

Merge pull request #1807 from pallets/pass-metaDavid Lordbefore adoptiona6fea31

lines 153–174why, one step back
153
154155def test_make_pass_meta_decorator(runner):156 @click.group()157 @click.pass_context158 def cli(ctx):159 ctx.meta["value"] = "good"160161 @cli.command()162 @pass_meta_key("value")163 def show(value):164 return value165166 result = runner.invoke(cli, ["show"], standalone_mode=False)167 assert result.return_value == "good"168169170def test_make_pass_meta_decorator_doc():171 pass_value = pass_meta_key("value")172 assert "the 'value' key from :attr:`click.Context.meta`" in pass_value.__doc__173 pass_value = pass_meta_key("value", doc_description="the test value")174 assert "passes the test value" in pass_value.__doc__

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

lines 175–206why, one step back
175
176177def test_context_pushing():178 rv = []179180 @click.command()181 def cli():182 pass183184 ctx = click.Context(cli)185186 @ctx.call_on_close187 def test_callback():188 rv.append(42)189190 with ctx.scope(cleanup=False):191 # Internal192 assert ctx._depth == 2193194 assert rv == []195196 with ctx.scope():197 # Internal198 assert ctx._depth == 1199200 assert rv == [42]201202203def test_pass_obj(runner):204 @click.group()205 @click.pass_context206 def cli(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 207why, one step back
207 ctx.obj = "test"

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

lines 208–213why, one step back
208
209 @cli.command()210 @click.pass_obj211 def test(obj):212 click.echo(obj)213

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 217–224why, one step back
217
218219def test_close_before_pop(runner):220 called = []221222 @click.command()223 @click.pass_context224 def cli(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 225why, one step back
225 ctx.obj = "test"

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

lines 226–228why, one step back
226
227 @ctx.call_on_close228 def foo():

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 230–231why, one step back
230 called.append(True)
231

Merge branch '7.x'David Lordbefore adoption7239794

lines 232–233why, one step back
232 click.echo("aha!")
233

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge pull request #1191 from smurfix/upstream2David Lordbefore adoption563b1a7

lines 238–253why, one step back
238
239240def test_with_resource():241 @contextmanager242 def manager():243 val = [1]244 yield val245 val[0] = 0246247 ctx = click.Context(click.Command("test"))248249 with ctx.scope():250 rv = ctx.with_resource(manager())251 assert rv[0] == 1252253 assert rv == [0]

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

lines 254–260why, one step back
254
255256def test_make_pass_decorator_args(runner):257 """258 Test to check that make_pass_decorator doesn't consume arguments based on259 invocation order.260 """

Merge branch '7.x'David Lordbefore adoption7239794

line 261why, one step back
261

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

line 262why, one step back
262 class Foo:

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 264–283why, one step back
264
265 pass_foo = click.make_pass_decorator(Foo)266267 @click.group()268 @click.pass_context269 def cli(ctx):270 ctx.obj = Foo()271272 @cli.command()273 @click.pass_context274 @pass_foo275 def test1(foo, ctx):276 click.echo(foo.title)277278 @cli.command()279 @pass_foo280 @click.pass_context281 def test2(ctx, foo):282 click.echo(foo.title)283

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 287why, one step back
287

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 291–292why, one step back
291
292

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

lines 293–306why, one step back
293def test_propagate_show_default_setting(runner):
294 """A context's ``show_default`` setting defaults to the value from295 the parent context.296 """297 group = click.Group(298 commands={299 "sub": click.Command("sub", params=[click.Option(["-a"], default="a")]),300 },301 context_settings={"show_default": True},302 )303 result = runner.invoke(group, ["sub", "--help"])304 assert "[default: a]" in result.output305306

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

lines 307–312why, one step back
307def test_exit_not_standalone():
308 @click.command()309 @click.pass_context310 def cli(ctx):311 ctx.exit(1)312

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 314–319why, one step back
314
315 @click.command()316 @click.pass_context317 def cli(ctx):318 ctx.exit(0)319

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 321why, one step back
321

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

line 322why, one step back
322

Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047

lines 323–359why, one step back
323@pytest.mark.parametrize(
324 ("option_args", "invoke_args", "expect"),325 [326 pytest.param({}, {}, ParameterSource.DEFAULT, id="default"),327 pytest.param(328 {},329 {"default_map": {"option": 1}},330 ParameterSource.DEFAULT_MAP,331 id="default_map",332 ),333 pytest.param(334 {},335 {"args": ["-o", "1"]},336 ParameterSource.COMMANDLINE,337 id="commandline short",338 ),339 pytest.param(340 {},341 {"args": ["--option", "1"]},342 ParameterSource.COMMANDLINE,343 id="commandline long",344 ),345 pytest.param(346 {},347 {"auto_envvar_prefix": "TEST", "env": {"TEST_OPTION": "1"}},348 ParameterSource.ENVIRONMENT,349 id="environment auto",350 ),351 pytest.param(352 {"envvar": "NAME"},353 {"env": {"NAME": "1"}},354 ParameterSource.ENVIRONMENT,355 id="environment manual",356 ),357 ],358)359def test_parameter_source(runner, option_args, invoke_args, expect):

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

lines 360–361why, one step back
360 @click.command()
361 @click.pass_context

Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047

line 362why, one step back
362 @click.option("-o", "--option", default=1, **option_args)

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

line 363why, one step back
363 def cli(ctx, option):

Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047

line 364why, one step back
364 return ctx.get_parameter_source("option")

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

line 365why, one step back
365

Merge pull request #1535 from alanbato/parameter-source-enumDavid Lordbefore adoption861e047

lines 366–367why, one step back
366 rv = runner.invoke(cli, standalone_mode=False, **invoke_args)
367 assert rv.return_value == expect