Skip to content
Myr

test_basic.py

This file

Path

tests/test_basic.py

559 lines · 15.4 KiB · Python

Last changed by

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

History of this file

Take it

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

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

lines 1–2why, one step back
1import os
2import uuid

Merge branch '7.x'David Lordbefore adoption7239794

line 3why, one step back
3

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

lines 4–10why, one step back
4import click
567def test_basic_functionality(runner):8 @click.command()9 def cli():10 """Hello World!"""

Merge branch '7.x'David Lordbefore adoption7239794

line 11why, one step back
11 click.echo("I EXECUTED")

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

line 12why, one step back
12

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 15–16why, one step back
15 assert "Hello World!" in result.output
16 assert "Show this message and exit." in result.output

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 18why, one step back
18 assert "I EXECUTED" not in result.output

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 22why, one step back
22 assert "I EXECUTED" in result.output

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

lines 23–25why, one step back
23 assert result.exit_code == 0
2425

Merge pull request #1295 from jzengg/issue-1267-add-name-to-command-reprDavid Lordbefore adoptione6eac0a

lines 26–38why, one step back
26def test_repr():
27 @click.command()28 def command():29 pass3031 @click.group()32 def group():33 pass3435 @group.command()36 def subcommand():37 pass38

Merge branch '7.x'David Lordbefore adoption7239794

lines 39–41why, one step back
39 assert repr(command) == "<Command command>"
40 assert repr(group) == "<Group group>"41 assert repr(subcommand) == "<Command subcommand>"

Merge pull request #1295 from jzengg/issue-1267-add-name-to-command-reprDavid Lordbefore adoptione6eac0a

lines 42–43why, one step back
42
43

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

lines 44–48why, one step back
44def test_return_values():
45 @click.command()46 def cli():47 return 4248

Merge branch '7.x'David Lordbefore adoption7239794

line 49why, one step back
49 with cli.make_context("foo", []) as ctx:

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

line 50why, one step back
50 rv = cli.invoke(ctx)

Merge pull request #1445 from tirkarthi/fix-syntax-warningJoshua Bronsonbefore adoption6c454c8

line 51why, one step back
51 assert rv == 42

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

lines 52–57why, one step back
52
5354def test_basic_group(runner):55 @click.group()56 def cli():57 """This is the root."""

Merge branch '7.x'David Lordbefore adoption7239794

line 58why, one step back
58 click.echo("ROOT EXECUTED")

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

lines 59–62why, one step back
59
60 @cli.command()61 def subcommand():62 """This is a subcommand."""

Merge branch '7.x'David Lordbefore adoption7239794

line 63why, one step back
63 click.echo("SUBCOMMAND EXECUTED")

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

line 64why, one step back
64

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 67–69why, one step back
67 assert "COMMAND [ARGS]..." in result.output
68 assert "This is the root" in result.output69 assert "This is a subcommand." in result.output

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 71why, one step back
71 assert "ROOT EXECUTED" not in result.output

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

line 72why, one step back
72

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 74–75why, one step back
74 assert not result.exception
75 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

lines 76–77why, one step back
76 assert "ROOT EXECUTED" in result.output
77 assert "SUBCOMMAND EXECUTED" in result.output

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

lines 78–81why, one step back
78
7980def test_basic_option(runner):81 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 82why, one step back
82 @click.option("--foo", default="no value")

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

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

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

line 84why, one step back
84 click.echo(f"FOO:[{foo}]")

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 88why, one step back
88 assert "FOO:[no value]" in result.output

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

line 89why, one step back
89

Merge branch '7.x'David Lordbefore adoption7239794

line 90why, one step back
90 result = runner.invoke(cli, ["--foo=42"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 92why, one step back
92 assert "FOO:[42]" in result.output

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

line 93why, one step back
93

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 96why, one step back
96 assert "--foo option requires an argument" in result.output

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

line 97why, one step back
97

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 100why, one step back
100 assert "FOO:[]" in result.output

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

line 101why, one step back
101

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

line 102why, one step back
102 result = runner.invoke(cli, ["--foo=\N{SNOWMAN}"])

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

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

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

line 104why, one step back
104 assert "FOO:[\N{SNOWMAN}]" in result.output

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

lines 105–108why, one step back
105
106107def test_int_option(runner):108 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 109why, one step back
109 @click.option("--foo", default=42)

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

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

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

line 111why, one step back
111 click.echo(f"FOO:[{foo * 2}]")

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 115why, one step back
115 assert "FOO:[84]" in result.output

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

line 116why, one step back
116

Merge branch '7.x'David Lordbefore adoption7239794

line 117why, one step back
117 result = runner.invoke(cli, ["--foo=23"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 119why, one step back
119 assert "FOO:[46]" in result.output

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

line 120why, one step back
120

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 123why, one step back
123 assert "Invalid value for '--foo': bar is not a valid integer" in result.output

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

lines 124–127why, one step back
124
125126def test_uuid_option(runner):127 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 128–130why, one step back
128 @click.option(
129 "--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID130 )

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

lines 131–132why, one step back
131 def cli(u):
132 assert type(u) is uuid.UUID

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

line 133why, one step back
133 click.echo(f"U:[{u}]")

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 137why, one step back
137 assert "U:[ba122011-349f-423b-873b-9d6a79c688ab]" in result.output

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

line 138why, one step back
138

Merge branch '7.x'David Lordbefore adoption7239794

line 139why, one step back
139 result = runner.invoke(cli, ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 141why, one step back
141 assert "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]" in result.output

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

line 142why, one step back
142

Merge branch '7.x'David Lordbefore adoption7239794

line 143why, one step back
143 result = runner.invoke(cli, ["--u=bar"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 145why, one step back
145 assert "Invalid value for '--u': bar is not a valid UUID value" in result.output

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

lines 146–149why, one step back
146
147148def test_float_option(runner):149 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 150why, one step back
150 @click.option("--foo", default=42, type=click.FLOAT)

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

lines 151–152why, one step back
151 def cli(foo):
152 assert type(foo) is float

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

line 153why, one step back
153 click.echo(f"FOO:[{foo}]")

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 157why, one step back
157 assert "FOO:[42.0]" in result.output

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

line 158why, one step back
158

Merge branch '7.x'David Lordbefore adoption7239794

line 159why, one step back
159 result = runner.invoke(cli, ["--foo=23.5"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 161why, one step back
161 assert "FOO:[23.5]" in result.output

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

line 162why, one step back
162

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 165why, one step back
165 assert "Invalid value for '--foo': bar is not a valid float" in result.output

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

lines 166–169why, one step back
166
167168def test_boolean_option(runner):169 for default in True, False:

Merge branch '7.x'David Lordbefore adoption7239794

line 170why, one step back
170

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 172why, one step back
172 @click.option("--with-foo/--without-foo", default=default)

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

lines 173–175why, one step back
173 def cli(with_foo):
174 click.echo(with_foo)175

Merge branch '7.x'David Lordbefore adoption7239794

line 176why, one step back
176 result = runner.invoke(cli, ["--with-foo"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 178–179why, one step back
178 assert result.output == "True\n"
179 result = runner.invoke(cli, ["--without-foo"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 181why, one step back
181 assert result.output == "False\n"

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

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

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

line 184why, one step back
184 assert result.output == f"{default}\n"

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

lines 185–186why, one step back
185
186 for default in True, False:

Merge branch '7.x'David Lordbefore adoption7239794

line 187why, one step back
187

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 189why, one step back
189 @click.option("--flag", is_flag=True, default=default)

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

lines 190–192why, one step back
190 def cli(flag):
191 click.echo(flag)192

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

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

line 195why, one step back
195 assert result.output == f"{not default}\n"

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

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

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

line 198why, one step back
198 assert result.output == f"{default}\n"

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

lines 199–200why, one step back
199
200

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 201–202why, one step back
201def test_boolean_conversion(runner):
202 for default in True, False:

Merge branch '7.x'David Lordbefore adoption7239794

line 203why, one step back
203

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

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

Merge branch '7.x'David Lordbefore adoption7239794

line 205why, one step back
205 @click.option("--flag", default=default, type=bool)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 206–208why, one step back
206 def cli(flag):
207 click.echo(flag)208

Merge branch '7.x'David Lordbefore adoption7239794

lines 209–210why, one step back
209 for value in "true", "t", "1", "yes", "y":
210 result = runner.invoke(cli, ["--flag", value])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 213why, one step back
213

Merge branch '7.x'David Lordbefore adoption7239794

lines 214–215why, one step back
214 for value in "false", "f", "0", "no", "n":
215 result = runner.invoke(cli, ["--flag", value])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

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

Merge branch '7.x'David Lordbefore adoption7239794

line 217why, one step back
217 assert result.output == "False\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

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

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

line 221why, one step back
221 assert result.output == f"{default}\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 222–223why, one step back
222
223

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

lines 224–225why, one step back
224def test_file_option(runner):
225 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 226why, one step back
226 @click.option("--file", type=click.File("w"))

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

line 227why, one step back
227 def input(file):

Merge branch '7.x'David Lordbefore adoption7239794

line 228why, one step back
228 file.write("Hello World!\n")

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

lines 229–230why, one step back
229
230 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 231why, one step back
231 @click.option("--file", type=click.File("r"))

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

lines 232–235why, one step back
232 def output(file):
233 click.echo(file.read())234235 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 236–237why, one step back
236 result_in = runner.invoke(input, ["--file=example.txt"])
237 result_out = runner.invoke(output, ["--file=example.txt"])

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

lines 238–239why, one step back
238
239 assert not result_in.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 240why, one step back
240 assert result_in.output == ""

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

line 241why, one step back
241 assert not result_out.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 242why, one step back
242 assert result_out.output == "Hello World!\n\n"

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

lines 243–248why, one step back
243
244245def test_file_lazy_mode(runner):246 do_io = False247248 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 249why, one step back
249 @click.option("--file", type=click.File("w"))

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

lines 250–251why, one step back
250 def input(file):
251 if do_io:

Merge branch '7.x'David Lordbefore adoption7239794

line 252why, one step back
252 file.write("Hello World!\n")

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

lines 253–254why, one step back
253
254 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 255why, one step back
255 @click.option("--file", type=click.File("r"))

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

lines 256–259why, one step back
256 def output(file):
257 pass258259 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

line 260why, one step back
260 os.mkdir("example.txt")

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

lines 261–262why, one step back
261
262 do_io = True

Merge branch '7.x'David Lordbefore adoption7239794

line 263why, one step back
263 result_in = runner.invoke(input, ["--file=example.txt"])

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

lines 264–266why, one step back
264 assert result_in.exit_code == 1
265266 do_io = False

Merge branch '7.x'David Lordbefore adoption7239794

line 267why, one step back
267 result_in = runner.invoke(input, ["--file=example.txt"])

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

lines 268–269why, one step back
268 assert result_in.exit_code == 0
269

Merge branch '7.x'David Lordbefore adoption7239794

line 270why, one step back
270 result_out = runner.invoke(output, ["--file=example.txt"])

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

lines 271–273why, one step back
271 assert result_out.exception
272273 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 274why, one step back
274 @click.option("--file", type=click.File("w", lazy=False))

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

line 275why, one step back
275 def input_non_lazy(file):

Merge branch '7.x'David Lordbefore adoption7239794

line 276why, one step back
276 file.write("Hello World!\n")

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

lines 277–278why, one step back
277
278 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 279–280why, one step back
279 os.mkdir("example.txt")
280 result_in = runner.invoke(input_non_lazy, ["--file=example.txt"])

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

line 281why, one step back
281 assert result_in.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 282–283why, one step back
282 assert (
283 "Invalid value for '--file': Could not open file: example.txt"

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

line 284why, one step back
284 in result_in.output

Merge branch '7.x'David Lordbefore adoption7239794

line 285why, one step back
285 )

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

lines 286–289why, one step back
286
287288def test_path_option(runner):289 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 290why, one step back
290 @click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True))

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

line 291why, one step back
291 def write_to_dir(o):

Merge branch '7.x'David Lordbefore adoption7239794

lines 292–293why, one step back
292 with open(os.path.join(o, "foo.txt"), "wb") as f:
293 f.write(b"meh\n")

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

lines 294–295why, one step back
294
295 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

line 296why, one step back
296 os.mkdir("test")

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

line 297why, one step back
297

Merge branch '7.x'David Lordbefore adoption7239794

line 298why, one step back
298 result = runner.invoke(write_to_dir, ["-O", "test"])

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

lines 299–300why, one step back
299 assert not result.exception
300

Merge branch '7.x'David Lordbefore adoption7239794

lines 301–302why, one step back
301 with open("test/foo.txt", "rb") as f:
302 assert f.read() == b"meh\n"

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

line 303why, one step back
303

Merge branch '7.x'David Lordbefore adoption7239794

lines 304–305why, one step back
304 result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"])
305 assert "is a file" in result.output

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

lines 306–307why, one step back
306
307 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 308why, one step back
308 @click.option("-f", type=click.Path(exists=True))

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

line 309why, one step back
309 def showtype(f):

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

lines 310–311why, one step back
310 click.echo(f"is_file={os.path.isfile(f)}")
311 click.echo(f"is_dir={os.path.isdir(f)}")

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

lines 312–313why, one step back
312
313 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 314–315why, one step back
314 result = runner.invoke(showtype, ["-f", "xxx"])
315 assert "does not exist" in result.output

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

line 316why, one step back
316

Merge branch '7.x'David Lordbefore adoption7239794

lines 317–319why, one step back
317 result = runner.invoke(showtype, ["-f", "."])
318 assert "is_file=False" in result.output319 assert "is_dir=True" in result.output

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

lines 320–321why, one step back
320
321 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 322why, one step back
322 @click.option("-f", type=click.Path())

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

line 323why, one step back
323 def exists(f):

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

line 324why, one step back
324 click.echo(f"exists={os.path.exists(f)}")

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

lines 325–326why, one step back
325
326 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 327–328why, one step back
327 result = runner.invoke(exists, ["-f", "xxx"])
328 assert "exists=False" in result.output

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

line 329why, one step back
329

Merge branch '7.x'David Lordbefore adoption7239794

lines 330–331why, one step back
330 result = runner.invoke(exists, ["-f", "."])
331 assert "exists=True" in result.output

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

lines 332–335why, one step back
332
333334def test_choice_option(runner):335 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 336why, one step back
336 @click.option("--method", type=click.Choice(["foo", "bar", "baz"]))

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

lines 337–339why, one step back
337 def cli(method):
338 click.echo(method)339

Merge branch '7.x'David Lordbefore adoption7239794

line 340why, one step back
340 result = runner.invoke(cli, ["--method=foo"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

line 343why, one step back
343

Merge branch '7.x'David Lordbefore adoption7239794

line 344why, one step back
344 result = runner.invoke(cli, ["--method=meh"])

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

line 345why, one step back
345 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 346–349why, one step back
346 assert (
347 "Invalid value for '--method': invalid choice: meh."348 " (choose from foo, bar, baz)" in result.output349 )

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

line 350why, one step back
350

Merge branch '7.x'David Lordbefore adoption7239794

lines 351–352why, one step back
351 result = runner.invoke(cli, ["--help"])
352 assert "--method [foo|bar|baz]" in result.output

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

lines 353–354why, one step back
353
354

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

lines 355–356why, one step back
355def test_datetime_option_default(runner):
356 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 357why, one step back
357 @click.option("--start_date", type=click.DateTime())

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 358why, one step back
358 def cli(start_date):

Merge branch '7.x'David Lordbefore adoption7239794

line 359why, one step back
359 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 360why, one step back
360

Merge branch '7.x'David Lordbefore adoption7239794

line 361why, one step back
361 result = runner.invoke(cli, ["--start_date=2015-09-29"])

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

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

Merge branch '7.x'David Lordbefore adoption7239794

line 363why, one step back
363 assert result.output == "2015-09-29T00:00:00\n"

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 364why, one step back
364

Merge branch '7.x'David Lordbefore adoption7239794

line 365why, one step back
365 result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"])

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

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

Merge branch '7.x'David Lordbefore adoption7239794

line 367why, one step back
367 assert result.output == "2015-09-29T09:11:22\n"

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 368why, one step back
368

Merge branch '7.x'David Lordbefore adoption7239794

line 369why, one step back
369 result = runner.invoke(cli, ["--start_date=2015-09"])

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 370why, one step back
370 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 371–375why, one step back
371 assert (
372 "Invalid value for '--start_date':"373 " invalid datetime format: 2015-09."374 " (choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)"375 ) in result.output

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 376why, one step back
376

Merge branch '7.x'David Lordbefore adoption7239794

lines 377–380why, one step back
377 result = runner.invoke(cli, ["--help"])
378 assert (379 "--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output380 )

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

lines 381–384why, one step back
381
382383def test_datetime_option_custom(runner):384 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 385why, one step back
385 @click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"]))

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 386why, one step back
386 def cli(start_date):

Merge branch '7.x'David Lordbefore adoption7239794

line 387why, one step back
387 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

line 388why, one step back
388

Merge branch '7.x'David Lordbefore adoption7239794

line 389why, one step back
389 result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"])

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

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

Merge branch '7.x'David Lordbefore adoption7239794

line 391why, one step back
391 assert result.output == "2010-06-05T00:00:00\n"

Merge pull request #1099 from sirosen/type-datetimeDavid Lordbefore adoption2c622ee

lines 392–393why, one step back
392
393

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

lines 394–395why, one step back
394def test_int_range_option(runner):
395 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 396why, one step back
396 @click.option("--x", type=click.IntRange(0, 5))

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

lines 397–399why, one step back
397 def cli(x):
398 click.echo(x)399

Merge branch '7.x'David Lordbefore adoption7239794

line 400why, one step back
400 result = runner.invoke(cli, ["--x=5"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 402why, one step back
402 assert result.output == "5\n"

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

line 403why, one step back
403

Merge branch '7.x'David Lordbefore adoption7239794

line 404why, one step back
404 result = runner.invoke(cli, ["--x=6"])

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

line 405why, one step back
405 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 406–407why, one step back
406 assert (
407 "Invalid value for '--x': 6 is not in the valid range of 0 to 5.\n"

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 409why, one step back
409 )

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

lines 410–411why, one step back
410
411 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 412why, one step back
412 @click.option("--x", type=click.IntRange(0, 5, clamp=True))

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

lines 413–415why, one step back
413 def clamp(x):
414 click.echo(x)415

Merge branch '7.x'David Lordbefore adoption7239794

line 416why, one step back
416 result = runner.invoke(clamp, ["--x=5"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 418why, one step back
418 assert result.output == "5\n"

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

line 419why, one step back
419

Merge branch '7.x'David Lordbefore adoption7239794

line 420why, one step back
420 result = runner.invoke(clamp, ["--x=6"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 422why, one step back
422 assert result.output == "5\n"

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

line 423why, one step back
423

Merge branch '7.x'David Lordbefore adoption7239794

line 424why, one step back
424 result = runner.invoke(clamp, ["--x=-1"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 427–430why, one step back
427
428429def test_float_range_option(runner):430 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 431why, one step back
431 @click.option("--x", type=click.FloatRange(0, 5))

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

lines 432–434why, one step back
432 def cli(x):
433 click.echo(x)434

Merge branch '7.x'David Lordbefore adoption7239794

line 435why, one step back
435 result = runner.invoke(cli, ["--x=5.0"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 437why, one step back
437 assert result.output == "5.0\n"

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

line 438why, one step back
438

Merge branch '7.x'David Lordbefore adoption7239794

line 439why, one step back
439 result = runner.invoke(cli, ["--x=6.0"])

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

line 440why, one step back
440 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

lines 441–442why, one step back
441 assert (
442 "Invalid value for '--x': 6.0 is not in the valid range of 0 to 5.\n"

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 444why, one step back
444 )

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

lines 445–446why, one step back
445
446 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 447why, one step back
447 @click.option("--x", type=click.FloatRange(0, 5, clamp=True))

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

lines 448–450why, one step back
448 def clamp(x):
449 click.echo(x)450

Merge branch '7.x'David Lordbefore adoption7239794

line 451why, one step back
451 result = runner.invoke(clamp, ["--x=5.0"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 453why, one step back
453 assert result.output == "5.0\n"

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

line 454why, one step back
454

Merge branch '7.x'David Lordbefore adoption7239794

line 455why, one step back
455 result = runner.invoke(clamp, ["--x=6.0"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 457why, one step back
457 assert result.output == "5\n"

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

line 458why, one step back
458

Merge branch '7.x'David Lordbefore adoption7239794

line 459why, one step back
459 result = runner.invoke(clamp, ["--x=-1.0"])

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

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

lines 462–465why, one step back
462
463464def test_required_option(runner):465 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 466why, one step back
466 @click.option("--foo", required=True)

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

lines 467–471why, one step back
467 def cli(foo):
468 click.echo(foo)469470 result = runner.invoke(cli, [])471 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

line 472why, one step back
472 assert "Missing option '--foo'" in result.output

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

lines 473–477why, one step back
473
474475def test_evaluation_order(runner):476 called = []477

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

line 478why, one step back
478 def memo(ctx, param, value):

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

lines 479–482why, one step back
479 called.append(value)
480 return value481482 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 483–489why, one step back
483 @click.option("--missing", default="missing", is_eager=False, callback=memo)
484 @click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo)485 @click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo)486 @click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo)487 @click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo)488 @click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo)489 @click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo)

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

lines 490–492why, one step back
490 def cli(**x):
491 pass492

Merge branch '7.x'David Lordbefore adoption7239794

lines 493–505why, one step back
493 result = runner.invoke(
494 cli,495 [496 "--eager-flag2",497 "--eager-flag1",498 "--normal-flag2",499 "--eager-flag3",500 "--normal-flag3",501 "--normal-flag3",502 "--normal-flag1",503 "--normal-flag1",504 ],505 )

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

lines 506–507why, one step back
506 assert not result.exception
507 assert called == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 508–514why, one step back
508 "eager2",
509 "eager1",510 "eager3",511 "normal2",512 "normal3",513 "normal1",514 "missing",

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

lines 515–519why, one step back
515 ]
516517518def test_hidden_option(runner):519 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 520why, one step back
520 @click.option("--nope", hidden=True)

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

lines 521–523why, one step back
521 def cli(nope):
522 click.echo(nope)523

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 526why, one step back
526 assert "--nope" not in result.output

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

lines 527–537why, one step back
527
528529def test_hidden_command(runner):530 @click.group()531 def cli():532 pass533534 @cli.command(hidden=True)535 def nope():536 pass537

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

line 540why, one step back
540 assert "nope" not in result.output

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

lines 541–555why, one step back
541
542543def test_hidden_group(runner):544 @click.group()545 def cli():546 pass547548 @cli.group(hidden=True)549 def subgroup():550 pass551552 @subgroup.command()553 def nope():554 pass555

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge branch '7.x'David Lordbefore adoption7239794

lines 558–559why, one step back
558 assert "subgroup" not in result.output
559 assert "nope" not in result.output