Debian Codegraph @

tests/test_transforms/test_transforms_move_module_targets.py

raw
1 from __future__ import annotations
2
3 from typing import TYPE_CHECKING
4
5 import pytest
6 from docutils import nodes
7
8 from sphinx import addnodes
9 from sphinx.testing.util import SphinxTestApp
10 from sphinx.transforms import MoveModuleTargets
11
12 if TYPE_CHECKING:
13 from pathlib import Path
14
15 CONTENT_PY = """\
16 move-module-targets
17 ===================
18
19 .. py:module:: fish_licence.halibut
20 """
21 CONTENT_JS = """\
22 move-module-targets
23 ===================
24
25 .. js:module:: fish_licence.halibut
26 """
27
28
29 @pytest.mark.parametrize(
30 'content',
31 [
32 CONTENT_PY, # Python
33 CONTENT_JS, # JavaScript
34 ],
35 )
36 @pytest.mark.usefixtures('rollback_sysmodules')
37 def test_move_module_targets(tmp_path: Path, content: str) -> None:
38 # Test for the MoveModuleTargets transform
39 tmp_path.joinpath('conf.py').touch()
40 tmp_path.joinpath('index.rst').write_text(content, encoding='utf-8')
41
42 app = SphinxTestApp('dummy', srcdir=tmp_path)
43 app.build(force_all=True)
44 document = app.env.get_doctree('index')
45 section: nodes.section = document[0] # type: ignore[assignment]
46
47 # target ID has been lifted into the section node
48 assert section['ids'] == ['module-fish_licence.halibut', 'move-module-targets']
49 # nodes.target has been removed from 'section'
50 assert isinstance(section[0], nodes.title)
51 assert isinstance(section[1], addnodes.index)
52 assert len(section) == 2
53
54
55 @pytest.mark.usefixtures('rollback_sysmodules')
56 def test_move_module_targets_no_section(tmp_path: Path) -> None:
57 # Test for the MoveModuleTargets transform
58 tmp_path.joinpath('conf.py').touch()
59 tmp_path.joinpath('index.rst').write_text(
60 '.. py:module:: fish_licence.halibut\n', encoding='utf-8'
61 )
62
63 app = SphinxTestApp('dummy', srcdir=tmp_path)
64 app.build(force_all=True)
65 document = app.env.get_doctree('index')
66
67 assert document['ids'] == []
68
69
70 @pytest.mark.usefixtures('rollback_sysmodules')
71 def test_move_module_targets_disabled(tmp_path: Path) -> None:
72 # Test for the MoveModuleTargets transform
73 tmp_path.joinpath('conf.py').touch()
74 tmp_path.joinpath('index.rst').write_text(CONTENT_PY, encoding='utf-8')
75
76 app = SphinxTestApp('dummy', srcdir=tmp_path)
77 app.registry.transforms.remove(MoveModuleTargets) # disable the transform
78 app.build(force_all=True)
79 document = app.env.get_doctree('index')
80 section: nodes.section = document[0] # type: ignore[assignment]
81
82 # target ID is not lifted into the section node
83 assert section['ids'] == ['move-module-targets']
84 assert section[2]['ids'] == ['module-fish_licence.halibut'] # type: ignore[index]
85 # nodes.target remains in 'section'
86 assert isinstance(section[0], nodes.title)
87 assert isinstance(section[1], addnodes.index)
88 assert isinstance(section[2], nodes.target)
89 assert len(section) == 3