Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

297 lines · 7.2 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 5d1df0e point at it.

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

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

line 3why, one step back
3from io import BytesIO

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 4–6why, one step back
4
5import pytest6

Merge branch '7.x'David Lordbefore adoption7239794

lines 7–8why, one step back
7import click
8from click._compat import WIN

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 9–14why, one step back
9from click.testing import CliRunner
101112def test_runner():13 @click.command()14 def test():

Merge branch '7.x'David Lordbefore adoption7239794

lines 15–16why, one step back
15 i = click.get_binary_stream("stdin")
16 o = click.get_binary_stream("stdout")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 17–24why, one step back
17 while 1:
18 chunk = i.read(4096)19 if not chunk:20 break21 o.write(chunk)22 o.flush()2324 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 25why, one step back
25 result = runner.invoke(test, input="Hello World!\n")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 28–29why, one step back
28
29 runner = CliRunner(echo_stdin=True)

Merge branch '7.x'David Lordbefore adoption7239794

line 30why, one step back
30 result = runner.invoke(test, input="Hello World!\n")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

line 32why, one step back
32 assert result.output == "Hello World!\nHello World!\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 33–37why, one step back
33
3435def test_runner_with_stream():36 @click.command()37 def test():

Merge branch '7.x'David Lordbefore adoption7239794

lines 38–39why, one step back
38 i = click.get_binary_stream("stdin")
39 o = click.get_binary_stream("stdout")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 40–47why, one step back
40 while 1:
41 chunk = i.read(4096)42 if not chunk:43 break44 o.write(chunk)45 o.flush()4647 runner = CliRunner()

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

line 48why, one step back
48 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 51–52why, one step back
51
52 runner = CliRunner(echo_stdin=True)

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

line 53why, one step back
53 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

line 55why, one step back
55 assert result.output == "Hello World!\nHello World!\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 56–59why, one step back
56
5758def test_prompts():59 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 61why, one step back
61 def test(foo):

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

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 63–64why, one step back
63
64 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 65why, one step back
65 result = runner.invoke(test, input="wau wau\n")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

line 67why, one step back
67 assert result.output == "Foo: wau wau\nfoo=wau wau\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 68–69why, one step back
68
69 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 70why, one step back
70 @click.option("--foo", prompt=True, hide_input=True)

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 71why, one step back
71 def test(foo):

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

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 73–74why, one step back
73
74 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 75why, one step back
75 result = runner.invoke(test, input="wau wau\n")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

line 77why, one step back
77 assert result.output == "Foo: \nfoo=wau wau\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 78–85why, one step back
78
7980def test_getchar():81 @click.command()82 def continue_it():83 click.echo(click.getchar())8485 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 86why, one step back
86 result = runner.invoke(continue_it, input="y")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

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

Merge branch '7.x'David Lordbefore adoption7239794

line 88why, one step back
88 assert result.output == "y\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 89–114why, one step back
89
9091def test_catch_exceptions():92 class CustomError(Exception):93 pass9495 @click.command()96 def cli():97 raise CustomError(1)9899 runner = CliRunner()100101 result = runner.invoke(cli)102 assert isinstance(result.exception, CustomError)103 assert type(result.exc_info) is tuple104 assert len(result.exc_info) == 3105106 with pytest.raises(CustomError):107 runner.invoke(cli, catch_exceptions=False)108109 CustomError = SystemExit110111 result = runner.invoke(cli)112 assert result.exit_code == 1113114

Merge branch '7.x'David Lordbefore adoption7239794

line 115why, one step back
115@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 116–118why, one step back
116def test_with_color():
117 @click.command()118 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

line 119why, one step back
119 click.secho("hello world", fg="blue")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 120–123why, one step back
120
121 runner = CliRunner()122123 result = runner.invoke(cli)

Merge branch '7.x'David Lordbefore adoption7239794

line 124why, one step back
124 assert result.output == "hello world\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 125–127why, one step back
125 assert not result.exception
126127 result = runner.invoke(cli, color=True)

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

line 128why, one step back
128 assert result.output == f"{click.style('hello world', fg='blue')}\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 129–139why, one step back
129 assert not result.exception
130131132def test_with_color_but_pause_not_blocking():133 @click.command()134 def cli():135 click.pause()136137 runner = CliRunner()138 result = runner.invoke(cli, color=True)139 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 140why, one step back
140 assert result.output == ""

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 141–146why, one step back
141
142143def test_exit_code_and_output_from_sys_exit():144 # See issue #362145 @click.command()146 def cli_string():

Merge branch '7.x'David Lordbefore adoption7239794

lines 147–148why, one step back
147 click.echo("hello world")
148 sys.exit("error")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 149–150why, one step back
149
150 @click.command()

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

lines 151–152why, one step back
151 @click.pass_context
152 def cli_string_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 153–154why, one step back
153 click.echo("hello world")
154 ctx.exit("error")

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

lines 155–156why, one step back
155
156 @click.command()

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 157why, one step back
157 def cli_int():

Merge branch '7.x'David Lordbefore adoption7239794

line 158why, one step back
158 click.echo("hello world")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 159–161why, one step back
159 sys.exit(1)
160161 @click.command()

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

lines 162–163why, one step back
162 @click.pass_context
163 def cli_int_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 164why, one step back
164 click.echo("hello world")

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

lines 165–167why, one step back
165 ctx.exit(1)
166167 @click.command()

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 168why, one step back
168 def cli_float():

Merge branch '7.x'David Lordbefore adoption7239794

line 169why, one step back
169 click.echo("hello world")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 170–172why, one step back
170 sys.exit(1.0)
171172 @click.command()

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

lines 173–174why, one step back
173 @click.pass_context
174 def cli_float_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 175why, one step back
175 click.echo("hello world")

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

lines 176–178why, one step back
176 ctx.exit(1.0)
177178 @click.command()

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 179why, one step back
179 def cli_no_error():

Merge branch '7.x'David Lordbefore adoption7239794

line 180why, one step back
180 click.echo("hello world")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 181–185why, one step back
181
182 runner = CliRunner()183184 result = runner.invoke(cli_string)185 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 186why, one step back
186 assert result.output == "hello world\nerror\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 187why, one step back
187

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

lines 188–189why, one step back
188 result = runner.invoke(cli_string_ctx_exit)
189 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 190why, one step back
190 assert result.output == "hello world\nerror\n"

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

line 191why, one step back
191

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 192–193why, one step back
192 result = runner.invoke(cli_int)
193 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 194why, one step back
194 assert result.output == "hello world\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 195why, one step back
195

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

lines 196–197why, one step back
196 result = runner.invoke(cli_int_ctx_exit)
197 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 198why, one step back
198 assert result.output == "hello world\n"

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

line 199why, one step back
199

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 200why, one step back
200 result = runner.invoke(cli_float)

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

line 201why, one step back
201 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 202why, one step back
202 assert result.output == "hello world\n1.0\n"

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

lines 203–204why, one step back
203
204 result = runner.invoke(cli_float_ctx_exit)

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 205why, one step back
205 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 206why, one step back
206 assert result.output == "hello world\n1.0\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 207–209why, one step back
207
208 result = runner.invoke(cli_no_error)209 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 210why, one step back
210 assert result.output == "hello world\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 211–215why, one step back
211
212213def test_env():214 @click.command()215 def cli_env():

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

line 216why, one step back
216 click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 217–221why, one step back
217
218 runner = CliRunner()219220 env_orig = dict(os.environ)221 env = dict(env_orig)

Merge branch '7.x'David Lordbefore adoption7239794

lines 222–223why, one step back
222 assert "TEST_CLICK_ENV" not in env
223 env["TEST_CLICK_ENV"] = "some_value"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 224–225why, one step back
224 result = runner.invoke(cli_env, env=env)
225 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 226why, one step back
226 assert result.output == "ENV=some_value\n"

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 227–230why, one step back
227
228 assert os.environ == env_orig229230

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

lines 231–240why, one step back
231def test_stderr():
232 @click.command()233 def cli_stderr():234 click.echo("stdout")235 click.echo("stderr", err=True)236237 runner = CliRunner(mix_stderr=False)238239 result = runner.invoke(cli_stderr)240

Merge branch '7.x'David Lordbefore adoption7239794

lines 241–243why, one step back
241 assert result.output == "stdout\n"
242 assert result.stdout == "stdout\n"243 assert result.stderr == "stderr\n"

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

lines 244–247why, one step back
244
245 runner_mix = CliRunner(mix_stderr=True)246 result_mix = runner_mix.invoke(cli_stderr)247

Merge branch '7.x'David Lordbefore adoption7239794

lines 248–249why, one step back
248 assert result_mix.output == "stdout\nstderr\n"
249 assert result_mix.stdout == "stdout\nstderr\n"

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

lines 250–253why, one step back
250
251 with pytest.raises(ValueError):252 result_mix.stderr253

Merge branch '7.x'David Lordbefore adoption2269786

lines 254–261why, one step back
254 @click.command()
255 def cli_empty_stderr():256 click.echo("stdout")257258 runner = CliRunner(mix_stderr=False)259260 result = runner.invoke(cli_empty_stderr)261

Merge branch '7.x'David Lordbefore adoption7239794

lines 262–264why, one step back
262 assert result.output == "stdout\n"
263 assert result.stdout == "stdout\n"264 assert result.stderr == ""

Merge branch '7.x'David Lordbefore adoption2269786

line 265why, one step back
265

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 266why, one step back
266

Merge branch '7.x'David Lordbefore adoption7239794

lines 267–276why, one step back
267@pytest.mark.parametrize(
268 "args, expected_output",269 [270 (None, "bar\n"),271 ([], "bar\n"),272 ("", "bar\n"),273 (["--foo", "one two"], "one two\n"),274 ('--foo "one two"', "one two\n"),275 ],276)

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 277–278why, one step back
277def test_args(args, expected_output):
278 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 280–286why, one step back
280 def cli_args(foo):
281 click.echo(foo)282283 runner = CliRunner()284 result = runner.invoke(cli_args, args=args)285 assert result.exit_code == 0286 assert result.output == expected_output

Merge pull request #999 from kporangehat/invoke_with_prog_nameDan Sullybefore adoption467fb8d

lines 287–296why, one step back
287
288289def test_setting_prog_name_in_extra():290 @click.command()291 def cli():292 click.echo("ok")293294 runner = CliRunner()295 result = runner.invoke(cli, prog_name="foobar")296 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 297why, one step back
297 assert result.output == "ok\n"