Skip to content

Commit fe728f4

Browse files
committed
Add test to assert unique ids in singlehtml output
Since the singlehtml aggregates all doc files into a single html page during the write step, and the ids must be unique for proper link anchoring, add test that collects all ids in the page and checks if all ids are unique, by asserting the length of the list against it as a set.
1 parent f5457f1 commit fe728f4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tests/test_builders/test_build_html_tocdepth.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, cast
66

77
import pytest
88

@@ -12,7 +12,7 @@
1212
if TYPE_CHECKING:
1313
from collections.abc import Callable
1414
from pathlib import Path
15-
from xml.etree.ElementTree import ElementTree
15+
from xml.etree.ElementTree import Element, ElementTree
1616

1717
from sphinx.testing.util import SphinxTestApp
1818

@@ -134,3 +134,17 @@ def test_tocdepth_singlehtml(
134134
) -> None:
135135
app.build()
136136
check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
137+
138+
139+
@pytest.mark.sphinx('singlehtml', testroot='tocdepth')
140+
@pytest.mark.test_params(shared_result='test_build_html_tocdepth')
141+
def test_unique_ids_singlehtml(
142+
app: SphinxTestApp,
143+
cached_etree_parse: Callable[[Path], ElementTree],
144+
) -> None:
145+
app.build()
146+
tree = cached_etree_parse(app.outdir / 'index.html')
147+
root = cast('Element', tree.getroot())
148+
149+
ids = [el.attrib['id'] for el in root.findall('.//*[@id]')]
150+
assert len(ids) == len(set(ids))

0 commit comments

Comments
 (0)