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