-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] move utilities and static data into dedicated modules and rem…
…ove ``html5lib`` (#12173) Since #12168, HTML files are now XML compliant, hence ``html5lib`` is no more needed as a testing dependencies.
- Loading branch information
Showing
28 changed files
with
289 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Final | ||
|
||
FIGURE_CAPTION: Final[str] = ".//figure/figcaption/p" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
import textwrap | ||
from typing import TYPE_CHECKING | ||
from xml.etree.ElementTree import tostring | ||
|
||
if TYPE_CHECKING: | ||
import os | ||
from collections.abc import Callable, Iterable, Sequence | ||
from xml.etree.ElementTree import Element, ElementTree | ||
|
||
|
||
def _get_text(node: Element) -> str: | ||
if node.text is not None: | ||
# the node has only one text | ||
return node.text | ||
|
||
# the node has tags and text; gather texts just under the node | ||
return ''.join(n.tail or '' for n in node) | ||
|
||
|
||
def _prettify(nodes: Iterable[Element]) -> str: | ||
def pformat(node: Element) -> str: | ||
return tostring(node, encoding='unicode', method='html') | ||
|
||
return ''.join(f'(i={index}) {pformat(node)}\n' for index, node in enumerate(nodes)) | ||
|
||
|
||
def check_xpath( | ||
etree: ElementTree, | ||
filename: str | os.PathLike[str], | ||
xpath: str, | ||
check: str | re.Pattern[str] | Callable[[Sequence[Element]], None] | None, | ||
be_found: bool = True, | ||
*, | ||
min_count: int = 1, | ||
) -> None: | ||
"""Check that one or more nodes satisfy a predicate. | ||
:param etree: The element tree. | ||
:param filename: The element tree source name (for errors only). | ||
:param xpath: An XPath expression to use. | ||
:param check: Optional regular expression or a predicate the nodes must validate. | ||
:param be_found: If false, negate the predicate. | ||
:param min_count: Minimum number of nodes expected to satisfy the predicate. | ||
* If *check* is empty (``''``), only the minimum count is checked. | ||
* If *check* is ``None``, no node should satisfy the XPath expression. | ||
""" | ||
nodes = etree.findall(xpath) | ||
assert isinstance(nodes, list) | ||
|
||
if check is None: | ||
# use == to have a nice pytest diff | ||
assert nodes == [], f'found nodes matching xpath {xpath!r} in file {filename}' | ||
return | ||
|
||
assert len(nodes) >= min_count, (f'expecting at least {min_count} node(s) ' | ||
f'to satisfy {xpath!r} in file {filename}') | ||
|
||
if check == '': | ||
return | ||
|
||
if callable(check): | ||
check(nodes) | ||
return | ||
|
||
rex = re.compile(check) | ||
if be_found: | ||
if any(rex.search(_get_text(node)) for node in nodes): | ||
return | ||
else: | ||
if all(not rex.search(_get_text(node)) for node in nodes): | ||
return | ||
|
||
ctx = textwrap.indent(_prettify(nodes), ' ' * 2) | ||
msg = f'{check!r} not found in any node matching {xpath!r} in file {filename}:\n{ctx}' | ||
raise AssertionError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from unittest.mock import Mock | ||
|
||
# NEVER import those objects from sphinx.ext.autodoc directly | ||
from sphinx.ext.autodoc.directive import DocumenterBridge, process_documenter_options | ||
from sphinx.util.docutils import LoggingReporter | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
from docutils.statemachine import StringList | ||
|
||
from sphinx.application import Sphinx | ||
|
||
|
||
def do_autodoc( | ||
app: Sphinx, | ||
objtype: str, | ||
name: str, | ||
options: dict[str, Any] | None = None, | ||
) -> StringList: | ||
options = {} if options is None else options.copy() | ||
app.env.temp_data.setdefault('docname', 'index') # set dummy docname | ||
doccls = app.registry.documenters[objtype] | ||
docoptions = process_documenter_options(doccls, app.config, options) | ||
state = Mock() | ||
state.document.settings.tab_width = 8 | ||
bridge = DocumenterBridge(app.env, LoggingReporter(''), docoptions, 1, state) | ||
documenter = doccls(bridge, name) | ||
documenter.generate() | ||
return bridge.result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.