diff --git a/sphinx/testing/util.py b/sphinx/testing/util.py index 3463dc50ecb..4d034006b38 100644 --- a/sphinx/testing/util.py +++ b/sphinx/testing/util.py @@ -1,7 +1,6 @@ """Sphinx test suite utilities""" from __future__ import annotations -import functools import os import re import sys @@ -20,24 +19,7 @@ from io import StringIO from pathlib import Path -__all__ = [ - 'Struct', 'SphinxTestApp', 'SphinxTestAppWrapperForSkipBuilding', -] - - -def assert_re_search(regex: re.Pattern, text: str, flags: int = 0) -> None: - if not re.search(regex, text, flags): - raise AssertionError(f'{regex!r} did not match {text!r}') - - -def assert_not_re_search(regex: re.Pattern, text: str, flags: int = 0) -> None: - if re.search(regex, text, flags): - raise AssertionError(f'{regex!r} did match {text!r}') - - -def assert_startswith(thing: str, prefix: str) -> None: - if not thing.startswith(prefix): - raise AssertionError(f'{thing!r} does not start with {prefix!r}') +__all__ = 'SphinxTestApp', 'SphinxTestAppWrapperForSkipBuilding' def assert_node(node: Node, cls: Any = None, xpath: str = "", **kwargs: Any) -> None: @@ -86,11 +68,6 @@ def etree_parse(path: str) -> Any: return ElementTree.parse(path) # NoQA: S314 # using known data in tests -class Struct: - def __init__(self, **kwargs: Any) -> None: - self.__dict__.update(kwargs) - - class SphinxTestApp(application.Sphinx): """ A subclass of :class:`Sphinx` that runs on the test root, with some @@ -190,18 +167,5 @@ def build(self, *args: Any, **kwargs: Any) -> None: # otherwise, we can use built cache -_unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')') - - def strip_escseq(text: str) -> str: return re.sub('\x1b.*?m', '', text) - - -def simple_decorator(f): - """ - A simple decorator that does nothing, for tests to use. - """ - @functools.wraps(f) - def wrapper(*args, **kwargs): - return f(*args, **kwargs) - return wrapper diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index af2ab3fd348..657d576249b 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -5,6 +5,7 @@ """ import sys +from types import SimpleNamespace from unittest.mock import Mock from warnings import catch_warnings @@ -14,7 +15,6 @@ from sphinx import addnodes from sphinx.ext.autodoc import ALL, ModuleLevelDocumenter, Options from sphinx.ext.autodoc.directive import DocumenterBridge, process_documenter_options -from sphinx.testing.util import SphinxTestApp, Struct # noqa: F401 from sphinx.util.docutils import LoggingReporter try: @@ -59,7 +59,7 @@ def make_directive_bridge(env): ignore_module_all = False, ) - directive = Struct( + directive = SimpleNamespace( env = env, genopt = options, result = ViewList(), diff --git a/tests/test_ext_napoleon.py b/tests/test_ext_napoleon.py index 2c434f0bd00..00b7ac1c980 100644 --- a/tests/test_ext_napoleon.py +++ b/tests/test_ext_napoleon.py @@ -1,5 +1,6 @@ """Tests for :mod:`sphinx.ext.napoleon.__init__` module.""" +import functools from collections import namedtuple from unittest import mock @@ -7,7 +8,16 @@ from sphinx.application import Sphinx from sphinx.ext.napoleon import Config, _process_docstring, _skip_member, setup -from sphinx.testing.util import simple_decorator + + +def simple_decorator(f): + """ + A simple decorator that does nothing, for tests to use. + """ + @functools.wraps(f) + def wrapper(*args, **kwargs): + return f(*args, **kwargs) + return wrapper def _private_doc(): diff --git a/tests/test_intl.py b/tests/test_intl.py index 9a47345879a..cb36491acf7 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -17,14 +17,7 @@ from docutils import nodes from sphinx import locale -from sphinx.testing.util import ( - assert_node, - assert_not_re_search, - assert_re_search, - assert_startswith, - etree_parse, - strip_escseq, -) +from sphinx.testing.util import assert_node, etree_parse, strip_escseq from sphinx.util.nodes import NodeMatcher sphinx_intl = pytest.mark.sphinx( @@ -105,7 +98,7 @@ def test_text_emit_warnings(app, warning): warnings = getwarning(warning) warning_expr = ('.*/warnings.txt:4::1: ' 'WARNING: Inline literal start-string without end-string.\n') - assert_re_search(warning_expr, warnings) + assert re.search(warning_expr, warnings), f'{warning_expr!r} did not match {warnings!r}' @sphinx_intl @@ -142,7 +135,7 @@ def test_text_subdirs(app): app.build() # --- check translation in subdirs result = (app.outdir / 'subdir' / 'index.txt').read_text(encoding='utf8') - assert_startswith(result, "1. subdir contents\n******************\n") + assert result.startswith('1. subdir contents\n******************\n') @sphinx_intl @@ -187,12 +180,12 @@ def test_text_inconsistency_warnings(app, warning): 'original': "\\[\\]", 'translated': "\\['`I18N WITH REFS INCONSISTENCY`_'\\]", }) - assert_re_search(expected_warning_expr, warnings) + assert re.search(expected_warning_expr, warnings), f'{expected_warning_expr!r} did not match {warnings!r}' expected_citation_warning_expr = ( '.*/refs_inconsistency.txt:\\d+: WARNING: Citation \\[ref2\\] is not referenced.\n' + '.*/refs_inconsistency.txt:\\d+: WARNING: citation not found: ref3') - assert_re_search(expected_citation_warning_expr, warnings) + assert re.search(expected_citation_warning_expr, warnings), f'{expected_citation_warning_expr!r} did not match {warnings!r}' @sphinx_intl @@ -235,12 +228,12 @@ def test_text_literalblock_warnings(app, warning): "\n literal block\n" "\nMISSING LITERAL BLOCK:\n" "\n