| 1 |
"""Test the Sphinx class.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
from io import StringIO |
| 8 |
from pathlib import Path |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
from unittest.mock import Mock |
| 11 |
|
| 12 |
import pytest |
| 13 |
from docutils import nodes |
| 14 |
|
| 15 |
import sphinx.application |
| 16 |
from sphinx._cli.util.errors import strip_escape_sequences |
| 17 |
from sphinx.errors import ExtensionError |
| 18 |
from sphinx.testing.util import SphinxTestApp |
| 19 |
from sphinx.util import logging |
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
import os |
| 23 |
from typing import Any |
| 24 |
|
| 25 |
|
| 26 |
def test_instantiation( |
| 27 |
tmp_path_factory: pytest.TempPathFactory, |
| 28 |
rootdir: str | os.PathLike[str] | None, |
| 29 |
) -> None: |
| 30 |
# Given |
| 31 |
src_dir = tmp_path_factory.getbasetemp() / 'root' |
| 32 |
|
| 33 |
# special support for sphinx/tests |
| 34 |
if rootdir and not src_dir.exists(): |
| 35 |
shutil.copytree(Path(str(rootdir)) / 'test-root', src_dir) |
| 36 |
|
| 37 |
saved_path = sys.path.copy() |
| 38 |
|
| 39 |
# When |
| 40 |
app_ = SphinxTestApp( |
| 41 |
srcdir=src_dir, |
| 42 |
status=StringIO(), |
| 43 |
warning=StringIO(), |
| 44 |
) |
| 45 |
sys.path[:] = saved_path |
| 46 |
app_.cleanup() |
| 47 |
|
| 48 |
# Then |
| 49 |
assert isinstance(app_, sphinx.application.Sphinx) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.sphinx('html', testroot='root') |
| 53 |
def test_events(app: SphinxTestApp) -> None: |
| 54 |
def empty() -> None: |
| 55 |
pass |
| 56 |
|
| 57 |
with pytest.raises(ExtensionError) as excinfo: |
| 58 |
app.connect('invalid', empty) |
| 59 |
assert 'Unknown event name: invalid' in str(excinfo.value) |
| 60 |
|
| 61 |
app.add_event('my_event') |
| 62 |
with pytest.raises(ExtensionError) as excinfo: |
| 63 |
app.add_event('my_event') |
| 64 |
assert "Event 'my_event' already present" in str(excinfo.value) |
| 65 |
|
| 66 |
def mock_callback(a_app: SphinxTestApp, *args: Any) -> str: |
| 67 |
assert a_app is app |
| 68 |
assert emit_args == args |
| 69 |
return 'ret' |
| 70 |
|
| 71 |
emit_args = (1, 3, 'string') |
| 72 |
listener_id = app.connect('my_event', mock_callback) |
| 73 |
assert app.emit('my_event', *emit_args) == ['ret'], 'Callback not called' |
| 74 |
|
| 75 |
app.disconnect(listener_id) |
| 76 |
assert app.emit('my_event', *emit_args) == [], 'Callback called when disconnected' |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.sphinx('html', testroot='root') |
| 80 |
def test_emit_with_nonascii_name_node(app: SphinxTestApp) -> None: |
| 81 |
node = nodes.section(names=['\u65e5\u672c\u8a9e']) |
| 82 |
app.emit('my_event', node) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.sphinx('html', testroot='root') |
| 86 |
def test_extensions(app: SphinxTestApp) -> None: |
| 87 |
app.setup_extension('shutil') |
| 88 |
warning = strip_escape_sequences(app.warning.getvalue()) |
| 89 |
assert "extension 'shutil' has no setup() function" in warning |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.sphinx('html', testroot='root') |
| 93 |
def test_extension_in_blacklist(app: SphinxTestApp) -> None: |
| 94 |
app.setup_extension('sphinxjp.themecore') |
| 95 |
msg = strip_escape_sequences(app.warning.getvalue()) |
| 96 |
assert msg.startswith("WARNING: the extension 'sphinxjp.themecore' was") |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.sphinx('html', testroot='add_source_parser') |
| 100 |
def test_add_source_parser(app: SphinxTestApp) -> None: |
| 101 |
assert set(app.config.source_suffix) == {'.rst', '.test'} |
| 102 |
|
| 103 |
# .rst; only in :confval:`source_suffix` |
| 104 |
assert '.rst' not in app.registry.get_source_parsers() |
| 105 |
assert app.registry.source_suffix['.rst'] == 'restructuredtext' |
| 106 |
|
| 107 |
# .test; configured by API |
| 108 |
assert app.registry.source_suffix['.test'] == 'test' |
| 109 |
assert 'test' in app.registry.get_source_parsers() |
| 110 |
assert app.registry.get_source_parsers()['test'].__name__ == 'TestSourceParser' |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.sphinx('html', testroot='extensions') |
| 114 |
def test_add_is_parallel_allowed(app: SphinxTestApp) -> None: |
| 115 |
logging.setup(app, app.status, app.warning) |
| 116 |
|
| 117 |
assert app.is_parallel_allowed('read') is True |
| 118 |
assert app.is_parallel_allowed('write') is True |
| 119 |
assert app.warning.getvalue() == '' |
| 120 |
|
| 121 |
app.setup_extension('read_parallel') |
| 122 |
assert app.is_parallel_allowed('read') is True |
| 123 |
assert app.is_parallel_allowed('write') is True |
| 124 |
assert app.warning.getvalue() == '' |
| 125 |
app.extensions.pop('read_parallel') |
| 126 |
|
| 127 |
app.setup_extension('write_parallel') |
| 128 |
assert app.is_parallel_allowed('read') is False |
| 129 |
assert app.is_parallel_allowed('write') is True |
| 130 |
assert ( |
| 131 |
'the write_parallel extension does not declare if it is safe ' |
| 132 |
"for parallel reading, assuming it isn't - please " |
| 133 |
) in app.warning.getvalue() |
| 134 |
app.extensions.pop('write_parallel') |
| 135 |
app.warning.truncate(0) # reset warnings |
| 136 |
|
| 137 |
app.setup_extension('read_serial') |
| 138 |
assert app.is_parallel_allowed('read') is False |
| 139 |
assert ( |
| 140 |
'the read_serial extension is not safe for parallel reading' |
| 141 |
) in app.warning.getvalue() |
| 142 |
app.warning.truncate(0) # reset warnings |
| 143 |
assert app.is_parallel_allowed('write') is True |
| 144 |
assert app.warning.getvalue() == '' |
| 145 |
app.extensions.pop('read_serial') |
| 146 |
|
| 147 |
app.setup_extension('write_serial') |
| 148 |
assert app.is_parallel_allowed('read') is False |
| 149 |
assert app.is_parallel_allowed('write') is False |
| 150 |
assert ( |
| 151 |
'the write_serial extension does not declare if it is safe ' |
| 152 |
"for parallel reading, assuming it isn't - please " |
| 153 |
) in app.warning.getvalue() |
| 154 |
app.extensions.pop('write_serial') |
| 155 |
app.warning.truncate(0) # reset warnings |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.sphinx('dummy', testroot='root') |
| 159 |
def test_build_specific(app: SphinxTestApp) -> None: |
| 160 |
app.builder.build = Mock() # type: ignore[method-assign,misc] |
| 161 |
filenames = [ |
| 162 |
app.srcdir / 'index.txt', # normal |
| 163 |
app.srcdir / 'images', # without suffix |
| 164 |
app.srcdir / 'notfound.txt', # not found |
| 165 |
app.srcdir / 'img.png', # unknown suffix |
| 166 |
Path('/index.txt'), # external file |
| 167 |
app.srcdir / 'subdir', # directory |
| 168 |
app.srcdir / 'subdir/includes.txt', # file on subdir |
| 169 |
app.srcdir / 'subdir/../subdir/excluded.txt', # not normalized |
| 170 |
] # fmt: skip |
| 171 |
app.build(False, filenames) |
| 172 |
|
| 173 |
expected = ['index', 'subdir/includes', 'subdir/excluded'] |
| 174 |
app.builder.build.assert_called_with( |
| 175 |
expected, |
| 176 |
method='specific', |
| 177 |
summary='3 source files given on command line', |
| 178 |
) |