test_arguments.py
This file
Path
tests/test_arguments.py
305 lines · 8.2 KiB · Python
Last changed by
Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
History of this fileTake it
The walk stopped after 200 commits: lines older than a50befd point at it.
Merge branch '7.x'David Lordbefore adoption7239794
lines 1–2why, one step back1import sys2Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
line 3why, one step back3import pytestMerge branch '7.x'David Lordbefore adoption7239794
line 4why, one step back4Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 5–9why, one step back5import click678def test_nargs_star(runner):9 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 10–11why, one step back10 @click.argument("src", nargs=-1)11 @click.argument("dst")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 12why, one step back12 def copy(src, dst):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 13–14why, one step back13 click.echo(f"src={'|'.join(src)}")14 click.echo(f"dst={dst}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 15why, one step back15Merge branch '7.x'David Lordbefore adoption7239794
line 16why, one step back16 result = runner.invoke(copy, ["foo.txt", "bar.txt", "dir"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 17why, one step back17 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 18why, one step back18 assert result.output.splitlines() == ["src=foo.txt|bar.txt", "dst=dir"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 19–21why, one step back192021def test_nargs_default(runner):Merge branch '7.x'David Lordbefore adoption7239794
lines 22–23why, one step back22 with pytest.raises(TypeError, match="nargs=-1"):23Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 24why, one step back24 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 25why, one step back25 @click.argument("src", nargs=-1, default=42)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 26–31why, one step back26 def copy(src):27 pass282930def test_nargs_tup(runner):31 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 32–33why, one step back32 @click.argument("name", nargs=1)33 @click.argument("point", nargs=2, type=click.INT)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 34why, one step back34 def copy(name, point):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 35–37why, one step back35 click.echo(f"name={name}")36 x, y = point37 click.echo(f"point={x}/{y}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 38why, one step back38Merge branch '7.x'David Lordbefore adoption7239794
line 39why, one step back39 result = runner.invoke(copy, ["peter", "1", "2"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 40why, one step back40 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 41why, one step back41 assert result.output.splitlines() == ["name=peter", "point=1/2"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 42–52why, one step back424344def test_nargs_tup_composite(runner):45 variations = [46 dict(type=(str, int)),47 dict(type=click.Tuple([str, int])),48 dict(nargs=2, type=click.Tuple([str, int])),49 dict(nargs=2, type=(str, int)),50 ]5152 for opts in variations:Merge branch '7.x'David Lordbefore adoption7239794
line 53why, one step back53Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 54why, one step back54 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 55why, one step back55 @click.argument("item", **opts)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 56why, one step back56 def copy(item):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 57–58why, one step back57 name, id = item58 click.echo(f"name={name} id={id:d}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 59why, one step back59Merge branch '7.x'David Lordbefore adoption7239794
line 60why, one step back60 result = runner.invoke(copy, ["peter", "1"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 61why, one step back61 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 62why, one step back62 assert result.output.splitlines() == ["name=peter id=1"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 63–66why, one step back636465def test_nargs_err(runner):66 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 67why, one step back67 @click.argument("x")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 68–70why, one step back68 def copy(x):69 click.echo(x)70Merge branch '7.x'David Lordbefore adoption7239794
line 71why, one step back71 result = runner.invoke(copy, ["foo"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 72why, one step back72 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 73why, one step back73 assert result.output == "foo\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 74why, one step back74Merge branch '7.x'David Lordbefore adoption7239794
line 75why, one step back75 result = runner.invoke(copy, ["foo", "bar"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 76why, one step back76 assert result.exit_code == 2Merge branch '7.x'David Lordbefore adoption7239794
line 77why, one step back77 assert "Got unexpected extra argument (bar)" in result.outputMerge branch '7.x'David Lordbefore adoption8f36eab
lines 78–81why, one step back787980def test_bytes_args(runner, monkeypatch):81 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 82why, one step back82 @click.argument("arg")Merge branch '7.x'David Lordbefore adoption8f36eab
line 83why, one step back83 def from_bytes(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 84why, one step back84 assert isinstance(Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 85why, one step back85 arg, strMerge branch '7.x'David Lordbefore adoption7239794
line 86why, one step back86 ), "UTF-8 encoded argument should be implicitly converted to Unicode"Merge branch '7.x'David Lordbefore adoption8f36eab
lines 87–88why, one step back8788 # Simulate empty locale environment variablesMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 89–91why, one step back89 monkeypatch.setattr(sys.stdin, "encoding", "utf-8")90 monkeypatch.setattr(sys, "getfilesystemencoding", lambda: "utf-8")91 monkeypatch.setattr(sys, "getdefaultencoding", lambda: "utf-8")Merge branch '7.x'David Lordbefore adoption8f36eab
line 92why, one step back92Merge branch '7.x'David Lordbefore adoption7239794
lines 93–94why, one step back93 runner.invoke(94 from_bytes,Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 95why, one step back95 ["Something outside of ASCII range: 林".encode()],Merge branch '7.x'David Lordbefore adoption7239794
lines 96–97why, one step back96 catch_exceptions=False,97 )Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 98–101why, one step back9899100def test_file_args(runner):101 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 102–103why, one step back102 @click.argument("input", type=click.File("rb"))103 @click.argument("output", type=click.File("wb"))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 104–111why, one step back104 def inout(input, output):105 while True:106 chunk = input.read(1024)107 if not chunk:108 break109 output.write(chunk)110111 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 112–113why, one step back112 result = runner.invoke(inout, ["-", "hello.txt"], input="Hey!")113 assert result.output == ""Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 114why, one step back114 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
lines 115–116why, one step back115 with open("hello.txt", "rb") as f:116 assert f.read() == b"Hey!"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 117why, one step back117Merge branch '7.x'David Lordbefore adoption7239794
lines 118–119why, one step back118 result = runner.invoke(inout, ["hello.txt", "-"])119 assert result.output == "Hey!"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 120–124why, one step back120 assert result.exit_code == 0121122123def test_path_args(runner):124 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 125why, one step back125 @click.argument("input", type=click.Path(dir_okay=False, allow_dash=True))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 126–128why, one step back126 def foo(input):127 click.echo(input)128Merge branch '7.x'David Lordbefore adoption7239794
lines 129–130why, one step back129 result = runner.invoke(foo, ["-"])130 assert result.output == "-\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 131–135why, one step back131 assert result.exit_code == 0132133134def test_file_atomics(runner):135 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 136why, one step back136 @click.argument("output", type=click.File("wb", atomic=True))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 137why, one step back137 def inout(output):Merge branch '7.x'David Lordbefore adoption7239794
line 138why, one step back138 output.write(b"Foo bar baz\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 139why, one step back139 output.flush()Merge branch '7.x'David Lordbefore adoption7239794
line 140why, one step back140 with open(output.name, "rb") as f:Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 141why, one step back141 old_content = f.read()Merge branch '7.x'David Lordbefore adoption7239794
line 142why, one step back142 assert old_content == b"OLD\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 143–144why, one step back143144 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 145–148why, one step back145 with open("foo.txt", "wb") as f:146 f.write(b"OLD\n")147 result = runner.invoke(inout, ["foo.txt"], input="Hey!", catch_exceptions=False)148 assert result.output == ""Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 149why, one step back149 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
lines 150–151why, one step back150 with open("foo.txt", "rb") as f:151 assert f.read() == b"Foo bar baz\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 152–155why, one step back152153154def test_stdout_default(runner):155 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 156why, one step back156 @click.argument("output", type=click.File("w"), default="-")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 157why, one step back157 def inout(output):Merge branch '7.x'David Lordbefore adoption7239794
line 158why, one step back158 output.write("Foo bar baz\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 159–162why, one step back159 output.flush()160161 result = runner.invoke(inout, [])162 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 163why, one step back163 assert result.output == "Foo bar baz\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 164–167why, one step back164165166def test_nargs_envvar(runner):167 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 168why, one step back168 @click.option("--arg", nargs=2)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 169why, one step back169 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 170why, one step back170 click.echo("|".join(arg))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 171why, one step back171Merge branch '7.x'David Lordbefore adoption7239794
lines 172–174why, one step back172 result = runner.invoke(173 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}174 )Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 175why, one step back175 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 176why, one step back176 assert result.output == "foo|bar\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 177–178why, one step back177178 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 179why, one step back179 @click.option("--arg", envvar="X", nargs=2)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 180why, one step back180 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 181why, one step back181 click.echo("|".join(arg))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 182why, one step back182Merge branch '7.x'David Lordbefore adoption7239794
line 183why, one step back183 result = runner.invoke(cmd, [], env={"X": "foo bar"})Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 184why, one step back184 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 185why, one step back185 assert result.output == "foo|bar\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 186–189why, one step back186187188def test_empty_nargs(runner):189 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 190why, one step back190 @click.argument("arg", nargs=-1)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 191why, one step back191 def cmd(arg):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 192why, one step back192 click.echo(f"arg:{'|'.join(arg)}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 193–195why, one step back193194 result = runner.invoke(cmd, [])195 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 196why, one step back196 assert result.output == "arg:\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 197–198why, one step back197198 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 199why, one step back199 @click.argument("arg", nargs=-1, required=True)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 200why, one step back200 def cmd2(arg):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 201why, one step back201 click.echo(f"arg:{'|'.join(arg)}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 202–204why, one step back202203 result = runner.invoke(cmd2, [])204 assert result.exit_code == 2Merge branch '7.x'David Lordbefore adoption7239794
line 205why, one step back205 assert "Missing argument 'ARG...'" in result.outputMerge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 206–209why, one step back206207208def test_missing_arg(runner):209 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 210why, one step back210 @click.argument("arg")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 211why, one step back211 def cmd(arg):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 212why, one step back212 click.echo(f"arg:{arg}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 213–215why, one step back213214 result = runner.invoke(cmd, [])215 assert result.exit_code == 2Merge branch '7.x'David Lordbefore adoption7239794
line 216why, one step back216 assert "Missing argument 'ARG'." in result.outputMerge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 217–218why, one step back217218Merge branch '7.x'David Lordbefore adoption2269786
lines 219–227why, one step back219def test_missing_argument_string_cast():220 ctx = click.Context(click.Command(""))221222 with pytest.raises(click.MissingParameter) as excinfo:223 click.Argument(["a"], required=True).full_process_value(ctx, None)224225 assert str(excinfo.value) == "missing parameter: a"226227Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 228–229why, one step back228def test_implicit_non_required(runner):229 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 230why, one step back230 @click.argument("f", default="test")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 231–235why, one step back231 def cli(f):232 click.echo(f)233234 result = runner.invoke(cli, [])235 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 236why, one step back236 assert result.output == "test\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 237–240why, one step back237238239def test_eat_options(runner):240 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 241–242why, one step back241 @click.option("-f")242 @click.argument("files", nargs=-1)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 243–247why, one step back243 def cmd(f, files):244 for filename in files:245 click.echo(filename)246 click.echo(f)247Merge branch '7.x'David Lordbefore adoption7239794
lines 248–249why, one step back248 result = runner.invoke(cmd, ["--", "-foo", "bar"])249 assert result.output.splitlines() == ["-foo", "bar", ""]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 250why, one step back250Merge branch '7.x'David Lordbefore adoption7239794
lines 251–252why, one step back251 result = runner.invoke(cmd, ["-f", "-x", "--", "-foo", "bar"])252 assert result.output.splitlines() == ["-foo", "bar", "-x"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 253–256why, one step back253254255def test_nargs_star_ordering(runner):256 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 257–259why, one step back257 @click.argument("a", nargs=-1)258 @click.argument("b")259 @click.argument("c")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 260–263why, one step back260 def cmd(a, b, c):261 for arg in (a, b, c):262 click.echo(arg)263Merge branch '7.x'David Lordbefore adoption7239794
line 264why, one step back264 result = runner.invoke(cmd, ["a", "b", "c"])Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 265why, one step back265 assert result.output.splitlines() == ["('a',)", "b", "c"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 266–269why, one step back266267268def test_nargs_specified_plus_star_ordering(runner):269 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 270–272why, one step back270 @click.argument("a", nargs=-1)271 @click.argument("b")272 @click.argument("c", nargs=2)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 273–276why, one step back273 def cmd(a, b, c):274 for arg in (a, b, c):275 click.echo(arg)276Merge branch '7.x'David Lordbefore adoption7239794
line 277why, one step back277 result = runner.invoke(cmd, ["a", "b", "c", "d", "e", "f"])Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 278why, one step back278 assert result.output.splitlines() == ["('a', 'b', 'c')", "d", "('e', 'f')"]Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 279–282why, one step back279280281def test_defaults_for_nargs(runner):282 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 283why, one step back283 @click.argument("a", nargs=2, type=int, default=(1, 2))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
lines 284–288why, one step back284 def cmd(a):285 x, y = a286 click.echo(x + y)287288 result = runner.invoke(cmd, [])Merge branch '7.x'David Lordbefore adoption7239794
line 289why, one step back289 assert result.output.strip() == "3"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 290why, one step back290Merge branch '7.x'David Lordbefore adoption7239794
lines 291–292why, one step back291 result = runner.invoke(cmd, ["3", "4"])292 assert result.output.strip() == "7"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 293why, one step back293Merge branch '7.x'David Lordbefore adoption7239794
line 294why, one step back294 result = runner.invoke(cmd, ["3"])Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiona50befd
line 295why, one step back295 assert result.exception is not NoneMerge branch '7.x'David Lordbefore adoption7239794
line 296why, one step back296 assert "argument a takes 2 values" in result.outputMerge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
lines 297–300why, one step back297298299def test_multiple_param_decls_not_allowed(runner):300 with pytest.raises(TypeError):Merge branch '7.x'David Lordbefore adoption7239794
line 301why, one step back301Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
line 302why, one step back302 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 303why, one step back303 @click.argument("x", click.Choice(["a", "b"]))Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
lines 304–305why, one step back304 def copy(x):305 click.echo(x)