Skip to content

Commit 462404c

Browse files
committed
Organise tests into directories
1 parent 841f2bd commit 462404c

File tree

121 files changed

+467
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+467
-442
lines changed

.ruff.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ select = [
510510
]
511511

512512
# these tests need old ``typing`` generic aliases
513-
"tests/test_util_typing.py" = ["UP006", "UP035"]
514-
"tests/typing_test_data.py" = ["FA100", "UP006", "UP035"]
513+
"tests/test_util/test_util_typing.py" = ["UP006", "UP035"]
514+
"tests/test_util/typing_test_data.py" = ["FA100", "UP006", "UP035"]
515515

516516
"utils/*" = [
517517
"T201", # whitelist ``print`` for stdout messages
@@ -520,3 +520,8 @@ select = [
520520

521521
[lint.flake8-quotes]
522522
inline-quotes = "single"
523+
524+
[lint.isort]
525+
forced-separate = [
526+
"tests",
527+
]

sphinx/testing/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
DEFAULT_ENABLED_MARKERS = [
2121
(
22-
'sphinx(builder, testroot=None, freshenv=False, confoverrides=None, tags=None,'
23-
' docutilsconf=None, parallel=0): arguments to initialize the sphinx test application.'
22+
'sphinx(builder, testroot=None, freshenv=False, confoverrides=None, tags=None, '
23+
'docutils_conf=None, parallel=0): arguments to initialize the sphinx test application.'
2424
),
2525
'test_params(shared_result=...): test parameters.',
2626
]

sphinx/testing/util.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
from xml.etree import ElementTree
1111

1212
from docutils import nodes
13-
from docutils.parsers.rst import directives, roles
1413

15-
from sphinx import application, locale
16-
from sphinx.pycode import ModuleAnalyzer
14+
from sphinx import application
1715

1816
if TYPE_CHECKING:
1917
from io import StringIO
@@ -89,14 +87,14 @@ def __init__(
8987
status: IO | None = None,
9088
warning: IO | None = None,
9189
tags: list[str] | None = None,
92-
docutilsconf: str | None = None,
90+
docutils_conf: str | None = None,
9391
parallel: int = 0,
9492
) -> None:
9593
assert srcdir is not None
9694

9795
self.docutils_conf_path = srcdir / 'docutils.conf'
98-
if docutilsconf is not None:
99-
self.docutils_conf_path.write_text(docutilsconf, encoding='utf8')
96+
if docutils_conf is not None:
97+
self.docutils_conf_path.write_text(docutils_conf, encoding='utf8')
10098

10199
if builddir is None:
102100
builddir = srcdir / '_build'
@@ -108,35 +106,32 @@ def __init__(
108106
doctreedir.mkdir(parents=True, exist_ok=True)
109107
if confoverrides is None:
110108
confoverrides = {}
111-
warningiserror = False
112109

113110
self._saved_path = sys.path.copy()
114-
self._saved_directives = directives._directives.copy() # type: ignore[attr-defined]
115-
self._saved_roles = roles._roles.copy() # type: ignore[attr-defined]
116-
117-
self._saved_nodeclasses = {v for v in dir(nodes.GenericNodeVisitor)
118-
if v.startswith('visit_')}
119111

120112
try:
121-
super().__init__(srcdir, confdir, outdir, doctreedir,
122-
buildername, confoverrides, status, warning,
123-
freshenv, warningiserror, tags, parallel=parallel)
113+
super().__init__(
114+
srcdir, confdir, outdir, doctreedir,
115+
buildername, confoverrides, status, warning, freshenv,
116+
warningiserror=False, tags=tags, parallel=parallel,
117+
)
124118
except Exception:
125119
self.cleanup()
126120
raise
127121

128122
def cleanup(self, doctrees: bool = False) -> None:
129-
ModuleAnalyzer.cache.clear()
130-
locale.translators.clear()
131-
sys.path[:] = self._saved_path
132-
sys.modules.pop('autodoc_fodder', None)
133-
directives._directives = self._saved_directives # type: ignore[attr-defined]
134-
roles._roles = self._saved_roles # type: ignore[attr-defined]
135-
for method in dir(nodes.GenericNodeVisitor):
136-
if method.startswith('visit_') and \
137-
method not in self._saved_nodeclasses:
138-
delattr(nodes.GenericNodeVisitor, 'visit_' + method[6:])
139-
delattr(nodes.GenericNodeVisitor, 'depart_' + method[6:])
123+
# ModuleAnalyzer.cache.clear()
124+
# locale.translators.clear()
125+
# sys.path[:] = self._saved_path
126+
# sys.modules.pop('autodoc_fodder', None)
127+
# directives._directives.clear() # type: ignore[attr-defined]
128+
# roles._roles.clear() # type: ignore[attr-defined]
129+
# for node in additional_nodes:
130+
# delattr(nodes.GenericNodeVisitor, f'visit_{node.__name__}')
131+
# delattr(nodes.GenericNodeVisitor, f'depart_{node.__name__}')
132+
# delattr(nodes.SparseNodeVisitor, f'visit_{node.__name__}')
133+
# delattr(nodes.SparseNodeVisitor, f'depart_{node.__name__}')
134+
# additional_nodes.clear()
140135
with contextlib.suppress(FileNotFoundError):
141136
os.remove(self.docutils_conf_path)
142137

tests/conftest.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import os
2+
import sys
23
from pathlib import Path
34

45
import docutils
56
import pytest
7+
from docutils import nodes
8+
from docutils.parsers.rst import directives, roles
69

710
import sphinx
811
import sphinx.locale
12+
import sphinx.pycode
13+
from sphinx.util.docutils import additional_nodes
914

1015

1116
def _init_console(locale_dir=sphinx.locale._LOCALE_DIR, catalog='sphinx'):
@@ -30,11 +35,33 @@ def _init_console(locale_dir=sphinx.locale._LOCALE_DIR, catalog='sphinx'):
3035

3136
@pytest.fixture(scope='session')
3237
def rootdir():
33-
return Path(__file__).parent.absolute() / 'roots'
38+
return Path(__file__).parent.resolve() / 'roots'
3439

3540

3641
def pytest_report_header(config):
3742
header = f"libraries: Sphinx-{sphinx.__display_version__}, docutils-{docutils.__version__}"
3843
if hasattr(config, '_tmp_path_factory'):
3944
header += f"\nbase tmp_path: {config._tmp_path_factory.getbasetemp()}"
4045
return header
46+
47+
48+
@pytest.fixture(autouse=True)
49+
def _cleanup_docutils():
50+
saved_path = sys.path
51+
yield # run the test
52+
sys.path[:] = saved_path
53+
54+
# clean up Docutils global state
55+
directives._directives.clear() # type: ignore[attr-defined]
56+
roles._roles.clear() # type: ignore[attr-defined]
57+
for node in additional_nodes:
58+
delattr(nodes.GenericNodeVisitor, f'visit_{node.__name__}')
59+
delattr(nodes.GenericNodeVisitor, f'depart_{node.__name__}')
60+
delattr(nodes.SparseNodeVisitor, f'visit_{node.__name__}')
61+
delattr(nodes.SparseNodeVisitor, f'depart_{node.__name__}')
62+
additional_nodes.clear()
63+
64+
# clean up Sphinx global state
65+
sphinx.locale.translators.clear()
66+
sphinx.pycode.ModuleAnalyzer.cache.clear()
67+
sys.modules.pop('autodoc_fodder', None)

tests/roots/test-ext-doctest-skipif/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
exclude_patterns = ['_build']
77

88
doctest_global_setup = '''
9-
from tests.test_ext_doctest import record
9+
from tests.test_extensions.test_ext_doctest import record
1010
1111
record('doctest_global_setup', 'body', True)
1212
'''

tests/roots/test-ext-doctest/doctest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Special directives
139139

140140
.. testcleanup:: *
141141

142-
from tests import test_ext_doctest
142+
from tests.test_extensions import test_ext_doctest
143143
test_ext_doctest.cleanup_call()
144144

145145
non-ASCII result

tests/test_builders/__init__.py

Whitespace-only changes.

tests/test_build.py renamed to tests/test_builders/test_build.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import shutil
55
from contextlib import contextmanager
6-
from pathlib import Path
76
from unittest import mock
87

98
import pytest
@@ -12,6 +11,8 @@
1211
from sphinx.cmd.build import build_main
1312
from sphinx.errors import SphinxError
1413

14+
from tests.utils import TESTS_ROOT
15+
1516

1617
def request_session_head(url, **kwargs):
1718
response = mock.Mock()
@@ -63,12 +64,12 @@ def test_root_doc_not_found(tmp_path, make_app):
6364

6465
app = make_app('dummy', srcdir=tmp_path)
6566
with pytest.raises(SphinxError):
66-
app.builder.build_all() # no index.rst
67+
app.build(force_all=True) # no index.rst
6768

6869

6970
@pytest.mark.sphinx(buildername='text', testroot='circular')
7071
def test_circular_toctree(app, status, warning):
71-
app.builder.build_all()
72+
app.build(force_all=True)
7273
warnings = warning.getvalue()
7374
assert (
7475
'circular toctree references detected, ignoring: '
@@ -80,7 +81,7 @@ def test_circular_toctree(app, status, warning):
8081

8182
@pytest.mark.sphinx(buildername='text', testroot='numbered-circular')
8283
def test_numbered_circular_toctree(app, status, warning):
83-
app.builder.build_all()
84+
app.build(force_all=True)
8485
warnings = warning.getvalue()
8586
assert (
8687
'circular toctree references detected, ignoring: '
@@ -92,7 +93,7 @@ def test_numbered_circular_toctree(app, status, warning):
9293

9394
@pytest.mark.sphinx(buildername='dummy', testroot='images')
9495
def test_image_glob(app, status, warning):
95-
app.builder.build_all()
96+
app.build(force_all=True)
9697

9798
# index.rst
9899
doctree = app.env.get_doctree('index')
@@ -155,7 +156,7 @@ def force_colors():
155156
def test_log_no_ansi_colors(tmp_path):
156157
with force_colors():
157158
wfile = tmp_path / 'warnings.txt'
158-
srcdir = Path(__file__).parent / 'roots/test-nitpicky-warnings'
159+
srcdir = TESTS_ROOT / 'roots' / 'test-nitpicky-warnings'
159160
argv = list(map(str, ['-b', 'html', srcdir, tmp_path, '-n', '-w', wfile]))
160161
retcode = build_main(argv)
161162
assert retcode == 0
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)