| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import enum |
| 4 |
import os |
| 5 |
from itertools import chain |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import click |
| 10 |
from click._utils import UNSET |
| 11 |
|
| 12 |
|
| 13 |
def test_basic_functionality(runner): |
| 14 |
@click.command() |
| 15 |
def cli(): |
| 16 |
"""Hello World!""" |
| 17 |
click.echo("I EXECUTED") |
| 18 |
|
| 19 |
result = runner.invoke(cli, ["--help"]) |
| 20 |
assert not result.exception |
| 21 |
assert "Hello World!" in result.output |
| 22 |
assert "Show this message and exit." in result.output |
| 23 |
assert result.exit_code == 0 |
| 24 |
assert "I EXECUTED" not in result.output |
| 25 |
|
| 26 |
result = runner.invoke(cli, []) |
| 27 |
assert not result.exception |
| 28 |
assert "I EXECUTED" in result.output |
| 29 |
assert result.exit_code == 0 |
| 30 |
|
| 31 |
|
| 32 |
def test_repr(): |
| 33 |
@click.command() |
| 34 |
def command(): |
| 35 |
pass |
| 36 |
|
| 37 |
@click.group() |
| 38 |
def group(): |
| 39 |
pass |
| 40 |
|
| 41 |
@group.command() |
| 42 |
def subcommand(): |
| 43 |
pass |
| 44 |
|
| 45 |
assert repr(command) == "<Command command>" |
| 46 |
assert repr(group) == "<Group group>" |
| 47 |
assert repr(subcommand) == "<Command subcommand>" |
| 48 |
|
| 49 |
|
| 50 |
def test_return_values(): |
| 51 |
@click.command() |
| 52 |
def cli(): |
| 53 |
return 42 |
| 54 |
|
| 55 |
with cli.make_context("foo", []) as ctx: |
| 56 |
rv = cli.invoke(ctx) |
| 57 |
assert rv == 42 |
| 58 |
|
| 59 |
|
| 60 |
def test_basic_group(runner): |
| 61 |
@click.group() |
| 62 |
def cli(): |
| 63 |
"""This is the root.""" |
| 64 |
click.echo("ROOT EXECUTED") |
| 65 |
|
| 66 |
@cli.command() |
| 67 |
def subcommand(): |
| 68 |
"""This is a subcommand.""" |
| 69 |
click.echo("SUBCOMMAND EXECUTED") |
| 70 |
|
| 71 |
result = runner.invoke(cli, ["--help"]) |
| 72 |
assert not result.exception |
| 73 |
assert "COMMAND [ARGS]..." in result.output |
| 74 |
assert "This is the root" in result.output |
| 75 |
assert "This is a subcommand." in result.output |
| 76 |
assert result.exit_code == 0 |
| 77 |
assert "ROOT EXECUTED" not in result.output |
| 78 |
|
| 79 |
result = runner.invoke(cli, ["subcommand"]) |
| 80 |
assert not result.exception |
| 81 |
assert result.exit_code == 0 |
| 82 |
assert "ROOT EXECUTED" in result.output |
| 83 |
assert "SUBCOMMAND EXECUTED" in result.output |
| 84 |
|
| 85 |
|
| 86 |
def test_group_commands_dict(runner): |
| 87 |
"""A Group can be built with a dict of commands.""" |
| 88 |
|
| 89 |
@click.command() |
| 90 |
def sub(): |
| 91 |
click.echo("sub", nl=False) |
| 92 |
|
| 93 |
cli = click.Group(commands={"other": sub}) |
| 94 |
result = runner.invoke(cli, ["other"]) |
| 95 |
assert result.output == "sub" |
| 96 |
|
| 97 |
|
| 98 |
def test_group_from_list(runner): |
| 99 |
"""A Group can be built with a list of commands.""" |
| 100 |
|
| 101 |
@click.command() |
| 102 |
def sub(): |
| 103 |
click.echo("sub", nl=False) |
| 104 |
|
| 105 |
cli = click.Group(commands=[sub]) |
| 106 |
result = runner.invoke(cli, ["sub"]) |
| 107 |
assert result.output == "sub" |
| 108 |
|
| 109 |
|
| 110 |
@pytest.mark.parametrize( |
| 111 |
("args", "expect"), |
| 112 |
[ |
| 113 |
([], "S:[no value]"), |
| 114 |
(["--s=42"], "S:[42]"), |
| 115 |
(["--s"], "Error: Option '--s' requires an argument."), |
| 116 |
(["--s="], "S:[]"), |
| 117 |
(["--s=\N{SNOWMAN}"], "S:[\N{SNOWMAN}]"), |
| 118 |
], |
| 119 |
) |
| 120 |
def test_string_option(runner, args, expect): |
| 121 |
@click.command() |
| 122 |
@click.option("--s", default="no value") |
| 123 |
def cli(s): |
| 124 |
click.echo(f"S:[{s}]") |
| 125 |
|
| 126 |
result = runner.invoke(cli, args) |
| 127 |
assert expect in result.output |
| 128 |
|
| 129 |
if expect.startswith("Error:"): |
| 130 |
assert result.exception is not None |
| 131 |
else: |
| 132 |
assert result.exception is None |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize( |
| 136 |
("args", "expect"), |
| 137 |
[ |
| 138 |
([], "I:[84]"), |
| 139 |
(["--i=23"], "I:[46]"), |
| 140 |
(["--i=x"], "Error: Invalid value for '--i': 'x' is not a valid integer."), |
| 141 |
], |
| 142 |
) |
| 143 |
def test_int_option(runner, args, expect): |
| 144 |
@click.command() |
| 145 |
@click.option("--i", default=42) |
| 146 |
def cli(i): |
| 147 |
click.echo(f"I:[{i * 2}]") |
| 148 |
|
| 149 |
result = runner.invoke(cli, args) |
| 150 |
assert expect in result.output |
| 151 |
|
| 152 |
if expect.startswith("Error:"): |
| 153 |
assert result.exception is not None |
| 154 |
else: |
| 155 |
assert result.exception is None |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize( |
| 159 |
("args", "expect"), |
| 160 |
[ |
| 161 |
([], "U:[ba122011-349f-423b-873b-9d6a79c688ab]"), |
| 162 |
( |
| 163 |
["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"], |
| 164 |
"U:[821592c1-c50e-4971-9cd6-e89dc6832f86]", |
| 165 |
), |
| 166 |
(["--u=x"], "Error: Invalid value for '--u': 'x' is not a valid UUID."), |
| 167 |
], |
| 168 |
) |
| 169 |
def test_uuid_option(runner, args, expect): |
| 170 |
@click.command() |
| 171 |
@click.option( |
| 172 |
"--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID |
| 173 |
) |
| 174 |
def cli(u): |
| 175 |
click.echo(f"U:[{u}]") |
| 176 |
|
| 177 |
result = runner.invoke(cli, args) |
| 178 |
assert expect in result.output |
| 179 |
|
| 180 |
if expect.startswith("Error:"): |
| 181 |
assert result.exception is not None |
| 182 |
else: |
| 183 |
assert result.exception is None |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize( |
| 187 |
("args", "expect"), |
| 188 |
[ |
| 189 |
([], "F:[42.0]"), |
| 190 |
("--f=23.5", "F:[23.5]"), |
| 191 |
("--f=x", "Error: Invalid value for '--f': 'x' is not a valid float."), |
| 192 |
], |
| 193 |
) |
| 194 |
def test_float_option(runner, args, expect): |
| 195 |
@click.command() |
| 196 |
@click.option("--f", default=42.0) |
| 197 |
def cli(f): |
| 198 |
click.echo(f"F:[{f}]") |
| 199 |
|
| 200 |
result = runner.invoke(cli, args) |
| 201 |
assert expect in result.output |
| 202 |
|
| 203 |
if expect.startswith("Error:"): |
| 204 |
assert result.exception is not None |
| 205 |
else: |
| 206 |
assert result.exception is None |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.parametrize( |
| 210 |
("args", "default", "expect"), |
| 211 |
[ |
| 212 |
(["--on"], True, True), |
| 213 |
(["--on"], False, True), |
| 214 |
(["--on"], None, True), |
| 215 |
(["--on"], UNSET, True), |
| 216 |
(["--off"], True, False), |
| 217 |
(["--off"], False, False), |
| 218 |
(["--off"], None, False), |
| 219 |
(["--off"], UNSET, False), |
| 220 |
([], True, True), |
| 221 |
([], False, False), |
| 222 |
([], None, None), |
| 223 |
([], UNSET, False), |
| 224 |
], |
| 225 |
) |
| 226 |
def test_boolean_switch(runner, args, default, expect): |
| 227 |
@click.command() |
| 228 |
@click.option("--on/--off", default=default) |
| 229 |
def cli(on): |
| 230 |
return on |
| 231 |
|
| 232 |
result = runner.invoke(cli, args, standalone_mode=False) |
| 233 |
assert result.return_value is expect |
| 234 |
|
| 235 |
|
| 236 |
@pytest.mark.parametrize( |
| 237 |
("default", "args", "expect"), |
| 238 |
( |
| 239 |
(True, ["--f"], True), |
| 240 |
(True, [], True), |
| 241 |
(False, ["--f"], True), |
| 242 |
(False, [], False), |
| 243 |
# Boolean flags have a 3-states logic. |
| 244 |
# See: https://github.com/pallets/click/issues/3024#issue-3285556668 |
| 245 |
(None, ["--f"], True), |
| 246 |
(None, [], None), |
| 247 |
), |
| 248 |
) |
| 249 |
def test_boolean_flag(runner, default, args, expect): |
| 250 |
@click.command() |
| 251 |
@click.option("--f", is_flag=True, default=default) |
| 252 |
def cli(f): |
| 253 |
return f |
| 254 |
|
| 255 |
result = runner.invoke(cli, args, standalone_mode=False) |
| 256 |
assert result.return_value is expect |
| 257 |
|
| 258 |
|
| 259 |
@pytest.mark.parametrize( |
| 260 |
("value", "expect"), |
| 261 |
chain( |
| 262 |
((x, "True") for x in ("1", "true", "t", "yes", "y", "on")), |
| 263 |
((x, "False") for x in ("0", "false", "f", "no", "n", "off")), |
| 264 |
), |
| 265 |
) |
| 266 |
def test_boolean_conversion(runner, value, expect): |
| 267 |
@click.command() |
| 268 |
@click.option("--flag", type=bool) |
| 269 |
def cli(flag): |
| 270 |
click.echo(flag, nl=False) |
| 271 |
|
| 272 |
result = runner.invoke(cli, ["--flag", value]) |
| 273 |
assert result.output == expect |
| 274 |
|
| 275 |
result = runner.invoke(cli, ["--flag", value.title()]) |
| 276 |
assert result.output == expect |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.parametrize( |
| 280 |
("default", "args", "expected"), |
| 281 |
# These test cases are similar to the ones in |
| 282 |
# tests/test_options.py::test_default_dual_option_callback, so keep them in sync. |
| 283 |
( |
| 284 |
# Each option is returning its own flag_value, whatever the default is. |
| 285 |
(True, ["--upper"], "upper"), |
| 286 |
(True, ["--lower"], "lower"), |
| 287 |
(False, ["--upper"], "upper"), |
| 288 |
(False, ["--lower"], "lower"), |
| 289 |
(None, ["--upper"], "upper"), |
| 290 |
(None, ["--lower"], "lower"), |
| 291 |
(UNSET, ["--upper"], "upper"), |
| 292 |
(UNSET, ["--lower"], "lower"), |
| 293 |
# Check that the last option wins when both are specified. |
| 294 |
(True, ["--upper", "--lower"], "lower"), |
| 295 |
(True, ["--lower", "--upper"], "upper"), |
| 296 |
# Check that the default is returned as-is when no option is specified. |
| 297 |
("upper", [], "upper"), |
| 298 |
("lower", [], "lower"), |
| 299 |
("uPPer", [], "uPPer"), |
| 300 |
("lOwEr", [], "lOwEr"), |
| 301 |
(" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "), |
| 302 |
(None, [], None), |
| 303 |
# Default is normalized to None if it is UNSET. |
| 304 |
(UNSET, [], None), |
| 305 |
# Special case: if default=True and flag_value is set, the value returned is the |
| 306 |
# flag_value, not the True Python value itself. |
| 307 |
(True, [], "upper"), |
| 308 |
# Non-string defaults are process as strings by the default Parameter's type. |
| 309 |
(False, [], "False"), |
| 310 |
(42, [], "42"), |
| 311 |
(12.3, [], "12.3"), |
| 312 |
), |
| 313 |
) |
| 314 |
def test_flag_value_dual_options(runner, default, args, expected): |
| 315 |
"""Check how default is processed when options compete for the same variable name. |
| 316 |
|
| 317 |
Covers the regression reported in |
| 318 |
https://github.com/pallets/click/issues/3024#issuecomment-3146199461 |
| 319 |
|
| 320 |
.. hint:: |
| 321 |
Similar to ``test_options.py::test_default_dual_option_callback``. |
| 322 |
|
| 323 |
``test_defaults.py::test_shared_param_prefers_first_default`` |
| 324 |
is a smoke-test complement that exercises both default placements. |
| 325 |
""" |
| 326 |
|
| 327 |
@click.command() |
| 328 |
@click.option("--upper", "case", flag_value="upper", default=default) |
| 329 |
@click.option("--lower", "case", flag_value="lower") |
| 330 |
def cli(case): |
| 331 |
click.echo(repr(case), nl=False) |
| 332 |
|
| 333 |
result = runner.invoke(cli, args) |
| 334 |
assert result.output == repr(expected) |
| 335 |
|
| 336 |
|
| 337 |
def test_file_option(runner): |
| 338 |
@click.command() |
| 339 |
@click.option("--file", type=click.File("w")) |
| 340 |
def input(file): |
| 341 |
file.write("Hello World!\n") |
| 342 |
|
| 343 |
@click.command() |
| 344 |
@click.option("--file", type=click.File("r")) |
| 345 |
def output(file): |
| 346 |
click.echo(file.read()) |
| 347 |
|
| 348 |
with runner.isolated_filesystem(): |
| 349 |
result_in = runner.invoke(input, ["--file=example.txt"]) |
| 350 |
result_out = runner.invoke(output, ["--file=example.txt"]) |
| 351 |
|
| 352 |
assert not result_in.exception |
| 353 |
assert result_in.output == "" |
| 354 |
assert not result_out.exception |
| 355 |
assert result_out.output == "Hello World!\n\n" |
| 356 |
|
| 357 |
|
| 358 |
def test_file_lazy_mode(runner): |
| 359 |
do_io = False |
| 360 |
|
| 361 |
@click.command() |
| 362 |
@click.option("--file", type=click.File("w")) |
| 363 |
def input(file): |
| 364 |
if do_io: |
| 365 |
file.write("Hello World!\n") |
| 366 |
|
| 367 |
@click.command() |
| 368 |
@click.option("--file", type=click.File("r")) |
| 369 |
def output(file): |
| 370 |
pass |
| 371 |
|
| 372 |
with runner.isolated_filesystem(): |
| 373 |
os.mkdir("example.txt") |
| 374 |
|
| 375 |
do_io = True |
| 376 |
result_in = runner.invoke(input, ["--file=example.txt"]) |
| 377 |
assert result_in.exit_code == 1 |
| 378 |
|
| 379 |
do_io = False |
| 380 |
result_in = runner.invoke(input, ["--file=example.txt"]) |
| 381 |
assert result_in.exit_code == 0 |
| 382 |
|
| 383 |
result_out = runner.invoke(output, ["--file=example.txt"]) |
| 384 |
assert result_out.exception |
| 385 |
|
| 386 |
@click.command() |
| 387 |
@click.option("--file", type=click.File("w", lazy=False)) |
| 388 |
def input_non_lazy(file): |
| 389 |
file.write("Hello World!\n") |
| 390 |
|
| 391 |
with runner.isolated_filesystem(): |
| 392 |
os.mkdir("example.txt") |
| 393 |
result_in = runner.invoke(input_non_lazy, ["--file=example.txt"]) |
| 394 |
assert result_in.exit_code == 2 |
| 395 |
assert "Invalid value for '--file': 'example.txt'" in result_in.output |
| 396 |
|
| 397 |
|
| 398 |
def test_path_option(runner): |
| 399 |
@click.command() |
| 400 |
@click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True)) |
| 401 |
def write_to_dir(o): |
| 402 |
with open(os.path.join(o, "foo.txt"), "wb") as f: |
| 403 |
f.write(b"meh\n") |
| 404 |
|
| 405 |
with runner.isolated_filesystem(): |
| 406 |
os.mkdir("test") |
| 407 |
|
| 408 |
result = runner.invoke(write_to_dir, ["-O", "test"]) |
| 409 |
assert not result.exception |
| 410 |
|
| 411 |
with open("test/foo.txt", "rb") as f: |
| 412 |
assert f.read() == b"meh\n" |
| 413 |
|
| 414 |
result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"]) |
| 415 |
assert "is a file" in result.output |
| 416 |
|
| 417 |
@click.command() |
| 418 |
@click.option("-f", type=click.Path(exists=True)) |
| 419 |
def showtype(f): |
| 420 |
click.echo(f"is_file={os.path.isfile(f)}") |
| 421 |
click.echo(f"is_dir={os.path.isdir(f)}") |
| 422 |
|
| 423 |
with runner.isolated_filesystem(): |
| 424 |
result = runner.invoke(showtype, ["-f", "xxx"]) |
| 425 |
assert "does not exist" in result.output |
| 426 |
|
| 427 |
result = runner.invoke(showtype, ["-f", "."]) |
| 428 |
assert "is_file=False" in result.output |
| 429 |
assert "is_dir=True" in result.output |
| 430 |
|
| 431 |
@click.command() |
| 432 |
@click.option("-f", type=click.Path()) |
| 433 |
def exists(f): |
| 434 |
click.echo(f"exists={os.path.exists(f)}") |
| 435 |
|
| 436 |
with runner.isolated_filesystem(): |
| 437 |
result = runner.invoke(exists, ["-f", "xxx"]) |
| 438 |
assert "exists=False" in result.output |
| 439 |
|
| 440 |
result = runner.invoke(exists, ["-f", "."]) |
| 441 |
assert "exists=True" in result.output |
| 442 |
|
| 443 |
|
| 444 |
def test_choice_option(runner): |
| 445 |
@click.command() |
| 446 |
@click.option("--method", type=click.Choice(["foo", "bar", "baz"])) |
| 447 |
def cli(method): |
| 448 |
click.echo(method) |
| 449 |
|
| 450 |
result = runner.invoke(cli, ["--method=foo"]) |
| 451 |
assert not result.exception |
| 452 |
assert result.output == "foo\n" |
| 453 |
|
| 454 |
result = runner.invoke(cli, ["--method=meh"]) |
| 455 |
assert result.exit_code == 2 |
| 456 |
assert ( |
| 457 |
"Invalid value for '--method': 'meh' is not one of 'foo', 'bar', 'baz'." |
| 458 |
in result.output |
| 459 |
) |
| 460 |
|
| 461 |
result = runner.invoke(cli, ["--help"]) |
| 462 |
assert "--method [foo|bar|baz]" in result.output |
| 463 |
|
| 464 |
|
| 465 |
def test_choice_argument(runner): |
| 466 |
@click.command() |
| 467 |
@click.argument("method", type=click.Choice(["foo", "bar", "baz"])) |
| 468 |
def cli(method): |
| 469 |
click.echo(method) |
| 470 |
|
| 471 |
result = runner.invoke(cli, ["foo"]) |
| 472 |
assert not result.exception |
| 473 |
assert result.output == "foo\n" |
| 474 |
|
| 475 |
result = runner.invoke(cli, ["meh"]) |
| 476 |
assert result.exit_code == 2 |
| 477 |
assert ( |
| 478 |
"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo'," |
| 479 |
" 'bar', 'baz'." in result.output |
| 480 |
) |
| 481 |
|
| 482 |
result = runner.invoke(cli, ["--help"]) |
| 483 |
assert "{foo|bar|baz}" in result.output |
| 484 |
|
| 485 |
|
| 486 |
def test_choice_argument_enum(runner): |
| 487 |
class MyEnum(str, enum.Enum): |
| 488 |
FOO = "foo-value" |
| 489 |
BAR = "bar-value" |
| 490 |
BAZ = "baz-value" |
| 491 |
|
| 492 |
@click.command() |
| 493 |
@click.argument("method", type=click.Choice(MyEnum, case_sensitive=False)) |
| 494 |
def cli(method: MyEnum): |
| 495 |
assert isinstance(method, MyEnum) |
| 496 |
click.echo(method) |
| 497 |
|
| 498 |
result = runner.invoke(cli, ["foo"]) |
| 499 |
assert result.output == "foo-value\n" |
| 500 |
assert not result.exception |
| 501 |
|
| 502 |
result = runner.invoke(cli, ["meh"]) |
| 503 |
assert result.exit_code == 2 |
| 504 |
assert ( |
| 505 |
"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo'," |
| 506 |
" 'bar', 'baz'." in result.output |
| 507 |
) |
| 508 |
|
| 509 |
result = runner.invoke(cli, ["--help"]) |
| 510 |
assert "{foo|bar|baz}" in result.output |
| 511 |
|
| 512 |
|
| 513 |
def test_choice_argument_custom_type(runner): |
| 514 |
class MyClass: |
| 515 |
def __init__(self, value: str) -> None: |
| 516 |
self.value = value |
| 517 |
|
| 518 |
def __str__(self) -> str: |
| 519 |
return self.value |
| 520 |
|
| 521 |
@click.command() |
| 522 |
@click.argument( |
| 523 |
"method", type=click.Choice([MyClass("foo"), MyClass("bar"), MyClass("baz")]) |
| 524 |
) |
| 525 |
def cli(method: MyClass): |
| 526 |
assert isinstance(method, MyClass) |
| 527 |
click.echo(method) |
| 528 |
|
| 529 |
result = runner.invoke(cli, ["foo"]) |
| 530 |
assert not result.exception |
| 531 |
assert result.output == "foo\n" |
| 532 |
|
| 533 |
result = runner.invoke(cli, ["meh"]) |
| 534 |
assert result.exit_code == 2 |
| 535 |
assert ( |
| 536 |
"Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo'," |
| 537 |
" 'bar', 'baz'." in result.output |
| 538 |
) |
| 539 |
|
| 540 |
result = runner.invoke(cli, ["--help"]) |
| 541 |
assert "{foo|bar|baz}" in result.output |
| 542 |
|
| 543 |
|
| 544 |
def test_choice_argument_none(runner): |
| 545 |
@click.command() |
| 546 |
@click.argument( |
| 547 |
"method", type=click.Choice(["not-none", None], case_sensitive=False) |
| 548 |
) |
| 549 |
def cli(method: str | None): |
| 550 |
assert isinstance(method, str) or method is None |
| 551 |
click.echo(repr(method), nl=False) |
| 552 |
|
| 553 |
result = runner.invoke(cli, ["not-none"]) |
| 554 |
assert not result.exception |
| 555 |
assert result.output == repr("not-none") |
| 556 |
|
| 557 |
result = runner.invoke(cli, ["none"]) |
| 558 |
assert not result.exception |
| 559 |
assert result.output == repr(None) |
| 560 |
|
| 561 |
result = runner.invoke(cli, []) |
| 562 |
assert result.exception |
| 563 |
assert ( |
| 564 |
"Error: Missing argument '{not-none|none}'. " |
| 565 |
"Choose from:\n\tnot-none,\n\tnone\n" in result.stderr |
| 566 |
) |
| 567 |
|
| 568 |
result = runner.invoke(cli, ["--help"]) |
| 569 |
assert result.output.startswith("Usage: cli [OPTIONS] {not-none|none}\n") |
| 570 |
|
| 571 |
|
| 572 |
def test_datetime_option_default(runner): |
| 573 |
@click.command() |
| 574 |
@click.option("--start_date", type=click.DateTime()) |
| 575 |
def cli(start_date): |
| 576 |
click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S")) |
| 577 |
|
| 578 |
result = runner.invoke(cli, ["--start_date=2015-09-29"]) |
| 579 |
assert not result.exception |
| 580 |
assert result.output == "2015-09-29T00:00:00\n" |
| 581 |
|
| 582 |
result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"]) |
| 583 |
assert not result.exception |
| 584 |
assert result.output == "2015-09-29T09:11:22\n" |
| 585 |
|
| 586 |
result = runner.invoke(cli, ["--start_date=2015-09"]) |
| 587 |
assert result.exit_code == 2 |
| 588 |
assert ( |
| 589 |
"Invalid value for '--start_date': '2015-09' does not match the formats" |
| 590 |
" '%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S'." |
| 591 |
) in result.output |
| 592 |
|
| 593 |
result = runner.invoke(cli, ["--help"]) |
| 594 |
assert ( |
| 595 |
"--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output |
| 596 |
) |
| 597 |
|
| 598 |
|
| 599 |
def test_datetime_option_custom(runner): |
| 600 |
@click.command() |
| 601 |
@click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"])) |
| 602 |
def cli(start_date): |
| 603 |
click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S")) |
| 604 |
|
| 605 |
result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"]) |
| 606 |
assert not result.exception |
| 607 |
assert result.output == "2010-06-05T00:00:00\n" |
| 608 |
|
| 609 |
|
| 610 |
def test_required_option(runner): |
| 611 |
@click.command() |
| 612 |
@click.option("--foo", required=True) |
| 613 |
def cli(foo): |
| 614 |
click.echo(foo) |
| 615 |
|
| 616 |
result = runner.invoke(cli, []) |
| 617 |
assert result.exit_code == 2 |
| 618 |
assert "Missing option '--foo'" in result.output |
| 619 |
|
| 620 |
|
| 621 |
def test_evaluation_order(runner): |
| 622 |
called = [] |
| 623 |
|
| 624 |
def memo(ctx, param, value): |
| 625 |
called.append(value) |
| 626 |
return value |
| 627 |
|
| 628 |
@click.command() |
| 629 |
@click.option("--missing", default="missing", is_eager=False, callback=memo) |
| 630 |
@click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo) |
| 631 |
@click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo) |
| 632 |
@click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo) |
| 633 |
@click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo) |
| 634 |
@click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo) |
| 635 |
@click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo) |
| 636 |
def cli(**x): |
| 637 |
pass |
| 638 |
|
| 639 |
result = runner.invoke( |
| 640 |
cli, |
| 641 |
[ |
| 642 |
"--eager-flag2", |
| 643 |
"--eager-flag1", |
| 644 |
"--normal-flag2", |
| 645 |
"--eager-flag3", |
| 646 |
"--normal-flag3", |
| 647 |
"--normal-flag3", |
| 648 |
"--normal-flag1", |
| 649 |
"--normal-flag1", |
| 650 |
], |
| 651 |
) |
| 652 |
assert not result.exception |
| 653 |
assert called == [ |
| 654 |
"eager2", |
| 655 |
"eager1", |
| 656 |
"eager3", |
| 657 |
"normal2", |
| 658 |
"normal3", |
| 659 |
"normal1", |
| 660 |
"missing", |
| 661 |
] |
| 662 |
|
| 663 |
|
| 664 |
def test_hidden_option(runner): |
| 665 |
@click.command() |
| 666 |
@click.option("--nope", hidden=True) |
| 667 |
def cli(nope): |
| 668 |
click.echo(nope) |
| 669 |
|
| 670 |
result = runner.invoke(cli, ["--help"]) |
| 671 |
assert result.exit_code == 0 |
| 672 |
assert "--nope" not in result.output |
| 673 |
|
| 674 |
|
| 675 |
def test_hidden_command(runner): |
| 676 |
@click.group() |
| 677 |
def cli(): |
| 678 |
pass |
| 679 |
|
| 680 |
@cli.command(hidden=True) |
| 681 |
def nope(): |
| 682 |
pass |
| 683 |
|
| 684 |
result = runner.invoke(cli, ["--help"]) |
| 685 |
assert result.exit_code == 0 |
| 686 |
assert "nope" not in result.output |
| 687 |
|
| 688 |
|
| 689 |
def test_hidden_group(runner): |
| 690 |
@click.group() |
| 691 |
def cli(): |
| 692 |
pass |
| 693 |
|
| 694 |
@cli.group(hidden=True) |
| 695 |
def subgroup(): |
| 696 |
pass |
| 697 |
|
| 698 |
@subgroup.command() |
| 699 |
def nope(): |
| 700 |
pass |
| 701 |
|
| 702 |
result = runner.invoke(cli, ["--help"]) |
| 703 |
assert result.exit_code == 0 |
| 704 |
assert "subgroup" not in result.output |
| 705 |
assert "nope" not in result.output |
| 706 |
|
| 707 |
|
| 708 |
def test_summary_line(runner): |
| 709 |
@click.group() |
| 710 |
def cli(): |
| 711 |
pass |
| 712 |
|
| 713 |
@cli.command() |
| 714 |
def cmd(): |
| 715 |
""" |
| 716 |
Summary line without period |
| 717 |
|
| 718 |
Here is a sentence. And here too. |
| 719 |
""" |
| 720 |
pass |
| 721 |
|
| 722 |
result = runner.invoke(cli, ["--help"]) |
| 723 |
assert "Summary line without period" in result.output |
| 724 |
assert "Here is a sentence." not in result.output |
| 725 |
|
| 726 |
|
| 727 |
def test_help_invalid_default(runner): |
| 728 |
cli = click.Command( |
| 729 |
"cli", |
| 730 |
params=[ |
| 731 |
click.Option( |
| 732 |
["-a"], |
| 733 |
type=click.Path(exists=True), |
| 734 |
default="not found", |
| 735 |
show_default=True, |
| 736 |
), |
| 737 |
], |
| 738 |
) |
| 739 |
result = runner.invoke(cli, ["--help"]) |
| 740 |
assert result.exit_code == 0 |
| 741 |
assert "default: not found" in result.output |