Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

691 lines · 20.6 KiB · Python

Last changed by

Merge pull request #1618 from MLH-Fellowship/prompt-requiredDavid Lordbefore adoption8efb348

History of this file

Take it

The walk stopped after 200 commits: lines older than becfca5 point at it.

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 1why, one step back
1import os

Merge branch '7.x'David Lordbefore adoption7239794

lines 2–3why, one step back
2import re
3

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 6why, one step back
6import click

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 7–10why, one step back
7
89def test_prefixes(runner):10 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 11–12why, one step back
11 @click.option("++foo", is_flag=True, help="das foo")
12 @click.option("--bar", is_flag=True, help="das bar")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 13why, one step back
13 def cli(foo, bar):

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

line 14why, one step back
14 click.echo(f"foo={foo} bar={bar}")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 15why, one step back
15

Merge branch '7.x'David Lordbefore adoption7239794

line 16why, one step back
16 result = runner.invoke(cli, ["++foo", "--bar"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 18why, one step back
18 assert result.output == "foo=True bar=True\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 19why, one step back
19

Merge branch '7.x'David Lordbefore adoption7239794

lines 20–22why, one step back
20 result = runner.invoke(cli, ["--help"])
21 assert re.search(r"\+\+foo\s+das foo", result.output) is not None22 assert re.search(r"--bar\s+das bar", result.output) is not None

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 23–25why, one step back
23
2425def test_invalid_option(runner):

Merge pull request #1355 from jtrakk/patch-1David Lordbefore adoption0ad4e79

lines 26–27why, one step back
26 with pytest.raises(TypeError, match="name was passed") as exc_info:
27 click.Option(["foo"])

Merge branch '7.x'David Lordbefore adoption7239794

line 28why, one step back
28

Merge pull request #1355 from jtrakk/patch-1David Lordbefore adoption0ad4e79

lines 29–32why, one step back
29 message = str(exc_info.value)
30 assert "name was passed (foo)" in message31 assert "declare an argument" in message32 assert "'--foo'" in message

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 33–35why, one step back
33
3435def test_invalid_nargs(runner):

Merge branch '7.x'David Lordbefore adoption7239794

lines 36–37why, one step back
36 with pytest.raises(TypeError, match="nargs < 0"):
37

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 38why, one step back
38 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 39why, one step back
39 @click.option("--foo", nargs=-1)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 40–45why, one step back
40 def cli(foo):
41 pass424344def test_nargs_tup_composite_mult(runner):45 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 46why, one step back
46 @click.option("--item", type=(str, int), multiple=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 47why, one step back
47 def copy(item):

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

lines 48–49why, one step back
48 for name, id in item:
49 click.echo(f"name={name} id={id:d}")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 50why, one step back
50

Merge branch '7.x'David Lordbefore adoption7239794

line 51why, one step back
51 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 53why, one step back
53 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 54–57why, one step back
54
5556def test_counting(runner):57 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 58why, one step back
58 @click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 59why, one step back
59 def cli(v):

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

line 60why, one step back
60 click.echo(f"verbosity={v:d}")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 61why, one step back
61

Merge branch '7.x'David Lordbefore adoption7239794

line 62why, one step back
62 result = runner.invoke(cli, ["-vvv"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 64why, one step back
64 assert result.output == "verbosity=3\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 65why, one step back
65

Merge branch '7.x'David Lordbefore adoption7239794

line 66why, one step back
66 result = runner.invoke(cli, ["-vvvv"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 67why, one step back
67 assert result.exception

Merge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab

line 68why, one step back
68 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 73why, one step back
73

Merge branch '7.x'David Lordbefore adoption7239794

lines 74–75why, one step back
74 result = runner.invoke(cli, ["--help"])
75 assert re.search(r"-v\s+Verbosity", result.output) is not None

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 76–77why, one step back
76
77

Merge branch '7.x'David Lordbefore adoption7239794

line 78why, one step back
78@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 79–85why, one step back
79def test_unknown_options(runner, unknown_flag):
80 @click.command()81 def cli():82 pass8384 result = runner.invoke(cli, [unknown_flag])85 assert result.exception

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

line 86why, one step back
86 assert f"no such option: {unknown_flag}" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 87–88why, one step back
87
88

Merge pull request #1455 from Na-Ne/masterDavid Lordbefore adoptiona141a2a

lines 89–104why, one step back
89@pytest.mark.parametrize(
90 ("value", "expect"),91 [92 ("--cat", "Did you mean --count?"),93 ("--bounds", "(Possible options: --bound, --count)"),94 ("--bount", "(Possible options: --bound, --count)"),95 ],96)97def test_suggest_possible_options(runner, value, expect):98 cli = click.Command(99 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]100 )101 result = runner.invoke(cli, [value])102 assert expect in result.output103104

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 105–106why, one step back
105def test_multiple_required(runner):
106 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 107why, one step back
107 @click.option("-m", "--message", multiple=True, required=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 108why, one step back
108 def cli(message):

Merge branch '7.x'David Lordbefore adoption7239794

line 109why, one step back
109 click.echo("\n".join(message))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 110why, one step back
110

Merge branch '7.x'David Lordbefore adoption7239794

line 111why, one step back
111 result = runner.invoke(cli, ["-m", "foo", "-mbar"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 113why, one step back
113 assert result.output == "foo\nbar\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 114–116why, one step back
114
115 result = runner.invoke(cli, [])116 assert result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 117why, one step back
117 assert "Error: Missing option '-m' / '--message'." in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 118–119why, one step back
118
119

Merge branch '7.x'David Lordbefore adoption2269786

lines 120–121why, one step back
120def test_empty_envvar(runner):
121 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 122why, one step back
122 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")

Merge branch '7.x'David Lordbefore adoption2269786

line 123why, one step back
123 def cli(mypath):

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

line 124why, one step back
124 click.echo(f"mypath: {mypath}")

Merge branch '7.x'David Lordbefore adoption2269786

line 125why, one step back
125

Merge branch '7.x'David Lordbefore adoption7239794

line 126why, one step back
126 result = runner.invoke(cli, [], env={"MYPATH": ""})

Merge branch '7.x'David Lordbefore adoption2269786

line 127why, one step back
127 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 128why, one step back
128 assert result.output == "mypath: None\n"

Merge branch '7.x'David Lordbefore adoption2269786

lines 129–130why, one step back
129
130

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 131–132why, one step back
131def test_multiple_envvar(runner):
132 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 133why, one step back
133 @click.option("--arg", multiple=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 134why, one step back
134 def cmd(arg):

Merge branch '7.x'David Lordbefore adoption7239794

line 135why, one step back
135 click.echo("|".join(arg))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 136why, one step back
136

Merge branch '7.x'David Lordbefore adoption7239794

lines 137–139why, one step back
137 result = runner.invoke(
138 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}139 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 141why, one step back
141 assert result.output == "foo|bar|baz\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 142–143why, one step back
142
143 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 144why, one step back
144 @click.option("--arg", multiple=True, envvar="X")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 145why, one step back
145 def cmd(arg):

Merge branch '7.x'David Lordbefore adoption7239794

line 146why, one step back
146 click.echo("|".join(arg))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 147why, one step back
147

Merge branch '7.x'David Lordbefore adoption7239794

line 148why, one step back
148 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 150why, one step back
150 assert result.output == "foo|bar|baz\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 151–152why, one step back
151
152 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 153why, one step back
153 @click.option("--arg", multiple=True, type=click.Path())

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 154why, one step back
154 def cmd(arg):

Merge branch '7.x'David Lordbefore adoption7239794

line 155why, one step back
155 click.echo("|".join(arg))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 156why, one step back
156

Merge branch '7.x'David Lordbefore adoption7239794

lines 157–160why, one step back
157 result = runner.invoke(
158 cmd,159 [],160 auto_envvar_prefix="TEST",

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

line 161why, one step back
161 env={"TEST_ARG": f"foo{os.path.pathsep}bar"},

Merge branch '7.x'David Lordbefore adoption7239794

line 162why, one step back
162 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 164why, one step back
164 assert result.output == "foo|bar\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 165–166why, one step back
165
166

Merge pull request #1606 from andy-maier/andy/fix-trailing-blanksDavid Lordbefore adoption25ee366

lines 167–177why, one step back
167def test_trailing_blanks_boolean_envvar(runner):
168 @click.command()169 @click.option("--shout/--no-shout", envvar="SHOUT")170 def cli(shout):171 click.echo(f"shout: {shout!r}")172173 result = runner.invoke(cli, [], env={"SHOUT": " true "})174 assert result.exit_code == 0175 assert result.output == "shout: True\n"176177

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 178–179why, one step back
178def test_multiple_default_help(runner):
179 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 180–181why, one step back
180 @click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)
181 @click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 182–184why, one step back
182 def cmd(arg, arg2):
183 pass184

Merge branch '7.x'David Lordbefore adoption7239794

line 185why, one step back
185 result = runner.invoke(cmd, ["--help"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 187–188why, one step back
187 assert "foo, bar" in result.output
188 assert "1, 2" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 189–190why, one step back
189
190

Merge pull request #1549 from ewjoachim/default-map-show-default-1548David Lordbefore adoption7332f00

lines 191–202why, one step back
191def test_show_default_default_map(runner):
192 @click.command()193 @click.option("--arg", default="a", show_default=True)194 def cmd(arg):195 click.echo(arg)196197 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})198199 assert not result.exception200 assert "[default: b]" in result.output201202

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 203–209why, one step back
203def test_multiple_default_type():
204 opt = click.Option(["-a"], multiple=True, default=(1, 2))205 assert opt.nargs == 1206 assert opt.multiple207 assert opt.type is click.INT208 ctx = click.Context(click.Command("test"))209 assert opt.get_default(ctx) == (1, 2)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 210–211why, one step back
210
211

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 212–219why, one step back
212def test_multiple_default_composite_type():
213 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])214 assert opt.nargs == 2215 assert opt.multiple216 assert isinstance(opt.type, click.Tuple)217 assert opt.type.types == [click.INT, click.STRING]218 ctx = click.Context(click.Command("test"))219 assert opt.get_default(ctx) == ((1, "a"),)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 220why, one step back
220

Merge branch '7.x'David Lordbefore adoption7239794

line 221why, one step back
221

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 222–235why, one step back
222def test_parse_multiple_default_composite_type(runner):
223 @click.command()224 @click.option("-a", multiple=True, default=("a", "b"))225 @click.option("-b", multiple=True, default=[(1, "a")])226 def cmd(a, b):227 click.echo(a)228 click.echo(b)229230 # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())231 # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"232 result = runner.invoke(cmd)233 assert result.output == "('a', 'b')\n((1, 'a'),)\n"234235

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 236–237why, one step back
236def test_dynamic_default_help_unset(runner):
237 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 238–243why, one step back
238 @click.option(
239 "--username",240 prompt=True,241 default=lambda: os.environ.get("USER", ""),242 show_default=True,243 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 244–246why, one step back
244 def cmd(username):
245 print("Hello,", username)246

Merge branch '7.x'David Lordbefore adoption7239794

line 247why, one step back
247 result = runner.invoke(cmd, ["--help"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 248why, one step back
248 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

lines 249–251why, one step back
249 assert "--username" in result.output
250 assert "lambda" not in result.output251 assert "(dynamic)" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 252why, one step back
252

Merge branch '7.x'David Lordbefore adoption7239794

line 253why, one step back
253

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 254–255why, one step back
254def test_dynamic_default_help_text(runner):
255 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 256–261why, one step back
256 @click.option(
257 "--username",258 prompt=True,259 default=lambda: os.environ.get("USER", ""),260 show_default="current user",261 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 262–264why, one step back
262 def cmd(username):
263 print("Hello,", username)264

Merge branch '7.x'David Lordbefore adoption7239794

line 265why, one step back
265 result = runner.invoke(cmd, ["--help"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 266why, one step back
266 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

lines 267–269why, one step back
267 assert "--username" in result.output
268 assert "lambda" not in result.output269 assert "(current user)" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 270why, one step back
270

Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d

line 271why, one step back
271

Merge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab

lines 272–281why, one step back
272@pytest.mark.parametrize(
273 ("type", "expect"),274 [275 (click.IntRange(1, 32), "1<=x<=32"),276 (click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),277 (click.IntRange(1), "x>=1"),278 (click.IntRange(max=32), "x<=32"),279 ],280)281def test_intrange_default_help_text(runner, type, expect):

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

line 282why, one step back
282 option = click.Option(["--count"], type=type, show_default=True, default=2)

Merge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab

lines 283–285why, one step back
283 context = click.Context(click.Command("test"))
284 result = option.get_help_record(context)[1]285 assert expect in result

Merge pull request #1586 from IamCathal/coc-intrangepreviewDavid Lordbefore adoptione8de91d

lines 286–287why, one step back
286
287

Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d

lines 288–289why, one step back
288def test_toupper_envvar_prefix(runner):
289 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 290why, one step back
290 @click.option("--arg")

Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d

lines 291–293why, one step back
291 def cmd(arg):
292 click.echo(arg)293

Merge branch '7.x'David Lordbefore adoption7239794

line 294why, one step back
294 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})

Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d

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

Merge branch '7.x'David Lordbefore adoption7239794

line 296why, one step back
296 assert result.output == "foo\n"

Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d

lines 297–298why, one step back
297
298

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 299–300why, one step back
299def test_nargs_envvar(runner):
300 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 301why, one step back
301 @click.option("--arg", nargs=2)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 302why, one step back
302 def cmd(arg):

Merge branch '7.x'David Lordbefore adoption7239794

line 303why, one step back
303 click.echo("|".join(arg))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 304why, one step back
304

Merge branch '7.x'David Lordbefore adoption7239794

lines 305–307why, one step back
305 result = runner.invoke(
306 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}307 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 309why, one step back
309 assert result.output == "foo|bar\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 310–311why, one step back
310
311 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 312why, one step back
312 @click.option("--arg", nargs=2, multiple=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 313–314why, one step back
313 def cmd(arg):
314 for item in arg:

Merge branch '7.x'David Lordbefore adoption7239794

line 315why, one step back
315 click.echo("|".join(item))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 316why, one step back
316

Merge branch '7.x'David Lordbefore adoption7239794

lines 317–319why, one step back
317 result = runner.invoke(
318 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}319 )

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 321why, one step back
321 assert result.output == "x|1\ny|2\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 322–325why, one step back
322
323324def test_show_envvar(runner):325 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 326why, one step back
326 @click.option("--arg1", envvar="ARG1", show_envvar=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 327–329why, one step back
327 def cmd(arg):
328 pass329

Merge branch '7.x'David Lordbefore adoption7239794

line 330why, one step back
330 result = runner.invoke(cmd, ["--help"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 332why, one step back
332 assert "ARG1" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 333–336why, one step back
333
334335def test_show_envvar_auto_prefix(runner):336 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 337why, one step back
337 @click.option("--arg1", show_envvar=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 338–340why, one step back
338 def cmd(arg):
339 pass340

Merge branch '7.x'David Lordbefore adoption7239794

line 341why, one step back
341 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

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

Merge branch '7.x'David Lordbefore adoption7239794

line 343why, one step back
343 assert "TEST_ARG1" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 344–345why, one step back
344
345

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 346–351why, one step back
346def test_show_envvar_auto_prefix_dash_in_command(runner):
347 @click.group()348 def cli():349 pass350351 @cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 352why, one step back
352 @click.option("--baz", show_envvar=True)

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 353–355why, one step back
353 def foo_bar(baz):
354 pass355

Merge branch '7.x'David Lordbefore adoption7239794

line 356why, one step back
356 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")

Merge branch '7.x'David Lordbefore adoption8b971f7

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

Merge branch '7.x'David Lordbefore adoption7239794

line 358why, one step back
358 assert "TEST_FOO_BAR_BAZ" in result.output

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 359–360why, one step back
359
360

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 361–363why, one step back
361def test_custom_validation(runner):
362 def validate_pos_int(ctx, param, value):363 if value < 0:

Merge branch '7.x'David Lordbefore adoption7239794

line 364why, one step back
364 raise click.BadParameter("Value needs to be positive")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 365–367why, one step back
365 return value
366367 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 368why, one step back
368 @click.option("--foo", callback=validate_pos_int, default=1)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 369–371why, one step back
369 def cmd(foo):
370 click.echo(foo)371

Merge branch '7.x'David Lordbefore adoption7239794

lines 372–373why, one step back
372 result = runner.invoke(cmd, ["--foo", "-1"])
373 assert "Invalid value for '--foo': Value needs to be positive" in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 374why, one step back
374

Merge branch '7.x'David Lordbefore adoption7239794

lines 375–376why, one step back
375 result = runner.invoke(cmd, ["--foo", "42"])
376 assert result.output == "42\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 377–380why, one step back
377
378379def test_winstyle_options(runner):380 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 381why, one step back
381 @click.option("/debug;/no-debug", help="Enables or disables debug mode.")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 382–384why, one step back
382 def cmd(debug):
383 click.echo(debug)384

Merge branch '7.x'David Lordbefore adoption7239794

lines 385–393why, one step back
385 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])
386 assert result.output == "True\n"387 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])388 assert result.output == "False\n"389 result = runner.invoke(cmd, [], help_option_names=["/?"])390 assert result.output == "False\n"391 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])392 assert "/debug; /no-debug Enables or disables debug mode." in result.output393 assert "/? Show this message and exit." in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 394–397why, one step back
394
395396def test_legacy_options(runner):397 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 398why, one step back
398 @click.option("-whatever")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 399–401why, one step back
399 def cmd(whatever):
400 click.echo(whatever)401

Merge branch '7.x'David Lordbefore adoption7239794

lines 402–405why, one step back
402 result = runner.invoke(cmd, ["-whatever", "42"])
403 assert result.output == "42\n"404 result = runner.invoke(cmd, ["-whatever=23"])405 assert result.output == "23\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 406–407why, one step back
406
407

Merge branch '7.x'David Lordbefore adoption2269786

lines 408–411why, one step back
408def test_missing_option_string_cast():
409 ctx = click.Context(click.Command(""))410411 with pytest.raises(click.MissingParameter) as excinfo:

Merge branch '7.x'David Lordbefore adoption7239794

line 412why, one step back
412 click.Option(["-a"], required=True).full_process_value(ctx, None)

Merge branch '7.x'David Lordbefore adoption2269786

lines 413–416why, one step back
413
414 assert str(excinfo.value) == "missing parameter: a"415416

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 417–418why, one step back
417def test_missing_choice(runner):
418 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 419why, one step back
419 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 420–424why, one step back
420 def cmd(foo):
421 click.echo(foo)422423 result = runner.invoke(cmd)424 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 425–429why, one step back
425 error, separator, choices = result.output.partition("Choose from")
426 assert "Error: Missing option '--foo'. " in error427 assert "Choose from" in separator428 assert "foo" in choices429 assert "bar" in choices

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 430–433why, one step back
430
431432def test_case_insensitive_choice(runner):433 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 434why, one step back
434 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 435–437why, one step back
435 def cmd(foo):
436 click.echo(foo)437

Merge branch '7.x'David Lordbefore adoption7239794

line 438why, one step back
438 result = runner.invoke(cmd, ["--foo", "apple"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 439why, one step back
439 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 440why, one step back
440 assert result.output == "Apple\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 441why, one step back
441

Merge branch '7.x'David Lordbefore adoption7239794

line 442why, one step back
442 result = runner.invoke(cmd, ["--foo", "oRANGe"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 443why, one step back
443 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 444why, one step back
444 assert result.output == "Orange\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 445why, one step back
445

Merge branch '7.x'David Lordbefore adoption7239794

line 446why, one step back
446 result = runner.invoke(cmd, ["--foo", "Apple"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 447why, one step back
447 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 448why, one step back
448 assert result.output == "Apple\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 449–450why, one step back
449
450 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 451why, one step back
451 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 452–454why, one step back
452 def cmd2(foo):
453 click.echo(foo)454

Merge branch '7.x'David Lordbefore adoption7239794

line 455why, one step back
455 result = runner.invoke(cmd2, ["--foo", "apple"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 456–457why, one step back
456 assert result.exit_code == 2
457

Merge branch '7.x'David Lordbefore adoption7239794

line 458why, one step back
458 result = runner.invoke(cmd2, ["--foo", "oRANGe"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 459–460why, one step back
459 assert result.exit_code == 2
460

Merge branch '7.x'David Lordbefore adoption7239794

line 461why, one step back
461 result = runner.invoke(cmd2, ["--foo", "Apple"])

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 462why, one step back
462 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption11abf2b

lines 463–466why, one step back
463
464465def test_case_insensitive_choice_returned_exactly(runner):466 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 467why, one step back
467 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))

Merge branch '7.x'David Lordbefore adoption11abf2b

lines 468–470why, one step back
468 def cmd(foo):
469 click.echo(foo)470

Merge branch '7.x'David Lordbefore adoption7239794

line 471why, one step back
471 result = runner.invoke(cmd, ["--foo", "apple"])

Merge branch '7.x'David Lordbefore adoption11abf2b

line 472why, one step back
472 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 473why, one step back
473 assert result.output == "Apple\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 474–475why, one step back
474
475

Merge branch '7.x'David Lordbefore adoption8b971f7

line 476why, one step back
476def test_option_help_preserve_paragraphs(runner):

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 477why, one step back
477 @click.command()

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 478–482why, one step back
478 @click.option(
479 "-C",480 "--config",481 type=click.Path(),482 help="""Configuration file to use.

Merge branch '7.x'David Lordbefore adoption7239794

line 483why, one step back
483

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 484–489why, one step back
484 If not given, the environment variable CONFIG_FILE is consulted
485 and used if set. If neither are given, a default configuration486 file is loaded.""",487 )488 def cmd(config):489 pass

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 490why, one step back
490

Merge branch '7.x'David Lordbefore adoption7239794

line 491why, one step back
491 result = runner.invoke(cmd, ["--help"],)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 492why, one step back
492 assert result.exit_code == 0

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

line 493why, one step back
493 i = " " * 21

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 494–495why, one step back
494 assert (
495 " -C, --config PATH Configuration file to use.\n"

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

lines 496–499why, one step back
496 f"{i}\n"
497 f"{i}If not given, the environment variable CONFIG_FILE is\n"498 f"{i}consulted and used if set. If neither are given, a default\n"499 f"{i}configuration file is loaded."

Merge branch '7.x'David Lordbefore adoption8b971f7

line 500why, one step back
500 ) in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 501why, one step back
501

Merge branch '7.x'David Lordbefore adoption7239794

line 502why, one step back
502

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 503–505why, one step back
503def test_argument_custom_class(runner):
504 class CustomArgument(click.Argument):505 def get_default(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 506–507why, one step back
506 """a dumb override of a default value for testing"""
507 return "I am a default"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 508–509why, one step back
508
509 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 510why, one step back
510 @click.argument("testarg", cls=CustomArgument, default="you wont see me")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 511–514why, one step back
511 def cmd(testarg):
512 click.echo(testarg)513514 result = runner.invoke(cmd)

Merge branch '7.x'David Lordbefore adoption7239794

lines 515–516why, one step back
515 assert "I am a default" in result.output
516 assert "you wont see me" not in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 517–521why, one step back
517
518519def test_option_custom_class(runner):520 class CustomOption(click.Option):521 def get_help_record(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 522–523why, one step back
522 """a dumb override of a help text for testing"""
523 return ("--help", "I am a help text")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 524–525why, one step back
524
525 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 526why, one step back
526 @click.option("--testoption", cls=CustomOption, help="you wont see me")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 527–529why, one step back
527 def cmd(testoption):
528 click.echo(testoption)529

Merge branch '7.x'David Lordbefore adoption7239794

lines 530–532why, one step back
530 result = runner.invoke(cmd, ["--help"])
531 assert "I am a help text" in result.output532 assert "you wont see me" not in result.output

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 533–539why, one step back
533
534535def test_option_custom_class_reusable(runner):536 """Ensure we can reuse a custom class option. See Issue #926"""537538 class CustomOption(click.Option):539 def get_help_record(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 540–541why, one step back
540 """a dumb override of a help text for testing"""
541 return ("--help", "I am a help text")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 542–543why, one step back
542
543 # Assign to a variable to re-use the decorator.

Merge branch '7.x'David Lordbefore adoption7239794

line 544why, one step back
544 testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 545–558why, one step back
545
546 @click.command()547 @testoption548 def cmd1(testoption):549 click.echo(testoption)550551 @click.command()552 @testoption553 def cmd2(testoption):554 click.echo(testoption)555556 # Both of the commands should have the --help option now.557 for cmd in (cmd1, cmd2):558

Merge branch '7.x'David Lordbefore adoption7239794

lines 559–561why, one step back
559 result = runner.invoke(cmd, ["--help"])
560 assert "I am a help text" in result.output561 assert "you wont see me" not in result.output

Merge branch '7.x'David Lordbefore adoption2269786

lines 562–565why, one step back
562
563564def test_bool_flag_with_type(runner):565 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 566why, one step back
566 @click.option("--shout/--no-shout", default=False, type=bool)

Merge branch '7.x'David Lordbefore adoption2269786

lines 567–571why, one step back
567 def cmd(shout):
568 pass569570 result = runner.invoke(cmd)571 assert not result.exception

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 572–575why, one step back
572
573574def test_aliases_for_flags(runner):575 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 576why, one step back
576 @click.option("--warnings/--no-warnings", " /-W", default=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 577–579why, one step back
577 def cli(warnings):
578 click.echo(warnings)579

Merge branch '7.x'David Lordbefore adoption7239794

lines 580–585why, one step back
580 result = runner.invoke(cli, ["--warnings"])
581 assert result.output == "True\n"582 result = runner.invoke(cli, ["--no-warnings"])583 assert result.output == "False\n"584 result = runner.invoke(cli, ["-W"])585 assert result.output == "False\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 586–587why, one step back
586
587 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 588why, one step back
588 @click.option("--warnings/--no-warnings", "-w", default=True)

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 589–591why, one step back
589 def cli_alt(warnings):
590 click.echo(warnings)591

Merge branch '7.x'David Lordbefore adoption7239794

lines 592–597why, one step back
592 result = runner.invoke(cli_alt, ["--warnings"])
593 assert result.output == "True\n"594 result = runner.invoke(cli_alt, ["--no-warnings"])595 assert result.output == "False\n"596 result = runner.invoke(cli_alt, ["-w"])597 assert result.output == "True\n"

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 598–599why, one step back
598
599

Merge branch '7.x'David Lordbefore adoption7239794

lines 600–615why, one step back
600@pytest.mark.parametrize(
601 "option_args,expected",602 [603 (["--aggressive", "--all", "-a"], "aggressive"),604 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),605 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),606 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),607 (["-a", "-b", "-c"], "a"),608 (["-c", "-b", "-a"], "c"),609 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),610 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),611 (["--from", "-f", "_from"], "_from"),612 (["--return", "-r", "_ret"], "_ret"),613 ],614)615def test_option_names(runner, option_args, expected):

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

lines 616–623why, one step back
616 @click.command()
617 @click.option(*option_args, is_flag=True)618 def cmd(**kwargs):619 click.echo(str(kwargs[expected]))620621 assert cmd.params[0].name == expected622623 for form in option_args:

Merge branch '7.x'David Lordbefore adoption7239794

line 624why, one step back
624 if form.startswith("-"):

Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5

line 625why, one step back
625 result = runner.invoke(cmd, [form])

Merge branch '7.x'David Lordbefore adoption7239794

line 626why, one step back
626 assert result.output == "True\n"

Merge pull request #1594 from MLH-Fellowship/duplicate-boolean-flagsDavid Lordbefore adoptiondfb842d

lines 627–631why, one step back
627
628629def test_flag_duplicate_names(runner):630 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):631 click.Option(["--foo/--foo"], default=False)

Merge pull request #1617 from MLH-Fellowship/issue1538David Lordbefore adoption66172ef

lines 632–660why, one step back
632
633634@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])635def test_show_default_boolean_flag_name(runner, default, expect):636 """When a boolean flag has distinct True/False opts, it should show637 the default opt name instead of the default value. It should only638 show one name even if multiple are declared.639 """640 opt = click.Option(641 ("--cache/--no-cache", "--c/--nc"),642 default=default,643 show_default=True,644 help="Enable/Disable the cache.",645 )646 ctx = click.Context(click.Command("test"))647 message = opt.get_help_record(ctx)[1]648 assert f"[default: {expect}]" in message649650651def test_show_default_boolean_flag_value(runner):652 """When a boolean flag only has one opt, it will show the default653 value, not the opt name.654 """655 opt = click.Option(656 ("--cache",), is_flag=True, show_default=True, help="Enable the cache.",657 )658 ctx = click.Context(click.Command("test"))659 message = opt.get_help_record(ctx)[1]660 assert "[default: False]" in message

Merge pull request #1618 from MLH-Fellowship/prompt-requiredDavid Lordbefore adoption8efb348

lines 661–691why, one step back
661
662663@pytest.mark.parametrize(664 ("args", "expect"),665 [666 (None, (None, None, ())),667 (["--opt"], ("flag", None, ())),668 (["--opt", "-a", 42], ("flag", "42", ())),669 (["--opt", "test", "-a", 42], ("test", "42", ())),670 (["--opt=test", "-a", 42], ("test", "42", ())),671 (["-o"], ("flag", None, ())),672 (["-o", "-a", 42], ("flag", "42", ())),673 (["-o", "test", "-a", 42], ("test", "42", ())),674 (["-otest", "-a", 42], ("test", "42", ())),675 (["a", "b", "c"], (None, None, ("a", "b", "c"))),676 (["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),677 (["--opt", "test"], ("test", None, ())),678 (["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),679 (["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),680 ],681)682def test_option_with_optional_value(runner, args, expect):683 @click.command()684 @click.option("-o", "--opt", is_flag=False, flag_value="flag")685 @click.option("-a")686 @click.argument("b", nargs=-1)687 def cli(opt, a, b):688 return opt, a, b689690 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)691 assert result.return_value == expect