Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Aug 16, 2024
1 parent 44ea4de commit fbb03b6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
45 changes: 39 additions & 6 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

# This is increased every time an environment attribute is added
# or changed to properly invalidate pickle files.
ENV_VERSION = 63
ENV_VERSION = 64

# config status
CONFIG_UNSET = -1
Expand Down Expand Up @@ -240,7 +240,7 @@ def __getstate__(self) -> dict[str, Any]:
"""Obtains serializable data for pickling."""
__dict__ = self.__dict__.copy()
# clear unpickable attributes
__dict__.update(app=None, domains={}, events=None)
__dict__.update(app=None, domains=None, events=None)
# clear in-memory doctree caches, to reduce memory consumption and
# ensure that, upon restoring the state, the most recent pickled files
# on the disk are used instead of those from a possibly outdated state
Expand All @@ -267,6 +267,10 @@ def setup(self, app: Sphinx) -> None:
self.project = app.project
self.version = app.registry.get_envversion(app)

# initialise domains
if self.domains is None:
# if we are unpickling an environment, we need to recreate the domains
self.domains = _DomainsType.from_environment(self)
# setup domains (must do after all initialization)
self.domains._setup_domains()

Expand Down Expand Up @@ -735,11 +739,40 @@ class _DomainsType:
}) # fmt: skip

@classmethod
def from_environment(cls, env: BuildEnvironment) -> Self:
registry = env.app.registry
def from_environment(cls, env: BuildEnvironment, /) -> Self:
create_domains = env.app.registry.create_domains
# Initialise domains
domains = {domain.name: domain for domain in registry.create_domains(env)}
return cls(**domains) # type: ignore[arg-type]
if domains := {domain.name: domain for domain in create_domains(env)}:
return cls(**domains) # type: ignore[arg-type]

return cls._from_environment_no_registry(env=env)

@classmethod
def _from_environment_no_registry(cls, *, env: BuildEnvironment) -> Self:
"""Return a default instance with every domain we require."""
from sphinx.domains.c import CDomain
from sphinx.domains.changeset import ChangeSetDomain
from sphinx.domains.citation import CitationDomain
from sphinx.domains.cpp import CPPDomain
from sphinx.domains.index import IndexDomain
from sphinx.domains.javascript import JavaScriptDomain
from sphinx.domains.math import MathDomain
from sphinx.domains.python import PythonDomain
from sphinx.domains.rst import ReSTDomain
from sphinx.domains.std import StandardDomain

return cls(
c=CDomain(env),
changeset=ChangeSetDomain(env),
citation=CitationDomain(env),
cpp=CPPDomain(env),
index=IndexDomain(env),
js=JavaScriptDomain(env),
math=MathDomain(env),
py=PythonDomain(env),
rst=ReSTDomain(env),
std=StandardDomain(env),
)

def __init__(
self,
Expand Down
15 changes: 9 additions & 6 deletions sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@
from inspect import Parameter
from os import path
from types import ModuleType
from types import SimpleNamespace as Struct
from typing import TYPE_CHECKING, Any, ClassVar, cast

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.states import RSTStateMachine, Struct, state_classes
from docutils.parsers.rst.states import RSTStateMachine, state_classes
from docutils.statemachine import StringList

import sphinx
Expand Down Expand Up @@ -151,21 +152,22 @@ def autosummary_table_visit_html(self: HTML5Translator, node: autosummary_table)
# -- autodoc integration -------------------------------------------------------

class FakeApplication:
def __init__(self) -> None:
def __init__(self, *, registry: SphinxComponentRegistry) -> None:
self.doctreedir = None
self.events = None
self.extensions: dict[str, Extension] = {}
self.srcdir = None
self.config = Config()
self.project = Project('', {})
self.registry = SphinxComponentRegistry()
# self.registry.domains |= registry.domains


class FakeDirective(DocumenterBridge):
def __init__(self) -> None:
def __init__(self, *, registry: SphinxComponentRegistry) -> None:
settings = Struct(tab_width=8)
document = Struct(settings=settings)
app = FakeApplication()
app = FakeApplication(registry=registry)
app.config.add('autodoc_class_signature', 'mixed', 'env', ())
env = BuildEnvironment(app) # type: ignore[arg-type]
state = Struct(document=document)
Expand All @@ -192,10 +194,11 @@ def get_documenter(app: Sphinx, obj: Any, parent: Any) -> type[Documenter]:
else:
parent_doc_cls = ModuleDocumenter

bridge = FakeDirective(registry=app.registry)
if hasattr(parent, '__name__'):
parent_doc = parent_doc_cls(FakeDirective(), parent.__name__)
parent_doc = parent_doc_cls(bridge, parent.__name__)
else:
parent_doc = parent_doc_cls(FakeDirective(), "")
parent_doc = parent_doc_cls(bridge, "")

# Get the correct documenter class for *obj*
classes = [cls for cls in app.registry.documenters.values()
Expand Down
4 changes: 3 additions & 1 deletion sphinx/testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def __init__(
# unknown keyword arguments
**extras: Any,
) -> None:
self._buildername = buildername

assert srcdir is not None

if verbosity == -1:
Expand Down Expand Up @@ -209,7 +211,7 @@ def cleanup(self, doctrees: bool = False) -> None:
os.remove(self.docutils_conf_path)

def __repr__(self) -> str:
return f'<{self.__class__.__name__} buildername={self.builder.name!r}>'
return f'<{self.__class__.__name__} buildername={self._buildername!r}>'

def build(self, force_all: bool = False, filenames: list[str] | None = None) -> None:
self.env._pickled_doctree_cache.clear()
Expand Down
2 changes: 1 addition & 1 deletion tests/js/fixtures/cpp/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/js/fixtures/multiterm/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/js/fixtures/partial/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/js/fixtures/titles/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions tests/test_extensions/test_ext_autosummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
from io import StringIO
from pathlib import Path
from unittest.mock import Mock, patch

import pytest
Expand Down Expand Up @@ -37,7 +36,6 @@
'extensions': ['sphinx.ext.autosummary'],
'autosummary_generate': True,
'autosummary_generate_overwrite': False,
'source_suffix': '.rst',
}


Expand Down Expand Up @@ -219,13 +217,10 @@ def str_content(elem):
def test_escaping(app):
app.build(force_all=True)

outdir = Path(app.builder.outdir)

docpage = outdir / 'underscore_module_.xml'
docpage = app.builder.outdir / 'underscore_module_.xml'
assert docpage.exists()

title = etree_parse(docpage).find('section/title')

assert str_content(title) == 'underscore_module_'


Expand Down

0 comments on commit fbb03b6

Please sign in to comment.