test_options.py
This file
Path
tests/test_options.py
2,575 lines · 84.9 KiB · Python
Last changed by
Show default string in prompt (#3328)Rowlando13before adoption76552ff
History of this fileTake it
The walk stopped after 200 commits: lines older than b498906 point at it.
Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1why, one step back1import enumBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 2–3why, one step back2import os3import reFix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 4why, one step back4import sysMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 5–8why, one step back5import tempfile6from contextlib import nullcontext7from typing import Literal8Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
lines 9–10why, one step back9if sys.version_info < (3, 11):10 enum.StrEnum = enum.Enum # type: ignore[assignment]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 11why, one step back11Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 12why, one step back12import pytestMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 13why, one step back13Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 14–15why, one step back14import click15from click import OptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 16–17why, one step back16from click import UNPROCESSED17from click._utils import UNSETFix handling of options which set both is_flag=False and flag_value=... (#3152)Rowlando13before adoptionebcd548
line 18why, one step back18from click.testing import CliRunnerMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 19–20why, one step back1920Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 21why, one step back21def test_prefixes(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 22why, one step back22 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 23–26why, one step back23 @click.option("++foo", is_flag=True, help="das foo")24 @click.option("--bar", is_flag=True, help="das bar")25 def cli(foo, bar):26 click.echo(f"foo={foo} bar={bar}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 27why, one step back27Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 28why, one step back28 result = runner.invoke(cli, ["++foo", "--bar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 29why, one step back29 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 30why, one step back30 assert result.output == "foo=True bar=True\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 31–32why, one step back3132 result = runner.invoke(cli, ["--help"])Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 33–34why, one step back33 assert re.search(r"\+\+foo\s+das foo", result.output) is not None34 assert re.search(r"--bar\s+das bar", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 35–36why, one step back3536Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 37–39why, one step back37def test_invalid_option(runner):38 with pytest.raises(TypeError, match="name was passed") as exc_info:39 click.Option(["foo"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 40why, one step back40Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 41–44why, one step back41 message = str(exc_info.value)42 assert "name was passed (foo)" in message43 assert "declare an argument" in message44 assert "'--foo'" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 45–47why, one step back454647@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 48why, one step back48def test_deprecated_usage(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 49why, one step back49 @click.command()Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 50why, one step back50 @click.option("--foo", default="bar", deprecated=deprecated)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 51–54why, one step back51 def cmd(foo):52 click.echo(foo)5354 result = runner.invoke(cmd, ["--help"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 55why, one step back55 assert "(DEPRECATED" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 56–61why, one step back5657 if isinstance(deprecated, str):58 assert deprecated in result.output596061@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 62why, one step back62def test_deprecated_warning(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 63–64why, one step back63 @click.command()64 @click.option(Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 65why, one step back65 "--my-option", required=False, deprecated=deprecated, default="default option"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 66why, one step back66 )Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 67–68why, one step back67 def cli(my_option: str):68 click.echo(f"{my_option}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 69why, one step back69Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 70why, one step back70 # defaults should not give a deprecated warningMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 71–72why, one step back71 result = runner.invoke(cli, [])72 assert result.exit_code == 0, result.outputIntroduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 73why, one step back73 assert "is deprecated" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 74why, one step back74Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 75why, one step back75 result = runner.invoke(cli, ["--my-option", "hello"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 76why, one step back76 assert result.exit_code == 0, result.outputIntroduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 77why, one step back77 assert "option 'my_option' is deprecated" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 78–82why, one step back7879 if isinstance(deprecated, str):80 assert deprecated in result.output8182Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 83–85why, one step back83def test_deprecated_required(runner):84 with pytest.raises(ValueError, match="is deprecated and still required"):85 click.Option(["--a"], required=True, deprecated=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 86–87why, one step back8687Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 88–90why, one step back88def test_deprecated_prompt(runner):89 with pytest.raises(ValueError, match="`deprecated` options cannot use `prompt`"):90 click.Option(["--a"], prompt=True, deprecated=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 91–92why, one step back9192Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 93why, one step back93def test_invalid_nargs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 94–96why, one step back94 with pytest.raises(TypeError, match="nargs=-1 is not supported for options."):9596 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 97–99why, one step back97 @click.option("--foo", nargs=-1)98 def cli(foo):99 passMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 100–101why, one step back100101Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 102why, one step back102def test_nargs_tup_composite_mult(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 103why, one step back103 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 104–107why, one step back104 @click.option("--item", type=(str, int), multiple=True)105 def copy(item):106 for name, id in item:107 click.echo(f"name={name} id={id:d}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 108why, one step back108Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 109why, one step back109 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 110why, one step back110 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 111why, one step back111 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 112–113why, one step back112113Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 114why, one step back114def test_counting(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 115why, one step back115 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 116–118why, one step back116 @click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))117 def cli(v):118 click.echo(f"verbosity={v:d}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 119why, one step back119Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 120why, one step back120 result = runner.invoke(cli, ["-vvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 121why, one step back121 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 122why, one step back122 assert result.output == "verbosity=3\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 123why, one step back123Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 124why, one step back124 result = runner.invoke(cli, ["-vvvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 125why, one step back125 assert result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 126why, one step back126 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 127–129why, one step back127128 result = runner.invoke(cli, [])129 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 130why, one step back130 assert result.output == "verbosity=0\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 131–132why, one step back131132 result = runner.invoke(cli, ["--help"])Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 133why, one step back133 assert re.search(r"-v\s+Verbosity", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 134–135why, one step back134135Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 136–137why, one step back136@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])137def test_unknown_options(runner, unknown_flag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 138–141why, one step back138 @click.command()139 def cli():140 pass141Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 142why, one step back142 result = runner.invoke(cli, [unknown_flag])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 143why, one step back143 assert result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 144why, one step back144 assert f"No such option: {unknown_flag}" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 145–147why, one step back145146147@pytest.mark.parametrize(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 148why, one step back148 ("value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 149why, one step back149 [Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 150–152why, one step back150 ("--cat", "Did you mean --count?"),151 ("--bounds", "(Possible options: --bound, --count)"),152 ("--bount", "(Possible options: --bound, --count)"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 153–154why, one step back153 ],154)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 155why, one step back155def test_suggest_possible_options(runner, value, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 156why, one step back156 cli = click.Command(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 157why, one step back157 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 158why, one step back158 )Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 159–160why, one step back159 result = runner.invoke(cli, [value])160 assert expect in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 161–162why, one step back161162Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 163why, one step back163def test_multiple_required(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 164why, one step back164 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 165–167why, one step back165 @click.option("-m", "--message", multiple=True, required=True)166 def cli(message):167 click.echo("\n".join(message))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 168why, one step back168Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 169why, one step back169 result = runner.invoke(cli, ["-m", "foo", "-mbar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 170why, one step back170 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 171why, one step back171 assert result.output == "foo\nbar\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 172–174why, one step back172173 result = runner.invoke(cli, [])174 assert result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 175why, one step back175 assert "Error: Missing option '-m' / '--message'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 176–225why, one step back176177178@pytest.mark.parametrize(179 ("multiple", "nargs", "default", "expected"),180 [181 # If multiple values are allowed, defaults should be iterable.182 (True, 1, [], ()),183 (True, 1, (), ()),184 (True, 1, tuple(), ()),185 (True, 1, set(), ()),186 (True, 1, frozenset(), ()),187 (True, 1, {}, ()),188 # Special values.189 (True, 1, None, ()),190 (True, 1, UNSET, ()),191 # Number of values are kept as-is in the default.192 (True, 1, [1], (1,)),193 (True, 1, [1, 2], (1, 2)),194 (True, 1, [1, 2, 3], (1, 2, 3)),195 (True, 1, [1.1, 2.2], (1.1, 2.2)),196 (True, 1, ["1", "2"], ("1", "2")),197 (True, 1, [None, None], (None, None)),198 # Contrary to list or tuples, native Python types not supported by Click are199 # not recognized and are converted to the default format: tuple of strings.200 # Refs: https://github.com/pallets/click/issues/3036201 (True, 1, {1, 2}, ("1", "2")),202 (True, 1, frozenset([1, 2]), ("1", "2")),203 (True, 1, {1: "a", 2: "b"}, ("1", "2")),204 # Multiple values with nargs > 1.205 (True, 2, [], ()),206 (True, 2, (), ()),207 (True, 2, tuple(), ()),208 (True, 2, set(), ()),209 (True, 2, frozenset(), ()),210 (True, 2, {}, ()),211 (True, 2, None, ()),212 (True, 2, UNSET, ()),213 (True, 2, [[1, 2]], ((1, 2),)),214 (True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),215 (True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),216 (True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),217 (True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),218 (True, 2, [[None, None], [None, None]], ((None, None), (None, None))),219 (True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),220 (True, 2, [[1, 2.2], None], ((1, 2.2), None)),221 # Default of the right length works for non-multiples.222 (False, 2, [1, 2], (1, 2)),223 ],224)225def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):Add cross-reference to tests from `test_defaults.py` (#3223)Rowlando13before adoptionfa7f035
lines 226–237why, one step back226 """Comprehensive check of default-value processing for options with227 ``multiple=True`` and/or ``nargs > 1``.228229 .. hint::230 An argument-specific equivalent is in231 ``test_arguments.py::test_good_defaults_for_nargs``.232233 Smoke tests are in ``test_defaults.py``:234 ``test_multiple_defaults`` (explicit ``type=FLOAT``)235 and ``test_nargs_plus_multiple`` (``nargs=2``).236 """237Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 238–394why, one step back238 @click.command()239 @click.option("-a", multiple=multiple, nargs=nargs, default=default)240 def cmd(a):241 click.echo(repr(a), nl=False)242243 result = runner.invoke(cmd)244 assert result.output == repr(expected)245246247@pytest.mark.parametrize(248 ("multiple", "nargs", "default", "exception", "message"),249 [250 # Non-iterables defaults.251 (252 True,253 1,254 "Yo",255 None,256 "Error: Invalid value for '-a': Value must be an iterable.",257 ),258 (259 True,260 1,261 "",262 None,263 "Error: Invalid value for '-a': Value must be an iterable.",264 ),265 (266 True,267 1,268 True,269 None,270 "Error: Invalid value for '-a': Value must be an iterable.",271 ),272 (273 True,274 1,275 False,276 None,277 "Error: Invalid value for '-a': Value must be an iterable.",278 ),279 (280 True,281 1,282 12,283 None,284 "Error: Invalid value for '-a': Value must be an iterable.",285 ),286 (287 True,288 1,289 7.9,290 None,291 "Error: Invalid value for '-a': Value must be an iterable.",292 ),293 (294 False,295 2,296 42,297 None,298 "Error: Invalid value for '-a': Value must be an iterable.",299 ),300 (301 True,302 2,303 ["test string which is not a list in the list"],304 None,305 "Error: Invalid value for '-a': Value must be an iterable.",306 ),307 # Multiple options, each with 2 args, but with wrong length.308 (309 True,310 2,311 (1,),312 None,313 "Error: Invalid value for '-a': Value must be an iterable.",314 ),315 (316 True,317 2,318 (1, 2, 3),319 None,320 "Error: Invalid value for '-a': Value must be an iterable.",321 ),322 (323 True,324 2,325 [tuple()],326 ValueError,327 r"'nargs' must be 0 \(or None\) for type <click\.types\.Tuple object at "328 r"0x[0-9A-Fa-f]+>, but it was 2\.",329 ),330 (331 True,332 2,333 [(1,)],334 ValueError,335 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "336 r"0x[0-9A-Fa-f]+>, but it was 2\.",337 ),338 (339 True,340 2,341 [(1, 2, 3)],342 ValueError,343 r"'nargs' must be 3 \(or None\) for type <click\.types\.Tuple object at "344 r"0x[0-9A-Fa-f]+>, but it was 2\.",345 ),346 # A mix of valid and invalid defaults.347 (348 True,349 2,350 [[1, 2.2], []],351 None,352 "Error: Invalid value for '-a': 2 values are required, but 0 were given.",353 ),354 # Default values that are iterable but not of the right length.355 (356 False,357 2,358 [1],359 None,360 "Error: Invalid value for '-a': Takes 2 values but 1 was given.",361 ),362 (363 True,364 2,365 [[1]],366 ValueError,367 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "368 r"0x[0-9A-Fa-f]+>, but it was 2\.",369 ),370 ],371)372def test_bad_defaults_for_multiple(373 runner, multiple, nargs, default, exception, message374):375 if exception:376 assert issubclass(exception, Exception)377 else:378 assert exception is None379380 with (381 pytest.raises(exception, match=re.compile(message))382 if exception383 else nullcontext()384 ):385386 @click.command()387 @click.option("-a", multiple=multiple, nargs=nargs, default=default)388 def cmd(a):389 click.echo(repr(a))390391 result = runner.invoke(cmd)392 assert message in result.stderr393394Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 395–396why, one step back395@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])396def test_empty_envvar(runner, env_key):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 397why, one step back397 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 398–400why, one step back398 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")399 def cli(mypath):400 click.echo(f"mypath: {mypath}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 401why, one step back401Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 402–404why, one step back402 result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")403 assert result.exception is None404 assert result.output == "mypath: None\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 405–406why, one step back405406Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 407why, one step back407def test_multiple_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 408why, one step back408 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 409why, one step back409 @click.option("--arg", multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 410–413why, one step back410 def cmd(arg):411 click.echo("|".join(arg))412413 result = runner.invoke(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 414why, one step back414 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 415–419why, one step back415 )416 assert not result.exception417 assert result.output == "foo|bar|baz\n"418419 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 420why, one step back420 @click.option("--arg", multiple=True, envvar="X")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 421–423why, one step back421 def cmd(arg):422 click.echo("|".join(arg))423Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 424why, one step back424 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 425–428why, one step back425 assert not result.exception426 assert result.output == "foo|bar|baz\n"427428 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 429why, one step back429 @click.option("--arg", multiple=True, type=click.Path())Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 430–433why, one step back430 def cmd(arg):431 click.echo("|".join(arg))432433 result = runner.invoke(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 434–437why, one step back434 cmd,435 [],436 auto_envvar_prefix="TEST",437 env={"TEST_ARG": f"foo{os.path.pathsep}bar"},Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 438–445why, one step back438 )439 assert not result.exception440 assert result.output == "foo|bar\n"441442443@pytest.mark.parametrize(444 ("envvar_name", "envvar_value", "expected"),445 (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 446–460why, one step back446 # Lower-cased variations of value.447 ("SHOUT", "true", True),448 ("SHOUT", "false", False),449 # Title-cased variations of value.450 ("SHOUT", "True", True),451 ("SHOUT", "False", False),452 # Upper-cased variations of value.453 ("SHOUT", "TRUE", True),454 ("SHOUT", "FALSE", False),455 # Random-cased variations of value.456 ("SHOUT", "TruE", True),457 ("SHOUT", "truE", True),458 ("SHOUT", "FaLsE", False),459 ("SHOUT", "falsE", False),460 # Extra spaces around the value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 461–466why, one step back461 ("SHOUT", "true ", True),462 ("SHOUT", " true ", True),463 ("SHOUT", " true", True),464 ("SHOUT", "false ", False),465 ("SHOUT", " false ", False),466 ("SHOUT", " false", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 467–471why, one step back467 # Integer variations.468 ("SHOUT", "1", True),469 ("SHOUT", "0", False),470 # Alternative states.471 ("SHOUT", "t", True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 472–473why, one step back472 ("SHOUT", "T", True),473 ("SHOUT", " T ", True),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 474–487why, one step back474 ("SHOUT", "f", False),475 ("SHOUT", "y", True),476 ("SHOUT", "n", False),477 ("SHOUT", "yes", True),478 ("SHOUT", "no", False),479 ("SHOUT", "on", True),480 ("SHOUT", "off", False),481 # Blank value variations.482 ("SHOUT", None, False),483 ("SHOUT", "", False),484 ("SHOUT", " ", False),485 ("SHOUT", " ", False),486 # Variable names are not stripped of spaces and so don't match the487 # flag, which then naturraly takes its default value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 488–489why, one step back488 ("SHOUT ", "True", False),489 ("SHOUT ", "False", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 490–491why, one step back490 (" SHOUT ", "True", False),491 (" SHOUT ", "False", False),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 492–495why, one step back492 (" SHOUT", "True", False),493 (" SHOUT", "False", False),494 ("SH OUT", "True", False),495 ("SH OUT", "False", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 496–500why, one step back496 # Same for random and reverse environment variable names: they are not497 # recognized by the option.498 ("RANDOM", "True", False),499 ("NO_SHOUT", "True", False),500 ("NO_SHOUT", "False", False),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 501–552why, one step back501 ("NOSHOUT", "True", False),502 ("NOSHOUT", "False", False),503 ),504)505def test_boolean_flag_envvar(runner, envvar_name, envvar_value, expected):506 assert isinstance(envvar_name, str)507 assert isinstance(envvar_value, str) or envvar_value is None508509 @click.command()510 @click.option("--shout/--no-shout", envvar="SHOUT")511 def cli(shout):512 click.echo(repr(shout), nl=False)513514 result = runner.invoke(cli, [], env={envvar_name: envvar_value})515 assert result.exit_code == 0516 assert result.output == repr(expected)517518519@pytest.mark.parametrize(520 "value",521 (522 # Extra spaces inside the value.523 "tr ue",524 "fa lse",525 # Numbers.526 "10",527 "01",528 "00",529 "11",530 "0.0",531 "1.1",532 # Random strings.533 "randomstring",534 "None",535 "'None'",536 "A B",537 " 1 2 ",538 "9.3",539 "a;n",540 "x:y",541 "i/o",542 ),543)544def test_boolean_envvar_bad_values(runner, value):545 @click.command()546 @click.option("--shout/--no-shout", envvar="SHOUT")547 def cli(shout):548 click.echo(shout)549550 result = runner.invoke(cli, [], env={"SHOUT": value})551 assert result.exit_code == 2552 assert (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 553why, one step back553 f"Invalid value for '--shout': {value!r} is not a valid boolean."Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 554–557why, one step back554 in result.output555 )556557Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 558why, one step back558def test_multiple_default_help(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 559why, one step back559 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 560–562why, one step back560 @click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)561 @click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)562 def cmd(arg, arg2):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 563–566why, one step back563 pass564565 result = runner.invoke(cmd, ["--help"])566 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 567–568why, one step back567 assert "foo, bar" in result.output568 assert "1, 2" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 569–570why, one step back569570Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 571why, one step back571def test_show_default_default_map(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 572why, one step back572 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 573why, one step back573 @click.option("--arg", default="a", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 574–576why, one step back574 def cmd(arg):575 click.echo(arg)576Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 577why, one step back577 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 578–579why, one step back578579 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 580why, one step back580 assert "[default: b]" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 581–582why, one step back581582Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 583–585why, one step back583def test_multiple_default_type():584 opt = click.Option(["-a"], multiple=True, default=(1, 2))585 assert opt.nargs == 1Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 586why, one step back586 assert opt.multipleBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 587why, one step back587 assert opt.type is click.INTMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 588why, one step back588 ctx = click.Context(click.Command("test"))Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 589why, one step back589 assert opt.get_default(ctx) == (1, 2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 590–591why, one step back590591Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 592–594why, one step back592def test_multiple_default_composite_type():593 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])594 assert opt.nargs == 2Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 595why, one step back595 assert opt.multipleBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 596–597why, one step back596 assert isinstance(opt.type, click.Tuple)597 assert opt.type.types == [click.INT, click.STRING]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 598why, one step back598 ctx = click.Context(click.Command("test"))Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 599why, one step back599 assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 600–601why, one step back600601Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 602why, one step back602def test_parse_multiple_default_composite_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 603why, one step back603 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 604–606why, one step back604 @click.option("-a", multiple=True, default=("a", "b"))605 @click.option("-b", multiple=True, default=[(1, "a")])606 def cmd(a, b):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 607why, one step back607 click.echo(a)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 608why, one step back608 click.echo(b)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 609why, one step back609Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 610–611why, one step back610 # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())611 # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 612why, one step back612 result = runner.invoke(cmd)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 613why, one step back613 assert result.output == "('a', 'b')\n((1, 'a'),)\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 614–615why, one step back614615Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 616why, one step back616def test_dynamic_default_help_unset(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 617–630why, one step back617 @click.command()618 @click.option(619 "--username",620 prompt=True,621 default=lambda: os.environ.get("USER", ""),622 show_default=True,623 )624 def cmd(username):625 print("Hello,", username)626627 result = runner.invoke(cmd, ["--help"])628 assert result.exit_code == 0629 assert "--username" in result.output630 assert "lambda" not in result.outputBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 631why, one step back631 assert "(dynamic)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 632–633why, one step back632633Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 634why, one step back634def test_dynamic_default_help_text(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 635–639why, one step back635 @click.command()636 @click.option(637 "--username",638 prompt=True,639 default=lambda: os.environ.get("USER", ""),Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 640why, one step back640 show_default="current user",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 641–648why, one step back641 )642 def cmd(username):643 print("Hello,", username)644645 result = runner.invoke(cmd, ["--help"])646 assert result.exit_code == 0647 assert "--username" in result.output648 assert "lambda" not in result.outputBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 649why, one step back649 assert "(current user)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 650–651why, one step back650651Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 652–655why, one step back652def test_dynamic_default_help_special_method(runner):653 class Value:654 def __call__(self):655 return 42Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 656why, one step back656Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 657–658why, one step back657 def __str__(self):658 return "special value"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 659why, one step back659Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 660why, one step back660 opt = click.Option(["-a"], default=Value(), show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 661why, one step back661 ctx = click.Context(click.Command("cli"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 662why, one step back662 assert opt.get_help_extra(ctx) == {"default": "special value"}Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 663why, one step back663 assert "special value" in opt.get_help_record(ctx)[1]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 664–666why, one step back664665666@pytest.mark.parametrize(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 667why, one step back667 ("type", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 668why, one step back668 [Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 669–672why, one step back669 (click.IntRange(1, 32), "1<=x<=32"),670 (click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),671 (click.IntRange(1), "x>=1"),672 (click.IntRange(max=32), "x<=32"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 673–674why, one step back673 ],674)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 675–676why, one step back675def test_intrange_default_help_text(type, expect):676 option = click.Option(["--num"], type=type, show_default=True, default=2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 677why, one step back677 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 678why, one step back678 assert option.get_help_extra(context) == {"default": "2", "range": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 679why, one step back679 result = option.get_help_record(context)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 680why, one step back680 assert expect in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 681–682why, one step back681682Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 683–685why, one step back683def test_count_default_type_help():684 """A count option with the default type should not show >=0 in help."""685 option = click.Option(["--count"], count=True, help="some words")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 686why, one step back686 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 687why, one step back687 assert option.get_help_extra(context) == {}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 688why, one step back688 result = option.get_help_record(context)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 689why, one step back689 assert result == "some words"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 690–691why, one step back690691Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 692–694why, one step back692def test_file_type_help_default():693 """The default for a File type is a filename string. The string694 should be displayed in help, not an open file object.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 695why, one step back695Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 696–697why, one step back696 Type casting is only applied to defaults in processing, not when697 getting the default value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 698why, one step back698 """Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 699–700why, one step back699 option = click.Option(700 ["--in"], type=click.File(), default=__file__, show_default=TrueMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 701–702why, one step back701 )702 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 703why, one step back703 assert option.get_help_extra(context) == {"default": __file__}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 704why, one step back704 result = option.get_help_record(context)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 705why, one step back705 assert __file__ in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 706–707why, one step back706707Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 708why, one step back708def test_toupper_envvar_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 709why, one step back709 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 710why, one step back710 @click.option("--arg")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 711–713why, one step back711 def cmd(arg):712 click.echo(arg)713Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 714why, one step back714 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 715why, one step back715 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 716why, one step back716 assert result.output == "foo\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 717–718why, one step back717718Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 719why, one step back719def test_nargs_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 720why, one step back720 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 721why, one step back721 @click.option("--arg", nargs=2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 722–725why, one step back722 def cmd(arg):723 click.echo("|".join(arg))724725 result = runner.invoke(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 726why, one step back726 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 727–731why, one step back727 )728 assert not result.exception729 assert result.output == "foo|bar\n"730731 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 732why, one step back732 @click.option("--arg", nargs=2, multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 733why, one step back733 def cmd(arg):Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 734–735why, one step back734 for item in arg:735 click.echo("|".join(item))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 736–737why, one step back736737 result = runner.invoke(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 738why, one step back738 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 739–740why, one step back739 )740 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 741why, one step back741 assert result.output == "x|1\ny|2\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 742–743why, one step back742743Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 744why, one step back744def test_show_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 745why, one step back745 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 746why, one step back746 @click.option("--arg1", envvar="ARG1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 747–751why, one step back747 def cmd(arg):748 pass749750 result = runner.invoke(cmd, ["--help"])751 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 752why, one step back752 assert "ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 753–754why, one step back753754Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 755why, one step back755def test_show_envvar_auto_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 756why, one step back756 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 757why, one step back757 @click.option("--arg1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 758–760why, one step back758 def cmd(arg):759 pass760Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 761why, one step back761 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 762why, one step back762 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 763why, one step back763 assert "TEST_ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 764–765why, one step back764765Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 766–767why, one step back766def test_show_envvar_auto_prefix_dash_in_command(runner):767 @click.group()Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 768–770why, one step back768 def cli():769 pass770Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 771–773why, one step back771 @cli.command()772 @click.option("--baz", show_envvar=True)773 def foo_bar(baz):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 774–775why, one step back774 pass775Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 776why, one step back776 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 777why, one step back777 assert not result.exceptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 778why, one step back778 assert "TEST_FOO_BAR_BAZ" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 779–780why, one step back779780Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 781–782why, one step back781def test_custom_validation(runner):782 def validate_pos_int(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 783why, one step back783 if value < 0:Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 784why, one step back784 raise click.BadParameter("Value needs to be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 785–787why, one step back785 return value786787 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 788why, one step back788 @click.option("--foo", callback=validate_pos_int, default=1)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 789–791why, one step back789 def cmd(foo):790 click.echo(foo)791Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 792–793why, one step back792 result = runner.invoke(cmd, ["--foo", "-1"])793 assert "Invalid value for '--foo': Value needs to be positive" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 794why, one step back794Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 795why, one step back795 result = runner.invoke(cmd, ["--foo", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 796–798why, one step back796 assert result.output == "42\n"797798Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 799–800why, one step back799def test_callback_validates_prompt(runner, monkeypatch):800 def validate(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 801why, one step back801 if value < 0:Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 802why, one step back802 raise click.BadParameter("should be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 803–806why, one step back803804 return value805806 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 807–808why, one step back807 @click.option("-a", type=int, callback=validate, prompt=True)808 def cli(a):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 809–810why, one step back809 click.echo(a)810Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 811–812why, one step back811 result = runner.invoke(cli, input="-12\n60\n")812 assert result.output == "A: -12\nError: should be positive\nA: 60\n60\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 813–814why, one step back813814Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 815why, one step back815def test_winstyle_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 816why, one step back816 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 817–819why, one step back817 @click.option("/debug;/no-debug", help="Enables or disables debug mode.")818 def cmd(debug):819 click.echo(debug)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 820why, one step back820Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 821why, one step back821 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 822why, one step back822 assert result.output == "True\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 823why, one step back823 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 824why, one step back824 assert result.output == "False\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 825why, one step back825 result = runner.invoke(cmd, [], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 826why, one step back826 assert result.output == "False\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 827–829why, one step back827 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])828 assert "/debug; /no-debug Enables or disables debug mode." in result.output829 assert "/? Show this message and exit." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 830–831why, one step back830831Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 832why, one step back832def test_legacy_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 833why, one step back833 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 834–836why, one step back834 @click.option("-whatever")835 def cmd(whatever):836 click.echo(whatever)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 837why, one step back837Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 838why, one step back838 result = runner.invoke(cmd, ["-whatever", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 839why, one step back839 assert result.output == "42\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 840–841why, one step back840 result = runner.invoke(cmd, ["-whatever=23"])841 assert result.output == "23\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 842–863why, one step back842843844@pytest.mark.parametrize(845 ("value", "expect_missing", "processed_value"),846 [847 (UNSET, True, None),848 (None, False, None),849 # Default type of the argument is str, so everything is processed as strings.850 ("", False, ""),851 (" ", False, " "),852 ("foo", False, "foo"),853 ("12", False, "12"),854 (12, False, "12"),855 (12.1, False, "12.1"),856 (list(), False, "[]"),857 (tuple(), False, "()"),858 (set(), False, "set()"),859 (frozenset(), False, "frozenset()"),860 (dict(), False, "{}"),861 ],862)863def test_required_option(value, expect_missing, processed_value):Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 864why, one step back864 ctx = click.Context(click.Command(""))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 865–876why, one step back865 argument = click.Option(["-a"], required=True)866867 if expect_missing:868 with pytest.raises(click.MissingParameter) as excinfo:869 argument.process_value(ctx, value)870 assert str(excinfo.value) == "Missing parameter: a"871872 else:873 value = argument.process_value(ctx, value)874 assert value == processed_value875876Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 877why, one step back877def test_missing_required_flag(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 878why, one step back878 cli = click.Command(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 879why, one step back879 "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 880–882why, one step back880 )881 result = runner.invoke(cli)882 assert result.exit_code == 2Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 883why, one step back883 assert "Error: Missing option '--on'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 884–885why, one step back884885Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 886why, one step back886def test_missing_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 887why, one step back887 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 888why, one step back888 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 889–893why, one step back889 def cmd(foo):890 click.echo(foo)891892 result = runner.invoke(cmd)893 assert result.exit_code == 2Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 894–898why, one step back894 error, separator, choices = result.output.partition("Choose from")895 assert "Error: Missing option '--foo'. " in error896 assert "Choose from" in separator897 assert "foo" in choices898 assert "bar" in choicesMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 899–900why, one step back899900show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 901why, one step back901def test_missing_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 902why, one step back902 cli = click.Command(show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 903why, one step back903 "cli", params=[click.Option(["--foo"], envvar="bar", required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 904–909why, one step back904 )905 result = runner.invoke(cli)906 assert result.exit_code == 2907 assert "Error: Missing option '--foo'." in result.output908 cli = click.Command(909 "cli",show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 910why, one step back910 params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 911–913why, one step back911 )912 result = runner.invoke(cli)913 assert result.exit_code == 2show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 914why, one step back914 assert "Error: Missing option '--foo' (env var: 'bar')." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 915–916why, one step back915916 cli = click.Command(Don't include envvar in error hint when envvar not configured (#2972)Rowlando13before adoption0be2bd3
line 917why, one step back917 "cli", params=[click.Option(["--foo"], show_envvar=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 918–923why, one step back918 )919 result = runner.invoke(cli)920 assert result.exit_code == 2921 assert "Error: Missing option '--foo'." in result.output922923Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 924why, one step back924def test_case_insensitive_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 925–933why, one step back925 @click.command()926 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))927 def cmd(foo):928 click.echo(foo)929930 result = runner.invoke(cmd, ["--foo", "apple"])931 assert result.exit_code == 0932 assert result.output == "Apple\n"933Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 934why, one step back934 result = runner.invoke(cmd, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 935why, one step back935 assert result.exit_code == 0Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 936why, one step back936 assert result.output == "Orange\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 937why, one step back937Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 938why, one step back938 result = runner.invoke(cmd, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 939–942why, one step back939 assert result.exit_code == 0940 assert result.output == "Apple\n"941942 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 943–944why, one step back943 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))944 def cmd2(foo):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 945–946why, one step back945 click.echo(foo)946Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 947why, one step back947 result = runner.invoke(cmd2, ["--foo", "apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 948–949why, one step back948 assert result.exit_code == 2949Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 950why, one step back950 result = runner.invoke(cmd2, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 951–952why, one step back951 assert result.exit_code == 2952Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 953why, one step back953 result = runner.invoke(cmd2, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 954–956why, one step back954 assert result.exit_code == 0955956Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 957why, one step back957def test_case_insensitive_choice_returned_exactly(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 958–967why, one step back958 @click.command()959 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))960 def cmd(foo):961 click.echo(foo)962963 result = runner.invoke(cmd, ["--foo", "apple"])964 assert result.exit_code == 0965 assert result.output == "Apple\n"966967Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 968why, one step back968def test_option_help_preserve_paragraphs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 969–970why, one step back969 @click.command()970 @click.option(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 971–974why, one step back971 "-C",972 "--config",973 type=click.Path(),974 help="""Configuration file to use.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 975why, one step back975Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 976–978why, one step back976 If not given, the environment variable CONFIG_FILE is consulted977 and used if set. If neither are given, a default configuration978 file is loaded.""",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 979why, one step back979 )Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 980why, one step back980 def cmd(config):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 981–984why, one step back981 pass982983 result = runner.invoke(cmd, ["--help"])984 assert result.exit_code == 0Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 985why, one step back985 i = " " * 21Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 986why, one step back986 assert (Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 987–992why, one step back987 " -C, --config PATH Configuration file to use.\n"988 f"{i}\n"989 f"{i}If not given, the environment variable CONFIG_FILE is\n"990 f"{i}consulted and used if set. If neither are given, a default\n"991 f"{i}configuration file is loaded."992 ) in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 993–994why, one step back993994Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 995–999why, one step back995def test_argument_custom_class(runner):996 class CustomArgument(click.Argument):997 def get_default(self, ctx, call=True):998 """a dumb override of a default value for testing"""999 return "I am a default"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1000–1001why, one step back10001001 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1002–1004why, one step back1002 @click.argument("testarg", cls=CustomArgument, default="you wont see me")1003 def cmd(testarg):1004 click.echo(testarg)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1005–1006why, one step back10051006 result = runner.invoke(cmd)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1007why, one step back1007 assert "I am a default" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1008–1010why, one step back1008 assert "you wont see me" not in result.output10091010Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1011why, one step back1011def test_option_custom_class(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1012–1017why, one step back1012 class CustomOption(click.Option):1013 def get_help_record(self, ctx):1014 """a dumb override of a help text for testing"""1015 return ("--help", "I am a help text")10161017 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1018–1019why, one step back1018 @click.option("--testoption", cls=CustomOption, help="you wont see me")1019 def cmd(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1020–1022why, one step back1020 click.echo(testoption)10211022 result = runner.invoke(cmd, ["--help"])Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1023why, one step back1023 assert "I am a help text" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1024–1026why, one step back1024 assert "you wont see me" not in result.output10251026Ensure that `type_cast_value` never gets `UNSET` (#3090)Rowlando13before adoption8d4051e
lines 1027–1062why, one step back1027@pytest.mark.parametrize(1028 ("param_decl", "option_kwargs", "pass_argv"),1029 (1030 # there is a large potential parameter space to explore here1031 # this is just a very small sample of it1032 ("--opt", {}, []),1033 ("--opt", {"multiple": True}, []),1034 ("--opt", {"is_flag": True}, []),1035 ("--opt/--no-opt", {"is_flag": True, "default": None}, []),1036 ("--req", {"is_flag": True, "required": True}, ["--req"]),1037 ),1038)1039def test_option_custom_class_can_override_type_cast_value_and_never_sees_unset(1040 runner, param_decl, option_kwargs, pass_argv1041):1042 """1043 Test that overriding type_cast_value is supported10441045 In particular, the option is never passed an UNSET sentinel value.1046 """10471048 class CustomOption(click.Option):1049 def type_cast_value(self, ctx, value):1050 assert value is not UNSET1051 return value10521053 @click.command()1054 @click.option("myparam", param_decl, **option_kwargs, cls=CustomOption)1055 def cmd(myparam):1056 click.echo("ok")10571058 result = runner.invoke(cmd, pass_argv)1059 assert not result.exception1060 assert result.exit_code == 010611062Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1063–1064why, one step back1063def test_option_custom_class_reusable(runner):1064 """Ensure we can reuse a custom class option. See Issue #926"""Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1065–1070why, one step back10651066 class CustomOption(click.Option):1067 def get_help_record(self, ctx):1068 """a dumb override of a help text for testing"""1069 return ("--help", "I am a help text")1070Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1071–1072why, one step back1071 # Assign to a variable to re-use the decorator.1072 testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1073–1075why, one step back10731074 @click.command()1075 @testoptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1076why, one step back1076 def cmd1(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1077–1080why, one step back1077 click.echo(testoption)10781079 @click.command()1080 @testoptionBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1081why, one step back1081 def cmd2(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1082–1083why, one step back1082 click.echo(testoption)1083Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1084–1088why, one step back1084 # Both of the commands should have the --help option now.1085 for cmd in (cmd1, cmd2):1086 result = runner.invoke(cmd, ["--help"])1087 assert "I am a help text" in result.output1088 assert "you wont see me" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1089–1090why, one step back10891090Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1091why, one step back1091@pytest.mark.parametrize("custom_class", (True, False))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1092why, one step back1092@pytest.mark.parametrize(Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1093why, one step back1093 ("name_specs", "expected"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1094–1095why, one step back1094 (1095 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1096–1097why, one step back1096 ("-h", "--help"),1097 " -h, --help Show this message and exit.\n",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1098–1099why, one step back1098 ),1099 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1100–1101why, one step back1100 ("-h",),1101 " -h Show this message and exit.\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1102–1104why, one step back1102 " --help Show this message and exit.\n",1103 ),1104 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1105why, one step back1105 ("--help",),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1106–1109why, one step back1106 " --help Show this message and exit.\n",1107 ),1108 ),1109)Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1110–1111why, one step back1110def test_help_option_custom_names_and_class(runner, custom_class, name_specs, expected):1111 class CustomHelpOption(click.Option):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1112–1113why, one step back1112 pass1113Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1114–1116why, one step back1114 option_attrs = {}1115 if custom_class:1116 option_attrs["cls"] = CustomHelpOptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1117–1118why, one step back11171118 @click.command()Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1119–1120why, one step back1119 @click.help_option(*name_specs, **option_attrs)1120 def cmd():Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1121–1122why, one step back1121 pass1122Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1123–1127why, one step back1123 for arg in name_specs:1124 result = runner.invoke(cmd, [arg])1125 assert not result.exception1126 assert result.exit_code == 01127 assert expected in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1128–1129why, one step back11281129Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1130why, one step back1130def test_bool_flag_with_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1131why, one step back1131 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1132–1133why, one step back1132 @click.option("--shout/--no-shout", default=False, type=bool)1133 def cmd(shout):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1134–1139why, one step back1134 pass11351136 result = runner.invoke(cmd)1137 assert not result.exception11381139Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1140why, one step back1140def test_aliases_for_flags(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1141why, one step back1141 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1142–1143why, one step back1142 @click.option("--warnings/--no-warnings", " /-W", default=True)1143 def cli(warnings):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1144–1145why, one step back1144 click.echo(warnings)1145Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1146why, one step back1146 result = runner.invoke(cli, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1147why, one step back1147 assert result.output == "True\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1148why, one step back1148 result = runner.invoke(cli, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1149why, one step back1149 assert result.output == "False\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1150why, one step back1150 result = runner.invoke(cli, ["-W"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1151–1153why, one step back1151 assert result.output == "False\n"11521153 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1154–1155why, one step back1154 @click.option("--warnings/--no-warnings", "-w", default=True)1155 def cli_alt(warnings):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1156–1157why, one step back1156 click.echo(warnings)1157Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1158why, one step back1158 result = runner.invoke(cli_alt, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1159why, one step back1159 assert result.output == "True\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1160why, one step back1160 result = runner.invoke(cli_alt, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1161why, one step back1161 assert result.output == "False\n"Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1162why, one step back1162 result = runner.invoke(cli_alt, ["-w"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1163–1166why, one step back1163 assert result.output == "True\n"116411651166@pytest.mark.parametrize(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1167why, one step back1167 "option_args,expected",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1168why, one step back1168 [Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1169–1178why, one step back1169 (["--aggressive", "--all", "-a"], "aggressive"),1170 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),1171 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),1172 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),1173 (["-a", "-b", "-c"], "a"),1174 (["-c", "-b", "-a"], "c"),1175 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),1176 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),1177 (["--from", "-f", "_from"], "_from"),1178 (["--return", "-r", "_ret"], "_ret"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1179–1180why, one step back1179 ],1180)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1181why, one step back1181def test_option_names(runner, option_args, expected):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1182why, one step back1182 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1183–1185why, one step back1183 @click.option(*option_args, is_flag=True)1184 def cmd(**kwargs):1185 click.echo(str(kwargs[expected]))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1186why, one step back1186Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1187why, one step back1187 assert cmd.params[0].name == expectedMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1188why, one step back1188Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1189–1192why, one step back1189 for form in option_args:1190 if form.startswith("-"):1191 result = runner.invoke(cmd, [form])1192 assert result.output == "True\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1193–1194why, one step back11931194Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1195–1197why, one step back1195def test_flag_duplicate_names(runner):1196 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):1197 click.Option(["--foo/--foo"], default=False)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1198–1199why, one step back11981199Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1200–1204why, one step back1200@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])1201def test_show_default_boolean_flag_name(runner, default, expect):1202 """When a boolean flag has distinct True/False opts, it should show1203 the default opt name instead of the default value. It should only1204 show one name even if multiple are declared.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1205–1206why, one step back1205 """1206 opt = click.Option(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1207why, one step back1207 ("--cache/--no-cache", "--c/--nc"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1208–1209why, one step back1208 default=default,1209 show_default=True,Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1210why, one step back1210 help="Enable/Disable the cache.",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1211–1212why, one step back1211 )1212 ctx = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1213why, one step back1213 assert opt.get_help_extra(ctx) == {"default": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1214why, one step back1214 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1215why, one step back1215 assert f"[default: {expect}]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1216–1217why, one step back12161217Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1218–1220why, one step back1218def test_show_true_default_boolean_flag_value(runner):1219 """When a boolean flag only has one opt and its default is True,1220 it will show the default value, not the opt name.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1221–1225why, one step back1221 """1222 opt = click.Option(1223 ("--cache",),1224 is_flag=True,1225 show_default=True,Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1226why, one step back1226 default=True,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1227–1229why, one step back1227 help="Enable the cache.",1228 )1229 ctx = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1230why, one step back1230 assert opt.get_help_extra(ctx) == {"default": "True"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1231why, one step back1231 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1232why, one step back1232 assert "[default: True]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1233–1234why, one step back12331234Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1235–1238why, one step back1235@pytest.mark.parametrize("default", [False, None])1236def test_hide_false_default_boolean_flag_value(runner, default):1237 """When a boolean flag only has one opt and its default is False or1238 None, it will not show the defaultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1239–1249why, one step back1239 """1240 opt = click.Option(1241 ("--cache",),1242 is_flag=True,1243 show_default=True,1244 default=default,1245 help="Enable the cache.",1246 )1247 ctx = click.Context(click.Command("test"))1248 assert opt.get_help_extra(ctx) == {}1249 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1250why, one step back1250 assert "[default: " not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1251–1252why, one step back12511252Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1253–1255why, one step back1253def test_show_default_string(runner):1254 """When show_default is a string show that value as default."""1255 opt = click.Option(["--limit"], show_default="unlimited")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1256why, one step back1256 ctx = click.Context(click.Command("cli"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1257why, one step back1257 assert opt.get_help_extra(ctx) == {"default": "(unlimited)"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1258why, one step back1258 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1259why, one step back1259 assert "[default: (unlimited)]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1260–1261why, one step back12601261Show default string in prompt (#3328)Rowlando13before adoption76552ff
lines 1262–1274why, one step back1262def test_string_show_default_shows_custom_string_in_prompt(runner):1263 @click.command()1264 @click.option(1265 "--arg1", show_default="custom", prompt=True, default="my-default-value"1266 )1267 def cmd(arg1):1268 pass12691270 result = runner.invoke(cmd, input="my-input", standalone_mode=False)1271 assert "(custom)" in result.output1272 assert "my-default-value" not in result.output12731274Fix speculative empty string check (#3299)Rowlando13before adoption1458800
lines 1275–1301why, one step back1275class _StrictEq:1276 """Object whose ``__eq__`` raises on string comparison (like semver.Version)."""12771278 def __eq__(self, other):1279 if isinstance(other, str):1280 raise ValueError("cannot compare to string")1281 return NotImplemented12821283 def __str__(self):1284 return "strict"128512861287@pytest.mark.parametrize(1288 ("default", "expected"),1289 [1290 ("", '[default: ""]'),1291 (_StrictEq(), "[default: strict]"),1292 ],1293 ids=["empty-string", "non-string-comparable-object"],1294)1295def test_show_default_with_empty_string(runner, default, expected):1296 """The empty-string check in help rendering must not break on objects1297 whose ``__eq__`` raises for string operands.12981299 Regression test for https://github.com/pallets/click/issues/3298.1300 """1301 opt = click.Option(["--limit"], default=default, show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1302–1303why, one step back1302 ctx = click.Context(click.Command("cli"))1303 message = opt.get_help_record(ctx)[1]Fix speculative empty string check (#3299)Rowlando13before adoption1458800
line 1304why, one step back1304 assert expected in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1305–1306why, one step back13051306Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1307–1309why, one step back1307def test_do_not_show_no_default(runner):1308 """When show_default is True and no default is set do not show None."""1309 opt = click.Option(["--limit"], show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1310–1312why, one step back1310 ctx = click.Context(click.Command("cli"))1311 assert opt.get_help_extra(ctx) == {}1312 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1313why, one step back1313 assert "[default: None]" not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1314–1315why, one step back13141315Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1316–1318why, one step back1316def test_do_not_show_default_empty_multiple():1317 """When show_default is True and multiple=True is set, it should not1318 print empty default value in --help output.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1319why, one step back1319 """Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1320why, one step back1320 opt = click.Option(["-a"], multiple=True, help="values", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1321–1323why, one step back1321 ctx = click.Context(click.Command("cli"))1322 assert opt.get_help_extra(ctx) == {}1323 message = opt.get_help_record(ctx)[1]Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1324why, one step back1324 assert message == "values"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1325–1327why, one step back132513261327@pytest.mark.parametrize(Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1328why, one step back1328 ("ctx_value", "opt_value", "extra_value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1329why, one step back1329 [Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
lines 1330–1339why, one step back1330 (None, None, {}, False),1331 (None, False, {}, False),1332 (None, True, {"default": "1"}, True),1333 (False, None, {}, False),1334 (False, False, {}, False),1335 (False, True, {"default": "1"}, True),1336 (True, None, {"default": "1"}, True),1337 (True, False, {}, False),1338 (True, True, {"default": "1"}, True),1339 (False, "one", {"default": "(one)"}, True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1340–1341why, one step back1340 ],1341)Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1342why, one step back1342def test_show_default_precedence(ctx_value, opt_value, extra_value, expect):Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1343–1344why, one step back1343 ctx = click.Context(click.Command("test"), show_default=ctx_value)1344 opt = click.Option("-a", default=1, help="value", show_default=opt_value)Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1345why, one step back1345 assert opt.get_help_extra(ctx) == extra_valueBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1346–1347why, one step back1346 help = opt.get_help_record(ctx)[1]1347 assert ("default:" in help) is expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1348–1350why, one step back134813491350@pytest.mark.parametrize(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1351why, one step back1351 ("args", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1352why, one step back1352 [Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1353–1366why, one step back1353 (None, (None, None, ())),1354 (["--opt"], ("flag", None, ())),1355 (["--opt", "-a", 42], ("flag", "42", ())),1356 (["--opt", "test", "-a", 42], ("test", "42", ())),1357 (["--opt=test", "-a", 42], ("test", "42", ())),1358 (["-o"], ("flag", None, ())),1359 (["-o", "-a", 42], ("flag", "42", ())),1360 (["-o", "test", "-a", 42], ("test", "42", ())),1361 (["-otest", "-a", 42], ("test", "42", ())),1362 (["a", "b", "c"], (None, None, ("a", "b", "c"))),1363 (["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),1364 (["--opt", "test"], ("test", None, ())),1365 (["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),1366 (["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1367–1368why, one step back1367 ],1368)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1369why, one step back1369def test_option_with_optional_value(runner, args, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1370why, one step back1370 @click.command()Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1371–1375why, one step back1371 @click.option("-o", "--opt", is_flag=False, flag_value="flag")1372 @click.option("-a")1373 @click.argument("b", nargs=-1)1374 def cli(opt, a, b):1375 return opt, a, bMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1376why, one step back1376Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1377–1378why, one step back1377 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)1378 assert result.return_value == expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1379–1380why, one step back13791380Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1381why, one step back1381def test_multiple_option_with_optional_value(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1382–1383why, one step back1382 cli = click.Command(1383 "cli",Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1384–1389why, one step back1384 params=[1385 click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True),1386 click.Option(["-a"]),1387 click.Argument(["b"], nargs=-1),1388 ],1389 callback=lambda **kwargs: kwargs,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1390–1391why, one step back1390 )1391 result = runner.invoke(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1392–1395why, one step back1392 cli,1393 ["-f", "-f", "other", "-f", "-a", "1", "a", "b"],1394 standalone_mode=False,1395 catch_exceptions=False,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1396why, one step back1396 )Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1397–1401why, one step back1397 assert result.return_value == {1398 "f": ("flag", "other", "flag"),1399 "a": "1",1400 "b": ("a", "b"),1401 }Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1402–1403why, one step back14021403Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1404–1405why, one step back1404def test_type_from_flag_value():1405 param = click.Option(["-a", "x"], default=True, flag_value=4)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1406why, one step back1406 assert param.type is click.INTBump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1407why, one step back1407 param = click.Option(["-b", "x"], flag_value=8)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1408–1472why, one step back1408 assert param.type is click.INT140914101411@pytest.mark.parametrize(1412 ("opt_params", "args", "expected"),1413 [1414 # The type passed to the option is responsible to converting the value, whether1415 # we pass the option flag or not.1416 ({"type": bool}, [], False),1417 ({"type": bool}, ["--foo"], True),1418 ({"type": click.BOOL}, [], False),1419 ({"type": click.BOOL}, ["--foo"], True),1420 ({"type": str}, [], None),1421 ({"type": str}, ["--foo"], "True"),1422 # Default value is given as-is to the --foo option when it is not passed,1423 # whatever the type. Now if --foo is passed, the value is always True, whatever1424 # the type. In both case the type of the option is responsible for the1425 # conversion of the value.1426 ({"type": bool, "default": True}, [], True),1427 ({"type": bool, "default": True}, ["--foo"], True),1428 ({"type": bool, "default": False}, [], False),1429 ({"type": bool, "default": False}, ["--foo"], True),1430 # ({"type": bool, "default": "foo"}, [], "foo"),1431 ({"type": bool, "default": "foo"}, ["--foo"], True),1432 ({"type": bool, "default": None}, [], None),1433 ({"type": bool, "default": None}, ["--foo"], True),1434 ({"type": click.BOOL, "default": True}, [], True),1435 ({"type": click.BOOL, "default": True}, ["--foo"], True),1436 ({"type": click.BOOL, "default": False}, [], False),1437 ({"type": click.BOOL, "default": False}, ["--foo"], True),1438 # ({"type": click.BOOL, "default": "foo"}, [], "foo"),1439 ({"type": click.BOOL, "default": "foo"}, ["--foo"], True),1440 ({"type": click.BOOL, "default": None}, [], None),1441 ({"type": click.BOOL, "default": None}, ["--foo"], True),1442 ({"type": str, "default": True}, [], "True"),1443 ({"type": str, "default": True}, ["--foo"], "True"),1444 ({"type": str, "default": False}, [], "False"),1445 ({"type": str, "default": False}, ["--foo"], "True"),1446 ({"type": str, "default": "foo"}, [], "foo"),1447 ({"type": str, "default": "foo"}, ["--foo"], "True"),1448 ({"type": str, "default": None}, [], None),1449 ({"type": str, "default": None}, ["--foo"], "True"),1450 # Flag value is given as-is to the --foo option when it is passed, then1451 # converted by the option type.1452 ({"type": bool, "flag_value": True}, [], False),1453 ({"type": bool, "flag_value": True}, ["--foo"], True),1454 ({"type": bool, "flag_value": False}, [], False),1455 ({"type": bool, "flag_value": False}, ["--foo"], False),1456 ({"type": bool, "flag_value": None}, [], False),1457 ({"type": bool, "flag_value": None}, ["--foo"], None),1458 ({"type": click.BOOL, "flag_value": True}, [], False),1459 ({"type": click.BOOL, "flag_value": True}, ["--foo"], True),1460 ({"type": click.BOOL, "flag_value": False}, [], False),1461 ({"type": click.BOOL, "flag_value": False}, ["--foo"], False),1462 ({"type": click.BOOL, "flag_value": None}, [], False),1463 ({"type": click.BOOL, "flag_value": None}, ["--foo"], None),1464 ({"type": str, "flag_value": True}, [], None),1465 ({"type": str, "flag_value": True}, ["--foo"], "True"),1466 ({"type": str, "flag_value": False}, [], None),1467 ({"type": str, "flag_value": False}, ["--foo"], "False"),1468 ({"type": str, "flag_value": "foo"}, [], None),1469 ({"type": str, "flag_value": "foo"}, ["--foo"], "foo"),1470 ({"type": str, "flag_value": None}, [], None),1471 ({"type": str, "flag_value": None}, ["--foo"], None),1472 # Not passing --foo returns the default value as-is, in its Python type, thenReconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 1473–1476why, one step back1473 # converted by the option type. For boolean flags, default=True is a literal1474 # value, not a sentinel meaning "activate flag". So it is NOT substituted with1475 # flag_value. See: https://github.com/pallets/click/issues/31111476 # https://github.com/pallets/click/pull/3239Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1477why, one step back1477 ({"type": bool, "default": True, "flag_value": True}, [], True),Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
line 1478why, one step back1478 ({"type": bool, "default": True, "flag_value": False}, [], True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1479–1514why, one step back1479 ({"type": bool, "default": False, "flag_value": True}, [], False),1480 ({"type": bool, "default": False, "flag_value": False}, [], False),1481 ({"type": bool, "default": None, "flag_value": True}, [], None),1482 ({"type": bool, "default": None, "flag_value": False}, [], None),1483 ({"type": str, "default": True, "flag_value": True}, [], "True"),1484 ({"type": str, "default": True, "flag_value": False}, [], "False"),1485 ({"type": str, "default": False, "flag_value": True}, [], "False"),1486 ({"type": str, "default": False, "flag_value": False}, [], "False"),1487 ({"type": str, "default": "foo", "flag_value": True}, [], "foo"),1488 ({"type": str, "default": "foo", "flag_value": False}, [], "foo"),1489 ({"type": str, "default": "foo", "flag_value": "bar"}, [], "foo"),1490 ({"type": str, "default": "foo", "flag_value": None}, [], "foo"),1491 ({"type": str, "default": None, "flag_value": True}, [], None),1492 ({"type": str, "default": None, "flag_value": False}, [], None),1493 # Passing --foo returns the flag_value that was explicitly set by the user,1494 # with its Python type, but still converted by the option type.1495 ({"type": bool, "default": True, "flag_value": True}, ["--foo"], True),1496 ({"type": bool, "default": True, "flag_value": False}, ["--foo"], False),1497 ({"type": bool, "default": False, "flag_value": True}, ["--foo"], True),1498 ({"type": bool, "default": False, "flag_value": False}, ["--foo"], False),1499 ({"type": bool, "default": None, "flag_value": True}, ["--foo"], True),1500 ({"type": bool, "default": None, "flag_value": False}, ["--foo"], False),1501 ({"type": str, "default": True, "flag_value": True}, ["--foo"], "True"),1502 ({"type": str, "default": True, "flag_value": False}, ["--foo"], "False"),1503 ({"type": str, "default": False, "flag_value": True}, ["--foo"], "True"),1504 ({"type": str, "default": False, "flag_value": False}, ["--foo"], "False"),1505 ({"type": str, "default": "foo", "flag_value": True}, ["--foo"], "True"),1506 ({"type": str, "default": "foo", "flag_value": False}, ["--foo"], "False"),1507 ({"type": str, "default": "foo", "flag_value": "bar"}, ["--foo"], "bar"),1508 ({"type": str, "default": "foo", "flag_value": None}, ["--foo"], None),1509 ({"type": str, "default": None, "flag_value": True}, ["--foo"], "True"),1510 ({"type": str, "default": None, "flag_value": False}, ["--foo"], "False"),1511 ],1512)1513def test_flag_value_and_default(runner, opt_params, args, expected):1514 @click.command()Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1515why, one step back1515 @click.option("--foo", is_flag=True, **opt_params)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1516–1533why, one step back1516 def cmd(foo):1517 click.echo(repr(foo), nl=False)15181519 result = runner.invoke(cmd, args)1520 assert result.output == repr(expected)152115221523@pytest.mark.parametrize(1524 ("args", "opts"),1525 [1526 ([], {"type": bool, "default": "foo"}),1527 ([], {"type": click.BOOL, "default": "foo"}),1528 (["--foo"], {"type": bool, "flag_value": "foo"}),1529 (["--foo"], {"type": click.BOOL, "flag_value": "foo"}),1530 ],1531)1532def test_invalid_flag_definition(runner, args, opts):1533 @click.command()Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
line 1534why, one step back1534 @click.option("--foo", is_flag=True, **opts)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1535–1539why, one step back1535 def cmd(foo):1536 click.echo(foo)15371538 result = runner.invoke(cmd, args)1539 assert (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1540why, one step back1540 "Error: Invalid value for '--foo': 'foo' is not a valid boolean"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1541–1586why, one step back1541 in result.output1542 )154315441545@pytest.mark.parametrize(1546 ("default", "args", "expected"),1547 # These test cases are similar to the ones in1548 # tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.1549 (1550 # Each option is returning its own flag_value, whatever the default is.1551 (True, ["--js"], "js"),1552 (True, ["--xml"], "xml"),1553 (False, ["--js"], "js"),1554 (False, ["--xml"], "xml"),1555 (None, ["--js"], "js"),1556 (None, ["--xml"], "xml"),1557 (UNSET, ["--js"], "js"),1558 (UNSET, ["--xml"], "xml"),1559 # Check that the last option wins when both are specified.1560 (True, ["--js", "--xml"], "xml"),1561 (True, ["--xml", "--js"], "js"),1562 # Check that the default is returned as-is when no option is specified.1563 ("js", [], "js"),1564 ("xml", [], "xml"),1565 ("jS", [], "jS"),1566 ("xMl", [], "xMl"),1567 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1568 (None, [], None),1569 # Special case: UNSET is not provided as-is to the callback, but normalized to1570 # None.1571 (UNSET, [], None),1572 # Special case: if default=True and flag_value is set, the value returned is the1573 # flag_value, not the True Python value itself.1574 (True, [], "js"),1575 # Non-string defaults are process as strings by the default Parameter's type.1576 (False, [], "False"),1577 (42, [], "42"),1578 (12.3, [], "12.3"),1579 ),1580)1581def test_default_dual_option_callback(runner, default, args, expected):1582 """Check how default is processed by the callback when options compete for the same1583 variable name.15841585 Reproduction of the issue reported in1586 https://github.com/pallets/click/pull/3030#discussion_r2271571819Add cross-reference to tests from `test_defaults.py` (#3223)Rowlando13before adoptionfa7f035
lines 1587–1592why, one step back15871588 .. hint::1589 Similar to ``test_basic.py::test_flag_value_dual_options``.15901591 ``test_defaults.py::test_shared_param_prefers_first_default``1592 is a smoke-test complement that exercises both default placements.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1593–1610why, one step back1593 """15941595 def _my_func(ctx, param, value):1596 # Print the value received by the callback as-is, so we can check for it.1597 return f"Callback value: {value!r}"15981599 @click.command()1600 @click.option("--js", "fmt", flag_value="js", callback=_my_func, default=default)1601 @click.option("--xml", "fmt", flag_value="xml", callback=_my_func)1602 def main(fmt):1603 click.secho(fmt, nl=False)16041605 result = runner.invoke(main, args)1606 assert result.output == f"Callback value: {expected!r}"1607 assert result.exit_code == 0160816091610@pytest.mark.parametrize(Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1611why, one step back1611 ("flag_value", "envvar_value", "expected"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1612–1613why, one step back1612 [1613 # The envvar match exactly the flag value and is case-sensitive.Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 1614–1616why, one step back1614 ("bar", "bar", "bar"),1615 ("BAR", "BAR", "BAR"),1616 (" bar ", " bar ", " bar "),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1617–1697why, one step back1617 ("42", "42", "42"),1618 ("None", "None", "None"),1619 ("True", "True", "True"),1620 ("False", "False", "False"),1621 ("true", "true", "true"),1622 ("false", "false", "false"),1623 ("A B", "A B", "A B"),1624 (" 1 2 ", " 1 2 ", " 1 2 "),1625 ("9.3", "9.3", "9.3"),1626 ("a;n", "a;n", "a;n"),1627 ("x:y", "x:y", "x:y"),1628 ("i/o", "i/o", "i/o"),1629 # Empty or absent envvar is consider unset, so the flag default value is1630 # returned, which is None in this case.1631 ("bar", "", None),1632 ("bar", None, None),1633 ("BAR", "", None),1634 ("BAR", None, None),1635 (" bar ", "", None),1636 (" bar ", None, None),1637 (42, "", None),1638 (42, None, None),1639 ("42", "", None),1640 ("42", None, None),1641 (None, "", None),1642 (None, None, None),1643 ("None", "", None),1644 ("None", None, None),1645 (True, "", None),1646 (True, None, None),1647 ("True", "", None),1648 ("True", None, None),1649 ("true", "", None),1650 ("true", None, None),1651 (False, "", None),1652 (False, None, None),1653 ("False", "", None),1654 ("False", None, None),1655 ("false", "", None),1656 ("false", None, None),1657 # Activate the flag with a value recognized as True by the envvar, which1658 # returns the flag_value.1659 ("bar", "True", "bar"),1660 ("bar", "true", "bar"),1661 ("bar", "trUe", "bar"),1662 ("bar", " TRUE ", "bar"),1663 ("bar", "1", "bar"),1664 ("bar", "yes", "bar"),1665 ("bar", "on", "bar"),1666 ("bar", "t", "bar"),1667 ("bar", "y", "bar"),1668 # Deactivating the flag with a value recognized as False by the envvar, which1669 # explicitly return the 'False' as the option is explicitly declared as a1670 # string.1671 ("bar", "False", "False"),1672 ("bar", "false", "False"),1673 ("bar", "faLse", "False"),1674 ("bar", " FALSE ", "False"),1675 ("bar", "0", "False"),1676 ("bar", "no", "False"),1677 ("bar", "off", "False"),1678 ("bar", "f", "False"),1679 ("bar", "n", "False"),1680 # Any other value than the flag_value, or a recogned True or False value, fails1681 # to explicitly activate or deactivate the flag. So the flag default value is1682 # returned, which is None in this case.1683 ("bar", " bar ", None),1684 ("bar", "BAR", None),1685 ("bar", "random", None),1686 ("bar", "bar random", None),1687 ("bar", "random bar", None),1688 ("BAR", "bar", None),1689 (" bar ", "bar", None),1690 (" bar ", "BAR", None),1691 (42, "42", None),1692 (42, "foo", None),1693 (None, "foo", None),1694 ("None", "foo", None),1695 ],1696)1697def test_envvar_string_flag_value(runner, flag_value, envvar_value, expected):Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1698why, one step back1698 """Ensure that flag_value is recognized by the envvar."""Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1699–1701why, one step back16991700 @click.command()1701 @click.option("--upper", type=str, flag_value=flag_value, envvar="UPPER")Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1702why, one step back1702 def cmd(upper):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1703–1704why, one step back1703 click.echo(repr(upper), nl=False)1704Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1705why, one step back1705 result = runner.invoke(cmd, env={"UPPER": envvar_value})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1706–1735why, one step back1706 assert result.exit_code == 01707 assert result.output == repr(expected)170817091710@pytest.mark.parametrize(1711 ("opt_decls", "opt_params", "expect_is_flag", "expect_is_bool_flag"),1712 [1713 # Not boolean flags.1714 ("-a", {"type": int}, False, False),1715 ("-a", {"type": bool}, False, False),1716 ("-a", {"default": True}, False, False),1717 ("-a", {"default": False}, False, False),1718 ("-a", {"flag_value": 1}, True, False),1719 # Boolean flags.1720 ("-a", {"is_flag": True}, True, True),1721 ("-a/-A", {}, True, True),1722 ("-a", {"flag_value": True}, True, True),1723 # Non-flag with flag_value.1724 ("-a", {"is_flag": False, "flag_value": 1}, False, False),1725 ],1726)1727def test_flag_auto_detection(1728 opt_decls, opt_params, expect_is_flag, expect_is_bool_flag1729):1730 option = Option([opt_decls], **opt_params)1731 assert option.is_flag is expect_is_flag1732 assert option.is_bool_flag is expect_is_bool_flag173317341735@pytest.mark.parametrize(Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1736why, one step back1736 ("kwargs", "message"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1737why, one step back1737 [Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1738–1739why, one step back1738 ({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),1739 ({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1740–1741why, one step back1740 ],1741)Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
lines 1742–1744why, one step back1742def test_invalid_flag_combinations(runner, kwargs, message):1743 with pytest.raises(TypeError) as e:1744 click.Option(["-a"], **kwargs)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1745why, one step back1745Bump actions/checkout from 3.5.2 to 3.5.3 (#2549)David Lordbefore adoptionb498906
line 1746why, one step back1746 assert message in str(e.value)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1747–1748why, one step back17471748Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
lines 1749–1752why, one step back1749def test_non_flag_with_non_negatable_default(runner):1750 class NonNegatable:1751 def __bool__(self):1752 raise ValueError("Cannot negate this object")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1753–1754why, one step back17531754 @click.command()Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
line 1755why, one step back1755 @click.option("--foo", default=NonNegatable())Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1756–1762why, one step back1756 def cmd(foo):1757 pass17581759 result = runner.invoke(cmd)1760 assert result.exit_code == 017611762Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1763why, one step back1763class HashType(enum.Enum):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1764–1768why, one step back1764 MD5 = "MD5"1765 SHA1 = "SHA1"1766 SHA256 = "SHA-256"17671768Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
lines 1769–1771why, one step back1769class Number(enum.IntEnum):1770 ONE = enum.auto()1771 TWO = enum.auto()Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1772–1773why, one step back17721773Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1774why, one step back1774class Letter(enum.StrEnum):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1775–1779why, one step back1775 NAME_1 = "Value-1"1776 NAME_2 = "Value_2"1777 NAME_3 = "42_value"17781779Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1780why, one step back1780class Color(enum.Flag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1781–1785why, one step back1781 RED = enum.auto()1782 GREEN = enum.auto()1783 BLUE = enum.auto()17841785Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1786why, one step back1786class ColorInt(enum.IntFlag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1787–1816why, one step back1787 RED = enum.auto()1788 GREEN = enum.auto()1789 BLUE = enum.auto()179017911792@pytest.mark.parametrize(1793 ("choices", "metavar"),1794 [1795 (["foo", "bar"], "[TEXT]"),1796 ([1, 2], "[INTEGER]"),1797 ([1.0, 2.0], "[FLOAT]"),1798 ([True, False], "[BOOLEAN]"),1799 (["foo", 1], "[TEXT|INTEGER]"),1800 (HashType, "[HASHTYPE]"),1801 (Number, "[NUMBER]"),1802 (Letter, "[LETTER]"),1803 (Color, "[COLOR]"),1804 (ColorInt, "[COLORINT]"),1805 ],1806)1807def test_choice_usage_rendering(runner, choices, metavar):1808 """BY default ``--help`` prints choice's values in the usage message.18091810 But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of1811 values.18121813 Also check that usage error message always suggests the actual values.1814 """18151816 @click.command()Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1817why, one step back1817 @click.option("-g", type=click.Choice(choices))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1818–1822why, one step back1818 def cli_with_choices(g):1819 pass18201821 @click.command()1822 @click.option("-g", type=click.Choice(choices), show_choices=False)Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1823why, one step back1823 def cli_without_choices(g):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1824–1836why, one step back1824 pass18251826 display_values = tuple(1827 i.name if isinstance(i, enum.Enum) else str(i) for i in choices1828 )18291830 # Check that the choices values are rendered as-is in the usage message.1831 result = runner.invoke(cli_with_choices, ["--help"])1832 assert f"[{'|'.join(display_values)}]" in result.stdout1833 assert not result.stderr1834 assert result.exit_code == 018351836 # Check that the metavar is rendered instead of the choices values themselves.Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1837why, one step back1837 result = runner.invoke(cli_without_choices, ["--help"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1838–1847why, one step back1838 assert metavar in result.stdout1839 assert not result.stderr1840 assert result.exit_code == 018411842 # Check the usage error message suggests the actual accepted values.1843 for cli in (cli_with_choices, cli_without_choices):1844 result = runner.invoke(cli, ["-g", "random"])1845 assert (1846 "\n\nError: Invalid value for '-g': 'random' is not one of "1847 f"{', '.join(map(repr, display_values))}.\n" in result.stderrtest: add two new tests for `Parameter.__init__` (recreation of #2861) (#3012)Rowlando13before adoptionc096a10
line 1848why, one step back1848 )Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1849–2203why, one step back1849 assert not result.stdout1850 assert result.exit_code == 2185118521853@pytest.mark.parametrize(1854 ("choices", "default", "default_string"),1855 [1856 (["foo", "bar"], "bar", "bar"),1857 # The default value is not enforced to be in the choices.1858 (["foo", "bar"], "random", "random"),1859 # None cannot be a default value as-is: it left the default value as unset.1860 (["foo", "bar"], None, None),1861 ([0, 1], 0, "0"),1862 # Values are not coerced to the type of the choice, even if equivalent.1863 ([0, 1], 0.0, "0.0"),1864 ([1, 2], 2, "2"),1865 ([1.0, 2.0], 2, "2"),1866 ([1.0, 2.0], 2.0, "2.0"),1867 ([True, False], True, "True"),1868 ([True, False], False, "False"),1869 (["foo", 1], "foo", "foo"),1870 (["foo", 1], 1, "1"),1871 # Enum choices are rendered as their names, not values.1872 # See: https://github.com/pallets/click/issues/29111873 (HashType, HashType.SHA1, "SHA1"),1874 # Enum choices allow defaults strings that are their names.1875 (HashType, HashType.SHA256, "SHA256"),1876 (HashType, "SHA256", "SHA256"),1877 (Number, Number.TWO, "TWO"),1878 (Number, "TWO", "TWO"),1879 (Letter, Letter.NAME_1, "NAME_1"),1880 (Letter, Letter.NAME_2, "NAME_2"),1881 (Letter, Letter.NAME_3, "NAME_3"),1882 (Letter, "NAME_1", "NAME_1"),1883 (Letter, "NAME_2", "NAME_2"),1884 (Letter, "NAME_3", "NAME_3"),1885 (Color, Color.GREEN, "GREEN"),1886 (Color, "GREEN", "GREEN"),1887 (ColorInt, ColorInt.GREEN, "GREEN"),1888 (ColorInt, "GREEN", "GREEN"),1889 ],1890)1891def test_choice_default_rendering(runner, choices, default, default_string):1892 @click.command()1893 @click.option("-g", type=click.Choice(choices), default=default, show_default=True)1894 def cli_with_choices(g):1895 pass18961897 # Check that the default value is kept normalized to the type of the choice.1898 assert cli_with_choices.params[0].default == default18991900 result = runner.invoke(cli_with_choices, ["--help"])1901 extra_usage = f"[default: {default_string}]"1902 if default_string is None:1903 assert extra_usage not in result.output1904 else:1905 assert extra_usage in result.output190619071908@pytest.mark.parametrize(1909 "opts_one,opts_two",1910 [1911 # No duplicate shortnames1912 (1913 ("-a", "--aardvark"),1914 ("-a", "--avocado"),1915 ),1916 # No duplicate long names1917 (1918 ("-a", "--aardvark"),1919 ("-b", "--aardvark"),1920 ),1921 ],1922)1923def test_duplicate_names_warning(runner, opts_one, opts_two):1924 @click.command()1925 @click.option(*opts_one)1926 @click.option(*opts_two)1927 def cli(one, two):1928 pass19291930 with pytest.warns(UserWarning):1931 runner.invoke(cli, [])193219331934OBJECT_SENTINEL = object()1935"""An object-based sentinel value."""193619371938class EnumSentinel(enum.Enum):1939 FALSY_SENTINEL = object()19401941 def __bool__(self) -> Literal[False]:1942 """Force the sentinel to be falsy to make sure it is not caught by Click1943 internal implementation.19441945 Falsy sentinels have been discussed in:1946 https://github.com/pallets/click/pull/3030#pullrequestreview-31066047951947 https://github.com/pallets/click/pull/3030#pullrequestreview-31084715521948 """1949 return False195019511952# Any kind of sentinel value is recognized by Click as a valid flag value.1953@pytest.mark.parametrize("sentinel", (OBJECT_SENTINEL, EnumSentinel.FALSY_SENTINEL))1954@pytest.mark.parametrize(1955 ("args", "expected"),1956 (1957 # Option default to None.1958 ([], None),1959 # Config has no default value, and no flag value, so it requires an argument.1960 (1961 ["--config"],1962 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1963 ),1964 (1965 ["--no-config", "--config"],1966 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1967 ),1968 # Passing --no-config defaults to the sentinel value because of the flag_value,1969 # and then the custom type receives that sentinel and returns a message.1970 (["--no-config"], "No configuration file provided."),1971 # Passing --config with an argument returns the file path.1972 (["--config", "foo.conf"], "foo.conf"),1973 # An argument is not allowed for --no-config, so it raises an error.1974 (1975 ["--no-config", "foo.conf"],1976 re.compile(1977 r"Usage: main \[OPTIONS\]\n"1978 r"Try 'main --help' for help.\n"1979 r"\n"1980 r"Error: Got unexpected extra argument (.+)\n"1981 ),1982 ),1983 # Passing --config with an argument that does not exist raises an error.1984 (1985 ["--config", "random-file.conf"],1986 re.compile(1987 r"Usage: main \[OPTIONS\]\n"1988 r"Try 'main --help' for help.\n"1989 r"\n"1990 r"Error: Invalid value for '-c' / '--config': "1991 r"File 'random-file.conf' does not exist.\n"1992 ),1993 ),1994 (1995 ["--config", "--no-config"],1996 re.compile(1997 r"Usage: main \[OPTIONS\]\n"1998 r"Try 'main --help' for help.\n"1999 r"\n"2000 r"Error: Invalid value for '-c' / '--config': "The first 2,000 of 2,575 lines.