mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Enable automatic formatting for `sphinx/testing/
` (#12969)
This commit is contained in:
parent
a0fe15c00d
commit
a404f3e66c
@ -476,7 +476,6 @@ exclude = [
|
|||||||
"sphinx/ext/todo.py",
|
"sphinx/ext/todo.py",
|
||||||
"sphinx/ext/viewcode.py",
|
"sphinx/ext/viewcode.py",
|
||||||
"sphinx/registry.py",
|
"sphinx/registry.py",
|
||||||
"sphinx/testing/*",
|
|
||||||
"sphinx/transforms/*",
|
"sphinx/transforms/*",
|
||||||
"sphinx/writers/*",
|
"sphinx/writers/*",
|
||||||
]
|
]
|
||||||
|
@ -86,7 +86,7 @@ def app_params(
|
|||||||
kwargs: dict[str, Any] = {}
|
kwargs: dict[str, Any] = {}
|
||||||
|
|
||||||
# to avoid stacking positional args
|
# to avoid stacking positional args
|
||||||
for info in reversed(list(request.node.iter_markers("sphinx"))):
|
for info in reversed(list(request.node.iter_markers('sphinx'))):
|
||||||
pargs |= dict(enumerate(info.args))
|
pargs |= dict(enumerate(info.args))
|
||||||
kwargs.update(info.kwargs)
|
kwargs.update(info.kwargs)
|
||||||
|
|
||||||
@ -203,6 +203,7 @@ def make_app(test_params: dict[str, Any]) -> Iterator[Callable[[], SphinxTestApp
|
|||||||
app_ = SphinxTestApp(*args, **kwargs)
|
app_ = SphinxTestApp(*args, **kwargs)
|
||||||
apps.append(app_)
|
apps.append(app_)
|
||||||
return app_
|
return app_
|
||||||
|
|
||||||
yield make
|
yield make
|
||||||
|
|
||||||
sys.path[:] = syspath
|
sys.path[:] = syspath
|
||||||
|
@ -12,9 +12,11 @@ if TYPE_CHECKING:
|
|||||||
import builtins
|
import builtins
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
warnings.warn("'sphinx.testing.path' is deprecated. "
|
warnings.warn(
|
||||||
"Use 'os.path' or 'pathlib' instead.",
|
"'sphinx.testing.path' is deprecated. " "Use 'os.path' or 'pathlib' instead.",
|
||||||
RemovedInSphinx90Warning, stacklevel=2)
|
RemovedInSphinx90Warning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
|
||||||
FILESYSTEMENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding()
|
FILESYSTEMENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding()
|
||||||
|
|
||||||
@ -86,7 +88,7 @@ class path(str):
|
|||||||
def rmtree(
|
def rmtree(
|
||||||
self,
|
self,
|
||||||
ignore_errors: bool = False,
|
ignore_errors: bool = False,
|
||||||
onerror: Callable[[Callable[..., Any], str, Any], object] | None = None,
|
onerror: Callable[[Callable[..., Any], str, Any], object] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Removes the file or directory and any files or directories it may
|
Removes the file or directory and any files or directories it may
|
||||||
|
@ -29,7 +29,7 @@ if TYPE_CHECKING:
|
|||||||
from docutils.nodes import Node
|
from docutils.nodes import Node
|
||||||
|
|
||||||
|
|
||||||
def assert_node(node: Node, cls: Any = None, xpath: str = "", **kwargs: Any) -> None:
|
def assert_node(node: Node, cls: Any = None, xpath: str = '', **kwargs: Any) -> None:
|
||||||
if cls:
|
if cls:
|
||||||
if isinstance(cls, list):
|
if isinstance(cls, list):
|
||||||
assert_node(node, cls[0], xpath=xpath, **kwargs)
|
assert_node(node, cls[0], xpath=xpath, **kwargs)
|
||||||
@ -37,36 +37,43 @@ def assert_node(node: Node, cls: Any = None, xpath: str = "", **kwargs: Any) ->
|
|||||||
if isinstance(cls[1], tuple):
|
if isinstance(cls[1], tuple):
|
||||||
assert_node(node, cls[1], xpath=xpath, **kwargs)
|
assert_node(node, cls[1], xpath=xpath, **kwargs)
|
||||||
else:
|
else:
|
||||||
assert isinstance(node, nodes.Element), \
|
assert (
|
||||||
'The node%s does not have any children' % xpath
|
isinstance(node, nodes.Element)
|
||||||
assert len(node) == 1, \
|
), f'The node{xpath} does not have any children' # fmt: skip
|
||||||
'The node%s has %d child nodes, not one' % (xpath, len(node))
|
assert (
|
||||||
assert_node(node[0], cls[1:], xpath=xpath + "[0]", **kwargs)
|
len(node) == 1
|
||||||
|
), f'The node{xpath} has {len(node)} child nodes, not one'
|
||||||
|
assert_node(node[0], cls[1:], xpath=xpath + '[0]', **kwargs)
|
||||||
elif isinstance(cls, tuple):
|
elif isinstance(cls, tuple):
|
||||||
assert isinstance(node, list | nodes.Element), \
|
assert (
|
||||||
'The node%s does not have any items' % xpath
|
isinstance(node, list | nodes.Element)
|
||||||
assert len(node) == len(cls), \
|
), f'The node{xpath} does not have any items' # fmt: skip
|
||||||
'The node%s has %d child nodes, not %r' % (xpath, len(node), len(cls))
|
assert (
|
||||||
|
len(node) == len(cls)
|
||||||
|
), f'The node{xpath} has {len(node)} child nodes, not {len(cls)!r}' # fmt: skip
|
||||||
for i, nodecls in enumerate(cls):
|
for i, nodecls in enumerate(cls):
|
||||||
path = xpath + "[%d]" % i
|
path = xpath + f'[{i}]'
|
||||||
assert_node(node[i], nodecls, xpath=path, **kwargs)
|
assert_node(node[i], nodecls, xpath=path, **kwargs)
|
||||||
elif isinstance(cls, str):
|
elif isinstance(cls, str):
|
||||||
assert node == cls, f'The node {xpath!r} is not {cls!r}: {node!r}'
|
assert node == cls, f'The node {xpath!r} is not {cls!r}: {node!r}'
|
||||||
else:
|
else:
|
||||||
assert isinstance(node, cls), \
|
assert (
|
||||||
f'The node{xpath} is not subclass of {cls!r}: {node!r}'
|
isinstance(node, cls)
|
||||||
|
), f'The node{xpath} is not subclass of {cls!r}: {node!r}' # fmt: skip
|
||||||
|
|
||||||
if kwargs:
|
if kwargs:
|
||||||
assert isinstance(node, nodes.Element), \
|
assert (
|
||||||
'The node%s does not have any attributes' % xpath
|
isinstance(node, nodes.Element)
|
||||||
|
), f'The node{xpath} does not have any attributes' # fmt: skip
|
||||||
|
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
if key not in node:
|
if key not in node:
|
||||||
if (key := key.replace('_', '-')) not in node:
|
if (key := key.replace('_', '-')) not in node:
|
||||||
msg = f'The node{xpath} does not have {key!r} attribute: {node!r}'
|
msg = f'The node{xpath} does not have {key!r} attribute: {node!r}'
|
||||||
raise AssertionError(msg)
|
raise AssertionError(msg)
|
||||||
assert node[key] == value, \
|
assert (
|
||||||
f'The node{xpath}[{key}] is not {value!r}: {node[key]!r}'
|
node[key] == value
|
||||||
|
), f'The node{xpath}[{key}] is not {value!r}: {node[key]!r}'
|
||||||
|
|
||||||
|
|
||||||
# keep this to restrict the API usage and to have a correct return type
|
# keep this to restrict the API usage and to have a correct return type
|
||||||
@ -138,7 +145,7 @@ class SphinxTestApp(sphinx.application.Sphinx):
|
|||||||
# but allow the stream to be /dev/null by passing verbosity=-1
|
# but allow the stream to be /dev/null by passing verbosity=-1
|
||||||
status = None if quiet else StringIO()
|
status = None if quiet else StringIO()
|
||||||
elif not isinstance(status, StringIO):
|
elif not isinstance(status, StringIO):
|
||||||
err = "%r must be an io.StringIO object, got: %s" % ('status', type(status))
|
err = f"'status' must be an io.StringIO object, got: {type(status)}"
|
||||||
raise TypeError(err)
|
raise TypeError(err)
|
||||||
|
|
||||||
if warning is None:
|
if warning is None:
|
||||||
@ -146,7 +153,7 @@ class SphinxTestApp(sphinx.application.Sphinx):
|
|||||||
# but allow the stream to be /dev/null by passing verbosity=-1
|
# but allow the stream to be /dev/null by passing verbosity=-1
|
||||||
warning = None if quiet else StringIO()
|
warning = None if quiet else StringIO()
|
||||||
elif not isinstance(warning, StringIO):
|
elif not isinstance(warning, StringIO):
|
||||||
err = '%r must be an io.StringIO object, got: %s' % ('warning', type(warning))
|
err = f"'warning' must be an io.StringIO object, got: {type(warning)}"
|
||||||
raise TypeError(err)
|
raise TypeError(err)
|
||||||
|
|
||||||
self.docutils_conf_path = srcdir / 'docutils.conf'
|
self.docutils_conf_path = srcdir / 'docutils.conf'
|
||||||
@ -170,11 +177,21 @@ class SphinxTestApp(sphinx.application.Sphinx):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
srcdir, confdir, outdir, doctreedir, buildername,
|
srcdir,
|
||||||
confoverrides=confoverrides, status=status, warning=warning,
|
confdir,
|
||||||
freshenv=freshenv, warningiserror=warningiserror, tags=tags,
|
outdir,
|
||||||
verbosity=verbosity, parallel=parallel,
|
doctreedir,
|
||||||
pdb=pdb, exception_on_warning=exception_on_warning,
|
buildername,
|
||||||
|
confoverrides=confoverrides,
|
||||||
|
status=status,
|
||||||
|
warning=warning,
|
||||||
|
freshenv=freshenv,
|
||||||
|
warningiserror=warningiserror,
|
||||||
|
tags=tags,
|
||||||
|
verbosity=verbosity,
|
||||||
|
parallel=parallel,
|
||||||
|
pdb=pdb,
|
||||||
|
exception_on_warning=exception_on_warning,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
@ -213,7 +230,9 @@ class SphinxTestApp(sphinx.application.Sphinx):
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'<{self.__class__.__name__} buildername={self._builder_name!r}>'
|
return f'<{self.__class__.__name__} buildername={self._builder_name!r}>'
|
||||||
|
|
||||||
def build(self, force_all: bool = False, filenames: list[str] | None = None) -> None:
|
def build(
|
||||||
|
self, force_all: bool = False, filenames: list[str] | None = None
|
||||||
|
) -> None:
|
||||||
self.env._pickled_doctree_cache.clear()
|
self.env._pickled_doctree_cache.clear()
|
||||||
super().build(force_all, filenames)
|
super().build(force_all, filenames)
|
||||||
|
|
||||||
@ -225,7 +244,9 @@ class SphinxTestAppWrapperForSkipBuilding(SphinxTestApp):
|
|||||||
if it has already been built and there are any output files.
|
if it has already been built and there are any output files.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def build(self, force_all: bool = False, filenames: list[str] | None = None) -> None:
|
def build(
|
||||||
|
self, force_all: bool = False, filenames: list[str] | None = None
|
||||||
|
) -> None:
|
||||||
if not os.listdir(self.outdir):
|
if not os.listdir(self.outdir):
|
||||||
# if listdir is empty, do build.
|
# if listdir is empty, do build.
|
||||||
super().build(force_all, filenames)
|
super().build(force_all, filenames)
|
||||||
|
Loading…
Reference in New Issue
Block a user