Debian Codegraph @

tests/test_docs_snapshots.py

raw symbols SCIP
1 """Snapshot tests for documentation generation.
2
3 These tests capture the generated markdown and RST output to detect
4 unintended changes to the documentation plugins.
5 """
6
7 import sys
8 from pathlib import Path
9
10 import pytest
11
12 # Skip all snapshot tests on Windows due to platform-specific output differences
13 pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="Snapshot tests not supported on Windows")
14
15 # Path to the complex-demo application
16 COMPLEX_DEMO_DIR = Path(__file__).parent / "apps" / "complex-demo"
17
18
19 @pytest.fixture
20 def ensure_complex_demo_importable():
21 """Ensure the complex_app module can be imported."""
22 sys.path.insert(0, str(COMPLEX_DEMO_DIR))
23 yield
24 sys.path.remove(str(COMPLEX_DEMO_DIR))
25 # Clean up any cached imports
26 modules_to_remove = [k for k in sys.modules.keys() if k.startswith("complex_app")]
27 for mod in modules_to_remove:
28 del sys.modules[mod]
29
30
31 class TestMarkdownSnapshots:
32 """Snapshot tests for markdown generation."""
33
34 def test_full_app_markdown(self, ensure_complex_demo_importable, md_snapshot):
35 """Snapshot the full app markdown output."""
36 from complex_app import app
37
38 from cyclopts.docs.markdown import generate_markdown_docs
39
40 markdown = generate_markdown_docs(
41 app,
42 recursive=True,
43 include_hidden=False,
44 heading_level=1,
45 generate_toc=True,
46 )
47
48 assert markdown == md_snapshot
49
50 def test_admin_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
51 """Snapshot markdown for admin commands (tests deep nesting)."""
52 from complex_app import app
53
54 from cyclopts.docs.markdown import generate_markdown_docs
55
56 markdown = generate_markdown_docs(
57 app,
58 recursive=True,
59 include_hidden=False,
60 heading_level=2,
61 generate_toc=False,
62 commands_filter=["admin"],
63 )
64
65 assert markdown == md_snapshot
66
67 def test_nested_permissions_markdown(self, ensure_complex_demo_importable, md_snapshot):
68 """Snapshot markdown for deeply nested permissions commands."""
69 from complex_app import app
70
71 from cyclopts.docs.markdown import generate_markdown_docs
72
73 markdown = generate_markdown_docs(
74 app,
75 recursive=True,
76 include_hidden=False,
77 heading_level=2,
78 generate_toc=False,
79 commands_filter=["admin.users.permissions"],
80 )
81
82 assert markdown == md_snapshot
83
84 def test_data_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
85 """Snapshot markdown for data commands (tests dataclass flattening)."""
86 from complex_app import app
87
88 from cyclopts.docs.markdown import generate_markdown_docs
89
90 markdown = generate_markdown_docs(
91 app,
92 recursive=True,
93 include_hidden=False,
94 heading_level=2,
95 generate_toc=False,
96 commands_filter=["data"],
97 )
98
99 assert markdown == md_snapshot
100
101 def test_flattened_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
102 """Snapshot markdown with flatten_commands=True."""
103 from complex_app import app
104
105 from cyclopts.docs.markdown import generate_markdown_docs
106
107 markdown = generate_markdown_docs(
108 app,
109 recursive=True,
110 include_hidden=False,
111 heading_level=2,
112 generate_toc=False,
113 flatten_commands=True,
114 commands_filter=["admin.users"], # Limit scope for readable snapshot
115 )
116
117 assert markdown == md_snapshot
118
119 def test_hidden_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
120 """Snapshot markdown with include_hidden=True."""
121 from complex_app import app
122
123 from cyclopts.docs.markdown import generate_markdown_docs
124
125 markdown = generate_markdown_docs(
126 app,
127 recursive=False, # Just top-level to keep snapshot small
128 include_hidden=True,
129 heading_level=2,
130 generate_toc=False,
131 )
132
133 assert markdown == md_snapshot
134
135 def test_exclude_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
136 """Snapshot markdown with exclude_commands."""
137 from complex_app import app
138
139 from cyclopts.docs.markdown import generate_markdown_docs
140
141 markdown = generate_markdown_docs(
142 app,
143 recursive=True,
144 include_hidden=False,
145 heading_level=2,
146 generate_toc=False,
147 commands_filter=["admin"],
148 exclude_commands=["admin.users.permissions"],
149 )
150
151 assert markdown == md_snapshot
152
153 def test_server_commands_markdown(self, ensure_complex_demo_importable, md_snapshot):
154 """Snapshot markdown for server commands (tests Pydantic if available)."""
155 from complex_app import app
156
157 from cyclopts.docs.markdown import generate_markdown_docs
158
159 markdown = generate_markdown_docs(
160 app,
161 recursive=True,
162 include_hidden=False,
163 heading_level=2,
164 generate_toc=False,
165 commands_filter=["server"],
166 )
167
168 assert markdown == md_snapshot
169
170 def test_utilities_markdown(self, ensure_complex_demo_importable, md_snapshot):
171 """Snapshot markdown for utility commands (tests enums, complex types)."""
172 from complex_app import app
173
174 from cyclopts.docs.markdown import generate_markdown_docs
175
176 markdown = generate_markdown_docs(
177 app,
178 recursive=True,
179 include_hidden=False,
180 heading_level=2,
181 generate_toc=False,
182 commands_filter=["cache", "complex-types", "version", "info"],
183 )
184
185 assert markdown == md_snapshot
186
187
188 class TestRstSnapshots:
189 """Snapshot tests for RST generation."""
190
191 def test_full_app_rst(self, ensure_complex_demo_importable, rst_snapshot):
192 """Snapshot the full app RST output."""
193 from complex_app import app
194
195 from cyclopts.docs.rst import generate_rst_docs
196
197 rst = generate_rst_docs(
198 app,
199 recursive=True,
200 include_hidden=False,
201 heading_level=1,
202 )
203
204 assert rst == rst_snapshot
205
206 def test_admin_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
207 """Snapshot RST for admin commands (tests deep nesting)."""
208 from complex_app import app
209
210 from cyclopts.docs.rst import generate_rst_docs
211
212 rst = generate_rst_docs(
213 app,
214 recursive=True,
215 include_hidden=False,
216 heading_level=2,
217 commands_filter=["admin"],
218 )
219
220 assert rst == rst_snapshot
221
222 def test_nested_permissions_rst(self, ensure_complex_demo_importable, rst_snapshot):
223 """Snapshot RST for deeply nested permissions commands."""
224 from complex_app import app
225
226 from cyclopts.docs.rst import generate_rst_docs
227
228 rst = generate_rst_docs(
229 app,
230 recursive=True,
231 include_hidden=False,
232 heading_level=2,
233 commands_filter=["admin.users.permissions"],
234 )
235
236 assert rst == rst_snapshot
237
238 def test_data_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
239 """Snapshot RST for data commands (tests dataclass flattening)."""
240 from complex_app import app
241
242 from cyclopts.docs.rst import generate_rst_docs
243
244 rst = generate_rst_docs(
245 app,
246 recursive=True,
247 include_hidden=False,
248 heading_level=2,
249 commands_filter=["data"],
250 )
251
252 assert rst == rst_snapshot
253
254 def test_flattened_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
255 """Snapshot RST with flatten_commands=True."""
256 from complex_app import app
257
258 from cyclopts.docs.rst import generate_rst_docs
259
260 rst = generate_rst_docs(
261 app,
262 recursive=True,
263 include_hidden=False,
264 heading_level=2,
265 flatten_commands=True,
266 commands_filter=["admin.users"], # Limit scope for readable snapshot
267 )
268
269 assert rst == rst_snapshot
270
271 def test_hidden_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
272 """Snapshot RST with include_hidden=True."""
273 from complex_app import app
274
275 from cyclopts.docs.rst import generate_rst_docs
276
277 rst = generate_rst_docs(
278 app,
279 recursive=False, # Just top-level to keep snapshot small
280 include_hidden=True,
281 heading_level=2,
282 )
283
284 assert rst == rst_snapshot
285
286 def test_server_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
287 """Snapshot RST for server commands (tests Pydantic if available)."""
288 from complex_app import app
289
290 from cyclopts.docs.rst import generate_rst_docs
291
292 rst = generate_rst_docs(
293 app,
294 recursive=True,
295 include_hidden=False,
296 heading_level=2,
297 commands_filter=["server"],
298 )
299
300 assert rst == rst_snapshot
301
302
303 class TestMkDocsDirectiveSnapshots:
304 """Snapshot tests for MkDocs directive processing."""
305
306 def test_simple_directive_output(self, ensure_complex_demo_importable, md_snapshot):
307 """Snapshot the output of a simple directive."""
308 import textwrap
309
310 from cyclopts.ext.mkdocs import process_cyclopts_directives
311
312 markdown = textwrap.dedent(
313 """\
314 # CLI Reference
315
316 ::: cyclopts
317 module: complex_app:app
318 heading_level: 2
319 recursive: false
320 generate_toc: false
321 """
322 )
323
324 result = process_cyclopts_directives(markdown, None)
325 assert result == md_snapshot
326
327 def test_filtered_directive_output(self, ensure_complex_demo_importable, md_snapshot):
328 """Snapshot the output of a filtered directive."""
329 import textwrap
330
331 from cyclopts.ext.mkdocs import process_cyclopts_directives
332
333 markdown = textwrap.dedent(
334 """\
335 # Admin Commands
336
337 ::: cyclopts
338 module: complex_app:app
339 heading_level: 2
340 recursive: true
341 commands: [admin.users]
342 generate_toc: false
343 """
344 )
345
346 result = process_cyclopts_directives(markdown, None)
347 assert result == md_snapshot
348
349 def test_nested_directive_output(self, ensure_complex_demo_importable, md_snapshot):
350 """Snapshot the output for deeply nested commands."""
351 import textwrap
352
353 from cyclopts.ext.mkdocs import process_cyclopts_directives
354
355 markdown = textwrap.dedent(
356 """\
357 # Permissions
358
359 ::: cyclopts
360 module: complex_app:app
361 heading_level: 3
362 recursive: true
363 commands: [admin.users.permissions.roles]
364 generate_toc: false
365 """
366 )
367
368 result = process_cyclopts_directives(markdown, None)
369 assert result == md_snapshot
370
371 def test_multiple_directives_output(self, ensure_complex_demo_importable, md_snapshot):
372 """Snapshot the output of multiple directives on one page."""
373 import textwrap
374
375 from cyclopts.ext.mkdocs import process_cyclopts_directives
376
377 markdown = textwrap.dedent(
378 """\
379 # CLI Reference
380
381 ## Data Commands
382
383 ::: cyclopts
384 module: complex_app:app
385 heading_level: 3
386 commands: [data]
387 generate_toc: false
388
389 ## Server Commands
390
391 ::: cyclopts
392 module: complex_app:app
393 heading_level: 3
394 commands: [server]
395 generate_toc: false
396 """
397 )
398
399 result = process_cyclopts_directives(markdown, None)
400 assert result == md_snapshot
401
402
403 class TestSphinxDirectiveSnapshots:
404 """Snapshot tests for Sphinx directive output."""
405
406 def test_simple_directive_rst(self, ensure_complex_demo_importable, rst_snapshot):
407 """Snapshot the RST output for a simple directive."""
408 from unittest.mock import MagicMock
409
410 from docutils.statemachine import StringList
411
412 from cyclopts.ext.sphinx import CycloptsDirective
413
414 mock_state = MagicMock()
415 captured_content = []
416
417 def capture_nested_parse(string_list, offset, parent):
418 captured_content.extend(string_list)
419
420 mock_state.nested_parse = capture_nested_parse
421
422 directive = CycloptsDirective(
423 name="cyclopts",
424 arguments=["complex_app:app"],
425 options={"heading-level": 2, "recursive": False},
426 content=StringList(),
427 lineno=1,
428 content_offset=0,
429 block_text="",
430 state=mock_state,
431 state_machine=MagicMock(),
432 )
433
434 directive.run()
435 rst_output = "\n".join(captured_content)
436 assert rst_output == rst_snapshot
437
438 def test_filtered_directive_rst(self, ensure_complex_demo_importable, rst_snapshot):
439 """Snapshot the RST output for a filtered directive."""
440 from unittest.mock import MagicMock
441
442 from docutils.statemachine import StringList
443
444 from cyclopts.ext.sphinx import CycloptsDirective
445
446 mock_state = MagicMock()
447 captured_content = []
448
449 def capture_nested_parse(string_list, offset, parent):
450 captured_content.extend(string_list)
451
452 mock_state.nested_parse = capture_nested_parse
453
454 directive = CycloptsDirective(
455 name="cyclopts",
456 arguments=["complex_app:app"],
457 options={"heading-level": 2, "recursive": True, "commands": "admin.users"},
458 content=StringList(),
459 lineno=1,
460 content_offset=0,
461 block_text="",
462 state=mock_state,
463 state_machine=MagicMock(),
464 )
465
466 directive.run()
467 rst_output = "\n".join(captured_content)
468 assert rst_output == rst_snapshot
469
470 def test_nested_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
471 """Snapshot the RST output for deeply nested commands."""
472 from unittest.mock import MagicMock
473
474 from docutils.statemachine import StringList
475
476 from cyclopts.ext.sphinx import CycloptsDirective
477
478 mock_state = MagicMock()
479 captured_content = []
480
481 def capture_nested_parse(string_list, offset, parent):
482 captured_content.extend(string_list)
483
484 mock_state.nested_parse = capture_nested_parse
485
486 directive = CycloptsDirective(
487 name="cyclopts",
488 arguments=["complex_app:app"],
489 options={"heading-level": 3, "recursive": True, "commands": "admin.users.permissions"},
490 content=StringList(),
491 lineno=1,
492 content_offset=0,
493 block_text="",
494 state=mock_state,
495 state_machine=MagicMock(),
496 )
497
498 directive.run()
499 rst_output = "\n".join(captured_content)
500 assert rst_output == rst_snapshot
501
502 def test_flattened_commands_rst(self, ensure_complex_demo_importable, rst_snapshot):
503 """Snapshot the RST output with flatten-commands."""
504 from unittest.mock import MagicMock
505
506 from docutils.statemachine import StringList
507
508 from cyclopts.ext.sphinx import CycloptsDirective
509
510 mock_state = MagicMock()
511 captured_content = []
512
513 def capture_nested_parse(string_list, offset, parent):
514 captured_content.extend(string_list)
515
516 mock_state.nested_parse = capture_nested_parse
517
518 directive = CycloptsDirective(
519 name="cyclopts",
520 arguments=["complex_app:app"],
521 options={"heading-level": 2, "recursive": True, "flatten-commands": True, "commands": "admin.users"},
522 content=StringList(),
523 lineno=1,
524 content_offset=0,
525 block_text="",
526 state=mock_state,
527 state_machine=MagicMock(),
528 )
529
530 directive.run()
531 rst_output = "\n".join(captured_content)
532 assert rst_output == rst_snapshot