Debian Codegraph @

tests/test_builders/test_build_html_toctree.py

raw
1 """Test the HTML builder and check output against XPath."""
2
3 from __future__ import annotations
4
5 import re
6 from typing import TYPE_CHECKING
7 from unittest.mock import patch
8
9 import pytest
10
11 from sphinx.builders.html import StandaloneHTMLBuilder
12
13 from tests.test_builders.xpath_html_util import _intradocument_hyperlink_check
14 from tests.test_builders.xpath_util import check_xpath
15
16 if TYPE_CHECKING:
17 from collections.abc import Callable, Sequence
18 from pathlib import Path
19 from xml.etree.ElementTree import Element, ElementTree
20
21 from sphinx.testing.util import SphinxTestApp
22
23
24 @pytest.mark.sphinx('html', testroot='toctree-glob')
25 def test_relations(app: SphinxTestApp) -> None:
26 app.build(force_all=True)
27 assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
28 assert app.builder.relations['index'] == [None, None, 'foo']
29 assert app.builder.relations['foo'] == ['index', 'index', 'bar/index']
30 assert app.builder.relations['bar/index'] == ['index', 'foo', 'bar/bar_1']
31 assert app.builder.relations['bar/bar_1'] == ['bar/index', 'bar/index', 'bar/bar_2']
32 assert app.builder.relations['bar/bar_2'] == ['bar/index', 'bar/bar_1', 'bar/bar_3']
33 assert app.builder.relations['bar/bar_3'] == [
34 'bar/index',
35 'bar/bar_2',
36 'bar/bar_4/index',
37 ]
38 assert app.builder.relations['bar/bar_4/index'] == ['bar/index', 'bar/bar_3', 'baz']
39 assert app.builder.relations['baz'] == ['index', 'bar/bar_4/index', 'qux/index']
40 assert app.builder.relations['qux/index'] == ['index', 'baz', 'qux/qux_1']
41 assert app.builder.relations['qux/qux_1'] == ['qux/index', 'qux/index', 'qux/qux_2']
42 assert app.builder.relations['qux/qux_2'] == ['qux/index', 'qux/qux_1', None]
43 assert 'quux' not in app.builder.relations
44
45
46 @pytest.mark.sphinx('singlehtml', testroot='toctree-empty')
47 def test_singlehtml_toctree(app: SphinxTestApp) -> None:
48 app.build(force_all=True)
49 assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
50 try:
51 app.builder._get_local_toctree('index')
52 except AttributeError:
53 pytest.fail('Unexpected AttributeError in app.builder.fix_refuris')
54
55
56 @pytest.mark.sphinx(
57 'html',
58 testroot='toctree',
59 srcdir='numbered-toctree',
60 )
61 def test_numbered_toctree(app: SphinxTestApp) -> None:
62 # give argument to :numbered: option
63 index = (app.srcdir / 'index.rst').read_text(encoding='utf8')
64 index = re.sub(':numbered:.*', ':numbered: 1', index)
65 (app.srcdir / 'index.rst').write_text(index, encoding='utf8')
66 app.build(force_all=True)
67
68
69 @pytest.mark.parametrize(
70 'expect',
71 [
72 # internal references should be same-document; external should not
73 (".//a[@class='reference internal']", _intradocument_hyperlink_check),
74 (".//a[@class='reference external']", r'https?://'),
75 ],
76 )
77 @pytest.mark.sphinx('singlehtml', testroot='toctree')
78 def test_singlehtml_hyperlinks(
79 app: SphinxTestApp,
80 cached_etree_parse: Callable[[Path], ElementTree],
81 expect: tuple[str, str | Callable[[Sequence[Element]], None]],
82 ) -> None:
83 app.build()
84 check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
85
86
87 @pytest.mark.sphinx(
88 'html',
89 testroot='toctree-multiple-parents',
90 confoverrides={'html_theme': 'alabaster'},
91 )
92 def test_toctree_multiple_parents(
93 app: SphinxTestApp, cached_etree_parse: Callable[[Path], ElementTree]
94 ) -> None:
95 # The lexicographically greatest parent of the document in global toctree
96 # should be chosen, regardless of the order in which files are read
97 with patch.object(app.builder, '_read_serial') as m:
98 # Read files in reversed order
99 _read_serial = type(app.builder)._read_serial
100 m.side_effect = lambda docnames: _read_serial(app.builder, docnames[::-1])
101 app.build()
102 # Check if charlie is a child of delta in charlie.html
103 xpath_delta_children = (
104 ".//ul[@class='current']//a[@href='delta.html']/../ul/li//a"
105 )
106 etree = cached_etree_parse(app.outdir / 'charlie.html')
107 check_xpath(etree, 'charlie.html', xpath=xpath_delta_children, check='Charlie')