From a8f76a846b271b48e6ff8a43f430634192de88a4 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 20 Sep 2023 07:20:00 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20for=20Sphinx=20v7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pre-commit-config.yaml | 2 +- src/sphinx_pytest/plugin.py | 38 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) 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..f438ee1 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,18 @@ 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. + By default, the src directory is replaced with , for reproducibility, + add the ``translation_progress`` is removed (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 +115,18 @@ 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. + + By default, the src directory is replaced with , for reproducibility, + add the ``translation_progress`` is removed (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 +156,16 @@ def __call__( self.srcdir.joinpath(filename).parent.mkdir(parents=True, exist_ok=True) self.srcdir.joinpath(filename).write_text(content, encoding="utf8") + if sphinx_version_info >= (7, 2): + srcdir = self.srcdir + else: + from sphinx.testing.path import path + + srcdir = path(str(self.srcdir)) # type: ignore + return AppWrapper( self._app_cls( - srcdir=path(str(self.srcdir)), + srcdir=srcdir, # type: ignore buildername=self.buildername, confoverrides=self._confoverrides, **kwargs, From 4362eae00147d0ddc0ad79f67c8d95f4afc955f8 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 20 Sep 2023 07:26:14 -0300 Subject: [PATCH 2/2] Update plugin.py --- src/sphinx_pytest/plugin.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/sphinx_pytest/plugin.py b/src/sphinx_pytest/plugin.py index f438ee1..c6363f7 100644 --- a/src/sphinx_pytest/plugin.py +++ b/src/sphinx_pytest/plugin.py @@ -95,8 +95,12 @@ def pformat( ) -> str: """Return an indented pseudo-XML representation. - By default, the src directory is replaced with , for reproducibility, - add the ``translation_progress`` is removed (added in sphinx 7.1). + 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.doctrees[docname].deepcopy() for attr_name in pop_doc_attrs: @@ -120,8 +124,12 @@ def get_resolved_pformat( ) -> str: """Return an indented pseudo-XML representation, after post-transforms. - By default, the src directory is replaced with , for reproducibility, - add the ``translation_progress`` is removed (added in sphinx 7.1). + 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: @@ -156,16 +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)) # type: ignore + srcdir = path(str(self.srcdir)) return AppWrapper( self._app_cls( - srcdir=srcdir, # type: ignore + srcdir=srcdir, buildername=self.buildername, confoverrides=self._confoverrides, **kwargs,