Debian Codegraph @

tests/test_enum_flag.py

raw symbols SCIP
1 """Tests for enum.Flag and enum.IntFlag support in cyclopts."""
2
3 from dataclasses import dataclass
4 from enum import Flag, IntFlag, auto
5 from textwrap import dedent
6 from typing import Annotated
7
8 import pytest
9
10 from cyclopts import MissingArgumentError, Parameter
11 from cyclopts.exceptions import CoercionError, UnknownOptionError
12
13
14 class Permission(Flag):
15 """File permissions as bit flags."""
16
17 READ = auto()
18 """Enable read permissions."""
19 WRITE = auto()
20 """Enable write permissions."""
21 EXECUTE = auto()
22 """Enable execute permissions."""
23
24
25 class HttpStatus(IntFlag):
26 INFORMATIONAL = 100
27 SUCCESS = 200
28 REDIRECTION = 300
29 CLIENT_ERROR = 400
30 SERVER_ERROR = 500
31
32
33 @dataclass
34 class User:
35 name: str
36 perms: Permission = Permission.READ
37
38
39 def test_flag_as_boolean_flags(app, assert_parse_args):
40 """Test that Flag enum members are exposed as boolean CLI flags."""
41
42 @app.default
43 def main(perms: Permission = Permission.READ):
44 pass
45
46 assert_parse_args(main, "--perms.read", perms=Permission.READ)
47 assert_parse_args(main, "--perms.write", perms=Permission.WRITE)
48 assert_parse_args(main, "--perms.read --perms.write", perms=Permission.READ | Permission.WRITE)
49
50
51 def test_int_flag_as_boolean_flags(app, assert_parse_args):
52 """Test that IntFlag enum members are exposed as boolean CLI flags."""
53
54 @app.default
55 def main(error_on: HttpStatus | None = None):
56 pass
57
58 assert_parse_args(main, "")
59 assert_parse_args(main, "--error-on.success", error_on=HttpStatus.SUCCESS)
60
61
62 @pytest.mark.parametrize(
63 "command",
64 ["--perms read --perms write", "read write"],
65 )
66 def test_flag_with_list_str_input(app, assert_parse_args, command):
67 """Test that Flag enum members are exposed as boolean CLI flags."""
68
69 @app.default
70 def main(perms: Permission = Permission.READ):
71 pass
72
73 assert_parse_args(main, command, Permission.READ | Permission.WRITE)
74
75
76 def test_flag_unknown_member_str(app):
77 @app.default
78 def main(perms: Permission = Permission.READ):
79 pass
80
81 with pytest.raises(CoercionError):
82 app("foo", exit_on_error=False)
83
84
85 def test_flag_unknown_member_option(app):
86 @app.default
87 def main(perms: Permission = Permission.READ):
88 pass
89
90 with pytest.raises(UnknownOptionError):
91 app("--perms.foo", exit_on_error=False)
92
93
94 def test_flag_as_boolean_flags_star_name(app, assert_parse_args, console):
95 """Test that Flag enum members are exposed as boolean CLI flags."""
96
97 @app.default
98 def main(perms: Annotated[Permission, Parameter(name="*", negative_bool="")] = Permission.READ):
99 """Manage file permissions."""
100 pass
101
102 # Test individual flags
103 assert_parse_args(main, "--read", Permission.READ)
104 assert_parse_args(main, "--write", Permission.WRITE)
105 assert_parse_args(main, "--execute", Permission.EXECUTE)
106
107 # Test multiple flags combined
108 assert_parse_args(main, "--read --write", Permission.READ | Permission.WRITE)
109 assert_parse_args(main, "--read --write --execute", Permission.READ | Permission.WRITE | Permission.EXECUTE)
110
111 # Test help output
112 with console.capture() as capture:
113 app.help_print([], console=console)
114
115 actual = capture.get()
116 expected = dedent(
117 """\
118 Usage: test_enum_flag [OPTIONS]
119
120 Manage file permissions.
121
122 ╭─ Commands ─────────────────────────────────────────────────────────╮
123 │ --help (-h) Display this message and exit. │
124 │ --version Display application version. │
125 ╰────────────────────────────────────────────────────────────────────╯
126 ╭─ Parameters ───────────────────────────────────────────────────────╮
127 │ --read Enable read permissions. [default: False] │
128 │ --write Enable write permissions. [default: False] │
129 │ --execute Enable execute permissions. [default: False] │
130 ╰────────────────────────────────────────────────────────────────────╯
131 """
132 )
133 assert expected == actual
134
135
136 def test_flag_with_default_value(app, assert_parse_args):
137 """Test Flag enum with non-zero default value."""
138
139 @app.default
140 def main(perms: Permission = Permission.READ | Permission.WRITE):
141 pass
142
143 assert_parse_args(main, "")
144
145
146 def test_flag_with_other_parameters(app, assert_parse_args):
147 """Test Flag enum alongside other parameters."""
148
149 @app.default
150 def main(name: str, perms: Permission = Permission.READ, verbose: bool = False):
151 pass
152
153 # Test with positional and flag parameters
154 assert_parse_args(main, "test.txt --perms.read --perms.write", "test.txt", Permission.READ | Permission.WRITE)
155
156 # Test with all parameter types
157 assert_parse_args(main, "test.txt --perms.execute --verbose", "test.txt", Permission.EXECUTE, True)
158
159
160 def test_flag_star_name_with_other_parameters(app, assert_parse_args):
161 """Test Flag enum with star name alongside other parameters."""
162
163 @app.default
164 def main(
165 name: str,
166 perms: Annotated[Permission, Parameter(name="*")] = Permission.READ,
167 verbose: bool = False,
168 ):
169 pass
170
171 # Test star name expansion with other parameters
172 assert_parse_args(main, "test.txt --read --write --verbose", "test.txt", Permission.READ | Permission.WRITE, True)
173
174
175 def test_flag_no_flags_provided(app):
176 """Test behavior when no flags are provided but parameter is required."""
177
178 @app.default
179 def main(perms: Permission): # No default value
180 pass
181
182 with pytest.raises(MissingArgumentError):
183 app([], exit_on_error=False)
184
185
186 def test_flag_help_shows_member_docstrings(app, console):
187 """Test that Flag enum member docstrings appear in help output."""
188
189 @app.default
190 def main(perms: Permission = Permission.READ):
191 """Manage file permissions."""
192 pass
193
194 with console.capture() as capture:
195 app.help_print([], console=console)
196
197 actual = capture.get()
198
199 expected = dedent(
200 """\
201 Usage: test_enum_flag [OPTIONS]
202
203 Manage file permissions.
204
205 ╭─ Commands ─────────────────────────────────────────────────────────╮
206 │ --help (-h) Display this message and exit. │
207 │ --version Display application version. │
208 ╰────────────────────────────────────────────────────────────────────╯
209 ╭─ Parameters ───────────────────────────────────────────────────────╮
210 │ --perms.read Enable read permissions. [default: False] │
211 │ --perms.no-read │
212 │ --perms.write Enable write permissions. [default: False] │
213 │ --perms.no-write │
214 │ --perms.execute Enable execute permissions. [default: False] │
215 │ --perms.no-execute │
216 ╰────────────────────────────────────────────────────────────────────╯
217 """
218 )
219 assert expected == actual
220
221
222 def test_flag_help_star_name_shows_member_docstrings(app, console):
223 """Test that Flag enum member docstrings appear with star name expansion."""
224
225 @app.default
226 def main(perms: Annotated[Permission, Parameter(name="*")] = Permission.READ):
227 """Manage file permissions."""
228 pass
229
230 with console.capture() as capture:
231 app.help_print([], console=console)
232
233 actual = capture.get()
234 expected = dedent(
235 """\
236 Usage: test_enum_flag [OPTIONS]
237
238 Manage file permissions.
239
240 ╭─ Commands ─────────────────────────────────────────────────────────╮
241 │ --help (-h) Display this message and exit. │
242 │ --version Display application version. │
243 ╰────────────────────────────────────────────────────────────────────╯
244 ╭─ Parameters ───────────────────────────────────────────────────────╮
245 │ --read --no-read Enable read permissions. [default: False] │
246 │ --write --no-write Enable write permissions. [default: False] │
247 │ --execute --no-execute Enable execute permissions. [default: │
248 │ False] │
249 ╰────────────────────────────────────────────────────────────────────╯
250 """
251 )
252
253 assert expected == actual
254
255
256 def test_flag_in_dataclass(app, assert_parse_args):
257 """Test Flag enum field in a dataclass."""
258
259 @app.default
260 def main(user: User):
261 pass
262
263 # Test with name only (should use default perms)
264 assert_parse_args(main, "Alice", User("Alice", Permission.READ))
265
266 # Test with name and flag permissions
267 assert_parse_args(
268 main, "Bob --user.perms.write --user.perms.execute", User("Bob", Permission.WRITE | Permission.EXECUTE)
269 )
270
271
272 def test_flag_in_dataclass_positionally(app, assert_parse_args):
273 """Test Flag enum field positionally in a dataclass."""
274
275 @app.default
276 def main(user: User):
277 pass
278
279 assert_parse_args(main, "Bob write execute", User("Bob", Permission.WRITE | Permission.EXECUTE))
280
281
282 def test_flag_in_dataclass_help(app, console):
283 """Test help output for dataclass with Flag enum field."""
284
285 @app.default
286 def main(user: User):
287 """Create a user with permissions."""
288 pass
289
290 with console.capture() as capture:
291 app.help_print([], console=console)
292
293 actual = capture.get()
294 expected = dedent(
295 """\
296 Usage: test_enum_flag [OPTIONS] USER.NAME
297
298 Create a user with permissions.
299
300 ╭─ Commands ─────────────────────────────────────────────────────────╮
301 │ --help (-h) Display this message and exit. │
302 │ --version Display application version. │
303 ╰────────────────────────────────────────────────────────────────────╯
304 ╭─ Parameters ───────────────────────────────────────────────────────╮
305 │ * USER.NAME --user.name [required] │
306 │ --user.perms.read Enable read permissions. [default: │
307 │ --user.perms.no-read False] │
308 │ --user.perms.write Enable write permissions. [default: │
309 │ --user.perms.no-write False] │
310 │ --user.perms.execute Enable execute permissions. │
311 │ --user.perms.no-execute [default: False] │
312 ╰────────────────────────────────────────────────────────────────────╯
313 """
314 )
315
316 assert expected == actual
317
318
319 def test_flag_in_dataclass_help_no_negative(app, console):
320 """Test help output for dataclass with Flag enum field."""
321
322 @app.default
323 def main(user: Annotated[User, Parameter(negative="")]):
324 """Create a user with permissions."""
325 pass
326
327 with console.capture() as capture:
328 app.help_print([], console=console)
329
330 actual = capture.get()
331 expected = dedent(
332 """\
333 Usage: test_enum_flag [OPTIONS] USER.NAME
334
335 Create a user with permissions.
336
337 ╭─ Commands ─────────────────────────────────────────────────────────╮
338 │ --help (-h) Display this message and exit. │
339 │ --version Display application version. │
340 ╰────────────────────────────────────────────────────────────────────╯
341 ╭─ Parameters ───────────────────────────────────────────────────────╮
342 │ * USER.NAME --user.name [required] │
343 │ --user.perms.read Enable read permissions. [default: │
344 │ False] │
345 │ --user.perms.write Enable write permissions. [default: │
346 │ False] │
347 │ --user.perms.execute Enable execute permissions. [default: │
348 │ False] │
349 ╰────────────────────────────────────────────────────────────────────╯
350 """
351 )
352
353 assert expected == actual
354
355
356 def test_flag_in_dataclass_help_no_keywords(app, assert_parse_args, console):
357 """Test help output for dataclass with Flag enum field."""
358
359 @dataclass
360 class User:
361 name: str
362 perms: Annotated[Permission, Parameter(accepts_keys=False)] = Permission.READ
363
364 @app.default
365 def main(user: Annotated[User, Parameter(negative="")]):
366 """Create a user with permissions."""
367 pass
368
369 with console.capture() as capture:
370 app.help_print([], console=console)
371
372 actual = capture.get()
373 expected = dedent(
374 """\
375 Usage: test_enum_flag USER.NAME [ARGS]
376
377 Create a user with permissions.
378
379 ╭─ Commands ─────────────────────────────────────────────────────────╮
380 │ --help (-h) Display this message and exit. │
381 │ --version Display application version. │
382 ╰────────────────────────────────────────────────────────────────────╯
383 ╭─ Parameters ───────────────────────────────────────────────────────╮
384 │ * USER.NAME --user.name [required] │
385 │ USER.PERMS --user.perms [choices: read, write, execute] │
386 │ [default: read] │
387 ╰────────────────────────────────────────────────────────────────────╯
388 """
389 )
390
391 assert expected == actual
392
393 assert_parse_args(main, "Bob read write", User("Bob", perms=Permission.READ | Permission.WRITE))