Skip to content
Myr

test_basic.py

This file

Path

tests/test_basic.py

576 lines · 15.2 KiB · Python

Last changed by

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

History of this file

Take it

The walk stopped after 200 commits: lines older than 5189067 point at it.

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 1why, one step back
1import os

Merge pull request #1630 from jwodder/more-boolsDavid Lordbefore adoption2815455

line 2why, one step back
2from itertools import chain

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 3why, one step back
3

Merge pull request #1630 from jwodder/more-boolsDavid Lordbefore adoption2815455

lines 4–5why, one step back
4import pytest
5

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 6–81why, one step back
6import click
789def test_basic_functionality(runner):10 @click.command()11 def cli():12 """Hello World!"""13 click.echo("I EXECUTED")1415 result = runner.invoke(cli, ["--help"])16 assert not result.exception17 assert "Hello World!" in result.output18 assert "Show this message and exit." in result.output19 assert result.exit_code == 020 assert "I EXECUTED" not in result.output2122 result = runner.invoke(cli, [])23 assert not result.exception24 assert "I EXECUTED" in result.output25 assert result.exit_code == 0262728def test_repr():29 @click.command()30 def command():31 pass3233 @click.group()34 def group():35 pass3637 @group.command()38 def subcommand():39 pass4041 assert repr(command) == "<Command command>"42 assert repr(group) == "<Group group>"43 assert repr(subcommand) == "<Command subcommand>"444546def test_return_values():47 @click.command()48 def cli():49 return 425051 with cli.make_context("foo", []) as ctx:52 rv = cli.invoke(ctx)53 assert rv == 42545556def test_basic_group(runner):57 @click.group()58 def cli():59 """This is the root."""60 click.echo("ROOT EXECUTED")6162 @cli.command()63 def subcommand():64 """This is a subcommand."""65 click.echo("SUBCOMMAND EXECUTED")6667 result = runner.invoke(cli, ["--help"])68 assert not result.exception69 assert "COMMAND [ARGS]..." in result.output70 assert "This is the root" in result.output71 assert "This is a subcommand." in result.output72 assert result.exit_code == 073 assert "ROOT EXECUTED" not in result.output7475 result = runner.invoke(cli, ["subcommand"])76 assert not result.exception77 assert result.exit_code == 078 assert "ROOT EXECUTED" in result.output79 assert "SUBCOMMAND EXECUTED" in result.output8081

Merge pull request #1405 from energizah/commands-listDavid Lordbefore adoption7147f41

lines 82–105why, one step back
82def test_group_commands_dict(runner):
83 """A Group can be built with a dict of commands."""8485 @click.command()86 def sub():87 click.echo("sub", nl=False)8889 cli = click.Group(commands={"other": sub})90 result = runner.invoke(cli, ["other"])91 assert result.output == "sub"929394def test_group_from_list(runner):95 """A Group can be built with a list of commands."""9697 @click.command()98 def sub():99 click.echo("sub", nl=False)100101 cli = click.Group(commands=[sub])102 result = runner.invoke(cli, ["sub"])103 assert result.output == "sub"104105

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 106–116why, one step back
106@pytest.mark.parametrize(
107 ("args", "expect"),108 [109 ([], "S:[no value]"),110 (["--s=42"], "S:[42]"),111 (["--s"], "Error: Option '--s' requires an argument."),112 (["--s="], "S:[]"),113 (["--s=\N{SNOWMAN}"], "S:[\N{SNOWMAN}]"),114 ],115)116def test_string_option(runner, args, expect):

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 117why, one step back
117 @click.command()

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 118–120why, one step back
118 @click.option("--s", default="no value")
119 def cli(s):120 click.echo(f"S:[{s}]")

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 121why, one step back
121

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 122–123why, one step back
122 result = runner.invoke(cli, args)
123 assert expect in result.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 124why, one step back
124

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 125–128why, one step back
125 if expect.startswith("Error:"):
126 assert result.exception is not None127 else:128 assert result.exception is None

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 129–130why, one step back
129
130

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 131–139why, one step back
131@pytest.mark.parametrize(
132 ("args", "expect"),133 [134 ([], "I:[84]"),135 (["--i=23"], "I:[46]"),136 (["--i=x"], "Error: Invalid value for '--i': 'x' is not a valid integer."),137 ],138)139def test_int_option(runner, args, expect):

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 140why, one step back
140 @click.command()

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 141–143why, one step back
141 @click.option("--i", default=42)
142 def cli(i):143 click.echo(f"I:[{i * 2}]")

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 144why, one step back
144

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 145–146why, one step back
145 result = runner.invoke(cli, args)
146 assert expect in result.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 147why, one step back
147

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 148–151why, one step back
148 if expect.startswith("Error:"):
149 assert result.exception is not None150 else:151 assert result.exception is None

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 152–153why, one step back
152
153

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 154–165why, one step back
154@pytest.mark.parametrize(
155 ("args", "expect"),156 [157 ([], "U:[ba122011-349f-423b-873b-9d6a79c688ab]"),158 (159 ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"],160 "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]",161 ),162 (["--u=x"], "Error: Invalid value for '--u': 'x' is not a valid UUID."),163 ],164)165def test_uuid_option(runner, args, expect):

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 166–172why, one step back
166 @click.command()
167 @click.option(168 "--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID169 )170 def cli(u):171 click.echo(f"U:[{u}]")172

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 173–174why, one step back
173 result = runner.invoke(cli, args)
174 assert expect in result.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 175why, one step back
175

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 176–179why, one step back
176 if expect.startswith("Error:"):
177 assert result.exception is not None178 else:179 assert result.exception is None

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 180–181why, one step back
180
181

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 182–190why, one step back
182@pytest.mark.parametrize(
183 ("args", "expect"),184 [185 ([], "F:[42.0]"),186 ("--f=23.5", "F:[23.5]"),187 ("--f=x", "Error: Invalid value for '--f': 'x' is not a valid float."),188 ],189)190def test_float_option(runner, args, expect):

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 191why, one step back
191 @click.command()

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 192–194why, one step back
192 @click.option("--f", default=42.0)
193 def cli(f):194 click.echo(f"F:[{f}]")

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 195why, one step back
195

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 196–197why, one step back
196 result = runner.invoke(cli, args)
197 assert expect in result.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 198why, one step back
198

Merge pull request #1845 from BALaka-18/organize-testsDavid Lordbefore adoption5828a16

lines 199–202why, one step back
199 if expect.startswith("Error:"):
200 assert result.exception is not None201 else:202 assert result.exception is None

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 203–237why, one step back
203
204205def test_boolean_option(runner):206 for default in True, False:207208 @click.command()209 @click.option("--with-foo/--without-foo", default=default)210 def cli(with_foo):211 click.echo(with_foo)212213 result = runner.invoke(cli, ["--with-foo"])214 assert not result.exception215 assert result.output == "True\n"216 result = runner.invoke(cli, ["--without-foo"])217 assert not result.exception218 assert result.output == "False\n"219 result = runner.invoke(cli, [])220 assert not result.exception221 assert result.output == f"{default}\n"222223 for default in True, False:224225 @click.command()226 @click.option("--flag", is_flag=True, default=default)227 def cli(flag):228 click.echo(flag)229230 result = runner.invoke(cli, ["--flag"])231 assert not result.exception232 assert result.output == f"{not default}\n"233 result = runner.invoke(cli, [])234 assert not result.exception235 assert result.output == f"{default}\n"236237

Merge pull request #1630 from jwodder/more-boolsDavid Lordbefore adoption2815455

lines 238–249why, one step back
238@pytest.mark.parametrize(
239 ("value", "expect"),240 chain(241 ((x, "True") for x in ("1", "true", "t", "yes", "y", "on")),242 ((x, "False") for x in ("0", "false", "f", "no", "n", "off")),243 ),244)245def test_boolean_conversion(runner, value, expect):246 @click.command()247 @click.option("--flag", type=bool)248 def cli(flag):249 click.echo(flag, nl=False)

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 250why, one step back
250

Merge pull request #1630 from jwodder/more-boolsDavid Lordbefore adoption2815455

lines 251–252why, one step back
251 result = runner.invoke(cli, ["--flag", value])
252 assert result.output == expect

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

line 253why, one step back
253

Merge pull request #1630 from jwodder/more-boolsDavid Lordbefore adoption2815455

lines 254–255why, one step back
254 result = runner.invoke(cli, ["--flag", value.title()])
255 assert result.output == expect

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 256–315why, one step back
256
257258def test_file_option(runner):259 @click.command()260 @click.option("--file", type=click.File("w"))261 def input(file):262 file.write("Hello World!\n")263264 @click.command()265 @click.option("--file", type=click.File("r"))266 def output(file):267 click.echo(file.read())268269 with runner.isolated_filesystem():270 result_in = runner.invoke(input, ["--file=example.txt"])271 result_out = runner.invoke(output, ["--file=example.txt"])272273 assert not result_in.exception274 assert result_in.output == ""275 assert not result_out.exception276 assert result_out.output == "Hello World!\n\n"277278279def test_file_lazy_mode(runner):280 do_io = False281282 @click.command()283 @click.option("--file", type=click.File("w"))284 def input(file):285 if do_io:286 file.write("Hello World!\n")287288 @click.command()289 @click.option("--file", type=click.File("r"))290 def output(file):291 pass292293 with runner.isolated_filesystem():294 os.mkdir("example.txt")295296 do_io = True297 result_in = runner.invoke(input, ["--file=example.txt"])298 assert result_in.exit_code == 1299300 do_io = False301 result_in = runner.invoke(input, ["--file=example.txt"])302 assert result_in.exit_code == 0303304 result_out = runner.invoke(output, ["--file=example.txt"])305 assert result_out.exception306307 @click.command()308 @click.option("--file", type=click.File("w", lazy=False))309 def input_non_lazy(file):310 file.write("Hello World!\n")311312 with runner.isolated_filesystem():313 os.mkdir("example.txt")314 result_in = runner.invoke(input_non_lazy, ["--file=example.txt"])315 assert result_in.exit_code == 2

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

line 316why, one step back
316 assert "Invalid value for '--file': 'example.txt'" in result_in.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 317–377why, one step back
317
318319def test_path_option(runner):320 @click.command()321 @click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True))322 def write_to_dir(o):323 with open(os.path.join(o, "foo.txt"), "wb") as f:324 f.write(b"meh\n")325326 with runner.isolated_filesystem():327 os.mkdir("test")328329 result = runner.invoke(write_to_dir, ["-O", "test"])330 assert not result.exception331332 with open("test/foo.txt", "rb") as f:333 assert f.read() == b"meh\n"334335 result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"])336 assert "is a file" in result.output337338 @click.command()339 @click.option("-f", type=click.Path(exists=True))340 def showtype(f):341 click.echo(f"is_file={os.path.isfile(f)}")342 click.echo(f"is_dir={os.path.isdir(f)}")343344 with runner.isolated_filesystem():345 result = runner.invoke(showtype, ["-f", "xxx"])346 assert "does not exist" in result.output347348 result = runner.invoke(showtype, ["-f", "."])349 assert "is_file=False" in result.output350 assert "is_dir=True" in result.output351352 @click.command()353 @click.option("-f", type=click.Path())354 def exists(f):355 click.echo(f"exists={os.path.exists(f)}")356357 with runner.isolated_filesystem():358 result = runner.invoke(exists, ["-f", "xxx"])359 assert "exists=False" in result.output360361 result = runner.invoke(exists, ["-f", "."])362 assert "exists=True" in result.output363364365def test_choice_option(runner):366 @click.command()367 @click.option("--method", type=click.Choice(["foo", "bar", "baz"]))368 def cli(method):369 click.echo(method)370371 result = runner.invoke(cli, ["--method=foo"])372 assert not result.exception373 assert result.output == "foo\n"374375 result = runner.invoke(cli, ["--method=meh"])376 assert result.exit_code == 2377 assert (

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 378–379why, one step back
378 "Invalid value for '--method': 'meh' is not one of 'foo', 'bar', 'baz'."
379 in result.output

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 380–385why, one step back
380 )
381382 result = runner.invoke(cli, ["--help"])383 assert "--method [foo|bar|baz]" in result.output384385

Merge pull request #1273 from 0xDEC0DE/issue/1272David Lordbefore adoptiond15f740

lines 386–398why, one step back
386def test_choice_argument(runner):
387 @click.command()388 @click.argument("method", type=click.Choice(["foo", "bar", "baz"]))389 def cli(method):390 click.echo(method)391392 result = runner.invoke(cli, ["foo"])393 assert not result.exception394 assert result.output == "foo\n"395396 result = runner.invoke(cli, ["meh"])397 assert result.exit_code == 2398 assert (

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 399–400why, one step back
399 "Invalid value for '{foo|bar|baz}': 'meh' is not one of 'foo',"
400 " 'bar', 'baz'." in result.output

Merge pull request #1273 from 0xDEC0DE/issue/1272David Lordbefore adoptiond15f740

lines 401–406why, one step back
401 )
402403 result = runner.invoke(cli, ["--help"])404 assert "{foo|bar|baz}" in result.output405406

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 407–423why, one step back
407def test_datetime_option_default(runner):
408 @click.command()409 @click.option("--start_date", type=click.DateTime())410 def cli(start_date):411 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))412413 result = runner.invoke(cli, ["--start_date=2015-09-29"])414 assert not result.exception415 assert result.output == "2015-09-29T00:00:00\n"416417 result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"])418 assert not result.exception419 assert result.output == "2015-09-29T09:11:22\n"420421 result = runner.invoke(cli, ["--start_date=2015-09"])422 assert result.exit_code == 2423 assert (

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

lines 424–425why, one step back
424 "Invalid value for '--start_date': '2015-09' does not match the formats"
425 " '%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S'."

Merge pull request #1632 from pallets/dependabot/pip/pip-tools-5.3.0dependabot-preview[bot]before adoption5189067

lines 426–540why, one step back
426 ) in result.output
427428 result = runner.invoke(cli, ["--help"])429 assert (430 "--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output431 )432433434def test_datetime_option_custom(runner):435 @click.command()436 @click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"]))437 def cli(start_date):438 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))439440 result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"])441 assert not result.exception442 assert result.output == "2010-06-05T00:00:00\n"443444445def test_required_option(runner):446 @click.command()447 @click.option("--foo", required=True)448 def cli(foo):449 click.echo(foo)450451 result = runner.invoke(cli, [])452 assert result.exit_code == 2453 assert "Missing option '--foo'" in result.output454455456def test_evaluation_order(runner):457 called = []458459 def memo(ctx, param, value):460 called.append(value)461 return value462463 @click.command()464 @click.option("--missing", default="missing", is_eager=False, callback=memo)465 @click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo)466 @click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo)467 @click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo)468 @click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo)469 @click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo)470 @click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo)471 def cli(**x):472 pass473474 result = runner.invoke(475 cli,476 [477 "--eager-flag2",478 "--eager-flag1",479 "--normal-flag2",480 "--eager-flag3",481 "--normal-flag3",482 "--normal-flag3",483 "--normal-flag1",484 "--normal-flag1",485 ],486 )487 assert not result.exception488 assert called == [489 "eager2",490 "eager1",491 "eager3",492 "normal2",493 "normal3",494 "normal1",495 "missing",496 ]497498499def test_hidden_option(runner):500 @click.command()501 @click.option("--nope", hidden=True)502 def cli(nope):503 click.echo(nope)504505 result = runner.invoke(cli, ["--help"])506 assert result.exit_code == 0507 assert "--nope" not in result.output508509510def test_hidden_command(runner):511 @click.group()512 def cli():513 pass514515 @cli.command(hidden=True)516 def nope():517 pass518519 result = runner.invoke(cli, ["--help"])520 assert result.exit_code == 0521 assert "nope" not in result.output522523524def test_hidden_group(runner):525 @click.group()526 def cli():527 pass528529 @cli.group(hidden=True)530 def subgroup():531 pass532533 @subgroup.command()534 def nope():535 pass536537 result = runner.invoke(cli, ["--help"])538 assert result.exit_code == 0539 assert "subgroup" not in result.output540 assert "nope" not in result.output

Merge pull request #1695 from srafi1/help-summaryAmy Leibefore adoptiona4763ef

lines 541–559why, one step back
541
542543def test_summary_line(runner):544 @click.group()545 def cli():546 pass547548 @cli.command()549 def cmd():550 """551 Summary line without period552553 Here is a sentence. And here too.554 """555 pass556557 result = runner.invoke(cli, ["--help"])558 assert "Summary line without period" in result.output559 assert "Here is a sentence." not in result.output

Merge branch '8.0.x'David Lordbefore adoption29df879

lines 560–576why, one step back
560
561562def test_help_invalid_default(runner):563 cli = click.Command(564 "cli",565 params=[566 click.Option(567 ["-a"],568 type=click.Path(exists=True),569 default="not found",570 show_default=True,571 ),572 ],573 )574 result = runner.invoke(cli, ["--help"])575 assert result.exit_code == 0576 assert "default: not found" in result.output