| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
import re |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from docutils import __version_info__ as docutils_version_info |
| 7 |
from docutils import nodes |
| 8 |
import pytest |
| 9 |
from sphinx import version_info |
| 10 |
from sphinx.testing.util import SphinxTestApp |
| 11 |
|
| 12 |
from sphinx_design._compat import findall |
| 13 |
|
| 14 |
pytest_plugins = "sphinx.testing.fixtures" |
| 15 |
|
| 16 |
if version_info >= (7, 2): |
| 17 |
# see https://github.com/sphinx-doc/sphinx/pull/11526 |
| 18 |
from pathlib import Path as sphinx_path # noqa: N813 |
| 19 |
else: |
| 20 |
from sphinx.testing.path import path as sphinx_path # type: ignore[no-redef] |
| 21 |
|
| 22 |
|
| 23 |
class SphinxBuilder: |
| 24 |
def __init__(self, app: SphinxTestApp, src_path: Path): |
| 25 |
self.app = app |
| 26 |
self._src_path = src_path |
| 27 |
|
| 28 |
@property |
| 29 |
def src_path(self) -> Path: |
| 30 |
return self._src_path |
| 31 |
|
| 32 |
@property |
| 33 |
def out_path(self) -> Path: |
| 34 |
return Path(self.app.outdir) |
| 35 |
|
| 36 |
def build(self, assert_pass=True): |
| 37 |
self.app.build() |
| 38 |
if assert_pass: |
| 39 |
assert self.warnings == "", self.status |
| 40 |
return self |
| 41 |
|
| 42 |
@property |
| 43 |
def status(self): |
| 44 |
return self.app._status.getvalue() |
| 45 |
|
| 46 |
@property |
| 47 |
def warnings(self): |
| 48 |
return self.app._warning.getvalue() |
| 49 |
|
| 50 |
def get_doctree( |
| 51 |
self, docname: str, post_transforms: bool = False |
| 52 |
) -> nodes.document: |
| 53 |
doctree = self.app.env.get_doctree(docname) |
| 54 |
if post_transforms: |
| 55 |
self.app.env.apply_post_transforms(doctree, docname) |
| 56 |
# make source path consistent for test comparisons |
| 57 |
for node in findall(doctree)(include_self=True): |
| 58 |
if not (hasattr(node, "get") and node.get("source")): |
| 59 |
continue |
| 60 |
node["source"] = Path(node["source"]).relative_to(self.src_path).as_posix() |
| 61 |
if node["source"].endswith(".rst"): |
| 62 |
node["source"] = node["source"][:-4] |
| 63 |
elif node["source"].endswith(".md"): |
| 64 |
node["source"] = node["source"][:-3] |
| 65 |
# remove mathjax classes added by myst parser |
| 66 |
if doctree.children and isinstance(doctree.children[0], nodes.section): |
| 67 |
doctree.children[0]["classes"] = [] |
| 68 |
return doctree |
| 69 |
|
| 70 |
|
| 71 |
@pytest.fixture() |
| 72 |
def sphinx_builder(tmp_path: Path, make_app, monkeypatch): |
| 73 |
def _create_project( |
| 74 |
buildername: str = "html", conf_kwargs: dict[str, Any] | None = None |
| 75 |
): |
| 76 |
src_path = tmp_path / "srcdir" |
| 77 |
src_path.mkdir() |
| 78 |
conf_kwargs = conf_kwargs or { |
| 79 |
"extensions": ["myst_parser", "sphinx_design"], |
| 80 |
"myst_enable_extensions": ["colon_fence"], |
| 81 |
} |
| 82 |
content = "\n".join( |
| 83 |
[f"{key} = {value!r}" for key, value in conf_kwargs.items()] |
| 84 |
) |
| 85 |
src_path.joinpath("conf.py").write_text(content, encoding="utf8") |
| 86 |
app = make_app( |
| 87 |
srcdir=sphinx_path(os.path.abspath(str(src_path))), # noqa: PTH100 |
| 88 |
buildername=buildername, |
| 89 |
) |
| 90 |
return SphinxBuilder(app, src_path) |
| 91 |
|
| 92 |
yield _create_project |
| 93 |
|
| 94 |
|
| 95 |
DOCUTILS_0_22_PLUS = docutils_version_info >= (0, 22) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.fixture |
| 99 |
def normalize_doctree_xml(): |
| 100 |
"""Normalize docutils XML output for cross-version compatibility. |
| 101 |
|
| 102 |
In docutils 0.22+, boolean attributes are serialized as "1"/"0" |
| 103 |
instead of "True"/"False". This function normalizes to the old format |
| 104 |
for consistent test fixtures. |
| 105 |
""" |
| 106 |
|
| 107 |
def _normalize(text: str) -> str: |
| 108 |
if DOCUTILS_0_22_PLUS: |
| 109 |
# Normalize new format (1/0) to old format (1/0) |
| 110 |
# Only replace when it's clearly a boolean attribute value |
| 111 |
# Pattern: attribute="1" or attribute="0" |
| 112 |
attrs = [ |
| 113 |
"checked", |
| 114 |
"force", |
| 115 |
"has_title", |
| 116 |
"internal", |
| 117 |
"is_div", |
| 118 |
"linenos", |
| 119 |
"opened", |
| 120 |
"refexplicit", |
| 121 |
"refwarn", |
| 122 |
"selected", |
| 123 |
] |
| 124 |
text = re.sub(rf' ({"|".join(attrs)})="1"', r' \1="True"', text) |
| 125 |
text = re.sub(rf' ({"|".join(attrs)})="0"', r' \1="False"', text) |
| 126 |
return text |
| 127 |
|
| 128 |
return _normalize |