Debian Codegraph @

tests/test_extensions/test_ext_apidoc.py

raw
1 """Test the sphinx.apidoc module."""
2
3 from __future__ import annotations
4
5 from collections import namedtuple
6 from typing import TYPE_CHECKING
7
8 import pytest
9
10 import sphinx.ext.apidoc._generate
11 from sphinx.ext.apidoc._cli import main as apidoc_main
12
13 if TYPE_CHECKING:
14 from pathlib import Path
15
16 from sphinx.testing.util import SphinxTestApp
17
18 _apidoc = namedtuple('_apidoc', 'coderoot,outdir') # NoQA: PYI024
19
20
21 @pytest.fixture
22 def apidoc(rootdir, tmp_path, apidoc_params):
23 _, kwargs = apidoc_params
24 coderoot = rootdir / kwargs.get('coderoot', 'test-root')
25 outdir = tmp_path / 'out'
26 excludes = [str(coderoot / e) for e in kwargs.get('excludes', [])]
27 args = [
28 '-o',
29 str(outdir),
30 '-F',
31 str(coderoot),
32 *excludes,
33 *kwargs.get('options', []),
34 ]
35 apidoc_main(args)
36 return _apidoc(coderoot, outdir)
37
38
39 @pytest.fixture
40 def apidoc_params(request):
41 pargs = {}
42 kwargs = {}
43
44 for info in reversed(list(request.node.iter_markers('apidoc'))):
45 pargs |= dict(enumerate(info.args))
46 kwargs.update(info.kwargs)
47
48 args = [pargs[i] for i in sorted(pargs.keys())]
49 return args, kwargs
50
51
52 @pytest.mark.apidoc(coderoot='test-root')
53 def test_simple(make_app, apidoc):
54 outdir = apidoc.outdir
55 assert (outdir / 'conf.py').is_file()
56 assert (outdir / 'index.rst').is_file()
57
58 app = make_app('text', srcdir=outdir)
59 app.build()
60 print(app._status.getvalue())
61 print(app._warning.getvalue())
62
63
64 @pytest.mark.apidoc(
65 coderoot='test-ext-apidoc-custom-templates',
66 options=[
67 '--separate',
68 '--templatedir=tests/roots/test-ext-apidoc-custom-templates/_templates',
69 ],
70 )
71 def test_custom_templates(make_app, apidoc):
72 outdir = apidoc.outdir
73 assert (outdir / 'conf.py').is_file()
74 assert (outdir / 'index.rst').is_file()
75
76 template_dir = apidoc.coderoot / '_templates'
77 assert sorted(template_dir.iterdir()) == [
78 template_dir / 'module.rst.jinja',
79 template_dir / 'module.rst_t',
80 template_dir / 'package.rst_t',
81 ]
82
83 app = make_app('text', srcdir=outdir)
84 app.build()
85
86 builddir = outdir / '_build' / 'text'
87
88 # Assert that the legacy filename is discovered
89 with open(builddir / 'mypackage.txt', encoding='utf-8') as f:
90 txt = f.read()
91 assert 'The legacy package template was found!' in txt
92
93 # Assert that the new filename is preferred
94 with open(builddir / 'mypackage.mymodule.txt', encoding='utf-8') as f:
95 txt = f.read()
96 assert 'The Jinja module template was found!' in txt
97
98
99 @pytest.mark.apidoc(
100 coderoot='test-ext-apidoc-pep420/a',
101 options=['--implicit-namespaces'],
102 )
103 def test_pep_0420_enabled(make_app, apidoc):
104 outdir = apidoc.outdir
105 assert (outdir / 'conf.py').is_file()
106 assert (outdir / 'a.b.c.rst').is_file()
107 assert (outdir / 'a.b.e.rst').is_file()
108 assert (outdir / 'a.b.x.rst').is_file()
109
110 with open(outdir / 'a.b.c.rst', encoding='utf-8') as f:
111 rst = f.read()
112 assert 'automodule:: a.b.c.d\n' in rst
113 assert 'automodule:: a.b.c\n' in rst
114
115 with open(outdir / 'a.b.e.rst', encoding='utf-8') as f:
116 rst = f.read()
117 assert 'automodule:: a.b.e.f\n' in rst
118
119 with open(outdir / 'a.b.x.rst', encoding='utf-8') as f:
120 rst = f.read()
121 assert 'automodule:: a.b.x.y\n' in rst
122 assert 'automodule:: a.b.x\n' not in rst
123
124 app = make_app('text', srcdir=outdir)
125 app.build()
126 print(app._status.getvalue())
127 print(app._warning.getvalue())
128
129 builddir = outdir / '_build' / 'text'
130 assert (builddir / 'a.b.c.txt').is_file()
131 assert (builddir / 'a.b.e.txt').is_file()
132 assert (builddir / 'a.b.x.txt').is_file()
133
134 with open(builddir / 'a.b.c.txt', encoding='utf-8') as f:
135 txt = f.read()
136 assert 'a.b.c package\n' in txt
137
138 with open(builddir / 'a.b.e.txt', encoding='utf-8') as f:
139 txt = f.read()
140 assert 'a.b.e.f module\n' in txt
141
142 with open(builddir / 'a.b.x.txt', encoding='utf-8') as f:
143 txt = f.read()
144 assert 'a.b.x namespace\n' in txt
145
146
147 @pytest.mark.apidoc(
148 coderoot='test-ext-apidoc-pep420/a',
149 options=['--implicit-namespaces', '--separate'],
150 )
151 def test_pep_0420_enabled_separate(make_app, apidoc):
152 outdir = apidoc.outdir
153 assert (outdir / 'conf.py').is_file()
154 assert (outdir / 'a.b.c.rst').is_file()
155 assert (outdir / 'a.b.e.rst').is_file()
156 assert (outdir / 'a.b.e.f.rst').is_file()
157 assert (outdir / 'a.b.x.rst').is_file()
158 assert (outdir / 'a.b.x.y.rst').is_file()
159
160 with open(outdir / 'a.b.c.rst', encoding='utf-8') as f:
161 rst = f.read()
162 assert '.. toctree::\n :maxdepth: 4\n\n a.b.c.d\n' in rst
163
164 with open(outdir / 'a.b.e.rst', encoding='utf-8') as f:
165 rst = f.read()
166 assert '.. toctree::\n :maxdepth: 4\n\n a.b.e.f\n' in rst
167
168 with open(outdir / 'a.b.x.rst', encoding='utf-8') as f:
169 rst = f.read()
170 assert '.. toctree::\n :maxdepth: 4\n\n a.b.x.y\n' in rst
171
172 app = make_app('text', srcdir=outdir)
173 app.build()
174 print(app._status.getvalue())
175 print(app._warning.getvalue())
176
177 builddir = outdir / '_build' / 'text'
178 assert (builddir / 'a.b.c.txt').is_file()
179 assert (builddir / 'a.b.e.txt').is_file()
180 assert (builddir / 'a.b.e.f.txt').is_file()
181 assert (builddir / 'a.b.x.txt').is_file()
182 assert (builddir / 'a.b.x.y.txt').is_file()
183
184 with open(builddir / 'a.b.c.txt', encoding='utf-8') as f:
185 txt = f.read()
186 assert 'a.b.c package\n' in txt
187
188 with open(builddir / 'a.b.e.f.txt', encoding='utf-8') as f:
189 txt = f.read()
190 assert 'a.b.e.f module\n' in txt
191
192 with open(builddir / 'a.b.x.txt', encoding='utf-8') as f:
193 txt = f.read()
194 assert 'a.b.x namespace\n' in txt
195
196
197 @pytest.mark.apidoc(coderoot='test-ext-apidoc-pep420/a')
198 def test_pep_0420_disabled(make_app, apidoc):
199 outdir = apidoc.outdir
200 assert (outdir / 'conf.py').is_file()
201 assert not (outdir / 'a.b.c.rst').exists()
202 assert not (outdir / 'a.b.x.rst').exists()
203
204 app = make_app('text', srcdir=outdir)
205 app.build()
206 print(app._status.getvalue())
207 print(app._warning.getvalue())
208
209
210 @pytest.mark.apidoc(coderoot='test-ext-apidoc-pep420/a/b')
211 def test_pep_0420_disabled_top_level_verify(make_app, apidoc):
212 outdir = apidoc.outdir
213 assert (outdir / 'conf.py').is_file()
214 assert (outdir / 'c.rst').is_file()
215 assert not (outdir / 'x.rst').exists()
216
217 with open(outdir / 'c.rst', encoding='utf-8') as f:
218 rst = f.read()
219 assert 'c package\n' in rst
220 assert 'automodule:: c.d\n' in rst
221 assert 'automodule:: c\n' in rst
222
223 app = make_app('text', srcdir=outdir)
224 app.build()
225 print(app._status.getvalue())
226 print(app._warning.getvalue())
227
228
229 @pytest.mark.apidoc(coderoot='test-ext-apidoc-trailing-underscore')
230 def test_trailing_underscore(make_app, apidoc):
231 outdir = apidoc.outdir
232 assert (outdir / 'conf.py').is_file()
233 assert (outdir / 'package_.rst').is_file()
234
235 app = make_app('text', srcdir=outdir)
236 app.build()
237 print(app._status.getvalue())
238 print(app._warning.getvalue())
239
240 builddir = outdir / '_build' / 'text'
241 with open(builddir / 'package_.txt', encoding='utf-8') as f:
242 rst = f.read()
243 assert 'package_ package\n' in rst
244 assert 'package_.module_ module\n' in rst
245
246
247 @pytest.mark.apidoc(
248 coderoot='test-ext-apidoc-pep420/a',
249 excludes=['b/c/d.py', 'b/e/f.py', 'b/e/__init__.py'],
250 options=['--implicit-namespaces', '--separate'],
251 )
252 def test_excludes(apidoc):
253 outdir = apidoc.outdir
254 assert (outdir / 'conf.py').is_file()
255 assert (outdir / 'a.rst').is_file()
256 assert (outdir / 'a.b.rst').is_file()
257 assert (outdir / 'a.b.c.rst').is_file() # generated because not empty
258 assert not (
259 outdir / 'a.b.e.rst'
260 ).is_file() # skipped because of empty after excludes
261 assert (outdir / 'a.b.x.rst').is_file()
262 assert (outdir / 'a.b.x.y.rst').is_file()
263
264
265 @pytest.mark.apidoc(
266 coderoot='test-ext-apidoc-pep420/a',
267 excludes=['b/e'],
268 options=['--implicit-namespaces', '--separate'],
269 )
270 def test_excludes_subpackage_should_be_skipped(apidoc):
271 """Subpackage exclusion should work."""
272 outdir = apidoc.outdir
273 assert (outdir / 'conf.py').is_file()
274 assert (outdir / 'a.rst').is_file()
275 assert (outdir / 'a.b.rst').is_file()
276 assert (outdir / 'a.b.c.rst').is_file() # generated because not empty
277 assert not (
278 outdir / 'a.b.e.f.rst'
279 ).is_file() # skipped because 'b/e' subpackage is skipped
280
281
282 @pytest.mark.apidoc(
283 coderoot='test-ext-apidoc-pep420/a',
284 excludes=['b/e/f.py'],
285 options=['--implicit-namespaces', '--separate'],
286 )
287 def test_excludes_module_should_be_skipped(apidoc):
288 """Module exclusion should work."""
289 outdir = apidoc.outdir
290 assert (outdir / 'conf.py').is_file()
291 assert (outdir / 'a.rst').is_file()
292 assert (outdir / 'a.b.rst').is_file()
293 assert (outdir / 'a.b.c.rst').is_file() # generated because not empty
294 assert not (
295 outdir / 'a.b.e.f.rst'
296 ).is_file() # skipped because of empty after excludes
297
298
299 @pytest.mark.apidoc(
300 coderoot='test-ext-apidoc-pep420/a',
301 excludes=[],
302 options=['--implicit-namespaces', '--separate'],
303 )
304 def test_excludes_module_should_not_be_skipped(apidoc):
305 """Module should be included if no excludes are used."""
306 outdir = apidoc.outdir
307 assert (outdir / 'conf.py').is_file()
308 assert (outdir / 'a.rst').is_file()
309 assert (outdir / 'a.b.rst').is_file()
310 assert (outdir / 'a.b.c.rst').is_file() # generated because not empty
311 assert (outdir / 'a.b.e.f.rst').is_file() # skipped because of empty after excludes
312
313
314 @pytest.mark.apidoc(
315 coderoot='test-root',
316 options=[
317 '--doc-project',
318 'プロジェクト名',
319 '--doc-author',
320 '著者名',
321 '--doc-version',
322 'バージョン',
323 '--doc-release',
324 'リリース',
325 ],
326 )
327 def test_multibyte_parameters(make_app, apidoc):
328 outdir = apidoc.outdir
329 assert (outdir / 'conf.py').is_file()
330 assert (outdir / 'index.rst').is_file()
331
332 conf_py = (outdir / 'conf.py').read_text(encoding='utf8')
333 assert "project = 'プロジェクト名'" in conf_py
334 assert "author = '著者名'" in conf_py
335 assert "version = 'バージョン'" in conf_py
336 assert "release = 'リリース'" in conf_py
337
338 app = make_app('text', srcdir=outdir)
339 app.build()
340 print(app._status.getvalue())
341 print(app._warning.getvalue())
342
343
344 @pytest.mark.apidoc(
345 coderoot='test-root',
346 options=['--ext-mathjax'],
347 )
348 def test_extension_parsed(apidoc):
349 outdir = apidoc.outdir
350 assert (outdir / 'conf.py').is_file()
351
352 with open(outdir / 'conf.py', encoding='utf-8') as f:
353 rst = f.read()
354 assert 'sphinx.ext.mathjax' in rst
355
356
357 @pytest.mark.apidoc(
358 coderoot='test-ext-apidoc-toc/mypackage',
359 options=['--implicit-namespaces'],
360 )
361 def test_toc_all_references_should_exist_pep420_enabled(apidoc):
362 """All references in toc should exist. This test doesn't say if
363 directories with empty __init__.py and and nothing else should be
364 skipped, just ensures consistency between what's referenced in the toc
365 and what is created. This is the variant with pep420 enabled.
366 """
367 outdir = apidoc.outdir
368 assert (outdir / 'conf.py').is_file()
369
370 toc = extract_toc(outdir / 'mypackage.rst')
371
372 refs = [l.strip() for l in toc.splitlines() if l.strip()]
373 found_refs = []
374 missing_files = []
375 for ref in refs:
376 if ref and ref[0] in {':', '#'}:
377 continue
378 found_refs.append(ref)
379 filename = f'{ref}.rst'
380 if not (outdir / filename).is_file():
381 missing_files.append(filename)
382
383 all_missing = ', '.join(missing_files)
384 assert len(missing_files) == 0, (
385 f'File(s) referenced in TOC not found: {all_missing}\nTOC:\n{toc}'
386 )
387
388
389 @pytest.mark.apidoc(
390 coderoot='test-ext-apidoc-toc/mypackage',
391 )
392 def test_toc_all_references_should_exist_pep420_disabled(apidoc):
393 """All references in toc should exist. This test doesn't say if
394 directories with empty __init__.py and and nothing else should be
395 skipped, just ensures consistency between what's referenced in the toc
396 and what is created. This is the variant with pep420 disabled.
397 """
398 outdir = apidoc.outdir
399 assert (outdir / 'conf.py').is_file()
400
401 toc = extract_toc(outdir / 'mypackage.rst')
402
403 refs = [l.strip() for l in toc.splitlines() if l.strip()]
404 found_refs = []
405 missing_files = []
406 for ref in refs:
407 if ref and ref[0] in {':', '#'}:
408 continue
409 filename = f'{ref}.rst'
410 found_refs.append(ref)
411 if not (outdir / filename).is_file():
412 missing_files.append(filename)
413
414 all_missing = ', '.join(missing_files)
415 assert len(missing_files) == 0, (
416 f'File(s) referenced in TOC not found: {all_missing}\nTOC:\n{toc}'
417 )
418
419
420 def extract_toc(path):
421 """Helper: Extract toc section from package rst file"""
422 with open(path, encoding='utf-8') as f:
423 rst = f.read()
424
425 # Read out the part containing the toctree
426 toctree_start = '\n.. toctree::\n'
427 toctree_end = '\nSubmodules'
428
429 start_idx = rst.index(toctree_start)
430 end_idx = rst.index(toctree_end, start_idx)
431 toctree = rst[start_idx + len(toctree_start) : end_idx]
432
433 return toctree
434
435
436 @pytest.mark.apidoc(
437 coderoot='test-ext-apidoc-subpackage-in-toc',
438 options=['--separate'],
439 )
440 def test_subpackage_in_toc(apidoc):
441 """Make sure that empty subpackages with non-empty subpackages in them
442 are not skipped
443 See: https://github.com/sphinx-doc/sphinx/issues/4520
444 """
445 outdir = apidoc.outdir
446 assert (outdir / 'conf.py').is_file()
447
448 assert (outdir / 'parent.rst').is_file()
449 with open(outdir / 'parent.rst', encoding='utf-8') as f:
450 parent = f.read()
451 assert 'parent.child' in parent
452
453 assert (outdir / 'parent.child.rst').is_file()
454 with open(outdir / 'parent.child.rst', encoding='utf-8') as f:
455 parent_child = f.read()
456 assert 'parent.child.foo' in parent_child
457
458 assert (outdir / 'parent.child.foo.rst').is_file()
459
460
461 def test_private(tmp_path):
462 (tmp_path / 'hello.py').touch()
463 (tmp_path / '_world.py').touch()
464
465 # without --private option
466 apidoc_main(['-o', str(tmp_path), str(tmp_path)])
467 assert (tmp_path / 'hello.rst').exists()
468 assert ':private-members:' not in (tmp_path / 'hello.rst').read_text(
469 encoding='utf8'
470 )
471 assert not (tmp_path / '_world.rst').exists()
472
473 # with --private option
474 apidoc_main(['--private', '-f', '-o', str(tmp_path), str(tmp_path)])
475 assert (tmp_path / 'hello.rst').exists()
476 assert ':private-members:' in (tmp_path / 'hello.rst').read_text(encoding='utf8')
477 assert (tmp_path / '_world.rst').exists()
478
479
480 def test_toc_file(tmp_path):
481 outdir = tmp_path
482 (outdir / 'module').mkdir(parents=True, exist_ok=True)
483 (outdir / 'example.py').touch()
484 (outdir / 'module' / 'example.py').touch()
485 apidoc_main(['-o', str(tmp_path), str(tmp_path)])
486 assert (outdir / 'modules.rst').exists()
487
488 content = (outdir / 'modules.rst').read_text(encoding='utf8')
489 assert content == (
490 'test_toc_file0\n'
491 '==============\n'
492 '\n'
493 '.. toctree::\n'
494 ' :maxdepth: 4\n'
495 '\n'
496 ' example\n'
497 )
498
499
500 def test_module_file(tmp_path):
501 outdir = tmp_path
502 (outdir / 'example.py').touch()
503 apidoc_main(['-o', str(tmp_path), str(tmp_path)])
504 assert (outdir / 'example.rst').exists()
505
506 content = (outdir / 'example.rst').read_text(encoding='utf8')
507 assert content == (
508 'example module\n'
509 '==============\n'
510 '\n'
511 '.. automodule:: example\n'
512 ' :members:\n'
513 ' :show-inheritance:\n'
514 ' :undoc-members:\n'
515 )
516
517
518 def test_module_file_noheadings(tmp_path):
519 outdir = tmp_path
520 (outdir / 'example.py').touch()
521 apidoc_main(['--no-headings', '-o', str(tmp_path), str(tmp_path)])
522 assert (outdir / 'example.rst').exists()
523
524 content = (outdir / 'example.rst').read_text(encoding='utf8')
525 assert content == (
526 '.. automodule:: example\n'
527 ' :members:\n'
528 ' :show-inheritance:\n'
529 ' :undoc-members:\n'
530 )
531
532
533 def test_package_file(tmp_path):
534 outdir = tmp_path
535 (outdir / 'testpkg').mkdir(parents=True, exist_ok=True)
536 (outdir / 'testpkg' / '__init__.py').touch()
537 (outdir / 'testpkg' / 'hello.py').touch()
538 (outdir / 'testpkg' / 'world.py').touch()
539 (outdir / 'testpkg' / 'subpkg').mkdir(parents=True, exist_ok=True)
540 (outdir / 'testpkg' / 'subpkg' / '__init__.py').touch()
541 apidoc_main(['-o', str(outdir), str(outdir / 'testpkg')])
542 assert (outdir / 'testpkg.rst').exists()
543 assert (outdir / 'testpkg.subpkg.rst').exists()
544
545 content = (outdir / 'testpkg.rst').read_text(encoding='utf8')
546 assert content == (
547 'testpkg package\n'
548 '===============\n'
549 '\n'
550 'Subpackages\n'
551 '-----------\n'
552 '\n'
553 '.. toctree::\n'
554 ' :maxdepth: 4\n'
555 '\n'
556 ' testpkg.subpkg\n'
557 '\n'
558 'Submodules\n'
559 '----------\n'
560 '\n'
561 'testpkg.hello module\n'
562 '--------------------\n'
563 '\n'
564 '.. automodule:: testpkg.hello\n'
565 ' :members:\n'
566 ' :show-inheritance:\n'
567 ' :undoc-members:\n'
568 '\n'
569 'testpkg.world module\n'
570 '--------------------\n'
571 '\n'
572 '.. automodule:: testpkg.world\n'
573 ' :members:\n'
574 ' :show-inheritance:\n'
575 ' :undoc-members:\n'
576 '\n'
577 'Module contents\n'
578 '---------------\n'
579 '\n'
580 '.. automodule:: testpkg\n'
581 ' :members:\n'
582 ' :show-inheritance:\n'
583 ' :undoc-members:\n'
584 )
585
586 content = (outdir / 'testpkg.subpkg.rst').read_text(encoding='utf8')
587 assert content == (
588 'testpkg.subpkg package\n'
589 '======================\n'
590 '\n'
591 'Module contents\n'
592 '---------------\n'
593 '\n'
594 '.. automodule:: testpkg.subpkg\n'
595 ' :members:\n'
596 ' :show-inheritance:\n'
597 ' :undoc-members:\n'
598 )
599
600
601 def test_package_file_separate(tmp_path):
602 outdir = tmp_path
603 (outdir / 'testpkg').mkdir(parents=True, exist_ok=True)
604 (outdir / 'testpkg' / '__init__.py').touch()
605 (outdir / 'testpkg' / 'example.py').touch()
606 apidoc_main(['--separate', '-o', str(tmp_path), str(tmp_path / 'testpkg')])
607 assert (outdir / 'testpkg.rst').exists()
608 assert (outdir / 'testpkg.example.rst').exists()
609
610 content = (outdir / 'testpkg.rst').read_text(encoding='utf8')
611 assert content == (
612 'testpkg package\n'
613 '===============\n'
614 '\n'
615 'Submodules\n'
616 '----------\n'
617 '\n'
618 '.. toctree::\n'
619 ' :maxdepth: 4\n'
620 '\n'
621 ' testpkg.example\n'
622 '\n'
623 'Module contents\n'
624 '---------------\n'
625 '\n'
626 '.. automodule:: testpkg\n'
627 ' :members:\n'
628 ' :show-inheritance:\n'
629 ' :undoc-members:\n'
630 )
631
632 content = (outdir / 'testpkg.example.rst').read_text(encoding='utf8')
633 assert content == (
634 'testpkg.example module\n'
635 '======================\n'
636 '\n'
637 '.. automodule:: testpkg.example\n'
638 ' :members:\n'
639 ' :show-inheritance:\n'
640 ' :undoc-members:\n'
641 )
642
643
644 def test_package_file_module_first(tmp_path):
645 outdir = tmp_path
646 (outdir / 'testpkg').mkdir(parents=True, exist_ok=True)
647 (outdir / 'testpkg' / '__init__.py').touch()
648 (outdir / 'testpkg' / 'example.py').touch()
649 apidoc_main(['--module-first', '-o', str(tmp_path), str(tmp_path)])
650
651 content = (outdir / 'testpkg.rst').read_text(encoding='utf8')
652 assert content == (
653 'testpkg package\n'
654 '===============\n'
655 '\n'
656 '.. automodule:: testpkg\n'
657 ' :members:\n'
658 ' :show-inheritance:\n'
659 ' :undoc-members:\n'
660 '\n'
661 'Submodules\n'
662 '----------\n'
663 '\n'
664 'testpkg.example module\n'
665 '----------------------\n'
666 '\n'
667 '.. automodule:: testpkg.example\n'
668 ' :members:\n'
669 ' :show-inheritance:\n'
670 ' :undoc-members:\n'
671 )
672
673
674 def test_package_file_without_submodules(tmp_path):
675 outdir = tmp_path
676 (outdir / 'testpkg').mkdir(parents=True, exist_ok=True)
677 (outdir / 'testpkg' / '__init__.py').touch()
678 apidoc_main(['-o', str(tmp_path), str(tmp_path / 'testpkg')])
679 assert (outdir / 'testpkg.rst').exists()
680
681 content = (outdir / 'testpkg.rst').read_text(encoding='utf8')
682 assert content == (
683 'testpkg package\n'
684 '===============\n'
685 '\n'
686 'Module contents\n'
687 '---------------\n'
688 '\n'
689 '.. automodule:: testpkg\n'
690 ' :members:\n'
691 ' :show-inheritance:\n'
692 ' :undoc-members:\n'
693 )
694
695
696 def test_namespace_package_file(tmp_path):
697 outdir = tmp_path
698 (outdir / 'testpkg').mkdir(parents=True, exist_ok=True)
699 (outdir / 'testpkg' / 'example.py').touch()
700 apidoc_main([
701 '--implicit-namespace',
702 '-o',
703 str(tmp_path),
704 str(tmp_path / 'testpkg'),
705 ])
706 assert (outdir / 'testpkg.rst').exists()
707
708 content = (outdir / 'testpkg.rst').read_text(encoding='utf8')
709 assert content == (
710 'testpkg namespace\n'
711 '=================\n'
712 '\n'
713 '.. py:module:: testpkg\n'
714 '\n'
715 'Submodules\n'
716 '----------\n'
717 '\n'
718 'testpkg.example module\n'
719 '----------------------\n'
720 '\n'
721 '.. automodule:: testpkg.example\n'
722 ' :members:\n'
723 ' :show-inheritance:\n'
724 ' :undoc-members:\n'
725 )
726
727
728 def test_no_duplicates(rootdir, tmp_path):
729 """Make sure that a ".pyx" and ".so" don't cause duplicate listings.
730
731 We can't use pytest.mark.apidoc here as we use a different set of arguments
732 to apidoc_main
733 """
734 original_suffixes = sphinx.ext.apidoc._generate.PY_SUFFIXES
735 try:
736 # Ensure test works on Windows
737 sphinx.ext.apidoc._generate.PY_SUFFIXES += ('.so',)
738
739 package = rootdir / 'test-ext-apidoc-duplicates' / 'fish_licence'
740 outdir = tmp_path / 'out'
741 apidoc_main(['-o', str(outdir), '-T', str(package), '--implicit-namespaces'])
742
743 # Ensure the module has been documented
744 assert (outdir / 'fish_licence.rst').is_file()
745
746 # Ensure the submodule only appears once
747 text = (outdir / 'fish_licence.rst').read_text(encoding='utf-8')
748 count_submodules = text.count(r'fish\_licence.halibut module')
749 assert count_submodules == 1
750
751 finally:
752 sphinx.ext.apidoc._generate.PY_SUFFIXES = original_suffixes
753
754
755 def test_remove_old_files(tmp_path: Path) -> None:
756 """Test that old files are removed when using the -r option.
757
758 Also ensure that pre-existing files are not re-written, if unchanged.
759 This is required to avoid unnecessary rebuilds.
760 """
761 module_dir = tmp_path / 'module'
762 module_dir.mkdir()
763 (module_dir / 'example.py').touch()
764 gen_dir = tmp_path / 'gen'
765 gen_dir.mkdir()
766 (gen_dir / 'other.rst').touch()
767 apidoc_main(['-o', str(gen_dir), str(module_dir)])
768 assert set(gen_dir.iterdir()) == {
769 gen_dir / 'modules.rst',
770 gen_dir / 'example.rst',
771 gen_dir / 'other.rst',
772 }
773 example_mtime = (gen_dir / 'example.rst').stat().st_mtime_ns
774 apidoc_main(['--remove-old', '-o', str(gen_dir), str(module_dir)])
775 assert set(gen_dir.iterdir()) == {gen_dir / 'modules.rst', gen_dir / 'example.rst'}
776 assert (gen_dir / 'example.rst').stat().st_mtime_ns == example_mtime
777
778
779 @pytest.mark.sphinx(testroot='ext-apidoc')
780 def test_sphinx_extension(app: SphinxTestApp) -> None:
781 """Test running apidoc as an extension."""
782 app.build()
783 assert app.warning.getvalue() == ''
784
785 toc_file = app.srcdir / 'generated' / 'modules.rst'
786 pkg_file = app.srcdir / 'generated' / 'my_package.rst'
787 assert set((app.srcdir / 'generated').iterdir()) == {toc_file, pkg_file}
788 modules_content = toc_file.read_text(encoding='utf8')
789 assert modules_content == (
790 'src\n===\n\n.. toctree::\n :maxdepth: 3\n\n my_package\n'
791 )
792 assert 'show-inheritance' not in pkg_file.read_text(encoding='utf8')
793 assert (app.outdir / 'generated' / 'my_package.html').is_file()
794
795 # test a re-build
796 app.build()
797 assert app.warning.getvalue() == ''
798
799 # TODO check nothing got re-built
800 # TODO test that old files are removed