Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

572 lines · 16.5 KiB · Python

Last changed by

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

History of this file

Take it

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 1why, one step back
1import os

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

line 6why, one step back
6import click

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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"

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 26–27why, one step back
26 with pytest.raises(TypeError, match="name was passed"):
27

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

line 29why, one step back
29 @click.option("foo")

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 30–34why, one step back
30 def cli(foo):
31 pass323334def test_invalid_nargs(runner):

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 39–44why, one step back
39 def cli(foo):
40 pass414243def test_nargs_tup_composite_mult(runner):44 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

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

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 49why, one step back
49

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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.splitlines() == ["name=peter id=1", "name=max id=2"]

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 53–56why, one step back
53
5455def test_counting(runner):56 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

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

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 60why, one step back
60

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 64why, one step back
64

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 67–68why, one step back
67 assert (
68 "Invalid value for '-v': 4 is not in the valid range of 0 to 3."

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 69why, one step back
69 in result.output

Merge branch '7.x'David Lordbefore adoption7239794

line 70why, one step back
70 )

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 75why, one step back
75

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 78–79why, one step back
78
79

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 81–87why, one step back
81def test_unknown_options(runner, unknown_flag):
82 @click.command()83 def cli():84 pass8586 result = runner.invoke(cli, [unknown_flag])87 assert result.exception

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

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 89–92why, one step back
89
9091def test_multiple_required(runner):92 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 96why, one step back
96

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 100–102why, one step back
100
101 result = runner.invoke(cli, [])102 assert result.exception

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 104–105why, one step back
104
105

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

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

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

Merge branch '7.x'David Lordbefore adoption2269786

line 111why, one step back
111

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 115–116why, one step back
115
116

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 122why, one step back
122

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 128–129why, one step back
128
129 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 133why, one step back
133

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 137–138why, one step back
137
138 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 142why, one step back
142

Merge branch '7.x'David Lordbefore adoption7239794

lines 143–146why, one step back
143 result = runner.invoke(
144 cmd,145 [],146 auto_envvar_prefix="TEST",

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 148why, one step back
148 )

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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\n"

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 151–154why, one step back
151
152153def test_multiple_default_help(runner):154 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 157–159why, one step back
157 def cmd(arg, arg2):
158 pass159

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 164–167why, one step back
164
165166def test_multiple_default_type(runner):167 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 168–169why, one step back
168 @click.option("--arg1", multiple=True, default=("foo", "bar"))
169 @click.option("--arg2", multiple=True, default=(1, "a"))

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 170why, one step back
170 def cmd(arg1, arg2):

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

lines 171–172why, one step back
171 assert all(isinstance(e[0], str) for e in arg1)
172 assert all(isinstance(e[1], str) for e in arg1)

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 173–174why, one step back
173
174 assert all(isinstance(e[0], int) for e in arg2)

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

line 175why, one step back
175 assert all(isinstance(e[1], str) for e in arg2)

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 176why, one step back
176

Merge branch '7.x'David Lordbefore adoption7239794

lines 177–179why, one step back
177 result = runner.invoke(
178 cmd, "--arg1 a b --arg1 test 1 --arg2 2 two --arg2 4 four".split()179 )

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 180–181why, one step back
180 assert not result.exception
181

Merge branch '7.x'David Lordbefore adoption7239794

line 182why, one step back
182

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 185–190why, one step back
185 @click.option(
186 "--username",187 prompt=True,188 default=lambda: os.environ.get("USER", ""),189 show_default=True,190 )

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

lines 191–192why, one step back
191 def cmd(username):
192 print("Hello,", username)

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 193why, one step back
193

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 196–198why, one step back
196 assert "--username" in result.output
197 assert "lambda" not in result.output198 assert "(dynamic)" in result.output

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

line 199why, one step back
199

Merge branch '7.x'David Lordbefore adoption7239794

line 200why, one step back
200

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 203–208why, one step back
203 @click.option(
204 "--username",205 prompt=True,206 default=lambda: os.environ.get("USER", ""),207 show_default="current user",208 )

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

lines 209–211why, one step back
209 def cmd(username):
210 print("Hello,", username)211

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 214–216why, one step back
214 assert "--username" in result.output
215 assert "lambda" not in result.output216 assert "(current user)" in result.output

Merge pull request #996 from dougharris/lambda-844-547Dan Sullybefore adoptionbc53a96

line 217why, one step back
217

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

line 218why, one step back
218

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

lines 219–228why, one step back
219def test_intrange_default_help_text(runner):
220 @click.command()221 @click.option("--count", type=click.IntRange(1, 32), show_default=True, default=1)222 def cmd(arg):223 click.echo(arg)224225 result = runner.invoke(cmd, ["--help"])226 assert "1-32 inclusive" in result.output227228

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 232–234why, one step back
232 def cmd(arg):
233 click.echo(arg)234

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 238–239why, one step back
238
239

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 245why, one step back
245

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 251–252why, one step back
251
252 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 257why, one step back
257

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 263–264why, one step back
263
264

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

lines 265–266why, one step back
265def test_show_envvar(runner):
266 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

lines 268–270why, one step back
268 def cmd(arg):
269 pass270

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

lines 274–277why, one step back
274
275276def test_show_envvar_auto_prefix(runner):277 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

lines 279–281why, one step back
279 def cmd(arg):
280 pass281

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #710 from iffy/show_envvarDan Sullybefore adoption961a441

lines 285–286why, one step back
285
286

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 287–292why, one step back
287def test_show_envvar_auto_prefix_dash_in_command(runner):
288 @click.group()289 def cli():290 pass291292 @cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 294–296why, one step back
294 def foo_bar(baz):
295 pass296

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption8b971f7

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 300–301why, one step back
300
301

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 302why, one step back
302def test_custom_validation(runner):

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption7404b06

line 303why, one step back
303 def validate_pos_int(ctx, param, value):

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 304why, one step back
304 if value < 0:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 306–308why, one step back
306 return value
307308 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 310–312why, one step back
310 def cmd(foo):
311 click.echo(foo)312

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 315why, one step back
315

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 318–321why, one step back
318
319320def test_winstyle_options(runner):321 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 323–325why, one step back
323 def cmd(debug):
324 click.echo(debug)325

Merge branch '7.x'David Lordbefore adoption7239794

lines 326–334why, one step back
326 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])
327 assert result.output == "True\n"328 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])329 assert result.output == "False\n"330 result = runner.invoke(cmd, [], help_option_names=["/?"])331 assert result.output == "False\n"332 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])333 assert "/debug; /no-debug Enables or disables debug mode." in result.output334 assert "/? Show this message and exit." in result.output

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 335–338why, one step back
335
336337def test_legacy_options(runner):338 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 340–342why, one step back
340 def cmd(whatever):
341 click.echo(whatever)342

Merge branch '7.x'David Lordbefore adoption7239794

lines 343–346why, one step back
343 result = runner.invoke(cmd, ["-whatever", "42"])
344 assert result.output == "42\n"345 result = runner.invoke(cmd, ["-whatever=23"])346 assert result.output == "23\n"

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 347–348why, one step back
347
348

Merge branch '7.x'David Lordbefore adoption2269786

lines 349–352why, one step back
349def test_missing_option_string_cast():
350 ctx = click.Context(click.Command(""))351352 with pytest.raises(click.MissingParameter) as excinfo:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 354–357why, one step back
354
355 assert str(excinfo.value) == "missing parameter: a"356357

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 361–365why, one step back
361 def cmd(foo):
362 click.echo(foo)363364 result = runner.invoke(cmd)365 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 366–370why, one step back
366 error, separator, choices = result.output.partition("Choose from")
367 assert "Error: Missing option '--foo'. " in error368 assert "Choose from" in separator369 assert "foo" in choices370 assert "bar" in choices

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 371–372why, one step back
371
372

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 373–374why, one step back
373def test_case_insensitive_choice(runner):
374 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 376–378why, one step back
376 def cmd(foo):
377 click.echo(foo)378

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

line 382why, one step back
382

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

line 386why, one step back
386

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 390–391why, one step back
390
391 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 393–395why, one step back
393 def cmd2(foo):
394 click.echo(foo)395

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 397–398why, one step back
397 assert result.exit_code == 2
398

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 400–401why, one step back
400 assert result.exit_code == 2
401

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

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

Merge branch '7.x'David Lordbefore adoption11abf2b

lines 404–407why, one step back
404
405406def test_case_insensitive_choice_returned_exactly(runner):407 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption11abf2b

lines 409–411why, one step back
409 def cmd(foo):
410 click.echo(foo)411

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption11abf2b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #887 from sirosen/feature/case-insensitive-choiceZachery Birbefore adoptionc70d463

lines 415–416why, one step back
415
416

Merge branch '7.x'David Lordbefore adoption8b971f7

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 419–423why, one step back
419 @click.option(
420 "-C",421 "--config",422 type=click.Path(),423 help="""Configuration file to use.

Merge branch '7.x'David Lordbefore adoption7239794

line 424why, one step back
424

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 425–430why, one step back
425 If not given, the environment variable CONFIG_FILE is consulted
426 and used if set. If neither are given, a default configuration427 file is loaded.""",428 )429 def cmd(config):430 pass

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 431why, one step back
431

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

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

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

Merge branch '7.x'David Lordbefore adoption8b971f7

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

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

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

Merge branch '7.x'David Lordbefore adoption8b971f7

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

line 442why, one step back
442

Merge branch '7.x'David Lordbefore adoption7239794

line 443why, one step back
443

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 444–446why, one step back
444def test_argument_custom_class(runner):
445 class CustomArgument(click.Argument):446 def get_default(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 452–455why, one step back
452 def cmd(testarg):
453 click.echo(testarg)454455 result = runner.invoke(cmd)

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 458–462why, one step back
458
459460def test_option_custom_class(runner):461 class CustomOption(click.Option):462 def get_help_record(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 465–466why, one step back
465
466 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 474–475why, one step back
474
475

Merge pull request #994 from dsully/issue-926David Lordbefore adoption1ea6a75

lines 476–480why, one step back
476def test_option_custom_class_reusable(runner):
477 """Ensure we can reuse a custom class option. See Issue #926"""478479 class CustomOption(click.Option):480 def get_help_record(self, ctx):

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #994 from dsully/issue-926David Lordbefore adoption1ea6a75

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #994 from dsully/issue-926David Lordbefore adoption1ea6a75

lines 486–499why, one step back
486
487 @click.command()488 @testoption489 def cmd1(testoption):490 click.echo(testoption)491492 @click.command()493 @testoption494 def cmd2(testoption):495 click.echo(testoption)496497 # Both of the commands should have the --help option now.498 for cmd in (cmd1, cmd2):499

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 503–506why, one step back
503
504505def test_bool_flag_with_type(runner):506 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 508–512why, one step back
508 def cmd(shout):
509 pass510511 result = runner.invoke(cmd)512 assert not result.exception

Merge pull request #994 from dsully/issue-926David Lordbefore adoption1ea6a75

lines 513–514why, one step back
513
514

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 515–516why, one step back
515def test_aliases_for_flags(runner):
516 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 518–520why, one step back
518 def cli(warnings):
519 click.echo(warnings)520

Merge branch '7.x'David Lordbefore adoption7239794

lines 521–526why, one step back
521 result = runner.invoke(cli, ["--warnings"])
522 assert result.output == "True\n"523 result = runner.invoke(cli, ["--no-warnings"])524 assert result.output == "False\n"525 result = runner.invoke(cli, ["-W"])526 assert result.output == "False\n"

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 527–528why, one step back
527
528 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #731 from jeffwidman/patch-1Markus Unterwaditzerbefore adoptiona72baa4

lines 530–532why, one step back
530 def cli_alt(warnings):
531 click.echo(warnings)532

Merge branch '7.x'David Lordbefore adoption7239794

lines 533–538why, one step back
533 result = runner.invoke(cli_alt, ["--warnings"])
534 assert result.output == "True\n"535 result = runner.invoke(cli_alt, ["--no-warnings"])536 assert result.output == "False\n"537 result = runner.invoke(cli_alt, ["-w"])538 assert result.output == "True\n"

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption945b4d3

line 539why, one step back
539

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

line 540why, one step back
540

Merge branch '7.x'David Lordbefore adoption7239794

lines 541–556why, one step back
541@pytest.mark.parametrize(
542 "option_args,expected",543 [544 (["--aggressive", "--all", "-a"], "aggressive"),545 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),546 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),547 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),548 (["-a", "-b", "-c"], "a"),549 (["-c", "-b", "-a"], "c"),550 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),551 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),552 (["--from", "-f", "_from"], "_from"),553 (["--return", "-r", "_ret"], "_ret"),554 ],555)556def test_option_names(runner, option_args, expected):

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption945b4d3

lines 557–564why, one step back
557 @click.command()
558 @click.option(*option_args, is_flag=True)559 def cmd(**kwargs):560 click.echo(str(kwargs[expected]))561562 assert cmd.params[0].name == expected563564 for form in option_args:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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