Debian Codegraph @

integrations/click/tests/test_fig.py

raw symbols SCIP
1 import pytest
2 from click_complete_fig import fig
3 import click
4
5 ### Fixtures
6 @pytest.fixture
7 def grouped_spec():
8 return '''
9 // Autogenerated by click_complete_fig
10 const completionSpec: Fig.Spec = {
11 "name": "fig",
12 "options": [
13 {
14 "name": [
15 "-r",
16 "--root-option"
17 ],
18 "description": "Root option"
19 },
20 {
21 "name": [
22 "--help"
23 ],
24 "description": "Show this message and exit."
25 }
26 ],
27 "args": [
28 {
29 "name": "choice",
30 "suggestions": [
31 "A",
32 "B"
33 ]
34 }
35 ],
36 "subcommands": [
37 {
38 "name": "command",
39 "description": "Debug fig",
40 "deprecated": true,
41 "hidden": true,
42 "options": [
43 {
44 "name": [
45 "--help"
46 ],
47 "description": "Show this message and exit."
48 }
49 ]
50 },
51 {
52 "name": "command-with-args",
53 "options": [
54 {
55 "name": [
56 "--help"
57 ],
58 "description": "Show this message and exit."
59 }
60 ],
61 "args": [
62 {
63 "name": "text",
64 "isVariadic": true
65 },
66 {
67 "name": "text"
68 },
69 {
70 "name": "text"
71 },
72 {
73 "name": "filename",
74 "template": "filepaths"
75 },
76 {
77 "name": "text"
78 },
79 {
80 "name": "integer"
81 },
82 {
83 "name": "path",
84 "template": [
85 "folders",
86 "filepaths"
87 ],
88 "suggestCurrentToken": true
89 },
90 {
91 "name": "path",
92 "template": [
93 "folders",
94 "filepaths"
95 ]
96 }
97 ]
98 },
99 {
100 "name": "command-with-options",
101 "options": [
102 {
103 "name": [
104 "-s",
105 "--string-to-echo"
106 ],
107 "args": [
108 {
109 "name": "text"
110 }
111 ]
112 },
113 {
114 "name": [
115 "--multiple"
116 ],
117 "isRepeatable": true
118 },
119 {
120 "name": [
121 "-r"
122 ],
123 "description": "Deprecated and required option",
124 "isRequired": true
125 },
126 {
127 "name": [
128 "-c"
129 ],
130 "args": [
131 {
132 "name": "choice",
133 "suggestions": [
134 "A",
135 "B"
136 ]
137 }
138 ]
139 },
140 {
141 "name": [
142 "--arg-repeatable"
143 ],
144 "args": [
145 {
146 "name": "text"
147 },
148 {
149 "name": "text"
150 }
151 ]
152 },
153 {
154 "name": [
155 "--shout"
156 ],
157 "exclusiveOn": [
158 "--no-shout"
159 ]
160 },
161 {
162 "name": [
163 "--no-shout"
164 ],
165 "exclusiveOn": [
166 "--shout"
167 ]
168 },
169 {
170 "name": [
171 "--help"
172 ],
173 "description": "Show this message and exit."
174 }
175 ]
176 },
177 {
178 "name": "nested-command",
179 "description": "Nested",
180 "options": [
181 {
182 "name": [
183 "--help"
184 ],
185 "description": "Show this message and exit."
186 }
187 ],
188 "subcommands": [
189 {
190 "name": "double-nested-command",
191 "description": "Double nested command",
192 "options": [
193 {
194 "name": [
195 "--help"
196 ],
197 "description": "Show this message and exit."
198 }
199 ],
200 "args": [
201 {
202 "name": "text"
203 }
204 ]
205 }
206 ]
207 }
208 ]
209 }
210
211 export default completionSpec;
212 '''
213
214 @pytest.fixture
215 def non_grouped_cli_spec():
216 return '''
217 // Autogenerated by click_complete_fig
218 const completionSpec: Fig.Spec = {
219 "name": "non-grouped-cli",
220 "description": "Simple program that greets NAME for a total of COUNT times.",
221 "options": [
222 {
223 "name": [
224 "--count"
225 ],
226 "description": "Number of greetings.",
227 "args": [
228 {
229 "name": "integer"
230 }
231 ]
232 },
233 {
234 "name": [
235 "--name"
236 ],
237 "description": "The person to greet.",
238 "args": [
239 {
240 "name": "text"
241 }
242 ]
243 },
244 {
245 "name": [
246 "--help"
247 ],
248 "description": "Show this message and exit."
249 }
250 ]
251 }
252
253 export default completionSpec;
254 '''
255
256
257 @pytest.fixture
258 def add_completion_spec_command_to_non_grouped_cli_thrown_error():
259 return """
260 Cannot add the completion spec command to an object which is not a `click.Group` instance.
261 - If you are tring to add the command to a `click.Command` instance use `generate_completion_spec`
262 or convert `click.Command` to a `click.Group` instance.
263 - Useful links: https://click.palletsprojects.com/en/8.0.x/commands/#commands-and-groups
264 """
265 ### CLI Definitions
266
267 #### Grouped Spec
268 @click.group("fig")
269 @click.option('-r', '--root-option', help="Root option", is_flag=True)
270 @click.argument("choices", type=click.Choice(['A', 'B']))
271 def grouped_cli():
272 pass
273
274 @grouped_cli.command("command", help="Debug fig", short_help="Debug", deprecated=True, hidden=True)
275 def cmd():
276 pass
277
278 @grouped_cli.group("nested-command", short_help="Nested")
279 def nested_command():
280 pass
281
282 @nested_command.command("double-nested-command", help="Double nested command")
283 @click.argument("string")
284 def double_nested_command():
285 pass
286
287 @grouped_cli.command("command-with-options")
288 @click.option('-s', '--string-to-echo')
289 @click.option('--multiple', multiple=True, is_flag=True)
290 @click.option('-r', required=True, help="Deprecated and required option", is_flag=True)
291 @click.option('-c', type=click.Choice(['A', 'B']))
292 @click.option('--arg-repeatable', nargs=2)
293 @click.option('--shout/--no-shout', default=False)
294 def command_with_options():
295 pass
296
297 @grouped_cli.command("command-with-args")
298 @click.argument('string', nargs=-1)
299 @click.argument('int', nargs=2)
300 @click.argument('filename', type=click.File())
301 @click.argument('filename', type=click.Tuple([str, int]))
302 @click.argument('filename', type=click.Path(dir_okay=True, file_okay=True))
303 @click.argument('filename', type=click.Path(dir_okay=True, file_okay=True, exists=True))
304 def command_with_args():
305 pass
306
307 #### Non grouped Spec
308 @click.command()
309 @click.option('--count', default=1, help='Number of greetings.')
310 @click.option('--name', prompt='Your name',
311 help='The person to greet.')
312 def non_grouped_cli(count, name):
313 """Simple program that greets NAME for a total of COUNT times."""
314 for x in range(count):
315 click.echo(f"Hello {name}!")
316
317 ### Test cases
318 def test_grouped_cli_generate(grouped_spec):
319 assert fig.generate_completion_spec(grouped_cli) == grouped_spec
320
321 def test_non_grouped_cli_generate(non_grouped_cli_spec):
322 assert fig.generate_completion_spec(non_grouped_cli) == non_grouped_cli_spec
323
324 def test_add_completion_spec_command_to_grouped_cli():
325 try:
326 fig.add_completion_spec_command(grouped_cli)
327 except:
328 assert False, f"An exception was raised"
329
330 def test_add_completion_spec_command_to_non_grouped_cli(
331 add_completion_spec_command_to_non_grouped_cli_thrown_error
332 ):
333 with pytest.raises(SystemExit) as err:
334 fig.add_completion_spec_command(non_grouped_cli)
335 assert str(err.value) == add_completion_spec_command_to_non_grouped_cli_thrown_error