test_testing.py
This file
Path
tests/test_testing.py
420 lines · 10.5 KiB · Python
Last changed by
Merge pull request #1820 from saebischer/fix-clirunner-echo_stdinDavid Lordbefore adoption30e7f76
History of this fileTake it
The walk stopped after 200 commits: lines older than 0b1e32b point at it.
Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 1–2why, one step back1import os2import sysMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 3why, one step back3from io import BytesIOMerge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 4–6why, one step back45import pytest6Merge branch '7.x'David Lordbefore adoption7239794
lines 7–8why, one step back7import click8from click._compat import WINMerge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 9–14why, one step back9from click.testing import CliRunner101112def test_runner():13 @click.command()14 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 15–16why, one step back15 i = click.get_binary_stream("stdin")16 o = click.get_binary_stream("stdout")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 17–24why, one step back17 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 back25 result = runner.invoke(test, input="Hello World!\n")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 26why, one step back26 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 27why, one step back27 assert result.output == "Hello World!\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 28why, one step back28Merge pull request #1820 from saebischer/fix-clirunner-echo_stdinDavid Lordbefore adoption30e7f76
lines 29–41why, one step back2930def test_echo_stdin_stream():31 @click.command()32 def test():33 i = click.get_binary_stream("stdin")34 o = click.get_binary_stream("stdout")35 while 1:36 chunk = i.read(4096)37 if not chunk:38 break39 o.write(chunk)40 o.flush()41Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 42why, one step back42 runner = CliRunner(echo_stdin=True)Merge branch '7.x'David Lordbefore adoption7239794
line 43why, one step back43 result = runner.invoke(test, input="Hello World!\n")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 44why, one step back44 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 45why, one step back45 assert result.output == "Hello World!\nHello World!\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 46–47why, one step back4647Merge pull request #1820 from saebischer/fix-clirunner-echo_stdinDavid Lordbefore adoption30e7f76
lines 48–87why, one step back48def test_echo_stdin_prompts():49 @click.command()50 def test_python_input():51 foo = input("Foo: ")52 click.echo(f"foo={foo}")5354 runner = CliRunner(echo_stdin=True)55 result = runner.invoke(test_python_input, input="bar bar\n")56 assert not result.exception57 assert result.output == "Foo: bar bar\nfoo=bar bar\n"5859 @click.command()60 @click.option("--foo", prompt=True)61 def test_prompt(foo):62 click.echo(f"foo={foo}")6364 result = runner.invoke(test_prompt, input="bar bar\n")65 assert not result.exception66 assert result.output == "Foo: bar bar\nfoo=bar bar\n"6768 @click.command()69 @click.option("--foo", prompt=True, hide_input=True)70 def test_hidden_prompt(foo):71 click.echo(f"foo={foo}")7273 result = runner.invoke(test_hidden_prompt, input="bar bar\n")74 assert not result.exception75 assert result.output == "Foo: \nfoo=bar bar\n"7677 @click.command()78 @click.option("--foo", prompt=True)79 @click.option("--bar", prompt=True)80 def test_multiple_prompts(foo, bar):81 click.echo(f"foo={foo}, bar={bar}")8283 result = runner.invoke(test_multiple_prompts, input="one\ntwo\n")84 assert not result.exception85 assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"8687Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 88–90why, one step back88def test_runner_with_stream():89 @click.command()90 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 91–92why, one step back91 i = click.get_binary_stream("stdin")92 o = click.get_binary_stream("stdout")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 93–100why, one step back93 while 1:94 chunk = i.read(4096)95 if not chunk:96 break97 o.write(chunk)98 o.flush()99100 runner = CliRunner()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 101why, one step back101 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 102why, one step back102 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 103why, one step back103 assert result.output == "Hello World!\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 104–105why, one step back104105 runner = CliRunner(echo_stdin=True)Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 106why, one step back106 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 107why, one step back107 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 108why, one step back108 assert result.output == "Hello World!\nHello World!\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 109–112why, one step back109110111def test_prompts():112 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 113why, one step back113 @click.option("--foo", prompt=True)Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 114why, one step back114 def test(foo):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 115why, one step back115 click.echo(f"foo={foo}")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 116–117why, one step back116117 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 118why, one step back118 result = runner.invoke(test, input="wau wau\n")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 119why, one step back119 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 120why, one step back120 assert result.output == "Foo: wau wau\nfoo=wau wau\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 121–122why, one step back121122 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 123why, one step back123 @click.option("--foo", prompt=True, hide_input=True)Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 124why, one step back124 def test(foo):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 125why, one step back125 click.echo(f"foo={foo}")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 126–127why, one step back126127 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 128why, one step back128 result = runner.invoke(test, input="wau wau\n")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 129why, one step back129 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 130why, one step back130 assert result.output == "Foo: \nfoo=wau wau\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 131–138why, one step back131132133def test_getchar():134 @click.command()135 def continue_it():136 click.echo(click.getchar())137138 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 139why, one step back139 result = runner.invoke(continue_it, input="y")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 140why, one step back140 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 141why, one step back141 assert result.output == "y\n"Merge pull request #1820 from saebischer/fix-clirunner-echo_stdinDavid Lordbefore adoption30e7f76
lines 142–160why, one step back142143 runner = CliRunner(echo_stdin=True)144 result = runner.invoke(continue_it, input="y")145 assert not result.exception146 assert result.output == "y\n"147148 @click.command()149 def getchar_echo():150 click.echo(click.getchar(echo=True))151152 runner = CliRunner()153 result = runner.invoke(getchar_echo, input="y")154 assert not result.exception155 assert result.output == "yy\n"156157 runner = CliRunner(echo_stdin=True)158 result = runner.invoke(getchar_echo, input="y")159 assert not result.exception160 assert result.output == "yy\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 161–186why, one step back161162163def test_catch_exceptions():164 class CustomError(Exception):165 pass166167 @click.command()168 def cli():169 raise CustomError(1)170171 runner = CliRunner()172173 result = runner.invoke(cli)174 assert isinstance(result.exception, CustomError)175 assert type(result.exc_info) is tuple176 assert len(result.exc_info) == 3177178 with pytest.raises(CustomError):179 runner.invoke(cli, catch_exceptions=False)180181 CustomError = SystemExit182183 result = runner.invoke(cli)184 assert result.exit_code == 1185186Merge branch '7.x'David Lordbefore adoption7239794
line 187why, one step back187@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 188–190why, one step back188def test_with_color():189 @click.command()190 def cli():Merge branch '7.x'David Lordbefore adoption7239794
line 191why, one step back191 click.secho("hello world", fg="blue")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 192–195why, one step back192193 runner = CliRunner()194195 result = runner.invoke(cli)Merge branch '7.x'David Lordbefore adoption7239794
line 196why, one step back196 assert result.output == "hello world\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 197–199why, one step back197 assert not result.exception198199 result = runner.invoke(cli, color=True)Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 200why, one step back200 assert result.output == f"{click.style('hello world', fg='blue')}\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 201–211why, one step back201 assert not result.exception202203204def test_with_color_but_pause_not_blocking():205 @click.command()206 def cli():207 click.pause()208209 runner = CliRunner()210 result = runner.invoke(cli, color=True)211 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 212why, one step back212 assert result.output == ""Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 213–218why, one step back213214215def test_exit_code_and_output_from_sys_exit():216 # See issue #362217 @click.command()218 def cli_string():Merge branch '7.x'David Lordbefore adoption7239794
lines 219–220why, one step back219 click.echo("hello world")220 sys.exit("error")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 221–224why, one step back221222 @click.command()223 @click.pass_context224 def cli_string_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 225–226why, one step back225 click.echo("hello world")226 ctx.exit("error")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 227–229why, one step back227228 @click.command()229 def cli_int():Merge branch '7.x'David Lordbefore adoption7239794
line 230why, one step back230 click.echo("hello world")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 231–235why, one step back231 sys.exit(1)232233 @click.command()234 @click.pass_context235 def cli_int_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 236why, one step back236 click.echo("hello world")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 237–240why, one step back237 ctx.exit(1)238239 @click.command()240 def cli_float():Merge branch '7.x'David Lordbefore adoption7239794
line 241why, one step back241 click.echo("hello world")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 242–246why, one step back242 sys.exit(1.0)243244 @click.command()245 @click.pass_context246 def cli_float_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 247why, one step back247 click.echo("hello world")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 248–251why, one step back248 ctx.exit(1.0)249250 @click.command()251 def cli_no_error():Merge branch '7.x'David Lordbefore adoption7239794
line 252why, one step back252 click.echo("hello world")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 253–257why, one step back253254 runner = CliRunner()255256 result = runner.invoke(cli_string)257 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 258why, one step back258 assert result.output == "hello world\nerror\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 259–261why, one step back259260 result = runner.invoke(cli_string_ctx_exit)261 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 262why, one step back262 assert result.output == "hello world\nerror\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 263–265why, one step back263264 result = runner.invoke(cli_int)265 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 266why, one step back266 assert result.output == "hello world\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 267–269why, one step back267268 result = runner.invoke(cli_int_ctx_exit)269 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 270why, one step back270 assert result.output == "hello world\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 271–273why, one step back271272 result = runner.invoke(cli_float)273 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 274why, one step back274 assert result.output == "hello world\n1.0\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 275–277why, one step back275276 result = runner.invoke(cli_float_ctx_exit)277 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 278why, one step back278 assert result.output == "hello world\n1.0\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 279–281why, one step back279280 result = runner.invoke(cli_no_error)281 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 282why, one step back282 assert result.output == "hello world\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 283–287why, one step back283284285def test_env():286 @click.command()287 def cli_env():Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 288why, one step back288 click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 289–293why, one step back289290 runner = CliRunner()291292 env_orig = dict(os.environ)293 env = dict(env_orig)Merge branch '7.x'David Lordbefore adoption7239794
lines 294–295why, one step back294 assert "TEST_CLICK_ENV" not in env295 env["TEST_CLICK_ENV"] = "some_value"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 296–297why, one step back296 result = runner.invoke(cli_env, env=env)297 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 298why, one step back298 assert result.output == "ENV=some_value\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 299–312why, one step back299300 assert os.environ == env_orig301302303def test_stderr():304 @click.command()305 def cli_stderr():306 click.echo("stdout")307 click.echo("stderr", err=True)308309 runner = CliRunner(mix_stderr=False)310311 result = runner.invoke(cli_stderr)312Merge branch '7.x'David Lordbefore adoption7239794
lines 313–315why, one step back313 assert result.output == "stdout\n"314 assert result.stdout == "stdout\n"315 assert result.stderr == "stderr\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 316–319why, one step back316317 runner_mix = CliRunner(mix_stderr=True)318 result_mix = runner_mix.invoke(cli_stderr)319Merge branch '7.x'David Lordbefore adoption7239794
lines 320–321why, one step back320 assert result_mix.output == "stdout\nstderr\n"321 assert result_mix.stdout == "stdout\nstderr\n"Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 322–325why, one step back322323 with pytest.raises(ValueError):324 result_mix.stderr325Merge branch '7.x'David Lordbefore adoption2269786
lines 326–333why, one step back326 @click.command()327 def cli_empty_stderr():328 click.echo("stdout")329330 runner = CliRunner(mix_stderr=False)331332 result = runner.invoke(cli_empty_stderr)333Merge branch '7.x'David Lordbefore adoption7239794
lines 334–336why, one step back334 assert result.output == "stdout\n"335 assert result.stdout == "stdout\n"336 assert result.stderr == ""Merge branch '7.x'David Lordbefore adoption2269786
line 337why, one step back337Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
line 338why, one step back338Merge branch '7.x'David Lordbefore adoption7239794
lines 339–348why, one step back339@pytest.mark.parametrize(340 "args, expected_output",341 [342 (None, "bar\n"),343 ([], "bar\n"),344 ("", "bar\n"),345 (["--foo", "one two"], "one two\n"),346 ('--foo "one two"', "one two\n"),347 ],348)Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 349–350why, one step back349def test_args(args, expected_output):350 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 351why, one step back351 @click.option("--foo", default="bar")Merge pull request #1320 from AlexTereshenkov/masterDavid Lordbefore adoption0b1e32b
lines 352–368why, one step back352 def cli_args(foo):353 click.echo(foo)354355 runner = CliRunner()356 result = runner.invoke(cli_args, args=args)357 assert result.exit_code == 0358 assert result.output == expected_output359360361def test_setting_prog_name_in_extra():362 @click.command()363 def cli():364 click.echo("ok")365366 runner = CliRunner()367 result = runner.invoke(cli, prog_name="foobar")368 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 369why, one step back369 assert result.output == "ok\n"Merge pull request #1312 from tom-dalton-fanduel/allow-testing-command-result-captureDavid Lordbefore adoption990ca8e
lines 370–382why, one step back370371372def test_command_standalone_mode_returns_value():373 @click.command()374 def cli():375 click.echo("ok")376 return "Hello, World!"377378 runner = CliRunner()379 result = runner.invoke(cli, standalone_mode=False)380 assert result.output == "ok\n"381 assert result.return_value == "Hello, World!"382 assert result.exit_code == 0Merge pull request #1497 from jyscao/clirunner_stdin_fixDavid Lordbefore adoption2571539
lines 383–393why, one step back383384385def test_file_stdin_attrs(runner):386 @click.command()387 @click.argument("f", type=click.File())388 def cli(f):389 click.echo(f.name)390 click.echo(f.mode, nl=False)391392 result = runner.invoke(cli, ["-"])393 assert result.output == "<stdin>\nr"Merge pull request #1715 from MLH-Fellowship/maaz/issue-395David Lordbefore adoptionb8a33bf
lines 394–408why, one step back394395396def test_isolated_runner(runner):397 with runner.isolated_filesystem() as d:398 assert os.path.exists(d)399400 assert not os.path.exists(d)401402403def test_isolated_runner_custom_tempdir(runner, tmp_path):404 with runner.isolated_filesystem(temp_dir=tmp_path) as d:405 assert os.path.exists(d)406407 assert os.path.exists(d)408 os.rmdir(d)Merge pull request #1834 from pallets/stderr-errorsDavid Lordbefore adoption9907a26
lines 409–420why, one step back409410411def test_isolation_stderr_errors():412 """Writing to stderr should escape invalid characters instead of413 raising a UnicodeEncodeError.414 """415 runner = CliRunner(mix_stderr=False)416417 with runner.isolation() as (_, err):418 click.echo("\udce2", err=True, nl=False)419420 assert err.getvalue() == b"\\udce2"