Debian Codegraph @

tests/test_info_dict.py

raw
1 import pytest
2
3 import click.types
4
5 # Common (obj, expect) pairs used to construct multiple tests.
6 STRING_PARAM_TYPE = (click.STRING, {"param_type": "String", "name": "text"})
7 INT_PARAM_TYPE = (click.INT, {"param_type": "Int", "name": "integer"})
8 BOOL_PARAM_TYPE = (click.BOOL, {"param_type": "Bool", "name": "boolean"})
9 HELP_OPTION = (
10 None,
11 {
12 "name": "help",
13 "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 )
30 NAME_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,
43 },
44 )
45 NUMBER_OPTION = (
46 click.Option(["-c", "--count", "number"], default=1),
47 {
48 "name": "number",
49 "param_type_name": "option",
50 "opts": ["-c", "--count"],
51 "secondary_opts": [],
52 "type": INT_PARAM_TYPE[1],
53 "required": False,
54 "nargs": 1,
55 "multiple": False,
56 "default": 1,
57 "envvar": None,
58 "help": None,
59 "prompt": None,
60 "is_flag": False,
61 "flag_value": None,
62 "count": False,
63 "hidden": False,
64 },
65 )
66 HELLO_COMMAND = (
67 click.Command("hello", params=[NUMBER_OPTION[0]]),
68 {
69 "name": "hello",
70 "params": [NUMBER_OPTION[1], HELP_OPTION[1]],
71 "help": None,
72 "epilog": None,
73 "short_help": None,
74 "hidden": False,
75 "deprecated": False,
76 },
77 )
78 HELLO_GROUP = (
79 click.Group("cli", [HELLO_COMMAND[0]]),
80 {
81 "name": "cli",
82 "params": [HELP_OPTION[1]],
83 "help": None,
84 "epilog": None,
85 "short_help": None,
86 "hidden": False,
87 "deprecated": False,
88 "commands": {"hello": HELLO_COMMAND[1]},
89 "chain": False,
90 },
91 )
92
93
94 @pytest.mark.parametrize(
95 ("obj", "expect"),
96 [
97 pytest.param(
98 click.types.FuncParamType(range),
99 {"param_type": "Func", "name": "range", "func": range},
100 id="Func ParamType",
101 ),
102 pytest.param(
103 click.UNPROCESSED,
104 {"param_type": "Unprocessed", "name": "text"},
105 id="UNPROCESSED ParamType",
106 ),
107 pytest.param(*STRING_PARAM_TYPE, id="STRING ParamType"),
108 pytest.param(
109 click.Choice(("a", "b")),
110 {
111 "param_type": "Choice",
112 "name": "choice",
113 "choices": ("a", "b"),
114 "case_sensitive": True,
115 },
116 id="Choice ParamType",
117 ),
118 pytest.param(
119 click.DateTime(["%Y-%m-%d"]),
120 {"param_type": "DateTime", "name": "datetime", "formats": ["%Y-%m-%d"]},
121 id="DateTime ParamType",
122 ),
123 pytest.param(*INT_PARAM_TYPE, id="INT ParamType"),
124 pytest.param(
125 click.IntRange(0, 10, clamp=True),
126 {
127 "param_type": "IntRange",
128 "name": "integer range",
129 "min": 0,
130 "max": 10,
131 "min_open": False,
132 "max_open": False,
133 "clamp": True,
134 },
135 id="IntRange ParamType",
136 ),
137 pytest.param(
138 click.FLOAT, {"param_type": "Float", "name": "float"}, id="FLOAT ParamType"
139 ),
140 pytest.param(
141 click.FloatRange(-0.5, 0.5),
142 {
143 "param_type": "FloatRange",
144 "name": "float range",
145 "min": -0.5,
146 "max": 0.5,
147 "min_open": False,
148 "max_open": False,
149 "clamp": False,
150 },
151 id="FloatRange ParamType",
152 ),
153 pytest.param(*BOOL_PARAM_TYPE, id="Bool ParamType"),
154 pytest.param(
155 click.UUID, {"param_type": "UUID", "name": "uuid"}, id="UUID ParamType"
156 ),
157 pytest.param(
158 click.File(),
159 {"param_type": "File", "name": "filename", "mode": "r", "encoding": None},
160 id="File ParamType",
161 ),
162 pytest.param(
163 click.Path(),
164 {
165 "param_type": "Path",
166 "name": "path",
167 "exists": False,
168 "file_okay": True,
169 "dir_okay": True,
170 "writable": False,
171 "readable": True,
172 "allow_dash": False,
173 },
174 id="Path ParamType",
175 ),
176 pytest.param(
177 click.Tuple((click.STRING, click.INT)),
178 {
179 "param_type": "Tuple",
180 "name": "<text integer>",
181 "types": [STRING_PARAM_TYPE[1], INT_PARAM_TYPE[1]],
182 },
183 id="Tuple ParamType",
184 ),
185 pytest.param(*NUMBER_OPTION, id="Option"),
186 pytest.param(
187 click.Option(["--cache/--no-cache", "-c/-u"]),
188 {
189 "name": "cache",
190 "param_type_name": "option",
191 "opts": ["--cache", "-c"],
192 "secondary_opts": ["--no-cache", "-u"],
193 "type": BOOL_PARAM_TYPE[1],
194 "required": False,
195 "nargs": 1,
196 "multiple": False,
197 "default": False,
198 "envvar": None,
199 "help": None,
200 "prompt": None,
201 "is_flag": True,
202 "flag_value": True,
203 "count": False,
204 "hidden": False,
205 },
206 id="Flag Option",
207 ),
208 pytest.param(*NAME_ARGUMENT, id="Argument"),
209 ],
210 )
211 def test_parameter(obj, expect):
212 out = obj.to_info_dict()
213 assert out == expect
214
215
216 @pytest.mark.parametrize(
217 ("obj", "expect"),
218 [
219 pytest.param(*HELLO_COMMAND, id="Command"),
220 pytest.param(*HELLO_GROUP, id="Group"),
221 pytest.param(
222 click.Group(
223 "base",
224 [click.Command("test", params=[NAME_ARGUMENT[0]]), HELLO_GROUP[0]],
225 ),
226 {
227 "name": "base",
228 "params": [HELP_OPTION[1]],
229 "help": None,
230 "epilog": None,
231 "short_help": None,
232 "hidden": False,
233 "deprecated": False,
234 "commands": {
235 "cli": HELLO_GROUP[1],
236 "test": {
237 "name": "test",
238 "params": [NAME_ARGUMENT[1], HELP_OPTION[1]],
239 "help": None,
240 "epilog": None,
241 "short_help": None,
242 "hidden": False,
243 "deprecated": False,
244 },
245 },
246 "chain": False,
247 },
248 id="Nested Group",
249 ),
250 ],
251 )
252 def test_command(obj, expect):
253 ctx = click.Context(obj)
254 out = obj.to_info_dict(ctx)
255 assert out == expect
256
257
258 def test_context():
259 ctx = click.Context(HELLO_COMMAND[0])
260 out = ctx.to_info_dict()
261 assert out == {
262 "command": HELLO_COMMAND[1],
263 "info_name": None,
264 "allow_extra_args": False,
265 "allow_interspersed_args": True,
266 "ignore_unknown_options": False,
267 "auto_envvar_prefix": None,
268 }
269
270
271 def test_paramtype_no_name():
272 class TestType(click.ParamType):
273 pass
274
275 assert TestType().to_info_dict()["name"] == "TestType"