Skip to content

Commit

Permalink
🐛 Fixes for Sphinx v7 (#22)
Browse files Browse the repository at this point in the history
Accounts for change in `srcdir` type in sphinx-doc/sphinx#11526,
and now removes `translation_progress` attribute of the doctree by default (added in sphinx 7.1).
  • Loading branch information
chrisjsewell authored Sep 20, 2023
1 parent b96eb29 commit 87e0fa2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ repos:
args: [--config-file=pyproject.toml]
additional_dependencies:
- types-docutils
- sphinx~=4.1
- sphinx~=7.0
- pytest
45 changes: 38 additions & 7 deletions src/sphinx_pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <src>, 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, "<src>/").rstrip()

def get_resolved_doctree(self, docname: str = "index") -> nodes.document:
Expand All @@ -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 <src>, 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, "<src>/").rstrip()


Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 87e0fa2

Please sign in to comment.