test(docs): cover the yaml parser and the directory resolver

Both extensions landed in bd15eae9fa without the mirrored unit test the
repository lint requires, which left that lint red.

The resolver's cases include the mistake its first version made: it
filtered on refdomain == 'doc', while myst builds a document reference
only when the target is_file(), so a directory arrives with refdomain
None and the transform did nothing at all. Reintroducing that filter
fails three of the six cases.

The parser's cases pin what the build relies on: one yaml-highlighted
literal block, no block quote, a title from the filename, and the orphan
docinfo as the first child, which is the only place sphinx reads metadata
from.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-25 18:27:40 +02:00
1 parent 7440371f62
commit f3d77f39ab
2 files changed
+200

No files matched your search

@@ -0,0 +1,120 @@
from __future__ import annotations
import importlib
import sys
import tempfile
import unittest
from pathlib import Path
from docutils.frontend import get_default_settings
from docutils.parsers.rst import Parser
from docutils.utils import new_document
from sphinx import addnodes
from . import PROJECT_ROOT
_TOOLING = str(PROJECT_ROOT / "roles" / "web-app-docs" / "files" / "python")
if _TOOLING not in sys.path:
sys.path.insert(0, _TOOLING)
directory_readme = importlib.import_module(
"infinito_docs.extensions.directory_readme"
)
REFDOC = "docs/index"
class _Env:
"""The three env members the transform reads, backed by a real tree."""
def __init__(self, srcdir: Path, docnames: set[str]):
self.srcdir = srcdir
self.docname = REFDOC
self.all_docs = dict.fromkeys(docnames, 0)
def relfn2path(self, target: str, _docname: str):
absolute = (self.srcdir / target).resolve()
return target, str(absolute)
def path2doc(self, path: str) -> str | None:
relative = Path(path).resolve().relative_to(self.srcdir)
return str(relative.with_suffix("")) if relative.suffix == ".md" else None
def _xref(target: str, refdomain=None) -> addnodes.pending_xref:
return addnodes.pending_xref(
reftype="myst", refdomain=refdomain, reftarget=target, refdoc=REFDOC
)
class TestDirectoryReadme(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.TemporaryDirectory(prefix="infinito-dirreadme-")
self.src = Path(self._tmp.name).resolve()
(self.src / "guide").mkdir()
(self.src / "guide" / "README.md").write_text("# Guide\n", encoding="utf-8")
(self.src / "empty").mkdir()
self.addCleanup(self._tmp.cleanup)
def _run(self, node) -> None:
document = new_document("/src/docs/index.md", get_default_settings(Parser))
document.settings.env = _Env(self.src, {"guide/README"})
document.append(node)
directory_readme.DirectoryReadme(document).run()
def test_a_directory_link_resolves_to_its_readme(self) -> None:
node = _xref("guide/")
self._run(node)
self.assertEqual(node["refdomain"], "doc")
self.assertEqual(node["reftarget"], "guide/README")
def test_the_transform_reads_the_domain_myst_actually_sets(self) -> None:
node = _xref("guide/", refdomain=None)
self._run(node)
self.assertEqual(
node["reftarget"],
"guide/README",
"myst builds a doc reference only when the target is_file(), so a "
"directory arrives with refdomain None; filtering on 'doc' made an "
"earlier version of this transform do nothing at all",
)
def test_an_anchor_survives_the_rewrite(self) -> None:
node = _xref("guide/#usage")
self._run(node)
self.assertEqual(node["reftarget"], "guide/README")
self.assertEqual(node["reftargetid"], "usage")
def test_a_directory_without_a_readme_is_left_to_be_reported(self) -> None:
node = _xref("empty/")
self._run(node)
self.assertIsNone(node["refdomain"])
self.assertEqual(node["reftarget"], "empty/")
def test_a_reference_of_another_type_is_untouched(self) -> None:
node = _xref("guide/")
node["reftype"] = "ref"
self._run(node)
self.assertEqual(node["reftarget"], "guide/")
def test_it_runs_before_the_myst_resolver(self) -> None:
from myst_parser.sphinx_ext.myst_refs import MystReferenceResolver
self.assertLess(
directory_readme.DirectoryReadme.default_priority,
MystReferenceResolver.default_priority,
)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,80 @@
from __future__ import annotations
import importlib
import sys
import unittest
from docutils import nodes
from docutils.frontend import get_default_settings
from docutils.parsers.rst import Parser
from docutils.utils import new_document
from . import PROJECT_ROOT
_TOOLING = str(PROJECT_ROOT / "roles" / "web-app-docs" / "files" / "python")
if _TOOLING not in sys.path:
sys.path.insert(0, _TOOLING)
yaml_source = importlib.import_module("infinito_docs.extensions.yaml_source")
SOURCE = "services:\n web:\n image: nginx\n"
def _parsed(path: str, text: str = SOURCE):
document = new_document(path, get_default_settings(Parser))
yaml_source.YamlParser().parse(text, document)
return document
class TestYamlParser(unittest.TestCase):
def test_the_file_becomes_one_literal_block(self) -> None:
document = _parsed("/src/compose.yml")
blocks = list(document.findall(nodes.literal_block))
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].astext(), SOURCE)
self.assertEqual(blocks[0]["language"], "yaml")
def test_indentation_never_reaches_the_rst_parser(self) -> None:
document = _parsed("/src/compose.yml")
self.assertEqual(
len(list(document.findall(nodes.block_quote))),
0,
"reading yaml as reStructuredText turned every indent into a block "
"quote, which is what produced the build's indentation errors",
)
def test_the_page_is_titled_after_the_file(self) -> None:
document = _parsed("/src/roles/web-app-x/meta/services.yml")
titles = [node.astext() for node in document.findall(nodes.title)]
self.assertEqual(titles, ["services.yml"])
def test_the_page_is_marked_orphan(self) -> None:
document = _parsed("/src/compose.yml")
fields = [
name.astext()
for info in document.findall(nodes.docinfo)
for field in info
for name in field.findall(nodes.field_name)
]
self.assertEqual(
fields,
["orphan"],
"without it every yaml page reports itself missing from the toctree",
)
def test_the_docinfo_comes_first_where_sphinx_reads_it(self) -> None:
document = _parsed("/src/compose.yml")
self.assertIsInstance(
document[0],
nodes.docinfo,
"sphinx reads metadata from the first child only",
)
if __name__ == "__main__":
unittest.main()