1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
import sys
import pytest
import click
from click.testing import CliRunner
from click._compat import PY2, WIN
# 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():
i = click.get_binary_stream('stdin')
o = click.get_binary_stream('stdout')
while 1:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
result = runner.invoke(test, input='Hello World!\n')
assert not result.exception
assert result.output == 'Hello World!\n'
runner = CliRunner(echo_stdin=True)
result = runner.invoke(test, input='Hello World!\n')
assert not result.exception
assert result.output == 'Hello World!\nHello World!\n'
def test_runner_with_stream():
@click.command()
def test():
i = click.get_binary_stream('stdin')
o = click.get_binary_stream('stdout')
while 1:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))
assert not result.exception
assert result.output == 'Hello World!\n'
runner = CliRunner(echo_stdin=True)
result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))
assert not result.exception
assert result.output == 'Hello World!\nHello World!\n'
def test_prompts():
@click.command()
@click.option('--foo', prompt=True)
def test(foo):
click.echo('foo=%s' % foo)
runner = CliRunner()
result = runner.invoke(test, input='wau wau\n')
assert not result.exception
assert result.output == 'Foo: wau wau\nfoo=wau wau\n'
@click.command()
@click.option('--foo', prompt=True, hide_input=True)
def test(foo):
click.echo('foo=%s' % foo)
runner = CliRunner()
result = runner.invoke(test, input='wau wau\n')
assert not result.exception
assert result.output == 'Foo: \nfoo=wau wau\n'
def test_getchar():
@click.command()
def continue_it():
click.echo(click.getchar())
runner = CliRunner()
result = runner.invoke(continue_it, input='y')
assert not result.exception
assert result.output == 'y\n'
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
@pytest.mark.skipif(WIN, reason='Test does not make sense on Windows.')
def test_with_color():
@click.command()
def cli():
click.secho('hello world', fg='blue')
runner = CliRunner()
result = runner.invoke(cli)
assert result.output == 'hello world\n'
assert not result.exception
result = runner.invoke(cli, color=True)
assert result.output == click.style('hello world', fg='blue') + '\n'
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
assert result.output == ''
def test_exit_code_and_output_from_sys_exit():
# See issue #362
@click.command()
def cli_string():
click.echo('hello world')
sys.exit('error')
@click.command()
def cli_int():
click.echo('hello world')
sys.exit(1)
@click.command()
def cli_float():
click.echo('hello world')
sys.exit(1.0)
@click.command()
def cli_no_error():
click.echo('hello world')
runner = CliRunner()
result = runner.invoke(cli_string)
assert result.exit_code == 1
assert result.output == 'hello world\nerror\n'
result = runner.invoke(cli_int)
assert result.exit_code == 1
assert result.output == 'hello world\n'
result = runner.invoke(cli_float)
assert result.exit_code == 1
assert result.output == 'hello world\n1.0\n'
result = runner.invoke(cli_no_error)
assert result.exit_code == 0
assert result.output == 'hello world\n'
|