| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gettext |
| 4 |
import os |
| 5 |
import sys |
| 6 |
from pathlib import Path |
| 7 |
from types import SimpleNamespace |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
import docutils |
| 11 |
import pytest |
| 12 |
|
| 13 |
import sphinx |
| 14 |
import sphinx.locale |
| 15 |
from sphinx.testing.util import _clean_up_global_state |
| 16 |
|
| 17 |
from tests.utils import TEST_ROOTS_DIR |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from collections.abc import Callable, Iterator |
| 21 |
from typing import Any |
| 22 |
|
| 23 |
from sphinx.testing.util import SphinxTestApp |
| 24 |
|
| 25 |
|
| 26 |
def _init_console( |
| 27 |
locale_dir: str | os.PathLike[str] | None = sphinx.locale._LOCALE_DIR, |
| 28 |
catalog: str = 'sphinx', |
| 29 |
) -> tuple[gettext.NullTranslations, bool]: |
| 30 |
"""Monkeypatch ``init_console`` to skip its action. |
| 31 |
|
| 32 |
Some tests rely on warning messages in English. We don't want |
| 33 |
CLI tests to bleed over those tests and make their warnings |
| 34 |
translated. |
| 35 |
""" |
| 36 |
return gettext.NullTranslations(), False |
| 37 |
|
| 38 |
|
| 39 |
sphinx.locale.init_console = _init_console |
| 40 |
|
| 41 |
pytest_plugins = ['sphinx.testing.fixtures'] |
| 42 |
|
| 43 |
# Exclude 'roots' dirs for pytest test collector |
| 44 |
collect_ignore = ['roots', 'roots-read-only'] |
| 45 |
|
| 46 |
os.environ['SPHINX_AUTODOC_RELOAD_MODULES'] = '1' |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(scope='session') |
| 50 |
def rootdir() -> Path: |
| 51 |
return TEST_ROOTS_DIR |
| 52 |
|
| 53 |
|
| 54 |
def pytest_report_header(config: pytest.Config) -> str: |
| 55 |
lines = [ |
| 56 |
f'libraries: Sphinx-{sphinx.__display_version__}, docutils-{docutils.__version__}', |
| 57 |
] |
| 58 |
if sys.version_info[:2] >= (3, 13): |
| 59 |
lines.append(f'GIL enabled?: {sys._is_gil_enabled()}') |
| 60 |
lines.append(f'test roots directory: {TEST_ROOTS_DIR}') |
| 61 |
if hasattr(config, '_tmp_path_factory'): |
| 62 |
lines.append(f'base tmp_path: {config._tmp_path_factory.getbasetemp()}') |
| 63 |
return '\n'.join(lines) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.fixture(autouse=True) |
| 67 |
def _cleanup_docutils() -> Iterator[None]: |
| 68 |
saved_path = sys.path |
| 69 |
yield # run the test |
| 70 |
sys.path[:] = saved_path |
| 71 |
|
| 72 |
_clean_up_global_state() |
| 73 |
|
| 74 |
|
| 75 |
@pytest.fixture |
| 76 |
def _http_teapot(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: |
| 77 |
"""Short-circuit HTTP requests. |
| 78 |
|
| 79 |
Windows takes too long to fail on connections, hence this fixture. |
| 80 |
""" |
| 81 |
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418 |
| 82 |
response = SimpleNamespace(status_code=418) |
| 83 |
|
| 84 |
def _request(*args: Any, **kwargs: Any) -> SimpleNamespace: |
| 85 |
return response |
| 86 |
|
| 87 |
with monkeypatch.context() as m: |
| 88 |
m.setattr('sphinx.util.requests._Session.request', _request) |
| 89 |
yield |
| 90 |
|
| 91 |
|
| 92 |
@pytest.fixture |
| 93 |
def make_app_with_empty_project( |
| 94 |
make_app: Callable[..., SphinxTestApp], |
| 95 |
tmp_path: Path, |
| 96 |
) -> Callable[..., SphinxTestApp]: |
| 97 |
(tmp_path / 'conf.py').touch() |
| 98 |
|
| 99 |
def _make_app(*args: Any, **kw: Any) -> SphinxTestApp: |
| 100 |
kw.setdefault('srcdir', Path(tmp_path)) |
| 101 |
return make_app(*args, **kw) |
| 102 |
|
| 103 |
return _make_app |