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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
import sys
from unittest import mock
import pytest
import asyncclick as click
from asyncclick._utils import UNSET
PY2 = False # churn
@pytest.mark.anyio
async def test_nargs_star(arunner):
@click.command()
@click.argument("src", nargs=-1)
@click.argument("dst")
def copy(src, dst):
click.echo(f"src={'|'.join(src)}")
click.echo(f"dst={dst}")
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
result = await arunner.invoke(copy, ["foo.txt", "bar.txt", "dir"])
if result.exception:
raise result.exception
assert not result.exception
assert result.output.splitlines() == ["src=foo.txt|bar.txt", "dst=dir"]
def test_nargs_tup(runner):
@click.command()
@click.argument("name", nargs=1)
@click.argument("point", nargs=2, type=click.INT)
def copy(name, point):
click.echo(f"name={name}")
x, y = point
click.echo(f"point={x}/{y}")
result = runner.invoke(copy, ["peter", "1", "2"])
assert not result.exception
assert result.output.splitlines() == ["name=peter", "point=1/2"]
@pytest.mark.parametrize(
"opts",
[
dict(type=(str, int)),
dict(type=click.Tuple([str, int])),
dict(nargs=2, type=click.Tuple([str, int])),
dict(nargs=2, type=(str, int)),
],
)
def test_nargs_tup_composite(runner, opts):
@click.command()
@click.argument("item", **opts)
def copy(item):
name, id = item
click.echo(f"name={name} id={id:d}")
result = runner.invoke(copy, ["peter", "1"])
assert result.exception is None
assert result.output.splitlines() == ["name=peter id=1"]
def test_nargs_mismatch_with_tuple_type():
with pytest.raises(ValueError, match="nargs.*must be 2.*but it was 3"):
@click.command()
@click.argument("test", type=(str, int), nargs=3)
def cli(_):
pass
def test_nargs_err(runner):
@click.command()
@click.argument("x")
def copy(x):
click.echo(x)
result = runner.invoke(copy, ["foo"])
assert not result.exception
assert result.output == "foo\n"
result = runner.invoke(copy, ["foo", "bar"])
assert result.exit_code == 2
assert "Got unexpected extra argument (bar)" in result.output
def test_bytes_args(runner, monkeypatch):
@click.command()
@click.argument("arg")
def from_bytes(arg):
assert isinstance(arg, str), (
"UTF-8 encoded argument should be implicitly converted to Unicode"
)
# Simulate empty locale environment variables
monkeypatch.setattr(sys, "getfilesystemencoding", lambda: "utf-8")
monkeypatch.setattr(sys, "getdefaultencoding", lambda: "utf-8")
# sys.stdin.encoding is readonly, needs some extra effort to patch.
stdin = mock.Mock(wraps=sys.stdin)
stdin.encoding = "utf-8"
monkeypatch.setattr(sys, "stdin", stdin)
runner.invoke(
from_bytes,
["Something outside of ASCII range: 林".encode()],
catch_exceptions=False,
)
def test_file_args(runner):
@click.command()
@click.argument("input", type=click.File("rb"))
@click.argument("output", type=click.File("wb"))
def inout(input, output):
while True:
chunk = input.read(1024)
if not chunk:
break
output.write(chunk)
with runner.isolated_filesystem():
result = runner.invoke(inout, ["-", "hello.txt"], input="Hey!")
assert result.output == ""
assert result.exit_code == 0
with open("hello.txt", "rb") as f:
assert f.read() == b"Hey!"
result = runner.invoke(inout, ["hello.txt", "-"])
assert result.output == "Hey!"
assert result.exit_code == 0
def test_path_allow_dash(runner):
@click.command()
@click.argument("input", type=click.Path(allow_dash=True))
def foo(input):
click.echo(input)
result = runner.invoke(foo, ["-"])
assert result.output == "-\n"
assert result.exit_code == 0
def test_file_atomics(runner):
@click.command()
@click.argument("output", type=click.File("wb", atomic=True))
def inout(output):
output.write(b"Foo bar baz\n")
output.flush()
with open(output.name, "rb") as f:
old_content = f.read()
assert old_content == b"OLD\n"
with runner.isolated_filesystem():
with open("foo.txt", "wb") as f:
f.write(b"OLD\n")
result = runner.invoke(inout, ["foo.txt"], input="Hey!", catch_exceptions=False)
assert result.output == ""
assert result.exit_code == 0
with open("foo.txt", "rb") as f:
assert f.read() == b"Foo bar baz\n"
def test_stdout_default(runner):
@click.command()
@click.argument("output", type=click.File("w"), default="-")
def inout(output):
output.write("Foo bar baz\n")
output.flush()
result = runner.invoke(inout, [])
assert not result.exception
assert result.output == "Foo bar baz\n"
assert result.stdout == "Foo bar baz\n"
assert not result.stderr
@pytest.mark.parametrize(
("nargs", "value", "expect"),
[
(2, "", None),
(2, "a", "Takes 2 values but 1 was given."),
(2, "a b", ("a", "b")),
(2, "a b c", "Takes 2 values but 3 were given."),
(-1, "a b c", ("a", "b", "c")),
(-1, "", ()),
],
)
def test_nargs_envvar(runner, nargs, value, expect):
if nargs == -1:
param = click.argument("arg", envvar="X", nargs=nargs)
else:
param = click.option("--arg", envvar="X", nargs=nargs)
@click.command()
@param
def cmd(arg):
return arg
result = runner.invoke(cmd, env={"X": value}, standalone_mode=False)
if isinstance(expect, str):
assert isinstance(result.exception, click.BadParameter)
assert expect in result.exception.format_message()
else:
assert result.return_value == expect
def test_nargs_envvar_only_if_values_empty(runner):
@click.command()
@click.argument("arg", envvar="X", nargs=-1)
def cli(arg):
return arg
result = runner.invoke(cli, ["a", "b"], standalone_mode=False)
assert result.return_value == ("a", "b")
result = runner.invoke(cli, env={"X": "a"}, standalone_mode=False)
assert result.return_value == ("a",)
def test_empty_nargs(runner):
@click.command()
@click.argument("arg", nargs=-1)
def cmd(arg):
click.echo(f"arg:{'|'.join(arg)}")
result = runner.invoke(cmd, [])
assert result.exit_code == 0
assert result.output == "arg:\n"
@click.command()
@click.argument("arg", nargs=-1, required=True)
def cmd2(arg):
click.echo(f"arg:{'|'.join(arg)}")
result = runner.invoke(cmd2, [])
assert result.exit_code == 2
assert "Missing argument 'ARG...'" in result.output
def test_missing_arg(runner):
@click.command()
@click.argument("arg")
def cmd(arg):
click.echo(f"arg:{arg}")
result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert "Missing argument 'ARG'." in result.output
@pytest.mark.parametrize(
("value", "expect_missing", "processed_value"),
[
# Unspecified type of the argument fallback to string, so everything is
# processed the click.STRING type.
("", False, ""),
(" ", False, " "),
("foo", False, "foo"),
("12", False, "12"),
(12, False, "12"),
(12.1, False, "12.1"),
(list(), False, "[]"),
(tuple(), False, "()"),
(set(), False, "set()"),
(frozenset(), False, "frozenset()"),
(dict(), False, "{}"),
# None is a value that is allowed to be processed by a required argument
# because at this stage, the process_value method happens after the default is
# applied.
(None, False, None),
# An UNSET required argument will raise MissingParameter.
(UNSET, True, None),
],
)
@pytest.mark.anyio
async def test_required_argument(value, expect_missing, processed_value):
"""Test how a required argument is processing the provided values."""
ctx = click.Context(click.Command(""))
argument = click.Argument(["a"], required=True)
if expect_missing:
with pytest.raises(click.MissingParameter) as excinfo:
await argument.process_value(ctx, value)
assert str(excinfo.value) == "Missing parameter: a"
else:
value = await argument.process_value(ctx, value)
assert value == processed_value
def test_implicit_non_required(runner):
@click.command()
@click.argument("f", default="test")
def cli(f):
click.echo(f)
result = runner.invoke(cli, [])
assert result.exit_code == 0
assert result.output == "test\n"
@pytest.mark.anyio
async def test_deprecated_usage(arunner):
@click.command()
@click.argument("f", required=False, deprecated=True)
def cli(f):
click.echo(f)
result = await arunner.invoke(cli, ["--help"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "[F!]" in result.output
@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])
def test_deprecated_warning(runner, deprecated):
@click.command()
@click.argument(
"my-argument", required=False, deprecated=deprecated, default="default argument"
)
def cli(my_argument: str):
click.echo(f"{my_argument}")
# defaults should not give a deprecated warning
result = runner.invoke(cli, [])
assert result.exit_code == 0, result.output
assert "is deprecated" not in result.output
result = runner.invoke(cli, ["hello"])
assert result.exit_code == 0, result.output
assert "argument 'MY_ARGUMENT' is deprecated" in result.output
if isinstance(deprecated, str):
assert deprecated in result.output
def test_deprecated_required(runner):
with pytest.raises(ValueError, match="is deprecated and still required"):
click.Argument(["a"], required=True, deprecated=True)
@pytest.mark.anyio
async def test_eat_options(arunner):
@click.command()
@click.option("-f")
@click.argument("files", nargs=-1)
def cmd(f, files):
for filename in files:
click.echo(filename)
click.echo(f)
result = await arunner.invoke(cmd, ["-f", "xx"])
assert result.output.splitlines() == ["xx"]
result = await arunner.invoke(cmd, ["--", "-foo", "bar"])
assert result.output.splitlines() == ["-foo", "bar", ""]
result = await arunner.invoke(cmd, ["-f", "-x", "--", "-foo", "bar"])
assert result.output.splitlines() == ["-foo", "bar", "-x"]
@pytest.mark.anyio
async def test_nargs_star_ordering(arunner):
@click.command()
@click.argument("a", nargs=-1)
@click.argument("b")
@click.argument("c")
def cmd(a, b, c):
for arg in (a, b, c):
click.echo(arg)
result = await arunner.invoke(cmd, ["a", "b", "c"])
assert result.output.splitlines() == ["('a',)", "b", "c"]
def test_nargs_specified_plus_star_ordering(runner):
@click.command()
@click.argument("a", nargs=-1)
@click.argument("b")
@click.argument("c", nargs=2)
def cmd(a, b, c):
for arg in (a, b, c):
click.echo(arg)
result = runner.invoke(cmd, ["a", "b", "c", "d", "e", "f"])
assert result.output.splitlines() == ["('a', 'b', 'c')", "d", "('e', 'f')"]
@pytest.mark.parametrize(
("argument_params", "args", "expected"),
[
# Any iterable with the same number of arguments as nargs is valid.
[{"nargs": 2, "default": (1, 2)}, [], (1, 2)],
[{"nargs": 2, "default": (1.1, 2.2)}, [], (1, 2)],
[{"nargs": 2, "default": ("1", "2")}, [], (1, 2)],
[{"nargs": 2, "default": (None, None)}, [], (None, None)],
[{"nargs": 2, "default": [1, 2]}, [], (1, 2)],
[{"nargs": 2, "default": {1, 2}}, [], (1, 2)],
[{"nargs": 2, "default": frozenset([1, 2])}, [], (1, 2)],
[{"nargs": 2, "default": {1: "a", 2: "b"}}, [], (1, 2)],
# Empty iterable is valid if default is None.
[{"nargs": 2, "default": None}, [], None],
# Arguments overrides the default.
[{"nargs": 2, "default": (1, 2)}, ["3", "4"], (3, 4)],
# Unbounded arguments are allowed to have a default.
# See: https://github.com/pallets/click/issues/2164
[{"nargs": -1, "default": [42]}, [], (42,)],
[{"nargs": -1, "default": None}, [], ()],
[{"nargs": -1, "default": {1, 2, 3, 4, 5}}, [], (1, 2, 3, 4, 5)],
],
)
def test_good_defaults_for_nargs(runner, argument_params, args, expected):
@click.command()
@click.argument("a", type=int, **argument_params)
def cmd(a):
click.echo(repr(a), nl=False)
result = runner.invoke(cmd, args)
assert result.output == repr(expected)
@pytest.mark.parametrize(
("default", "message"),
[
# Non-iterables defaults.
["Yo", "Error: Invalid value for '[A]...': Value must be an iterable."],
["", "Error: Invalid value for '[A]...': Value must be an iterable."],
[True, "Error: Invalid value for '[A]...': Value must be an iterable."],
[False, "Error: Invalid value for '[A]...': Value must be an iterable."],
[12, "Error: Invalid value for '[A]...': Value must be an iterable."],
[7.9, "Error: Invalid value for '[A]...': Value must be an iterable."],
# Generator default.
[(), "Error: Invalid value for '[A]...': Takes 2 values but 0 were given."],
# Unset default.
[UNSET, "Error: Missing argument 'A...'."],
# Tuples defaults with wrong length.
[
tuple(),
"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.",
],
[(1,), "Error: Invalid value for '[A]...': Takes 2 values but 1 was given."],
[
(1, 2, 3),
"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.",
],
# Lists defaults with wrong length.
[list(), "Error: Invalid value for '[A]...': Takes 2 values but 0 were given."],
[[1], "Error: Invalid value for '[A]...': Takes 2 values but 1 was given."],
[
[1, 2, 3],
"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.",
],
# Sets defaults with wrong length.
[set(), "Error: Invalid value for '[A]...': Takes 2 values but 0 were given."],
[
set([1]),
"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.",
],
[
set([1, 2, 3]),
"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.",
],
# Frozensets defaults with wrong length.
[
frozenset(),
"Error: Invalid value for '[A]...': Takes 2 values but 0 were given.",
],
[
frozenset([1]),
"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.",
],
[
frozenset([1, 2, 3]),
"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.",
],
# Dictionaries defaults with wrong length.
[dict(), "Error: Invalid value for '[A]...': Takes 2 values but 0 were given."],
[
{1: "a"},
"Error: Invalid value for '[A]...': Takes 2 values but 1 was given.",
],
[
{1: "a", 2: "b", 3: "c"},
"Error: Invalid value for '[A]...': Takes 2 values but 3 were given.",
],
],
)
def test_bad_defaults_for_nargs(runner, default, message):
"""Some defaults are not valid when nargs is set."""
@click.command()
@click.argument("a", nargs=2, type=int, default=default)
def cmd(a):
click.echo(repr(a))
result = runner.invoke(cmd, [])
assert message in result.stderr
def test_multiple_param_decls_not_allowed(runner):
with pytest.raises(TypeError):
@click.command()
@click.argument("x", click.Choice(["a", "b"]))
def copy(x):
click.echo(x)
def test_multiple_not_allowed():
with pytest.raises(TypeError, match="multiple"):
click.Argument(["a"], multiple=True)
def test_subcommand_help(runner):
@click.group()
@click.argument("name")
@click.argument("val")
@click.option("--opt")
@click.pass_context
def cli(ctx, name, val, opt):
ctx.obj = dict(name=name, val=val)
@cli.command()
@click.pass_obj
def cmd(obj):
click.echo(f"CMD for {obj['name']} with value {obj['val']}")
result = runner.invoke(cli, ["foo", "bar", "cmd", "--help"])
if result.exception:
raise result.exception
assert "Usage: cli NAME VAL cmd [OPTIONS]" in result.output
def test_nested_subcommand_help(runner):
@click.group()
@click.argument("arg1")
@click.option("--opt1")
def cli(arg1, opt1):
pass
@cli.group()
@click.argument("arg2")
@click.option("--opt2")
def cmd(arg2, opt2):
pass
@cmd.command()
def subcmd():
click.echo("subcommand")
result = runner.invoke(cli, ["arg1", "cmd", "arg2", "subcmd", "--help"])
assert not result.exception
assert "Usage: cli ARG1 cmd ARG2 subcmd [OPTIONS]" in result.output
def test_when_argument_decorator_is_used_multiple_times_cls_is_preserved():
class CustomArgument(click.Argument):
pass
reusable_argument = click.argument("art", cls=CustomArgument)
@click.command()
@reusable_argument
def foo(arg):
pass
@click.command()
@reusable_argument
def bar(arg):
pass
assert isinstance(foo.params[0], CustomArgument)
assert isinstance(bar.params[0], CustomArgument)
@pytest.mark.parametrize(
"args_one,args_two",
[
(
("aardvark",),
("aardvark",),
),
],
)
def test_duplicate_names_warning(runner, args_one, args_two):
@click.command()
@click.argument(*args_one)
@click.argument(*args_two)
def cli(one, two):
pass
with pytest.warns(UserWarning):
runner.invoke(cli, [])
|