Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixes for Sphinx v7 #22

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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