test_options.py
This file
Path
tests/test_options.py
691 lines · 20.6 KiB · Python
Last changed by
Merge pull request #1618 from MLH-Fellowship/prompt-requiredDavid Lordbefore adoption8efb348
History of this fileTake it
The walk stopped after 200 commits: lines older than becfca5 point at it.
Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 1why, one step back1import osMerge branch '7.x'David Lordbefore adoption7239794
lines 2–3why, one step back2import re3Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 4–5why, one step back4import pytest5Merge branch '7.x'David Lordbefore adoption7239794
line 6why, one step back6import clickAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 7–10why, one step back789def test_prefixes(runner):10 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 11–12why, one step back11 @click.option("++foo", is_flag=True, help="das foo")12 @click.option("--bar", is_flag=True, help="das bar")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 13why, one step back13 def cli(foo, bar):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 14why, one step back14 click.echo(f"foo={foo} bar={bar}")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 15why, one step back15Merge branch '7.x'David Lordbefore adoption7239794
line 16why, one step back16 result = runner.invoke(cli, ["++foo", "--bar"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 17why, one step back17 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 18why, one step back18 assert result.output == "foo=True bar=True\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 19why, one step back19Merge branch '7.x'David Lordbefore adoption7239794
lines 20–22why, one step back20 result = runner.invoke(cli, ["--help"])21 assert re.search(r"\+\+foo\s+das foo", result.output) is not None22 assert re.search(r"--bar\s+das bar", result.output) is not NoneAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 23–25why, one step back232425def test_invalid_option(runner):Merge pull request #1355 from jtrakk/patch-1David Lordbefore adoption0ad4e79
lines 26–27why, one step back26 with pytest.raises(TypeError, match="name was passed") as exc_info:27 click.Option(["foo"])Merge branch '7.x'David Lordbefore adoption7239794
line 28why, one step back28Merge pull request #1355 from jtrakk/patch-1David Lordbefore adoption0ad4e79
lines 29–32why, one step back29 message = str(exc_info.value)30 assert "name was passed (foo)" in message31 assert "declare an argument" in message32 assert "'--foo'" in messageAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 33–35why, one step back333435def test_invalid_nargs(runner):Merge branch '7.x'David Lordbefore adoption7239794
lines 36–37why, one step back36 with pytest.raises(TypeError, match="nargs < 0"):37Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 38why, one step back38 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 39why, one step back39 @click.option("--foo", nargs=-1)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 40–45why, one step back40 def cli(foo):41 pass424344def test_nargs_tup_composite_mult(runner):45 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 46why, one step back46 @click.option("--item", type=(str, int), multiple=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 47why, one step back47 def copy(item):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 48–49why, one step back48 for name, id in item:49 click.echo(f"name={name} id={id:d}")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 50why, one step back50Merge branch '7.x'David Lordbefore adoption7239794
line 51why, one step back51 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 52why, one step back52 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 53why, one step back53 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 54–57why, one step back545556def test_counting(runner):57 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 58why, one step back58 @click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 59why, one step back59 def cli(v):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 60why, one step back60 click.echo(f"verbosity={v:d}")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 61why, one step back61Merge branch '7.x'David Lordbefore adoption7239794
line 62why, one step back62 result = runner.invoke(cli, ["-vvv"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 63why, one step back63 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 64why, one step back64 assert result.output == "verbosity=3\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 65why, one step back65Merge branch '7.x'David Lordbefore adoption7239794
line 66why, one step back66 result = runner.invoke(cli, ["-vvvv"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 67why, one step back67 assert result.exceptionMerge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab
line 68why, one step back68 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 69–71why, one step back6970 result = runner.invoke(cli, [])71 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 72why, one step back72 assert result.output == "verbosity=0\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 73why, one step back73Merge branch '7.x'David Lordbefore adoption7239794
lines 74–75why, one step back74 result = runner.invoke(cli, ["--help"])75 assert re.search(r"-v\s+Verbosity", result.output) is not NoneAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 76–77why, one step back7677Merge branch '7.x'David Lordbefore adoption7239794
line 78why, one step back78@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 79–85why, one step back79def test_unknown_options(runner, unknown_flag):80 @click.command()81 def cli():82 pass8384 result = runner.invoke(cli, [unknown_flag])85 assert result.exceptionMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 86why, one step back86 assert f"no such option: {unknown_flag}" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 87–88why, one step back8788Merge pull request #1455 from Na-Ne/masterDavid Lordbefore adoptiona141a2a
lines 89–104why, one step back89@pytest.mark.parametrize(90 ("value", "expect"),91 [92 ("--cat", "Did you mean --count?"),93 ("--bounds", "(Possible options: --bound, --count)"),94 ("--bount", "(Possible options: --bound, --count)"),95 ],96)97def test_suggest_possible_options(runner, value, expect):98 cli = click.Command(99 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]100 )101 result = runner.invoke(cli, [value])102 assert expect in result.output103104Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 105–106why, one step back105def test_multiple_required(runner):106 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 107why, one step back107 @click.option("-m", "--message", multiple=True, required=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 108why, one step back108 def cli(message):Merge branch '7.x'David Lordbefore adoption7239794
line 109why, one step back109 click.echo("\n".join(message))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 110why, one step back110Merge branch '7.x'David Lordbefore adoption7239794
line 111why, one step back111 result = runner.invoke(cli, ["-m", "foo", "-mbar"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 112why, one step back112 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 113why, one step back113 assert result.output == "foo\nbar\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 114–116why, one step back114115 result = runner.invoke(cli, [])116 assert result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 117why, one step back117 assert "Error: Missing option '-m' / '--message'." in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 118–119why, one step back118119Merge branch '7.x'David Lordbefore adoption2269786
lines 120–121why, one step back120def test_empty_envvar(runner):121 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 122why, one step back122 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")Merge branch '7.x'David Lordbefore adoption2269786
line 123why, one step back123 def cli(mypath):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 124why, one step back124 click.echo(f"mypath: {mypath}")Merge branch '7.x'David Lordbefore adoption2269786
line 125why, one step back125Merge branch '7.x'David Lordbefore adoption7239794
line 126why, one step back126 result = runner.invoke(cli, [], env={"MYPATH": ""})Merge branch '7.x'David Lordbefore adoption2269786
line 127why, one step back127 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 128why, one step back128 assert result.output == "mypath: None\n"Merge branch '7.x'David Lordbefore adoption2269786
lines 129–130why, one step back129130Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 131–132why, one step back131def test_multiple_envvar(runner):132 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 133why, one step back133 @click.option("--arg", multiple=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 134why, one step back134 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 135why, one step back135 click.echo("|".join(arg))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 136why, one step back136Merge branch '7.x'David Lordbefore adoption7239794
lines 137–139why, one step back137 result = runner.invoke(138 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}139 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 140why, one step back140 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 141why, one step back141 assert result.output == "foo|bar|baz\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 142–143why, one step back142143 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 144why, one step back144 @click.option("--arg", multiple=True, envvar="X")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 145why, one step back145 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 146why, one step back146 click.echo("|".join(arg))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 147why, one step back147Merge branch '7.x'David Lordbefore adoption7239794
line 148why, one step back148 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 149why, one step back149 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 150why, one step back150 assert result.output == "foo|bar|baz\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 151–152why, one step back151152 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 153why, one step back153 @click.option("--arg", multiple=True, type=click.Path())Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 154why, one step back154 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 155why, one step back155 click.echo("|".join(arg))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 156why, one step back156Merge branch '7.x'David Lordbefore adoption7239794
lines 157–160why, one step back157 result = runner.invoke(158 cmd,159 [],160 auto_envvar_prefix="TEST",Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 161why, one step back161 env={"TEST_ARG": f"foo{os.path.pathsep}bar"},Merge branch '7.x'David Lordbefore adoption7239794
line 162why, one step back162 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 163why, one step back163 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 164why, one step back164 assert result.output == "foo|bar\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 165–166why, one step back165166Merge pull request #1606 from andy-maier/andy/fix-trailing-blanksDavid Lordbefore adoption25ee366
lines 167–177why, one step back167def test_trailing_blanks_boolean_envvar(runner):168 @click.command()169 @click.option("--shout/--no-shout", envvar="SHOUT")170 def cli(shout):171 click.echo(f"shout: {shout!r}")172173 result = runner.invoke(cli, [], env={"SHOUT": " true "})174 assert result.exit_code == 0175 assert result.output == "shout: True\n"176177Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 178–179why, one step back178def test_multiple_default_help(runner):179 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 180–181why, one step back180 @click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)181 @click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 182–184why, one step back182 def cmd(arg, arg2):183 pass184Merge branch '7.x'David Lordbefore adoption7239794
line 185why, one step back185 result = runner.invoke(cmd, ["--help"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 186why, one step back186 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
lines 187–188why, one step back187 assert "foo, bar" in result.output188 assert "1, 2" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 189–190why, one step back189190Merge pull request #1549 from ewjoachim/default-map-show-default-1548David Lordbefore adoption7332f00
lines 191–202why, one step back191def test_show_default_default_map(runner):192 @click.command()193 @click.option("--arg", default="a", show_default=True)194 def cmd(arg):195 click.echo(arg)196197 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})198199 assert not result.exception200 assert "[default: b]" in result.output201202Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7
lines 203–209why, one step back203def test_multiple_default_type():204 opt = click.Option(["-a"], multiple=True, default=(1, 2))205 assert opt.nargs == 1206 assert opt.multiple207 assert opt.type is click.INT208 ctx = click.Context(click.Command("test"))209 assert opt.get_default(ctx) == (1, 2)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 210–211why, one step back210211Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7
lines 212–219why, one step back212def test_multiple_default_composite_type():213 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])214 assert opt.nargs == 2215 assert opt.multiple216 assert isinstance(opt.type, click.Tuple)217 assert opt.type.types == [click.INT, click.STRING]218 ctx = click.Context(click.Command("test"))219 assert opt.get_default(ctx) == ((1, "a"),)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 220why, one step back220Merge branch '7.x'David Lordbefore adoption7239794
line 221why, one step back221Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7
lines 222–235why, one step back222def test_parse_multiple_default_composite_type(runner):223 @click.command()224 @click.option("-a", multiple=True, default=("a", "b"))225 @click.option("-b", multiple=True, default=[(1, "a")])226 def cmd(a, b):227 click.echo(a)228 click.echo(b)229230 # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())231 # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"232 result = runner.invoke(cmd)233 assert result.output == "('a', 'b')\n((1, 'a'),)\n"234235Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 236–237why, one step back236def test_dynamic_default_help_unset(runner):237 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 238–243why, one step back238 @click.option(239 "--username",240 prompt=True,241 default=lambda: os.environ.get("USER", ""),242 show_default=True,243 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 244–246why, one step back244 def cmd(username):245 print("Hello,", username)246Merge branch '7.x'David Lordbefore adoption7239794
line 247why, one step back247 result = runner.invoke(cmd, ["--help"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 248why, one step back248 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
lines 249–251why, one step back249 assert "--username" in result.output250 assert "lambda" not in result.output251 assert "(dynamic)" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 252why, one step back252Merge branch '7.x'David Lordbefore adoption7239794
line 253why, one step back253Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 254–255why, one step back254def test_dynamic_default_help_text(runner):255 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
lines 256–261why, one step back256 @click.option(257 "--username",258 prompt=True,259 default=lambda: os.environ.get("USER", ""),260 show_default="current user",261 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 262–264why, one step back262 def cmd(username):263 print("Hello,", username)264Merge branch '7.x'David Lordbefore adoption7239794
line 265why, one step back265 result = runner.invoke(cmd, ["--help"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 266why, one step back266 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
lines 267–269why, one step back267 assert "--username" in result.output268 assert "lambda" not in result.output269 assert "(current user)" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 270why, one step back270Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d
line 271why, one step back271Merge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab
lines 272–281why, one step back272@pytest.mark.parametrize(273 ("type", "expect"),274 [275 (click.IntRange(1, 32), "1<=x<=32"),276 (click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),277 (click.IntRange(1), "x>=1"),278 (click.IntRange(max=32), "x<=32"),279 ],280)281def test_intrange_default_help_text(runner, type, expect):Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7
line 282why, one step back282 option = click.Option(["--count"], type=type, show_default=True, default=2)Merge pull request #1303 from robintully/issue_1110_open_intervalDavid Lordbefore adoptionca75bab
lines 283–285why, one step back283 context = click.Context(click.Command("test"))284 result = option.get_help_record(context)[1]285 assert expect in resultMerge pull request #1586 from IamCathal/coc-intrangepreviewDavid Lordbefore adoptione8de91d
lines 286–287why, one step back286287Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d
lines 288–289why, one step back288def test_toupper_envvar_prefix(runner):289 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 @click.option("--arg")Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d
lines 291–293why, one step back291 def cmd(arg):292 click.echo(arg)293Merge branch '7.x'David Lordbefore adoption7239794
line 294why, one step back294 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d
line 295why, one step back295 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 296why, one step back296 assert result.output == "foo\n"Merge pull request #1105 from sirosen/bugfix/autoenv-uppercaseDavid Lordbefore adoption3e17a3d
lines 297–298why, one step back297298Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 299–300why, one step back299def test_nargs_envvar(runner):300 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 301why, one step back301 @click.option("--arg", nargs=2)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 302why, one step back302 def cmd(arg):Merge branch '7.x'David Lordbefore adoption7239794
line 303why, one step back303 click.echo("|".join(arg))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 304why, one step back304Merge branch '7.x'David Lordbefore adoption7239794
lines 305–307why, one step back305 result = runner.invoke(306 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}307 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 308why, one step back308 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 309why, one step back309 assert result.output == "foo|bar\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 310–311why, one step back310311 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 312why, one step back312 @click.option("--arg", nargs=2, multiple=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 313–314why, one step back313 def cmd(arg):314 for item in arg:Merge branch '7.x'David Lordbefore adoption7239794
line 315why, one step back315 click.echo("|".join(item))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 316why, one step back316Merge branch '7.x'David Lordbefore adoption7239794
lines 317–319why, one step back317 result = runner.invoke(318 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}319 )Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 320why, one step back320 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 321why, one step back321 assert result.output == "x|1\ny|2\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 322–325why, one step back322323324def test_show_envvar(runner):325 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 326why, one step back326 @click.option("--arg1", envvar="ARG1", show_envvar=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 327–329why, one step back327 def cmd(arg):328 pass329Merge branch '7.x'David Lordbefore adoption7239794
line 330why, one step back330 result = runner.invoke(cmd, ["--help"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 331why, one step back331 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 332why, one step back332 assert "ARG1" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 333–336why, one step back333334335def test_show_envvar_auto_prefix(runner):336 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 337why, one step back337 @click.option("--arg1", show_envvar=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 338–340why, one step back338 def cmd(arg):339 pass340Merge branch '7.x'David Lordbefore adoption7239794
line 341why, one step back341 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 342why, one step back342 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 343why, one step back343 assert "TEST_ARG1" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 344–345why, one step back344345Merge branch '7.x'David Lordbefore adoption8b971f7
lines 346–351why, one step back346def test_show_envvar_auto_prefix_dash_in_command(runner):347 @click.group()348 def cli():349 pass350351 @cli.command()Merge branch '7.x'David Lordbefore adoption7239794
line 352why, one step back352 @click.option("--baz", show_envvar=True)Merge branch '7.x'David Lordbefore adoption8b971f7
lines 353–355why, one step back353 def foo_bar(baz):354 pass355Merge branch '7.x'David Lordbefore adoption7239794
line 356why, one step back356 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")Merge branch '7.x'David Lordbefore adoption8b971f7
line 357why, one step back357 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 358why, one step back358 assert "TEST_FOO_BAR_BAZ" in result.outputMerge branch '7.x'David Lordbefore adoption8b971f7
lines 359–360why, one step back359360Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 361–363why, one step back361def test_custom_validation(runner):362 def validate_pos_int(ctx, param, value):363 if value < 0:Merge branch '7.x'David Lordbefore adoption7239794
line 364why, one step back364 raise click.BadParameter("Value needs to be positive")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 365–367why, one step back365 return value366367 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 368why, one step back368 @click.option("--foo", callback=validate_pos_int, default=1)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 369–371why, one step back369 def cmd(foo):370 click.echo(foo)371Merge branch '7.x'David Lordbefore adoption7239794
lines 372–373why, one step back372 result = runner.invoke(cmd, ["--foo", "-1"])373 assert "Invalid value for '--foo': Value needs to be positive" in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 374why, one step back374Merge branch '7.x'David Lordbefore adoption7239794
lines 375–376why, one step back375 result = runner.invoke(cmd, ["--foo", "42"])376 assert result.output == "42\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 377–380why, one step back377378379def test_winstyle_options(runner):380 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 381why, one step back381 @click.option("/debug;/no-debug", help="Enables or disables debug mode.")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 382–384why, one step back382 def cmd(debug):383 click.echo(debug)384Merge branch '7.x'David Lordbefore adoption7239794
lines 385–393why, one step back385 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])386 assert result.output == "True\n"387 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])388 assert result.output == "False\n"389 result = runner.invoke(cmd, [], help_option_names=["/?"])390 assert result.output == "False\n"391 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])392 assert "/debug; /no-debug Enables or disables debug mode." in result.output393 assert "/? Show this message and exit." in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 394–397why, one step back394395396def test_legacy_options(runner):397 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 398why, one step back398 @click.option("-whatever")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 399–401why, one step back399 def cmd(whatever):400 click.echo(whatever)401Merge branch '7.x'David Lordbefore adoption7239794
lines 402–405why, one step back402 result = runner.invoke(cmd, ["-whatever", "42"])403 assert result.output == "42\n"404 result = runner.invoke(cmd, ["-whatever=23"])405 assert result.output == "23\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 406–407why, one step back406407Merge branch '7.x'David Lordbefore adoption2269786
lines 408–411why, one step back408def test_missing_option_string_cast():409 ctx = click.Context(click.Command(""))410411 with pytest.raises(click.MissingParameter) as excinfo:Merge branch '7.x'David Lordbefore adoption7239794
line 412why, one step back412 click.Option(["-a"], required=True).full_process_value(ctx, None)Merge branch '7.x'David Lordbefore adoption2269786
lines 413–416why, one step back413414 assert str(excinfo.value) == "missing parameter: a"415416Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 417–418why, one step back417def test_missing_choice(runner):418 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 419why, one step back419 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 420–424why, one step back420 def cmd(foo):421 click.echo(foo)422423 result = runner.invoke(cmd)424 assert result.exit_code == 2Merge branch '7.x'David Lordbefore adoption7239794
lines 425–429why, one step back425 error, separator, choices = result.output.partition("Choose from")426 assert "Error: Missing option '--foo'. " in error427 assert "Choose from" in separator428 assert "foo" in choices429 assert "bar" in choicesAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 430–433why, one step back430431432def test_case_insensitive_choice(runner):433 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 434why, one step back434 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 435–437why, one step back435 def cmd(foo):436 click.echo(foo)437Merge branch '7.x'David Lordbefore adoption7239794
line 438why, one step back438 result = runner.invoke(cmd, ["--foo", "apple"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 439why, one step back439 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 440why, one step back440 assert result.output == "Apple\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 441why, one step back441Merge branch '7.x'David Lordbefore adoption7239794
line 442why, one step back442 result = runner.invoke(cmd, ["--foo", "oRANGe"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 443why, one step back443 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 444why, one step back444 assert result.output == "Orange\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 445why, one step back445Merge branch '7.x'David Lordbefore adoption7239794
line 446why, one step back446 result = runner.invoke(cmd, ["--foo", "Apple"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 447why, one step back447 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 448why, one step back448 assert result.output == "Apple\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 449–450why, one step back449450 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 451why, one step back451 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 452–454why, one step back452 def cmd2(foo):453 click.echo(foo)454Merge branch '7.x'David Lordbefore adoption7239794
line 455why, one step back455 result = runner.invoke(cmd2, ["--foo", "apple"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 456–457why, one step back456 assert result.exit_code == 2457Merge branch '7.x'David Lordbefore adoption7239794
line 458why, one step back458 result = runner.invoke(cmd2, ["--foo", "oRANGe"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 459–460why, one step back459 assert result.exit_code == 2460Merge branch '7.x'David Lordbefore adoption7239794
line 461why, one step back461 result = runner.invoke(cmd2, ["--foo", "Apple"])Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 462why, one step back462 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption11abf2b
lines 463–466why, one step back463464465def test_case_insensitive_choice_returned_exactly(runner):466 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 467why, one step back467 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))Merge branch '7.x'David Lordbefore adoption11abf2b
lines 468–470why, one step back468 def cmd(foo):469 click.echo(foo)470Merge branch '7.x'David Lordbefore adoption7239794
line 471why, one step back471 result = runner.invoke(cmd, ["--foo", "apple"])Merge branch '7.x'David Lordbefore adoption11abf2b
line 472why, one step back472 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 473why, one step back473 assert result.output == "Apple\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 474–475why, one step back474475Merge branch '7.x'David Lordbefore adoption8b971f7
line 476why, one step back476def test_option_help_preserve_paragraphs(runner):Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 477why, one step back477 @click.command()Merge branch '7.x'David Lordbefore adoption8b971f7
lines 478–482why, one step back478 @click.option(479 "-C",480 "--config",481 type=click.Path(),482 help="""Configuration file to use.Merge branch '7.x'David Lordbefore adoption7239794
line 483why, one step back483Merge branch '7.x'David Lordbefore adoption8b971f7
lines 484–489why, one step back484 If not given, the environment variable CONFIG_FILE is consulted485 and used if set. If neither are given, a default configuration486 file is loaded.""",487 )488 def cmd(config):489 passAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 490why, one step back490Merge branch '7.x'David Lordbefore adoption7239794
line 491why, one step back491 result = runner.invoke(cmd, ["--help"],)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 492why, one step back492 assert result.exit_code == 0Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 493why, one step back493 i = " " * 21Merge branch '7.x'David Lordbefore adoption8b971f7
lines 494–495why, one step back494 assert (495 " -C, --config PATH Configuration file to use.\n"Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 496–499why, one step back496 f"{i}\n"497 f"{i}If not given, the environment variable CONFIG_FILE is\n"498 f"{i}consulted and used if set. If neither are given, a default\n"499 f"{i}configuration file is loaded."Merge branch '7.x'David Lordbefore adoption8b971f7
line 500why, one step back500 ) in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 501why, one step back501Merge branch '7.x'David Lordbefore adoption7239794
line 502why, one step back502Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 503–505why, one step back503def test_argument_custom_class(runner):504 class CustomArgument(click.Argument):505 def get_default(self, ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 506–507why, one step back506 """a dumb override of a default value for testing"""507 return "I am a default"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 508–509why, one step back508509 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 510why, one step back510 @click.argument("testarg", cls=CustomArgument, default="you wont see me")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 511–514why, one step back511 def cmd(testarg):512 click.echo(testarg)513514 result = runner.invoke(cmd)Merge branch '7.x'David Lordbefore adoption7239794
lines 515–516why, one step back515 assert "I am a default" in result.output516 assert "you wont see me" not in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 517–521why, one step back517518519def test_option_custom_class(runner):520 class CustomOption(click.Option):521 def get_help_record(self, ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 522–523why, one step back522 """a dumb override of a help text for testing"""523 return ("--help", "I am a help text")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 524–525why, one step back524525 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 526why, one step back526 @click.option("--testoption", cls=CustomOption, help="you wont see me")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 527–529why, one step back527 def cmd(testoption):528 click.echo(testoption)529Merge branch '7.x'David Lordbefore adoption7239794
lines 530–532why, one step back530 result = runner.invoke(cmd, ["--help"])531 assert "I am a help text" in result.output532 assert "you wont see me" not in result.outputAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 533–539why, one step back533534535def test_option_custom_class_reusable(runner):536 """Ensure we can reuse a custom class option. See Issue #926"""537538 class CustomOption(click.Option):539 def get_help_record(self, ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 540–541why, one step back540 """a dumb override of a help text for testing"""541 return ("--help", "I am a help text")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 542–543why, one step back542543 # Assign to a variable to re-use the decorator.Merge branch '7.x'David Lordbefore adoption7239794
line 544why, one step back544 testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 545–558why, one step back545546 @click.command()547 @testoption548 def cmd1(testoption):549 click.echo(testoption)550551 @click.command()552 @testoption553 def cmd2(testoption):554 click.echo(testoption)555556 # Both of the commands should have the --help option now.557 for cmd in (cmd1, cmd2):558Merge branch '7.x'David Lordbefore adoption7239794
lines 559–561why, one step back559 result = runner.invoke(cmd, ["--help"])560 assert "I am a help text" in result.output561 assert "you wont see me" not in result.outputMerge branch '7.x'David Lordbefore adoption2269786
lines 562–565why, one step back562563564def test_bool_flag_with_type(runner):565 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 566why, one step back566 @click.option("--shout/--no-shout", default=False, type=bool)Merge branch '7.x'David Lordbefore adoption2269786
lines 567–571why, one step back567 def cmd(shout):568 pass569570 result = runner.invoke(cmd)571 assert not result.exceptionAdding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 572–575why, one step back572573574def test_aliases_for_flags(runner):575 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 576why, one step back576 @click.option("--warnings/--no-warnings", " /-W", default=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 577–579why, one step back577 def cli(warnings):578 click.echo(warnings)579Merge branch '7.x'David Lordbefore adoption7239794
lines 580–585why, one step back580 result = runner.invoke(cli, ["--warnings"])581 assert result.output == "True\n"582 result = runner.invoke(cli, ["--no-warnings"])583 assert result.output == "False\n"584 result = runner.invoke(cli, ["-W"])585 assert result.output == "False\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 586–587why, one step back586587 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 588why, one step back588 @click.option("--warnings/--no-warnings", "-w", default=True)Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 589–591why, one step back589 def cli_alt(warnings):590 click.echo(warnings)591Merge branch '7.x'David Lordbefore adoption7239794
lines 592–597why, one step back592 result = runner.invoke(cli_alt, ["--warnings"])593 assert result.output == "True\n"594 result = runner.invoke(cli_alt, ["--no-warnings"])595 assert result.output == "False\n"596 result = runner.invoke(cli_alt, ["-w"])597 assert result.output == "True\n"Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 598–599why, one step back598599Merge branch '7.x'David Lordbefore adoption7239794
lines 600–615why, one step back600@pytest.mark.parametrize(601 "option_args,expected",602 [603 (["--aggressive", "--all", "-a"], "aggressive"),604 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),605 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),606 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),607 (["-a", "-b", "-c"], "a"),608 (["-c", "-b", "-a"], "c"),609 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),610 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),611 (["--from", "-f", "_from"], "_from"),612 (["--return", "-r", "_ret"], "_ret"),613 ],614)615def test_option_names(runner, option_args, expected):Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
lines 616–623why, one step back616 @click.command()617 @click.option(*option_args, is_flag=True)618 def cmd(**kwargs):619 click.echo(str(kwargs[expected]))620621 assert cmd.params[0].name == expected622623 for form in option_args:Merge branch '7.x'David Lordbefore adoption7239794
line 624why, one step back624 if form.startswith("-"):Adding a few changes to look at formattingjacrottsbefore adoptionbecfca5
line 625why, one step back625 result = runner.invoke(cmd, [form])Merge branch '7.x'David Lordbefore adoption7239794
line 626why, one step back626 assert result.output == "True\n"Merge pull request #1594 from MLH-Fellowship/duplicate-boolean-flagsDavid Lordbefore adoptiondfb842d
lines 627–631why, one step back627628629def test_flag_duplicate_names(runner):630 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):631 click.Option(["--foo/--foo"], default=False)Merge pull request #1617 from MLH-Fellowship/issue1538David Lordbefore adoption66172ef
lines 632–660why, one step back632633634@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])635def test_show_default_boolean_flag_name(runner, default, expect):636 """When a boolean flag has distinct True/False opts, it should show637 the default opt name instead of the default value. It should only638 show one name even if multiple are declared.639 """640 opt = click.Option(641 ("--cache/--no-cache", "--c/--nc"),642 default=default,643 show_default=True,644 help="Enable/Disable the cache.",645 )646 ctx = click.Context(click.Command("test"))647 message = opt.get_help_record(ctx)[1]648 assert f"[default: {expect}]" in message649650651def test_show_default_boolean_flag_value(runner):652 """When a boolean flag only has one opt, it will show the default653 value, not the opt name.654 """655 opt = click.Option(656 ("--cache",), is_flag=True, show_default=True, help="Enable the cache.",657 )658 ctx = click.Context(click.Command("test"))659 message = opt.get_help_record(ctx)[1]660 assert "[default: False]" in messageMerge pull request #1618 from MLH-Fellowship/prompt-requiredDavid Lordbefore adoption8efb348
lines 661–691why, one step back661662663@pytest.mark.parametrize(664 ("args", "expect"),665 [666 (None, (None, None, ())),667 (["--opt"], ("flag", None, ())),668 (["--opt", "-a", 42], ("flag", "42", ())),669 (["--opt", "test", "-a", 42], ("test", "42", ())),670 (["--opt=test", "-a", 42], ("test", "42", ())),671 (["-o"], ("flag", None, ())),672 (["-o", "-a", 42], ("flag", "42", ())),673 (["-o", "test", "-a", 42], ("test", "42", ())),674 (["-otest", "-a", 42], ("test", "42", ())),675 (["a", "b", "c"], (None, None, ("a", "b", "c"))),676 (["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),677 (["--opt", "test"], ("test", None, ())),678 (["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),679 (["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),680 ],681)682def test_option_with_optional_value(runner, args, expect):683 @click.command()684 @click.option("-o", "--opt", is_flag=False, flag_value="flag")685 @click.option("-a")686 @click.argument("b", nargs=-1)687 def cli(opt, a, b):688 return opt, a, b689690 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)691 assert result.return_value == expect