test_info_dict.py
This file
Path
tests/test_info_dict.py
345 lines · 10.1 KiB · Python
Last changed by
Merge stable into main (#3876)Rowlando13before adoption3cbaa76
History of this fileTake it
The walk stopped after 200 commits: lines older than 9ce34f2 point at it.
Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1–11why, one step back1import pytest23import click.types45# Common (obj, expect) pairs used to construct multiple tests.6STRING_PARAM_TYPE = (click.STRING, {"param_type": "String", "name": "text"})7INT_PARAM_TYPE = (click.INT, {"param_type": "Int", "name": "integer"})8BOOL_PARAM_TYPE = (click.BOOL, {"param_type": "Bool", "name": "boolean"})9HELP_OPTION = (10 None,11 {Fix parsing when a parameter is named `help` (#3678)Rowlando13before adoptionb67832c
line 12why, one step back12 "name": "_click_default_help",Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 13–42why, one step back13 "param_type_name": "option",14 "opts": ["--help"],15 "secondary_opts": [],16 "type": BOOL_PARAM_TYPE[1],17 "required": False,18 "nargs": 1,19 "multiple": False,20 "default": False,21 "envvar": None,22 "help": "Show this message and exit.",23 "prompt": None,24 "is_flag": True,25 "flag_value": True,26 "count": False,27 "hidden": False,28 },29)30NAME_ARGUMENT = (31 click.Argument(["name"]),32 {33 "name": "name",34 "param_type_name": "argument",35 "opts": ["name"],36 "secondary_opts": [],37 "type": STRING_PARAM_TYPE[1],38 "required": True,39 "nargs": 1,40 "multiple": False,41 "default": None,42 "envvar": None,Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
line 43why, one step back43 "help": None,Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 44–276why, one step back44 },45)46NUMBER_OPTION = (47 click.Option(["-c", "--count", "number"], default=1),48 {49 "name": "number",50 "param_type_name": "option",51 "opts": ["-c", "--count"],52 "secondary_opts": [],53 "type": INT_PARAM_TYPE[1],54 "required": False,55 "nargs": 1,56 "multiple": False,57 "default": 1,58 "envvar": None,59 "help": None,60 "prompt": None,61 "is_flag": False,62 "flag_value": None,63 "count": False,64 "hidden": False,65 },66)67HELLO_COMMAND = (68 click.Command("hello", params=[NUMBER_OPTION[0]]),69 {70 "name": "hello",71 "params": [NUMBER_OPTION[1], HELP_OPTION[1]],72 "help": None,73 "epilog": None,74 "short_help": None,75 "hidden": False,76 "deprecated": False,77 },78)79HELLO_GROUP = (80 click.Group("cli", [HELLO_COMMAND[0]]),81 {82 "name": "cli",83 "params": [HELP_OPTION[1]],84 "help": None,85 "epilog": None,86 "short_help": None,87 "hidden": False,88 "deprecated": False,89 "commands": {"hello": HELLO_COMMAND[1]},90 "chain": False,91 },92)939495@pytest.mark.parametrize(96 ("obj", "expect"),97 [98 pytest.param(99 click.types.FuncParamType(range),100 {"param_type": "Func", "name": "range", "func": range},101 id="Func ParamType",102 ),103 pytest.param(104 click.UNPROCESSED,105 {"param_type": "Unprocessed", "name": "text"},106 id="UNPROCESSED ParamType",107 ),108 pytest.param(*STRING_PARAM_TYPE, id="STRING ParamType"),109 pytest.param(110 click.Choice(("a", "b")),111 {112 "param_type": "Choice",113 "name": "choice",114 "choices": ("a", "b"),115 "case_sensitive": True,116 },117 id="Choice ParamType",118 ),119 pytest.param(120 click.DateTime(["%Y-%m-%d"]),121 {"param_type": "DateTime", "name": "datetime", "formats": ["%Y-%m-%d"]},122 id="DateTime ParamType",123 ),124 pytest.param(*INT_PARAM_TYPE, id="INT ParamType"),125 pytest.param(126 click.IntRange(0, 10, clamp=True),127 {128 "param_type": "IntRange",129 "name": "integer range",130 "min": 0,131 "max": 10,132 "min_open": False,133 "max_open": False,134 "clamp": True,135 },136 id="IntRange ParamType",137 ),138 pytest.param(139 click.FLOAT, {"param_type": "Float", "name": "float"}, id="FLOAT ParamType"140 ),141 pytest.param(142 click.FloatRange(-0.5, 0.5),143 {144 "param_type": "FloatRange",145 "name": "float range",146 "min": -0.5,147 "max": 0.5,148 "min_open": False,149 "max_open": False,150 "clamp": False,151 },152 id="FloatRange ParamType",153 ),154 pytest.param(*BOOL_PARAM_TYPE, id="Bool ParamType"),155 pytest.param(156 click.UUID, {"param_type": "UUID", "name": "uuid"}, id="UUID ParamType"157 ),158 pytest.param(159 click.File(),160 {"param_type": "File", "name": "filename", "mode": "r", "encoding": None},161 id="File ParamType",162 ),163 pytest.param(164 click.Path(),165 {166 "param_type": "Path",167 "name": "path",168 "exists": False,169 "file_okay": True,170 "dir_okay": True,171 "writable": False,172 "readable": True,173 "allow_dash": False,174 },175 id="Path ParamType",176 ),177 pytest.param(178 click.Tuple((click.STRING, click.INT)),179 {180 "param_type": "Tuple",181 "name": "<text integer>",182 "types": [STRING_PARAM_TYPE[1], INT_PARAM_TYPE[1]],183 },184 id="Tuple ParamType",185 ),186 pytest.param(*NUMBER_OPTION, id="Option"),187 pytest.param(188 click.Option(["--cache/--no-cache", "-c/-u"]),189 {190 "name": "cache",191 "param_type_name": "option",192 "opts": ["--cache", "-c"],193 "secondary_opts": ["--no-cache", "-u"],194 "type": BOOL_PARAM_TYPE[1],195 "required": False,196 "nargs": 1,197 "multiple": False,198 "default": False,199 "envvar": None,200 "help": None,201 "prompt": None,202 "is_flag": True,203 "flag_value": True,204 "count": False,205 "hidden": False,206 },207 id="Flag Option",208 ),209 pytest.param(*NAME_ARGUMENT, id="Argument"),210 ],211)212def test_parameter(obj, expect):213 out = obj.to_info_dict()214 assert out == expect215216217@pytest.mark.parametrize(218 ("obj", "expect"),219 [220 pytest.param(*HELLO_COMMAND, id="Command"),221 pytest.param(*HELLO_GROUP, id="Group"),222 pytest.param(223 click.Group(224 "base",225 [click.Command("test", params=[NAME_ARGUMENT[0]]), HELLO_GROUP[0]],226 ),227 {228 "name": "base",229 "params": [HELP_OPTION[1]],230 "help": None,231 "epilog": None,232 "short_help": None,233 "hidden": False,234 "deprecated": False,235 "commands": {236 "cli": HELLO_GROUP[1],237 "test": {238 "name": "test",239 "params": [NAME_ARGUMENT[1], HELP_OPTION[1]],240 "help": None,241 "epilog": None,242 "short_help": None,243 "hidden": False,244 "deprecated": False,245 },246 },247 "chain": False,248 },249 id="Nested Group",250 ),251 ],252)253def test_command(obj, expect):254 ctx = click.Context(obj)255 out = obj.to_info_dict(ctx)256 assert out == expect257258259def test_context():260 ctx = click.Context(HELLO_COMMAND[0])261 out = ctx.to_info_dict()262 assert out == {263 "command": HELLO_COMMAND[1],264 "info_name": None,265 "allow_extra_args": False,266 "allow_interspersed_args": True,267 "ignore_unknown_options": False,268 "auto_envvar_prefix": None,269 }270271272def test_paramtype_no_name():273 class TestType(click.ParamType):274 pass275276 assert TestType().to_info_dict()["name"] == "TestType"Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
lines 277–278why, one step back277278Merge stable into main (#3876)Rowlando13before adoption3cbaa76
lines 279–286why, one step back279PARAM_KINDS = [280 pytest.param(lambda **kwargs: click.Option(["--name"], **kwargs), id="option"),281 pytest.param(lambda **kwargs: click.Argument(["name"], **kwargs), id="argument"),282]283"""Both concrete kinds of parameter, built from the same keyword arguments."""284285286@pytest.mark.parametrize("make_param", PARAM_KINDS)Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
lines 287–299why, one step back287@pytest.mark.parametrize(288 ("help_in", "help_out"),289 [290 pytest.param(None, None, id="None"),291 pytest.param("", "", id="empty"),292 pytest.param("single line", "single line", id="single-line"),293 pytest.param(294 "\n first line\n second line\n ",295 "first line\nsecond line",296 id="multi-line",297 ),298 ],299)Merge stable into main (#3876)Rowlando13before adoption3cbaa76
lines 300–324why, one step back300def test_param_to_info_dict_help(make_param, help_in, help_out):301 """`Parameter` owns the help string, so every kind dedents it the same way."""302 assert make_param(help=help_in).to_info_dict()["help"] == help_out303304305@pytest.mark.parametrize("make_param", PARAM_KINDS)306@pytest.mark.parametrize(307 ("help_in", "deprecated", "help_out"),308 [309 pytest.param(None, True, "(DEPRECATED)", id="no-help"),310 pytest.param(311 "Pack the basket.", True, "Pack the basket. (DEPRECATED)", id="help"312 ),313 pytest.param(314 "Pack the basket.",315 "USE THE CRATE",316 "Pack the basket. (DEPRECATED: USE THE CRATE)",317 id="custom-label",318 ),319 ],320)321def test_param_to_info_dict_deprecated_help(make_param, help_in, deprecated, help_out):322 """A deprecated parameter of any kind gets the same label appended."""323 param = make_param(help=help_in, required=False, deprecated=deprecated)324 assert param.to_info_dict()["help"] == help_outAdd help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
lines 325–345why, one step back325326327def test_argument_to_info_dict_nargs():328 arg = click.Argument(["files"], nargs=-1, help="files to process")329 info = arg.to_info_dict()330 assert info["nargs"] == -1331 assert info["help"] == "files to process"332333334def test_command_to_info_dict_multiple_arguments():335 @click.command()336 @click.argument("src", help="source path")337 @click.argument("dst", help="destination path")338 def cli(src, dst):339 pass340341 ctx = click.Context(cli)342 params = cli.to_info_dict(ctx)["params"]343 args = [p for p in params if p["param_type_name"] == "argument"]344 assert [p["name"] for p in args] == ["src", "dst"]345 assert [p["help"] for p in args] == ["source path", "destination path"]