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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
import os
import sys
from io import BytesIO
import pytest
import asyncclick as click
from asyncclick.exceptions import ClickException
from asyncclick.testing import CliRunner
@pytest.mark.anyio
async def test_runner():
@click.command()
def test():
i = click.get_binary_stream("stdin")
o = click.get_binary_stream("stdout")
while True:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
result = await runner.invoke(test, input="Hello World!\n")
if result.exception:
raise result.exception
assert result.output == "Hello World!\n"
@pytest.mark.anyio
async def test_echo_stdin_stream():
@click.command()
def test():
i = click.get_binary_stream("stdin")
o = click.get_binary_stream("stdout")
while True:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner(echo_stdin=True)
result = await runner.invoke(test, input="Hello World!\n")
if result.exception:
raise result.exception
assert result.output == "Hello World!\nHello World!\n"
@pytest.mark.anyio
async def test_echo_stdin_prompts():
@click.command()
def test_python_input():
foo = input("Foo: ")
click.echo(f"foo={foo}")
runner = CliRunner(echo_stdin=True)
result = await runner.invoke(test_python_input, input="bar bar\n")
if result.exception:
raise result.exception
assert result.output == "Foo: bar bar\nfoo=bar bar\n"
@click.command()
@click.option("--foo", prompt=True)
def test_prompt(foo):
click.echo(f"foo={foo}")
result = await runner.invoke(test_prompt, input="bar bar\n")
if result.exception:
raise result.exception
assert result.output == "Foo: bar bar\nfoo=bar bar\n"
@click.command()
@click.option("--foo", prompt=True, hide_input=True)
def test_hidden_prompt(foo):
click.echo(f"foo={foo}")
result = await runner.invoke(test_hidden_prompt, input="bar bar\n")
if result.exception:
raise result.exception
assert result.output == "Foo: \nfoo=bar bar\n"
@click.command()
@click.option("--foo", prompt=True)
@click.option("--bar", prompt=True)
def test_multiple_prompts(foo, bar):
click.echo(f"foo={foo}, bar={bar}")
result = await runner.invoke(test_multiple_prompts, input="one\ntwo\n")
if result.exception:
raise result.exception
assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"
@pytest.mark.anyio
async def test_runner_with_stream():
@click.command()
def test():
i = click.get_binary_stream("stdin")
o = click.get_binary_stream("stdout")
while True:
chunk = i.read(4096)
if not chunk:
break
o.write(chunk)
o.flush()
runner = CliRunner()
result = await runner.invoke(test, input=BytesIO(b"Hello World!\n"))
if result.exception:
raise result.exception
assert result.output == "Hello World!\n"
runner = CliRunner(echo_stdin=True)
result = await runner.invoke(test, input=BytesIO(b"Hello World!\n"))
if result.exception:
raise result.exception
assert result.output == "Hello World!\nHello World!\n"
@pytest.mark.anyio
async def test_prompts():
@click.command()
@click.option("--foo", prompt=True)
def test(foo):
click.echo(f"foo={foo}")
runner = CliRunner()
result = await runner.invoke(test, input="wau wau\n")
if result.exception:
raise 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(f"foo={foo}")
runner = CliRunner()
result = await runner.invoke(test, input="wau wau\n")
if result.exception:
raise result.exception
assert result.output == "Foo: \nfoo=wau wau\n"
@pytest.mark.anyio
async def test_getchar():
@click.command()
def continue_it():
click.echo(click.getchar())
runner = CliRunner()
result = await runner.invoke(continue_it, input="y")
if result.exception:
raise result.exception
assert result.output == "y\n"
runner = CliRunner(echo_stdin=True)
result = await runner.invoke(continue_it, input="y")
if result.exception:
raise result.exception
assert result.output == "y\n"
@click.command()
def getchar_echo():
click.echo(click.getchar(echo=True))
runner = CliRunner()
result = await runner.invoke(getchar_echo, input="y")
if result.exception:
raise result.exception
assert result.output == "yy\n"
runner = CliRunner(echo_stdin=True)
result = await runner.invoke(getchar_echo, input="y")
if result.exception:
raise result.exception
assert result.output == "yy\n"
@pytest.mark.anyio
async def test_catch_exceptions():
class CustomError(Exception):
pass
@click.command()
def cli():
raise CustomError(1)
runner = CliRunner()
result = await 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):
await runner.invoke(cli, catch_exceptions=False)
CustomError = SystemExit
result = await runner.invoke(cli)
assert result.exit_code == 1
@pytest.mark.anyio
async def test_with_color():
@click.command()
def cli():
click.secho("hello world", fg="blue")
runner = CliRunner()
result = await runner.invoke(cli)
assert result.output == "hello world\n"
if result.exception:
raise result.exception
result = await runner.invoke(cli, color=True)
assert result.output == f"{click.style('hello world', fg='blue')}\n"
if result.exception:
raise result.exception
@pytest.mark.anyio
async def test_with_color_errors():
class CLIError(ClickException):
def format_message(self) -> str:
return click.style(self.message, fg="red")
@click.command()
def cli():
raise CLIError("Red error")
runner = CliRunner()
result = await runner.invoke(cli)
assert result.output == "Error: Red error\n"
assert result.exception
result = await runner.invoke(cli, color=True)
assert result.output == f"Error: {click.style('Red error', fg='red')}\n"
assert result.exception
@pytest.mark.anyio
async def test_with_color_but_pause_not_blocking():
@click.command()
def cli():
click.pause()
runner = CliRunner()
result = await runner.invoke(cli, color=True)
if result.exception:
raise result.exception
assert result.output == ""
@pytest.mark.anyio
async 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()
@click.pass_context
def cli_string_ctx_exit(ctx):
click.echo("hello world")
ctx.exit("error")
@click.command()
def cli_int():
click.echo("hello world")
sys.exit(1)
@click.command()
@click.pass_context
def cli_int_ctx_exit(ctx):
click.echo("hello world")
ctx.exit(1)
@click.command()
def cli_float():
click.echo("hello world")
sys.exit(1.0)
@click.command()
@click.pass_context
def cli_float_ctx_exit(ctx):
click.echo("hello world")
ctx.exit(1.0)
@click.command()
def cli_no_error():
click.echo("hello world")
runner = CliRunner()
result = await runner.invoke(cli_string)
assert result.exit_code == 1
assert result.output == "hello world\nerror\n"
result = await runner.invoke(cli_string_ctx_exit)
assert result.exit_code == 1
assert result.output == "hello world\nerror\n"
result = await runner.invoke(cli_int)
assert result.exit_code == 1
assert result.output == "hello world\n"
result = await runner.invoke(cli_int_ctx_exit)
assert result.exit_code == 1
assert result.output == "hello world\n"
result = await runner.invoke(cli_float)
assert result.exit_code == 1
assert result.output == "hello world\n1.0\n"
result = await runner.invoke(cli_float_ctx_exit)
assert result.exit_code == 1
assert result.output == "hello world\n1.0\n"
result = await runner.invoke(cli_no_error)
assert result.exit_code == 0
assert result.output == "hello world\n"
@pytest.mark.anyio
async def test_env():
@click.command()
def cli_env():
click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")
runner = CliRunner()
env_orig = dict(os.environ)
env = dict(env_orig)
assert "TEST_CLICK_ENV" not in env
env["TEST_CLICK_ENV"] = "some_value"
result = await runner.invoke(cli_env, env=env)
assert result.exit_code == 0
assert result.output == "ENV=some_value\n"
assert os.environ == env_orig
@pytest.mark.anyio
async def test_stderr():
@click.command()
def cli_stderr():
click.echo("stdout")
click.echo("stderr", err=True)
runner = CliRunner(mix_stderr=False)
result = await runner.invoke(cli_stderr)
assert result.output == "stdout\n"
assert result.stdout == "stdout\n"
assert result.stderr == "stderr\n"
runner_mix = CliRunner(mix_stderr=True)
result_mix = await runner_mix.invoke(cli_stderr)
assert result_mix.output == "stdout\nstderr\n"
assert result_mix.stdout == "stdout\nstderr\n"
with pytest.raises(ValueError):
result_mix.stderr # noqa B018
@click.command()
def cli_empty_stderr():
click.echo("stdout")
runner = CliRunner(mix_stderr=False)
result = await runner.invoke(cli_empty_stderr)
assert result.output == "stdout\n"
assert result.stdout == "stdout\n"
assert result.stderr == ""
@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"),
],
)
@pytest.mark.anyio
async def test_args(args, expected_output):
@click.command()
@click.option("--foo", default="bar")
def cli_args(foo):
click.echo(foo)
runner = CliRunner()
result = await runner.invoke(cli_args, args=args)
assert result.exit_code == 0
assert result.output == expected_output
@pytest.mark.anyio
async def test_setting_prog_name_in_extra():
@click.command()
def cli():
click.echo("ok")
runner = CliRunner()
result = await runner.invoke(cli, prog_name="foobar")
if result.exception:
raise result.exception
assert result.output == "ok\n"
@pytest.mark.anyio
async def test_command_standalone_mode_returns_value():
@click.command()
def cli():
click.echo("ok")
return "Hello, World!"
runner = CliRunner()
result = await runner.invoke(cli, standalone_mode=False)
assert result.output == "ok\n"
assert result.return_value == "Hello, World!"
assert result.exit_code == 0
@pytest.mark.anyio
async def test_file_stdin_attrs(runner):
@click.command()
@click.argument("f", type=click.File())
def cli(f):
click.echo(f.name)
click.echo(f.mode, nl=False)
result = runner.invoke(cli, ["-"])
assert result.output == "<stdin>\nr"
def test_isolated_runner(runner):
with runner.isolated_filesystem() as d:
assert os.path.exists(d)
assert not os.path.exists(d)
def test_isolated_runner_custom_tempdir(runner, tmp_path):
with runner.isolated_filesystem(temp_dir=tmp_path) as d:
assert os.path.exists(d)
assert os.path.exists(d)
os.rmdir(d)
def test_isolation_stderr_errors():
"""Writing to stderr should escape invalid characters instead of
raising a UnicodeEncodeError.
"""
runner = CliRunner(mix_stderr=False)
with runner.isolation() as (_, err):
click.echo("\udce2", err=True, nl=False)
assert err.getvalue() == b"\\udce2"
|