Skip to content

Commit 63d817a

Browse files
committed
Add dirhtml build test
1 parent 750ada8 commit 63d817a

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

tests/extension/__init__.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,70 @@ def test_skip_identical_code(tmp_path: Path):
313313
assert "sphinx-codeautolink-a" in str(blocks[1])
314314

315315

316+
def test_dirhtml_builder(tmp_path: Path):
317+
index = """
318+
Test project
319+
============
320+
321+
.. toctree::
322+
:maxdepth: 2
323+
324+
page1/index
325+
page2
326+
subdir/page3
327+
328+
Index Page Code
329+
---------------
330+
331+
.. code:: python
332+
333+
import test_project
334+
test_project.bar()
335+
336+
.. automodule:: test_project
337+
"""
338+
339+
page = """
340+
Page {idx}
341+
===========
342+
343+
.. code:: python
344+
345+
import test_project
346+
test_project.bar()
347+
348+
.. autolink-examples:: test_project.bar
349+
"""
350+
351+
files = {
352+
"conf.py": default_conf,
353+
"index.rst": index,
354+
"page1/index.rst": page.format(idx=1),
355+
"page2.rst": page.format(idx=2),
356+
"subdir/page3.rst": page.format(idx=3),
357+
}
358+
links = ["test_project", "test_project.bar"]
359+
360+
result_dir = _sphinx_build(tmp_path, "dirhtml", files)
361+
362+
assert_links(result_dir / "index.html", links)
363+
assert_links(result_dir / "page1/index.html", links)
364+
assert_links(result_dir / "page2/index.html", links)
365+
assert_links(result_dir / "subdir/page3/index.html", links)
366+
367+
assert check_link_targets(result_dir) == len(links) * 4
368+
369+
316370
def _sphinx_build(
317371
folder: Path, builder: str, files: dict[str, str], n_processes: int | None = None
318372
) -> Path:
319373
"""Build Sphinx documentation and return result folder."""
320374
src_dir = folder / "src"
321375
src_dir.mkdir(exist_ok=True)
322376
for name, content in files.items():
323-
(src_dir / name).write_text(content, "utf-8")
377+
path = src_dir / name
378+
path.parent.mkdir(exist_ok=True, parents=True)
379+
path.write_text(content, "utf-8")
324380

325381
build_dir = folder / "build"
326382
args = ["-M", builder, str(src_dir), str(build_dir), "-W"]

tests/extension/_check.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def check_link_targets(root: Path) -> int:
1313
"""Validate links in HTML site at root, return number of links found."""
1414
site_docs = {
15-
p.relative_to(root): BeautifulSoup(p.read_text("utf-8"), "html.parser")
15+
p: BeautifulSoup(p.read_text("utf-8"), "html.parser")
1616
for p in root.glob("**/*.html")
1717
}
1818
site_ids = {k: gather_ids(v) for k, v in site_docs.items()}
@@ -27,14 +27,18 @@ def check_link_targets(root: Path) -> int:
2727
external_site_ids[base] = gather_ids(sub_soup)
2828
ids = external_site_ids[base]
2929
else:
30-
if base == "":
31-
base = str(doc)
32-
elif base == "../":
33-
base = str(doc.parent.parent / "index.html")
34-
ids = site_ids[Path(base)]
30+
target_path = (doc.parent / base).resolve()
31+
if target_path.is_dir():
32+
target_path /= "index.html"
33+
assert target_path.exists(), (
34+
f"Target path {target_path!s} not found while validating"
35+
f" link for `{link.string}` in {doc.relative_to(root)!s}!"
36+
)
37+
ids = site_ids[target_path]
38+
3539
assert id_ in ids, (
36-
f"ID {id_} not found in {base}"
37-
f" while validating link for `{link.string}` in {doc!s}!"
40+
f"ID {id_} not found in {base} while validating link"
41+
f" for `{link.string}` in {doc.relative_to(root)!s}!"
3842
)
3943
total += 1
4044
return total

0 commit comments

Comments
 (0)