Skip to content
Myr

test_basic.py

This file

Path

tests/test_basic.py

566 lines · 15.3 KiB · Python

Last changed by

Merge pull request #1916 from pallets/default-resilientDavid Lordbefore adoptionfcf8205

History of this file

Take it

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 1–2why, one step back
1import os
2import uuid

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

line 4why, one step back
4

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 7–82why, one step back
7import click
8910def test_basic_functionality(runner):11 @click.command()12 def cli():13 """Hello World!"""14 click.echo("I EXECUTED")1516 result = runner.invoke(cli, ["--help"])17 assert not result.exception18 assert "Hello World!" in result.output19 assert "Show this message and exit." in result.output20 assert result.exit_code == 021 assert "I EXECUTED" not in result.output2223 result = runner.invoke(cli, [])24 assert not result.exception25 assert "I EXECUTED" in result.output26 assert result.exit_code == 0272829def test_repr():30 @click.command()31 def command():32 pass3334 @click.group()35 def group():36 pass3738 @group.command()39 def subcommand():40 pass4142 assert repr(command) == "<Command command>"43 assert repr(group) == "<Group group>"44 assert repr(subcommand) == "<Command subcommand>"454647def test_return_values():48 @click.command()49 def cli():50 return 425152 with cli.make_context("foo", []) as ctx:53 rv = cli.invoke(ctx)54 assert rv == 42555657def test_basic_group(runner):58 @click.group()59 def cli():60 """This is the root."""61 click.echo("ROOT EXECUTED")6263 @cli.command()64 def subcommand():65 """This is a subcommand."""66 click.echo("SUBCOMMAND EXECUTED")6768 result = runner.invoke(cli, ["--help"])69 assert not result.exception70 assert "COMMAND [ARGS]..." in result.output71 assert "This is the root" in result.output72 assert "This is a subcommand." in result.output73 assert result.exit_code == 074 assert "ROOT EXECUTED" not in result.output7576 result = runner.invoke(cli, ["subcommand"])77 assert not result.exception78 assert result.exit_code == 079 assert "ROOT EXECUTED" in result.output80 assert "SUBCOMMAND EXECUTED" in result.output8182

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

lines 83–106why, one step back
83def test_group_commands_dict(runner):
84 """A Group can be built with a dict of commands."""8586 @click.command()87 def sub():88 click.echo("sub", nl=False)8990 cli = click.Group(commands={"other": sub})91 result = runner.invoke(cli, ["other"])92 assert result.output == "sub"939495def test_group_from_list(runner):96 """A Group can be built with a list of commands."""9798 @click.command()99 def sub():100 click.echo("sub", nl=False)101102 cli = click.Group(commands=[sub])103 result = runner.invoke(cli, ["sub"])104 assert result.output == "sub"105106

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 107–122why, one step back
107def test_basic_option(runner):
108 @click.command()109 @click.option("--foo", default="no value")110 def cli(foo):111 click.echo(f"FOO:[{foo}]")112113 result = runner.invoke(cli, [])114 assert not result.exception115 assert "FOO:[no value]" in result.output116117 result = runner.invoke(cli, ["--foo=42"])118 assert not result.exception119 assert "FOO:[42]" in result.output120121 result = runner.invoke(cli, ["--foo"])122 assert result.exception

Merge pull request #1829 from MLH-Fellowship/add-i18nDavid Lordbefore adoptionc454226

line 123why, one step back
123 assert "Option '--foo' requires an argument." in result.output

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 124–149why, one step back
124
125 result = runner.invoke(cli, ["--foo="])126 assert not result.exception127 assert "FOO:[]" in result.output128129 result = runner.invoke(cli, ["--foo=\N{SNOWMAN}"])130 assert not result.exception131 assert "FOO:[\N{SNOWMAN}]" in result.output132133134def test_int_option(runner):135 @click.command()136 @click.option("--foo", default=42)137 def cli(foo):138 click.echo(f"FOO:[{foo * 2}]")139140 result = runner.invoke(cli, [])141 assert not result.exception142 assert "FOO:[84]" in result.output143144 result = runner.invoke(cli, ["--foo=23"])145 assert not result.exception146 assert "FOO:[46]" in result.output147148 result = runner.invoke(cli, ["--foo=bar"])149 assert result.exception

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

line 150why, one step back
150 assert "Invalid value for '--foo': 'bar' is not a valid integer." in result.output

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 151–171why, one step back
151
152153def test_uuid_option(runner):154 @click.command()155 @click.option(156 "--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID157 )158 def cli(u):159 assert type(u) is uuid.UUID160 click.echo(f"U:[{u}]")161162 result = runner.invoke(cli, [])163 assert not result.exception164 assert "U:[ba122011-349f-423b-873b-9d6a79c688ab]" in result.output165166 result = runner.invoke(cli, ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"])167 assert not result.exception168 assert "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]" in result.output169170 result = runner.invoke(cli, ["--u=bar"])171 assert result.exception

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

line 172why, one step back
172 assert "Invalid value for '--u': 'bar' is not a valid UUID." in result.output

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 173–191why, one step back
173
174175def test_float_option(runner):176 @click.command()177 @click.option("--foo", default=42, type=click.FLOAT)178 def cli(foo):179 assert type(foo) is float180 click.echo(f"FOO:[{foo}]")181182 result = runner.invoke(cli, [])183 assert not result.exception184 assert "FOO:[42.0]" in result.output185186 result = runner.invoke(cli, ["--foo=23.5"])187 assert not result.exception188 assert "FOO:[23.5]" in result.output189190 result = runner.invoke(cli, ["--foo=bar"])191 assert result.exception

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

line 192why, one step back
192 assert "Invalid value for '--foo': 'bar' is not a valid float." in result.output

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 193–227why, one step back
193
194195def test_boolean_option(runner):196 for default in True, False:197198 @click.command()199 @click.option("--with-foo/--without-foo", default=default)200 def cli(with_foo):201 click.echo(with_foo)202203 result = runner.invoke(cli, ["--with-foo"])204 assert not result.exception205 assert result.output == "True\n"206 result = runner.invoke(cli, ["--without-foo"])207 assert not result.exception208 assert result.output == "False\n"209 result = runner.invoke(cli, [])210 assert not result.exception211 assert result.output == f"{default}\n"212213 for default in True, False:214215 @click.command()216 @click.option("--flag", is_flag=True, default=default)217 def cli(flag):218 click.echo(flag)219220 result = runner.invoke(cli, ["--flag"])221 assert not result.exception222 assert result.output == f"{not default}\n"223 result = runner.invoke(cli, [])224 assert not result.exception225 assert result.output == f"{default}\n"226227

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

lines 228–239why, one step back
228@pytest.mark.parametrize(
229 ("value", "expect"),230 chain(231 ((x, "True") for x in ("1", "true", "t", "yes", "y", "on")),232 ((x, "False") for x in ("0", "false", "f", "no", "n", "off")),233 ),234)235def test_boolean_conversion(runner, value, expect):236 @click.command()237 @click.option("--flag", type=bool)238 def cli(flag):239 click.echo(flag, nl=False)

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

line 240why, one step back
240

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

line 243why, one step back
243

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 246–305why, one step back
246
247248def test_file_option(runner):249 @click.command()250 @click.option("--file", type=click.File("w"))251 def input(file):252 file.write("Hello World!\n")253254 @click.command()255 @click.option("--file", type=click.File("r"))256 def output(file):257 click.echo(file.read())258259 with runner.isolated_filesystem():260 result_in = runner.invoke(input, ["--file=example.txt"])261 result_out = runner.invoke(output, ["--file=example.txt"])262263 assert not result_in.exception264 assert result_in.output == ""265 assert not result_out.exception266 assert result_out.output == "Hello World!\n\n"267268269def test_file_lazy_mode(runner):270 do_io = False271272 @click.command()273 @click.option("--file", type=click.File("w"))274 def input(file):275 if do_io:276 file.write("Hello World!\n")277278 @click.command()279 @click.option("--file", type=click.File("r"))280 def output(file):281 pass282283 with runner.isolated_filesystem():284 os.mkdir("example.txt")285286 do_io = True287 result_in = runner.invoke(input, ["--file=example.txt"])288 assert result_in.exit_code == 1289290 do_io = False291 result_in = runner.invoke(input, ["--file=example.txt"])292 assert result_in.exit_code == 0293294 result_out = runner.invoke(output, ["--file=example.txt"])295 assert result_out.exception296297 @click.command()298 @click.option("--file", type=click.File("w", lazy=False))299 def input_non_lazy(file):300 file.write("Hello World!\n")301302 with runner.isolated_filesystem():303 os.mkdir("example.txt")304 result_in = runner.invoke(input_non_lazy, ["--file=example.txt"])305 assert result_in.exit_code == 2

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 307–367why, one step back
307
308309def test_path_option(runner):310 @click.command()311 @click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True))312 def write_to_dir(o):313 with open(os.path.join(o, "foo.txt"), "wb") as f:314 f.write(b"meh\n")315316 with runner.isolated_filesystem():317 os.mkdir("test")318319 result = runner.invoke(write_to_dir, ["-O", "test"])320 assert not result.exception321322 with open("test/foo.txt", "rb") as f:323 assert f.read() == b"meh\n"324325 result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"])326 assert "is a file" in result.output327328 @click.command()329 @click.option("-f", type=click.Path(exists=True))330 def showtype(f):331 click.echo(f"is_file={os.path.isfile(f)}")332 click.echo(f"is_dir={os.path.isdir(f)}")333334 with runner.isolated_filesystem():335 result = runner.invoke(showtype, ["-f", "xxx"])336 assert "does not exist" in result.output337338 result = runner.invoke(showtype, ["-f", "."])339 assert "is_file=False" in result.output340 assert "is_dir=True" in result.output341342 @click.command()343 @click.option("-f", type=click.Path())344 def exists(f):345 click.echo(f"exists={os.path.exists(f)}")346347 with runner.isolated_filesystem():348 result = runner.invoke(exists, ["-f", "xxx"])349 assert "exists=False" in result.output350351 result = runner.invoke(exists, ["-f", "."])352 assert "exists=True" in result.output353354355def test_choice_option(runner):356 @click.command()357 @click.option("--method", type=click.Choice(["foo", "bar", "baz"]))358 def cli(method):359 click.echo(method)360361 result = runner.invoke(cli, ["--method=foo"])362 assert not result.exception363 assert result.output == "foo\n"364365 result = runner.invoke(cli, ["--method=meh"])366 assert result.exit_code == 2367 assert (

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 370–375why, one step back
370 )
371372 result = runner.invoke(cli, ["--help"])373 assert "--method [foo|bar|baz]" in result.output374375

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

lines 376–388why, one step back
376def test_choice_argument(runner):
377 @click.command()378 @click.argument("method", type=click.Choice(["foo", "bar", "baz"]))379 def cli(method):380 click.echo(method)381382 result = runner.invoke(cli, ["foo"])383 assert not result.exception384 assert result.output == "foo\n"385386 result = runner.invoke(cli, ["meh"])387 assert result.exit_code == 2388 assert (

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

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

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

lines 391–396why, one step back
391 )
392393 result = runner.invoke(cli, ["--help"])394 assert "{foo|bar|baz}" in result.output395396

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 397–413why, one step back
397def test_datetime_option_default(runner):
398 @click.command()399 @click.option("--start_date", type=click.DateTime())400 def cli(start_date):401 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))402403 result = runner.invoke(cli, ["--start_date=2015-09-29"])404 assert not result.exception405 assert result.output == "2015-09-29T00:00:00\n"406407 result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"])408 assert not result.exception409 assert result.output == "2015-09-29T09:11:22\n"410411 result = runner.invoke(cli, ["--start_date=2015-09"])412 assert result.exit_code == 2413 assert (

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

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

Merge pull request #1575 from pallets/dependabot/pip/tox-3.15.2dependabot-preview[bot]before adoption7b9a72f

lines 416–530why, one step back
416 ) in result.output
417418 result = runner.invoke(cli, ["--help"])419 assert (420 "--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output421 )422423424def test_datetime_option_custom(runner):425 @click.command()426 @click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"]))427 def cli(start_date):428 click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))429430 result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"])431 assert not result.exception432 assert result.output == "2010-06-05T00:00:00\n"433434435def test_required_option(runner):436 @click.command()437 @click.option("--foo", required=True)438 def cli(foo):439 click.echo(foo)440441 result = runner.invoke(cli, [])442 assert result.exit_code == 2443 assert "Missing option '--foo'" in result.output444445446def test_evaluation_order(runner):447 called = []448449 def memo(ctx, param, value):450 called.append(value)451 return value452453 @click.command()454 @click.option("--missing", default="missing", is_eager=False, callback=memo)455 @click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo)456 @click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo)457 @click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo)458 @click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo)459 @click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo)460 @click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo)461 def cli(**x):462 pass463464 result = runner.invoke(465 cli,466 [467 "--eager-flag2",468 "--eager-flag1",469 "--normal-flag2",470 "--eager-flag3",471 "--normal-flag3",472 "--normal-flag3",473 "--normal-flag1",474 "--normal-flag1",475 ],476 )477 assert not result.exception478 assert called == [479 "eager2",480 "eager1",481 "eager3",482 "normal2",483 "normal3",484 "normal1",485 "missing",486 ]487488489def test_hidden_option(runner):490 @click.command()491 @click.option("--nope", hidden=True)492 def cli(nope):493 click.echo(nope)494495 result = runner.invoke(cli, ["--help"])496 assert result.exit_code == 0497 assert "--nope" not in result.output498499500def test_hidden_command(runner):501 @click.group()502 def cli():503 pass504505 @cli.command(hidden=True)506 def nope():507 pass508509 result = runner.invoke(cli, ["--help"])510 assert result.exit_code == 0511 assert "nope" not in result.output512513514def test_hidden_group(runner):515 @click.group()516 def cli():517 pass518519 @cli.group(hidden=True)520 def subgroup():521 pass522523 @subgroup.command()524 def nope():525 pass526527 result = runner.invoke(cli, ["--help"])528 assert result.exit_code == 0529 assert "subgroup" not in result.output530 assert "nope" not in result.output

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

lines 531–549why, one step back
531
532533def test_summary_line(runner):534 @click.group()535 def cli():536 pass537538 @cli.command()539 def cmd():540 """541 Summary line without period542543 Here is a sentence. And here too.544 """545 pass546547 result = runner.invoke(cli, ["--help"])548 assert "Summary line without period" in result.output549 assert "Here is a sentence." not in result.output

Merge pull request #1916 from pallets/default-resilientDavid Lordbefore adoptionfcf8205

lines 550–566why, one step back
550
551552def test_help_invalid_default(runner):553 cli = click.Command(554 "cli",555 params=[556 click.Option(557 ["-a"],558 type=click.Path(exists=True),559 default="not found",560 show_default=True,561 ),562 ],563 )564 result = runner.invoke(cli, ["--help"])565 assert result.exit_code == 0566 assert "default: not found" in result.output