python-click/tests/test_testing.py

304 lines
7.4 KiB
Python
Raw Normal View History

2017-07-19 20:06:01 +02:00
import os
2015-08-23 03:10:31 +02:00
import sys
2014-10-16 20:40:34 +02:00
import pytest
2020-07-21 08:23:42 +02:00
import click
from click._compat import PY2
from click._compat import WIN
2014-10-16 20:40:34 +02:00
from click.testing import CliRunner
# Use the most reasonable io that users would use for the python version.
if PY2:
from cStringIO import StringIO as ReasonableBytesIO
else:
from io import BytesIO as ReasonableBytesIO
def test_runner():
@click.command()
def test():
2020-07-21 08:23:42 +02:00
i = click.get_binary_stream("stdin")
o = click.get_binary_stream("stdout")
2014-10-16 20:40:34 +02:00
while 1:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input="Hello World!\n")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Hello World!\n"
2014-10-16 20:40:34 +02:00
runner = CliRunner(echo_stdin=True)
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input="Hello World!\n")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Hello World!\nHello World!\n"
2014-10-16 20:40:34 +02:00
def test_runner_with_stream():
@click.command()
def test():
2020-07-21 08:23:42 +02:00
i = click.get_binary_stream("stdin")
o = click.get_binary_stream("stdout")
2014-10-16 20:40:34 +02:00
while 1:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Hello World!\n"
2014-10-16 20:40:34 +02:00
runner = CliRunner(echo_stdin=True)
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Hello World!\nHello World!\n"
2014-10-16 20:40:34 +02:00
def test_prompts():
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", prompt=True)
2014-10-16 20:40:34 +02:00
def test(foo):
2020-07-21 08:23:42 +02:00
click.echo("foo={}".format(foo))
2014-10-16 20:40:34 +02:00
runner = CliRunner()
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input="wau wau\n")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Foo: wau wau\nfoo=wau wau\n"
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", prompt=True, hide_input=True)
2014-10-16 20:40:34 +02:00
def test(foo):
2020-07-21 08:23:42 +02:00
click.echo("foo={}".format(foo))
2014-10-16 20:40:34 +02:00
runner = CliRunner()
2020-07-21 08:23:42 +02:00
result = runner.invoke(test, input="wau wau\n")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "Foo: \nfoo=wau wau\n"
2014-10-16 20:40:34 +02:00
def test_getchar():
@click.command()
def continue_it():
click.echo(click.getchar())
runner = CliRunner()
2020-07-21 08:23:42 +02:00
result = runner.invoke(continue_it, input="y")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "y\n"
2014-10-16 20:40:34 +02:00
def test_catch_exceptions():
class CustomError(Exception):
pass
@click.command()
def cli():
raise CustomError(1)
runner = CliRunner()
result = runner.invoke(cli)
assert isinstance(result.exception, CustomError)
assert type(result.exc_info) is tuple
assert len(result.exc_info) == 3
with pytest.raises(CustomError):
runner.invoke(cli, catch_exceptions=False)
CustomError = SystemExit
result = runner.invoke(cli)
assert result.exit_code == 1
2015-07-16 14:26:14 +02:00
2020-07-21 08:23:42 +02:00
@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")
2015-07-16 14:26:14 +02:00
def test_with_color():
@click.command()
def cli():
2020-07-21 08:23:42 +02:00
click.secho("hello world", fg="blue")
2015-07-16 14:26:14 +02:00
runner = CliRunner()
result = runner.invoke(cli)
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n"
2015-07-16 14:26:14 +02:00
assert not result.exception
result = runner.invoke(cli, color=True)
2020-07-21 08:23:42 +02:00
assert result.output == "{}\n".format(click.style("hello world", fg="blue"))
2015-07-16 14:26:14 +02:00
assert not result.exception
def test_with_color_but_pause_not_blocking():
@click.command()
def cli():
click.pause()
runner = CliRunner()
result = runner.invoke(cli, color=True)
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == ""
2015-08-23 03:10:31 +02:00
def test_exit_code_and_output_from_sys_exit():
# See issue #362
@click.command()
def cli_string():
2020-07-21 08:23:42 +02:00
click.echo("hello world")
sys.exit("error")
2015-08-23 03:10:31 +02:00
2018-09-06 20:55:10 +02:00
@click.command()
@click.pass_context
def cli_string_ctx_exit(ctx):
2020-07-21 08:23:42 +02:00
click.echo("hello world")
ctx.exit("error")
2018-09-06 20:55:10 +02:00
2015-08-23 03:10:31 +02:00
@click.command()
def cli_int():
2020-07-21 08:23:42 +02:00
click.echo("hello world")
2015-08-23 03:10:31 +02:00
sys.exit(1)
2018-09-06 20:55:10 +02:00
@click.command()
@click.pass_context
def cli_int_ctx_exit(ctx):
2020-07-21 08:23:42 +02:00
click.echo("hello world")
2018-09-06 20:55:10 +02:00
ctx.exit(1)
2015-08-23 03:10:31 +02:00
@click.command()
def cli_float():
2020-07-21 08:23:42 +02:00
click.echo("hello world")
2015-08-23 03:10:31 +02:00
sys.exit(1.0)
2018-09-06 20:55:10 +02:00
@click.command()
@click.pass_context
def cli_float_ctx_exit(ctx):
2020-07-21 08:23:42 +02:00
click.echo("hello world")
2018-09-06 20:55:10 +02:00
ctx.exit(1.0)
2015-08-23 03:10:31 +02:00
@click.command()
def cli_no_error():
2020-07-21 08:23:42 +02:00
click.echo("hello world")
2015-08-23 03:10:31 +02:00
runner = CliRunner()
result = runner.invoke(cli_string)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\nerror\n"
2015-08-23 03:10:31 +02:00
2018-09-06 20:55:10 +02:00
result = runner.invoke(cli_string_ctx_exit)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\nerror\n"
2018-09-06 20:55:10 +02:00
2015-08-23 03:10:31 +02:00
result = runner.invoke(cli_int)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n"
2015-08-23 03:10:31 +02:00
2018-09-06 20:55:10 +02:00
result = runner.invoke(cli_int_ctx_exit)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n"
2018-09-06 20:55:10 +02:00
2015-08-23 03:10:31 +02:00
result = runner.invoke(cli_float)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n1.0\n"
2015-08-23 03:10:31 +02:00
2018-09-06 20:55:10 +02:00
result = runner.invoke(cli_float_ctx_exit)
assert result.exit_code == 1
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n1.0\n"
2018-09-06 20:55:10 +02:00
2015-08-23 03:10:31 +02:00
result = runner.invoke(cli_no_error)
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert result.output == "hello world\n"
2017-07-19 20:06:01 +02:00
def test_env():
@click.command()
def cli_env():
2020-07-21 08:23:42 +02:00
click.echo("ENV={}".format(os.environ["TEST_CLICK_ENV"]))
2017-07-19 20:06:01 +02:00
runner = CliRunner()
env_orig = dict(os.environ)
env = dict(env_orig)
2020-07-21 08:23:42 +02:00
assert "TEST_CLICK_ENV" not in env
env["TEST_CLICK_ENV"] = "some_value"
2017-07-19 20:06:01 +02:00
result = runner.invoke(cli_env, env=env)
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert result.output == "ENV=some_value\n"
2017-07-19 20:06:01 +02:00
assert os.environ == env_orig
2018-09-06 20:55:10 +02:00
def test_stderr():
@click.command()
def cli_stderr():
click.echo("stdout")
click.echo("stderr", err=True)
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli_stderr)
2020-07-21 08:23:42 +02:00
assert result.output == "stdout\n"
assert result.stdout == "stdout\n"
assert result.stderr == "stderr\n"
2018-09-06 20:55:10 +02:00
runner_mix = CliRunner(mix_stderr=True)
result_mix = runner_mix.invoke(cli_stderr)
2020-07-21 08:23:42 +02:00
assert result_mix.output == "stdout\nstderr\n"
assert result_mix.stdout == "stdout\nstderr\n"
2018-09-06 20:55:10 +02:00
with pytest.raises(ValueError):
result_mix.stderr
2020-07-21 08:23:42 +02:00
@click.command()
def cli_empty_stderr():
click.echo("stdout")
2018-09-06 20:55:10 +02:00
2020-07-21 08:23:42 +02:00
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli_empty_stderr)
assert result.output == "stdout\n"
assert result.stdout == "stdout\n"
assert result.stderr == ""
2018-09-06 20:55:10 +02:00
2020-07-21 08:23:42 +02:00
@pytest.mark.parametrize(
"args, expected_output",
[
(None, "bar\n"),
([], "bar\n"),
("", "bar\n"),
(["--foo", "one two"], "one two\n"),
('--foo "one two"', "one two\n"),
],
)
def test_args(args, expected_output):
2018-09-06 20:55:10 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", default="bar")
2018-09-06 20:55:10 +02:00
def cli_args(foo):
click.echo(foo)
runner = CliRunner()
result = runner.invoke(cli_args, args=args)
assert result.exit_code == 0
assert result.output == expected_output
def test_setting_prog_name_in_extra():
@click.command()
def cli():
click.echo("ok")
runner = CliRunner()
result = runner.invoke(cli, prog_name="foobar")
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "ok\n"