diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5e2fd02..7c1b37b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,5 +41,5 @@ repos: args: [--config-file=pyproject.toml] additional_dependencies: - types-docutils - - sphinx~=4.1 + - sphinx~=7.0 - pytest diff --git a/src/sphinx_pytest/plugin.py b/src/sphinx_pytest/plugin.py index 1a1a5c0..c6363f7 100644 --- a/src/sphinx_pytest/plugin.py +++ b/src/sphinx_pytest/plugin.py @@ -9,8 +9,8 @@ from docutils import nodes from docutils.core import Publisher import pytest +from sphinx import version_info as sphinx_version_info from sphinx.environment import BuildEnvironment -from sphinx.testing.path import path from sphinx.testing.util import SphinxTestApp from .builders import DoctreeBuilder @@ -90,12 +90,22 @@ def doctrees(self) -> dict[str, nodes.document] | Doctrees: except AttributeError: return Doctrees(self.env) - def pformat(self, docname: str = "index") -> str: + def pformat( + self, docname: str = "index", pop_doc_attrs=("translation_progress",) + ) -> str: """Return an indented pseudo-XML representation. The src directory is replaced with , for reproducibility. + + :param pop_doc_attrs: Remove these attributes of the doctree node, + before converting to text. + By default, ``translation_progress`` is removed for compatibility + (added in sphinx 7.1). """ - text = self.doctrees[docname].pformat() + doctree = self.doctrees[docname].deepcopy() + for attr_name in pop_doc_attrs: + doctree.attributes.pop(attr_name, None) + text = doctree.pformat() return text.replace(str(self._app.srcdir) + os.sep, "/").rstrip() def get_resolved_doctree(self, docname: str = "index") -> nodes.document: @@ -109,9 +119,22 @@ def get_resolved_doctree(self, docname: str = "index") -> nodes.document: # https://github.com/sphinx-doc/sphinx/blob/05a898ecb4ff8e654a053a1ba5131715a4514812/sphinx/environment/__init__.py#L538 return doctree - def get_resolved_pformat(self, docname: str = "index") -> str: - """Return the pformat of the doctree after post-transforms.""" - text = self.get_resolved_doctree(docname).pformat() + def get_resolved_pformat( + self, docname: str = "index", pop_doc_attrs=("translation_progress",) + ) -> str: + """Return an indented pseudo-XML representation, after post-transforms. + + The src directory is replaced with , for reproducibility. + + :param pop_doc_attrs: Remove these attributes of the doctree node, + before converting to text. + By default, ``translation_progress`` is removed for compatibility + (added in sphinx 7.1). + """ + doctree = self.get_resolved_doctree(docname) + for attr_name in pop_doc_attrs: + doctree.attributes.pop(attr_name, None) + text = doctree.pformat() return text.replace(str(self._app.srcdir) + os.sep, "/").rstrip() @@ -141,9 +164,17 @@ def __call__( self.srcdir.joinpath(filename).parent.mkdir(parents=True, exist_ok=True) self.srcdir.joinpath(filename).write_text(content, encoding="utf8") + srcdir: Any + if sphinx_version_info >= (7, 2): + srcdir = self.srcdir + else: + from sphinx.testing.path import path + + srcdir = path(str(self.srcdir)) + return AppWrapper( self._app_cls( - srcdir=path(str(self.srcdir)), + srcdir=srcdir, buildername=self.buildername, confoverrides=self._confoverrides, **kwargs,