| 1 |
"""Test the sphinx.config.Config class.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import pickle |
| 6 |
from collections import Counter |
| 7 |
from typing import TYPE_CHECKING, Any |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import sphinx |
| 13 |
from sphinx.config import ( |
| 14 |
ENUM, |
| 15 |
Config, |
| 16 |
check_confval_types, |
| 17 |
is_serializable, |
| 18 |
) |
| 19 |
from sphinx.errors import ConfigError, ExtensionError, VersionRequirementError |
| 20 |
from sphinx.testing.util import SphinxTestApp |
| 21 |
from sphinx.util.tags import Tags |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from collections.abc import Iterable |
| 25 |
from pathlib import Path |
| 26 |
|
| 27 |
type CircularList = list[int | 'CircularList'] |
| 28 |
type CircularDict = dict[str, int | 'CircularDict'] |
| 29 |
|
| 30 |
|
| 31 |
def check_is_serializable(subject: object, *, circular: bool) -> None: |
| 32 |
assert is_serializable(subject) |
| 33 |
|
| 34 |
if circular: |
| 35 |
|
| 36 |
class UselessGuard(frozenset[int]): |
| 37 |
def __or__(self, other: object, /) -> UselessGuard: |
| 38 |
# do nothing |
| 39 |
return self |
| 40 |
|
| 41 |
def union(self, *args: Iterable[object]) -> UselessGuard: |
| 42 |
# do nothing |
| 43 |
return self |
| 44 |
|
| 45 |
# check that without recursive guards, a recursion error occurs |
| 46 |
with pytest.raises(RecursionError): |
| 47 |
assert is_serializable(subject, _seen=UselessGuard()) |
| 48 |
|
| 49 |
|
| 50 |
def test_is_serializable() -> None: |
| 51 |
subject = [1, [2, {3, 'a'}], {'x': {'y': frozenset((4, 5))}}] |
| 52 |
check_is_serializable(subject, circular=False) |
| 53 |
|
| 54 |
a: CircularList = [1] |
| 55 |
b: CircularList = [2] |
| 56 |
a.append(b) |
| 57 |
b.append(a) |
| 58 |
check_is_serializable(a, circular=True) |
| 59 |
check_is_serializable(b, circular=True) |
| 60 |
|
| 61 |
x: CircularDict = {'a': 1, 'b': {'c': 1}} |
| 62 |
x['b'] = x |
| 63 |
check_is_serializable(x, circular=True) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.sphinx( |
| 67 |
'html', |
| 68 |
testroot='config', |
| 69 |
confoverrides={ |
| 70 |
'root_doc': 'root', |
| 71 |
'nonexisting_value': 'True', |
| 72 |
'latex_elements.maketitle': 'blah blah blah', |
| 73 |
'modindex_common_prefix': 'path1,path2', |
| 74 |
}, |
| 75 |
) |
| 76 |
def test_core_config(app: SphinxTestApp) -> None: |
| 77 |
cfg = app.config |
| 78 |
|
| 79 |
# simple values |
| 80 |
assert 'project' in cfg.__dict__ |
| 81 |
assert cfg.project == 'Sphinx <Tests>' |
| 82 |
assert cfg.templates_path == ['_templates'] |
| 83 |
|
| 84 |
# overrides |
| 85 |
assert cfg.root_doc == 'root' |
| 86 |
assert cfg.latex_elements['maketitle'] == 'blah blah blah' |
| 87 |
assert cfg.modindex_common_prefix == ['path1', 'path2'] |
| 88 |
|
| 89 |
# simple default values |
| 90 |
assert 'locale_dirs' in cfg.__dict__ |
| 91 |
assert cfg.locale_dirs == ['locales'] |
| 92 |
assert cfg.trim_footnote_reference_space is False |
| 93 |
|
| 94 |
# complex default values |
| 95 |
assert 'html_title' not in cfg.__dict__ |
| 96 |
assert cfg.html_title == 'Sphinx <Tests> 0.6alpha1 documentation' |
| 97 |
|
| 98 |
# complex default values mustn't raise |
| 99 |
for valuename in cfg.config_values: |
| 100 |
getattr(cfg, valuename) |
| 101 |
|
| 102 |
# "contains" gives True both for set and unset values |
| 103 |
assert 'project' in cfg |
| 104 |
assert 'html_title' in cfg |
| 105 |
assert 'nonexisting_value' not in cfg |
| 106 |
|
| 107 |
# invalid values |
| 108 |
with pytest.raises(AttributeError): |
| 109 |
_ = cfg._value |
| 110 |
with pytest.raises(AttributeError): |
| 111 |
_ = cfg.nonexisting_value |
| 112 |
|
| 113 |
# non-value attributes are deleted from the namespace |
| 114 |
with pytest.raises(AttributeError): |
| 115 |
_ = cfg.sys |
| 116 |
|
| 117 |
# setting attributes |
| 118 |
cfg.project = 'Foo' |
| 119 |
assert cfg.project == 'Foo' |
| 120 |
|
| 121 |
# alternative access via item interface |
| 122 |
cfg['project'] = 'Sphinx Tests' |
| 123 |
assert cfg['project'] == cfg.project == 'Sphinx Tests' |
| 124 |
|
| 125 |
|
| 126 |
def test_config_not_found(tmp_path): |
| 127 |
with pytest.raises(ConfigError): |
| 128 |
Config.read(tmp_path, overrides={}, tags=Tags()) |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize('protocol', list(range(pickle.HIGHEST_PROTOCOL))) |
| 132 |
def test_config_pickle_protocol(protocol: int) -> None: |
| 133 |
config = Config() |
| 134 |
|
| 135 |
pickled_config = pickle.loads(pickle.dumps(config, protocol)) |
| 136 |
|
| 137 |
assert list(config._options) == list(pickled_config._options) |
| 138 |
assert repr(config) == repr(pickled_config) |
| 139 |
|
| 140 |
|
| 141 |
def test_config_pickle_circular_reference_in_list(): |
| 142 |
a: CircularList = [1] |
| 143 |
b: CircularList = [2] |
| 144 |
a.append(b) |
| 145 |
b.append(a) |
| 146 |
|
| 147 |
check_is_serializable(a, circular=True) |
| 148 |
check_is_serializable(b, circular=True) |
| 149 |
|
| 150 |
config = Config() |
| 151 |
config.add('a', [], '', types=list) |
| 152 |
config.add('b', [], '', types=frozenset({list})) |
| 153 |
config.a, config.b = a, b |
| 154 |
|
| 155 |
actual = pickle.loads(pickle.dumps(config)) |
| 156 |
assert isinstance(actual.a, list) |
| 157 |
check_is_serializable(actual.a, circular=True) |
| 158 |
|
| 159 |
assert isinstance(actual.b, list) |
| 160 |
check_is_serializable(actual.b, circular=True) |
| 161 |
|
| 162 |
assert actual.a[0] == 1 |
| 163 |
assert actual.a[1][0] == 2 |
| 164 |
assert actual.a[1][1][0] == 1 |
| 165 |
assert actual.a[1][1][1][0] == 2 |
| 166 |
|
| 167 |
assert actual.b[0] == 2 |
| 168 |
assert actual.b[1][0] == 1 |
| 169 |
assert actual.b[1][1][0] == 2 |
| 170 |
assert actual.b[1][1][1][0] == 1 |
| 171 |
|
| 172 |
assert len(actual.a) == 2 |
| 173 |
assert len(actual.a[1]) == 2 |
| 174 |
assert len(actual.a[1][1]) == 2 |
| 175 |
assert len(actual.a[1][1][1]) == 2 |
| 176 |
assert len(actual.a[1][1][1][1]) == 2 |
| 177 |
|
| 178 |
assert len(actual.b) == 2 |
| 179 |
assert len(actual.b[1]) == 2 |
| 180 |
assert len(actual.b[1][1]) == 2 |
| 181 |
assert len(actual.b[1][1][1]) == 2 |
| 182 |
assert len(actual.b[1][1][1][1]) == 2 |
| 183 |
|
| 184 |
def check( |
| 185 |
u: list[list[object] | int], |
| 186 |
v: list[list[object] | int], |
| 187 |
*, |
| 188 |
counter: Counter[type] | None = None, |
| 189 |
guard: frozenset[int] = frozenset(), |
| 190 |
) -> Counter[type]: |
| 191 |
counter = Counter() if counter is None else counter |
| 192 |
|
| 193 |
if id(u) in guard and id(v) in guard: |
| 194 |
return counter |
| 195 |
|
| 196 |
if isinstance(u, int): |
| 197 |
assert v.__class__ is u.__class__ |
| 198 |
assert u == v |
| 199 |
counter[type(u)] += 1 |
| 200 |
return counter |
| 201 |
|
| 202 |
assert isinstance(u, list) |
| 203 |
assert v.__class__ is u.__class__ |
| 204 |
for u_i, v_i in zip(u, v, strict=True): |
| 205 |
counter[type(u)] += 1 |
| 206 |
check(u_i, v_i, counter=counter, guard=guard | {id(u), id(v)}) # type: ignore[arg-type] |
| 207 |
|
| 208 |
return counter |
| 209 |
|
| 210 |
counter = check(actual.a, a) |
| 211 |
# check(actual.a, a) |
| 212 |
# check(actual.a[0], a[0]) -> ++counter[dict] |
| 213 |
# ++counter[int] (a[0] is an int) |
| 214 |
# check(actual.a[1], a[1]) -> ++counter[dict] |
| 215 |
# check(actual.a[1][0], a[1][0]) -> ++counter[dict] |
| 216 |
# ++counter[int] (a[1][0] is an int) |
| 217 |
# check(actual.a[1][1], a[1][1]) -> ++counter[dict] |
| 218 |
# recursive guard since a[1][1] == a |
| 219 |
assert counter[type(a[0])] == 2 |
| 220 |
assert counter[type(a[1])] == 4 |
| 221 |
|
| 222 |
# same logic as above |
| 223 |
counter = check(actual.b, b) |
| 224 |
assert counter[type(b[0])] == 2 |
| 225 |
assert counter[type(b[1])] == 4 |
| 226 |
|
| 227 |
|
| 228 |
def test_config_pickle_circular_reference_in_dict(): |
| 229 |
x: CircularDict = {'a': 1, 'b': {'c': 1}} |
| 230 |
x['b'] = x |
| 231 |
check_is_serializable(x, circular=True) |
| 232 |
|
| 233 |
config = Config() |
| 234 |
config.add('x', [], '', types=frozenset({dict})) |
| 235 |
config.x = x |
| 236 |
|
| 237 |
actual = pickle.loads(pickle.dumps(config)) |
| 238 |
check_is_serializable(actual.x, circular=True) |
| 239 |
assert isinstance(actual.x, dict) |
| 240 |
|
| 241 |
assert actual.x['a'] == 1 |
| 242 |
assert actual.x['b']['a'] == 1 |
| 243 |
|
| 244 |
assert len(actual.x) == 2 |
| 245 |
assert len(actual.x['b']) == 2 |
| 246 |
assert len(actual.x['b']['b']) == 2 |
| 247 |
|
| 248 |
def check( |
| 249 |
u: dict[str, dict[str, object] | int], |
| 250 |
v: dict[str, dict[str, object] | int], |
| 251 |
*, |
| 252 |
counter: Counter[type] | None = None, |
| 253 |
guard: frozenset[int] = frozenset(), |
| 254 |
) -> Counter: |
| 255 |
counter = Counter() if counter is None else counter |
| 256 |
|
| 257 |
if id(u) in guard and id(v) in guard: |
| 258 |
return counter |
| 259 |
|
| 260 |
if isinstance(u, int): |
| 261 |
assert v.__class__ is u.__class__ |
| 262 |
assert u == v |
| 263 |
counter[type(u)] += 1 |
| 264 |
return counter |
| 265 |
|
| 266 |
assert isinstance(u, dict) |
| 267 |
assert v.__class__ is u.__class__ |
| 268 |
for u_i, v_i in zip(u, v, strict=True): |
| 269 |
counter[type(u)] += 1 |
| 270 |
check(u[u_i], v[v_i], counter=counter, guard=guard | {id(u), id(v)}) # type: ignore[arg-type] |
| 271 |
return counter |
| 272 |
|
| 273 |
counters = check(actual.x, x, counter=Counter()) |
| 274 |
# check(actual.x, x) |
| 275 |
# check(actual.x['a'], x['a']) -> ++counter[dict] |
| 276 |
# ++counter[int] (x['a'] is an int) |
| 277 |
# check(actual.x['b'], x['b']) -> ++counter[dict] |
| 278 |
# recursive guard since x['b'] == x |
| 279 |
assert counters[type(x['a'])] == 1 |
| 280 |
assert counters[type(x['b'])] == 2 |
| 281 |
|
| 282 |
|
| 283 |
def test_extension_values(): |
| 284 |
config = Config() |
| 285 |
|
| 286 |
# check standard settings |
| 287 |
assert config.root_doc == 'index' |
| 288 |
|
| 289 |
# can't override it by add_config_value() |
| 290 |
with pytest.raises(ExtensionError) as excinfo: |
| 291 |
config.add('root_doc', 'index', 'env', None) |
| 292 |
assert 'already present' in str(excinfo.value) |
| 293 |
|
| 294 |
# add a new config value |
| 295 |
config.add('value_from_ext', [], 'env', None) |
| 296 |
assert config.value_from_ext == [] |
| 297 |
|
| 298 |
# can't override it by add_config_value() |
| 299 |
with pytest.raises(ExtensionError) as excinfo: |
| 300 |
config.add('value_from_ext', [], 'env', None) |
| 301 |
assert 'already present' in str(excinfo.value) |
| 302 |
|
| 303 |
|
| 304 |
def test_overrides() -> None: |
| 305 |
config = Config( |
| 306 |
{'value1': '1', 'value2': 2, 'value6': {'default': 6}}, |
| 307 |
{ |
| 308 |
'value2': 999, |
| 309 |
'value3': '999', |
| 310 |
'value5.attr1': 999, |
| 311 |
'value6.attr1': 999, |
| 312 |
'value7': 'abc,def,ghi', |
| 313 |
'value8': 'abc,def,ghi', |
| 314 |
}, |
| 315 |
) |
| 316 |
config.add('value1', None, 'env', ()) |
| 317 |
config.add('value2', None, 'env', ()) |
| 318 |
config.add('value3', 0, 'env', ()) |
| 319 |
config.add('value4', 0, 'env', ()) |
| 320 |
config.add('value5', {'default': 0}, 'env', ()) |
| 321 |
config.add('value6', {'default': 0}, 'env', ()) |
| 322 |
config.add('value7', None, 'env', ()) |
| 323 |
config.add('value8', [], 'env', ()) |
| 324 |
|
| 325 |
assert config.value1 == '1' |
| 326 |
assert config.value2 == 999 |
| 327 |
assert config.value3 == 999 |
| 328 |
assert config.value4 == 0 |
| 329 |
assert config.value5 == {'attr1': 999} |
| 330 |
assert config.value6 == {'default': 6, 'attr1': 999} |
| 331 |
assert config.value7 == 'abc,def,ghi' |
| 332 |
assert config.value8 == ['abc', 'def', 'ghi'] |
| 333 |
|
| 334 |
|
| 335 |
def test_overrides_boolean() -> None: |
| 336 |
config = Config({}, {'value1': '1', 'value2': '0', 'value3': '0'}) |
| 337 |
config.add('value1', None, 'env', [bool]) |
| 338 |
config.add('value2', None, 'env', [bool]) |
| 339 |
config.add('value3', True, 'env', ()) |
| 340 |
|
| 341 |
assert config.value1 is True |
| 342 |
assert config.value2 is False |
| 343 |
assert config.value3 is False |
| 344 |
|
| 345 |
|
| 346 |
@mock.patch('sphinx.config.logger') |
| 347 |
def test_overrides_dict_str(logger): |
| 348 |
config = Config({}, {'spam': 'lobster'}) |
| 349 |
|
| 350 |
config.add('spam', {'ham': 'eggs'}, 'env', {dict, str}) |
| 351 |
|
| 352 |
assert config.spam == {'ham': 'eggs'} |
| 353 |
|
| 354 |
# assert len(caplog.records) == 1 |
| 355 |
# msg = caplog.messages[0] |
| 356 |
assert logger.method_calls |
| 357 |
msg = str(logger.method_calls[0].args[1]) |
| 358 |
assert msg == ( |
| 359 |
"cannot override dictionary config setting 'spam', " |
| 360 |
"ignoring (use 'spam.key=value' to set individual elements)" |
| 361 |
) |
| 362 |
|
| 363 |
|
| 364 |
def test_callable_defer() -> None: |
| 365 |
config = Config() |
| 366 |
config.add('alias', lambda c: c.master_doc, '', str) |
| 367 |
|
| 368 |
assert config.master_doc == 'index' |
| 369 |
assert config.alias == 'index' |
| 370 |
|
| 371 |
config.master_doc = 'contents' |
| 372 |
assert config.alias == 'contents' |
| 373 |
|
| 374 |
config.master_doc = 'master_doc' |
| 375 |
config.alias = 'spam' |
| 376 |
assert config.alias == 'spam' |
| 377 |
|
| 378 |
|
| 379 |
@mock.patch('sphinx.config.logger') |
| 380 |
def test_errors_warnings(logger, tmp_path): |
| 381 |
# test the error for syntax errors in the config file |
| 382 |
(tmp_path / 'conf.py').write_text('project = \n', encoding='ascii') |
| 383 |
with pytest.raises(ConfigError) as excinfo: |
| 384 |
Config.read(tmp_path, overrides={}, tags=Tags()) |
| 385 |
assert 'conf.py' in str(excinfo.value) |
| 386 |
|
| 387 |
# test the automatic conversion of 2.x only code in configs |
| 388 |
(tmp_path / 'conf.py').write_text('project = u"Jägermeister"\n', encoding='utf8') |
| 389 |
cfg = Config.read(tmp_path, overrides={}, tags=Tags()) |
| 390 |
assert cfg.project == 'Jägermeister' |
| 391 |
assert logger.called is False |
| 392 |
|
| 393 |
|
| 394 |
def test_errors_if_setup_is_not_callable(tmp_path, make_app): |
| 395 |
# test the error to call setup() in the config file |
| 396 |
(tmp_path / 'conf.py').write_text('setup = 1', encoding='utf8') |
| 397 |
with pytest.raises(ConfigError) as excinfo: |
| 398 |
make_app(srcdir=tmp_path) |
| 399 |
assert 'callable' in str(excinfo.value) |
| 400 |
|
| 401 |
|
| 402 |
@mock.patch.object(sphinx, '__display_version__', '1.6.4') |
| 403 |
def test_needs_sphinx(make_app_with_empty_project): |
| 404 |
make_app = make_app_with_empty_project |
| 405 |
# micro version |
| 406 |
make_app(confoverrides={'needs_sphinx': '1.6.3'}) # OK: less |
| 407 |
make_app(confoverrides={'needs_sphinx': '1.6.4'}) # OK: equals |
| 408 |
with pytest.raises(VersionRequirementError): |
| 409 |
make_app(confoverrides={'needs_sphinx': '1.6.5'}) # NG: greater |
| 410 |
|
| 411 |
# minor version |
| 412 |
make_app(confoverrides={'needs_sphinx': '1.5'}) # OK: less |
| 413 |
make_app(confoverrides={'needs_sphinx': '1.6'}) # OK: equals |
| 414 |
with pytest.raises(VersionRequirementError): |
| 415 |
make_app(confoverrides={'needs_sphinx': '1.7'}) # NG: greater |
| 416 |
|
| 417 |
# major version |
| 418 |
make_app(confoverrides={'needs_sphinx': '0'}) # OK: less |
| 419 |
make_app(confoverrides={'needs_sphinx': '1'}) # OK: equals |
| 420 |
with pytest.raises(VersionRequirementError): |
| 421 |
make_app(confoverrides={'needs_sphinx': '2'}) # NG: greater |
| 422 |
|
| 423 |
|
| 424 |
@mock.patch('sphinx.config.logger') |
| 425 |
def test_config_eol(logger, tmp_path): |
| 426 |
# test config file's eol patterns: LF, CRLF |
| 427 |
configfile = tmp_path / 'conf.py' |
| 428 |
for eol in (b'\n', b'\r\n'): |
| 429 |
configfile.write_bytes(b'project = "spam"' + eol) |
| 430 |
cfg = Config.read(tmp_path, overrides={}, tags=Tags()) |
| 431 |
assert cfg.project == 'spam' |
| 432 |
assert logger.called is False |
| 433 |
|
| 434 |
|
| 435 |
@pytest.mark.sphinx( |
| 436 |
'html', |
| 437 |
testroot='root', |
| 438 |
confoverrides={'root_doc': 123, 'language': 'foo', 'primary_domain': None}, |
| 439 |
) |
| 440 |
def test_builtin_conf(app: SphinxTestApp) -> None: |
| 441 |
warnings = app.warning.getvalue() |
| 442 |
assert 'root_doc' in warnings, ( |
| 443 |
'override on builtin "root_doc" should raise a type warning' |
| 444 |
) |
| 445 |
assert 'language' not in warnings, ( |
| 446 |
'explicitly permitted override on builtin "language" should NOT raise ' |
| 447 |
'a type warning' |
| 448 |
) |
| 449 |
assert 'primary_domain' not in warnings, ( |
| 450 |
'override to None on builtin "primary_domain" should NOT raise a type warning' |
| 451 |
) |
| 452 |
|
| 453 |
|
| 454 |
# example classes for type checking |
| 455 |
class A: |
| 456 |
pass |
| 457 |
|
| 458 |
|
| 459 |
class B(A): |
| 460 |
pass |
| 461 |
|
| 462 |
|
| 463 |
class C(A): |
| 464 |
pass |
| 465 |
|
| 466 |
|
| 467 |
# name, default, annotation, actual, warned |
| 468 |
TYPECHECK_WARNINGS = [ |
| 469 |
('value1', 'string', None, 123, True), # wrong type |
| 470 |
('value2', lambda _: [], None, 123, True), # lambda with wrong type |
| 471 |
('value3', lambda _: [], None, [], False), # lambda with correct type |
| 472 |
('value4', 100, None, True, True), # child type |
| 473 |
('value5', False, None, True, False), # parent type |
| 474 |
('value6', [], None, (), True), # other sequence type |
| 475 |
('value7', 'string', [list], ['foo'], False), # explicit type annotation |
| 476 |
('value8', B(), None, C(), False), # sibling type |
| 477 |
('value9', None, None, 'foo', False), # no default or no annotations |
| 478 |
('value10', None, None, 123, False), # no default or no annotations |
| 479 |
('value11', None, [str], 'bar', False), # str |
| 480 |
('value12', 'string', None, 'bar', False), # str |
| 481 |
] # fmt: skip |
| 482 |
|
| 483 |
|
| 484 |
@mock.patch('sphinx.config.logger') |
| 485 |
@pytest.mark.parametrize( |
| 486 |
('name', 'default', 'annotation', 'actual', 'warned'), |
| 487 |
TYPECHECK_WARNINGS, |
| 488 |
) |
| 489 |
def test_check_types(logger, name, default, annotation, actual, warned): |
| 490 |
config = Config({name: actual}) |
| 491 |
config.add(name, default, 'env', annotation or ()) |
| 492 |
check_confval_types(None, config) |
| 493 |
assert logger.warning.called == warned |
| 494 |
|
| 495 |
|
| 496 |
TYPECHECK_WARNING_MESSAGES = [ |
| 497 |
( |
| 498 |
'value1', |
| 499 |
'string', |
| 500 |
[str], |
| 501 |
['foo', 'bar'], |
| 502 |
"The config value `value1' has type `list'; expected `str'.", |
| 503 |
), |
| 504 |
( |
| 505 |
'value1', |
| 506 |
'string', |
| 507 |
[str, int], |
| 508 |
['foo', 'bar'], |
| 509 |
"The config value `value1' has type `list'; expected `int' or `str'.", |
| 510 |
), |
| 511 |
( |
| 512 |
'value1', |
| 513 |
'string', |
| 514 |
[str, int, tuple], |
| 515 |
['foo', 'bar'], |
| 516 |
"The config value `value1' has type `list'; expected `int', `str', or `tuple'.", |
| 517 |
), |
| 518 |
] |
| 519 |
|
| 520 |
|
| 521 |
@mock.patch('sphinx.config.logger') |
| 522 |
@pytest.mark.parametrize( |
| 523 |
('name', 'default', 'annotation', 'actual', 'message'), |
| 524 |
TYPECHECK_WARNING_MESSAGES, |
| 525 |
) |
| 526 |
def test_conf_warning_message(logger, name, default, annotation, actual, message): |
| 527 |
config = Config({name: actual}) |
| 528 |
config.add(name, default, False, annotation or ()) |
| 529 |
check_confval_types(None, config) |
| 530 |
assert logger.warning.called |
| 531 |
assert logger.warning.call_args[0][0] == message |
| 532 |
|
| 533 |
|
| 534 |
@mock.patch('sphinx.config.logger') |
| 535 |
def test_check_enum(logger): |
| 536 |
config = Config() |
| 537 |
config.add('value', 'default', False, ENUM('default', 'one', 'two')) |
| 538 |
check_confval_types(None, config) |
| 539 |
logger.warning.assert_not_called() # not warned |
| 540 |
|
| 541 |
|
| 542 |
@mock.patch('sphinx.config.logger') |
| 543 |
def test_check_enum_failed(logger): |
| 544 |
config = Config({'value': 'invalid'}) |
| 545 |
config.add('value', 'default', False, ENUM('default', 'one', 'two')) |
| 546 |
check_confval_types(None, config) |
| 547 |
assert logger.warning.called |
| 548 |
|
| 549 |
|
| 550 |
@mock.patch('sphinx.config.logger') |
| 551 |
def test_check_enum_for_list(logger): |
| 552 |
config = Config({'value': ['one', 'two']}) |
| 553 |
config.add('value', 'default', False, ENUM('default', 'one', 'two')) |
| 554 |
check_confval_types(None, config) |
| 555 |
logger.warning.assert_not_called() # not warned |
| 556 |
|
| 557 |
|
| 558 |
@mock.patch('sphinx.config.logger') |
| 559 |
def test_check_enum_for_list_failed(logger): |
| 560 |
config = Config({'value': ['one', 'two', 'invalid']}) |
| 561 |
config.add('value', 'default', False, ENUM('default', 'one', 'two')) |
| 562 |
check_confval_types(None, config) |
| 563 |
assert logger.warning.called |
| 564 |
|
| 565 |
|
| 566 |
@mock.patch('sphinx.config.logger') |
| 567 |
def test_check_any(logger): |
| 568 |
config = Config({'value': None}) |
| 569 |
config.add('value', 'default', '', Any) |
| 570 |
check_confval_types(None, config) |
| 571 |
logger.warning.assert_not_called() # not warned |
| 572 |
|
| 573 |
|
| 574 |
nitpick_warnings = [ |
| 575 |
'WARNING: py:const reference target not found: prefix.anything.postfix', |
| 576 |
'WARNING: py:class reference target not found: prefix.anything', |
| 577 |
'WARNING: py:class reference target not found: anything.postfix', |
| 578 |
'WARNING: js:class reference target not found: prefix.anything.postfix', |
| 579 |
] |
| 580 |
|
| 581 |
|
| 582 |
@pytest.mark.sphinx('html', testroot='nitpicky-warnings') |
| 583 |
def test_nitpick_base(app: SphinxTestApp) -> None: |
| 584 |
app.build(force_all=True) |
| 585 |
|
| 586 |
warning = app.warning.getvalue().strip().split('\n') |
| 587 |
for actual, expected in zip(warning, nitpick_warnings, strict=True): |
| 588 |
assert expected in actual |
| 589 |
|
| 590 |
|
| 591 |
@pytest.mark.sphinx( |
| 592 |
'html', |
| 593 |
testroot='nitpicky-warnings', |
| 594 |
confoverrides={ |
| 595 |
'nitpick_ignore': { |
| 596 |
('py:const', 'prefix.anything.postfix'), |
| 597 |
('py:class', 'prefix.anything'), |
| 598 |
('py:class', 'anything.postfix'), |
| 599 |
('js:class', 'prefix.anything.postfix'), |
| 600 |
}, |
| 601 |
}, |
| 602 |
) |
| 603 |
def test_nitpick_ignore(app: SphinxTestApp) -> None: |
| 604 |
app.build(force_all=True) |
| 605 |
assert not len(app.warning.getvalue().strip()) |
| 606 |
|
| 607 |
|
| 608 |
@pytest.mark.sphinx( |
| 609 |
'html', |
| 610 |
testroot='nitpicky-warnings', |
| 611 |
confoverrides={ |
| 612 |
'nitpick_ignore_regex': [ |
| 613 |
(r'py:.*', r'.*postfix'), |
| 614 |
(r'.*:class', r'prefix.*'), |
| 615 |
], |
| 616 |
}, |
| 617 |
) |
| 618 |
def test_nitpick_ignore_regex1(app: SphinxTestApp) -> None: |
| 619 |
app.build(force_all=True) |
| 620 |
assert not len(app.warning.getvalue().strip()) |
| 621 |
|
| 622 |
|
| 623 |
@pytest.mark.sphinx( |
| 624 |
'html', |
| 625 |
testroot='nitpicky-warnings', |
| 626 |
confoverrides={ |
| 627 |
'nitpick_ignore_regex': [ |
| 628 |
(r'py:.*', r'prefix.*'), |
| 629 |
(r'.*:class', r'.*postfix'), |
| 630 |
], |
| 631 |
}, |
| 632 |
) |
| 633 |
def test_nitpick_ignore_regex2(app: SphinxTestApp) -> None: |
| 634 |
app.build(force_all=True) |
| 635 |
assert not len(app.warning.getvalue().strip()) |
| 636 |
|
| 637 |
|
| 638 |
@pytest.mark.sphinx( |
| 639 |
'html', |
| 640 |
testroot='nitpicky-warnings', |
| 641 |
confoverrides={ |
| 642 |
'nitpick_ignore_regex': [ |
| 643 |
# None of these should match |
| 644 |
(r'py:', r'.*'), |
| 645 |
(r':class', r'.*'), |
| 646 |
(r'', r'.*'), |
| 647 |
(r'.*', r'anything'), |
| 648 |
(r'.*', r'prefix'), |
| 649 |
(r'.*', r'postfix'), |
| 650 |
(r'.*', r''), |
| 651 |
], |
| 652 |
}, |
| 653 |
) |
| 654 |
def test_nitpick_ignore_regex_fullmatch(app: SphinxTestApp) -> None: |
| 655 |
app.build(force_all=True) |
| 656 |
|
| 657 |
warning = app.warning.getvalue().strip().split('\n') |
| 658 |
for actual, expected in zip(warning, nitpick_warnings, strict=True): |
| 659 |
assert expected in actual |
| 660 |
|
| 661 |
|
| 662 |
def test_conf_py_language_none(tmp_path): |
| 663 |
"""Regression test for https://github.com/sphinx-doc/sphinx/issues/10474.""" |
| 664 |
# Given a conf.py file with language = None |
| 665 |
(tmp_path / 'conf.py').write_text('language = None', encoding='utf-8') |
| 666 |
|
| 667 |
# When we load conf.py into a Config object |
| 668 |
cfg = Config.read(tmp_path, overrides={}, tags=Tags()) |
| 669 |
|
| 670 |
# Then the language is coerced to English |
| 671 |
assert cfg.language == 'en' |
| 672 |
|
| 673 |
|
| 674 |
@mock.patch('sphinx.config.logger') |
| 675 |
def test_conf_py_language_none_warning(logger, tmp_path): |
| 676 |
"""Regression test for https://github.com/sphinx-doc/sphinx/issues/10474.""" |
| 677 |
# Given a conf.py file with language = None |
| 678 |
(tmp_path / 'conf.py').write_text('language = None', encoding='utf-8') |
| 679 |
|
| 680 |
# When we load conf.py into a Config object |
| 681 |
Config.read(tmp_path, overrides={}, tags=Tags()) |
| 682 |
|
| 683 |
# Then a warning is raised |
| 684 |
assert logger.warning.called |
| 685 |
assert logger.warning.call_args[0][0] == ( |
| 686 |
"Invalid configuration value found: 'language = None'. " |
| 687 |
'Update your configuration to a valid language code. ' |
| 688 |
"Falling back to 'en' (English)." |
| 689 |
) |
| 690 |
|
| 691 |
|
| 692 |
def test_conf_py_no_language(tmp_path): |
| 693 |
"""Regression test for https://github.com/sphinx-doc/sphinx/issues/10474.""" |
| 694 |
# Given a conf.py file with no language attribute |
| 695 |
(tmp_path / 'conf.py').touch() |
| 696 |
|
| 697 |
# When we load conf.py into a Config object |
| 698 |
cfg = Config.read(tmp_path, overrides={}, tags=Tags()) |
| 699 |
|
| 700 |
# Then the language is coerced to English |
| 701 |
assert cfg.language == 'en' |
| 702 |
|
| 703 |
|
| 704 |
def test_conf_py_nitpick_ignore_list(tmp_path): |
| 705 |
"""Regression test for https://github.com/sphinx-doc/sphinx/issues/11355.""" |
| 706 |
# Given a conf.py file with no language attribute |
| 707 |
(tmp_path / 'conf.py').touch() |
| 708 |
|
| 709 |
# When we load conf.py into a Config object |
| 710 |
cfg = Config.read(tmp_path, overrides={}, tags=Tags()) |
| 711 |
|
| 712 |
# Then the default nitpick_ignore[_regex] is an empty list |
| 713 |
assert cfg.nitpick_ignore == [] |
| 714 |
assert cfg.nitpick_ignore_regex == [] |
| 715 |
|
| 716 |
|
| 717 |
def test_gettext_compact_command_line_true() -> None: |
| 718 |
config = Config({}, {'gettext_compact': '1'}) |
| 719 |
config.add('gettext_compact', True, '', {bool, str}) |
| 720 |
|
| 721 |
# regression test for https://github.com/sphinx-doc/sphinx/issues/8549 |
| 722 |
# (-D gettext_compact=1) |
| 723 |
assert config.gettext_compact is True |
| 724 |
|
| 725 |
|
| 726 |
def test_gettext_compact_command_line_false() -> None: |
| 727 |
config = Config({}, {'gettext_compact': '0'}) |
| 728 |
config.add('gettext_compact', True, '', {bool, str}) |
| 729 |
|
| 730 |
# regression test for https://github.com/sphinx-doc/sphinx/issues/8549 |
| 731 |
# (-D gettext_compact=0) |
| 732 |
assert config.gettext_compact is False |
| 733 |
|
| 734 |
|
| 735 |
def test_gettext_compact_command_line_str() -> None: |
| 736 |
config = Config({}, {'gettext_compact': 'spam'}) |
| 737 |
config.add('gettext_compact', True, '', {bool, str}) |
| 738 |
|
| 739 |
# regression test for https://github.com/sphinx-doc/sphinx/issues/8549 |
| 740 |
# (-D gettext_compact=spam) |
| 741 |
assert config.gettext_compact == 'spam' |
| 742 |
|
| 743 |
|
| 744 |
def test_translation_progress_classes_command_line() -> None: |
| 745 |
config = Config({}, {'translation_progress_classes': '1'}) |
| 746 |
|
| 747 |
# regression test for --define translation_progress_classes=1 |
| 748 |
# https://github.com/sphinx-doc/sphinx/issues/13071 |
| 749 |
assert config.translation_progress_classes is True |
| 750 |
|
| 751 |
|
| 752 |
def test_translation_progress_classes_command_line_false() -> None: |
| 753 |
config = Config({}, {'translation_progress_classes': '0'}) |
| 754 |
|
| 755 |
# regression test for --define translation_progress_classes=0 |
| 756 |
# https://github.com/sphinx-doc/sphinx/issues/13071 |
| 757 |
assert config.translation_progress_classes is False |
| 758 |
|
| 759 |
|
| 760 |
def test_translation_progress_classes_command_line_str() -> None: |
| 761 |
config = Config({}, {'translation_progress_classes': 'translated'}) |
| 762 |
|
| 763 |
# regression test for --define translation_progress_classes=translated |
| 764 |
# https://github.com/sphinx-doc/sphinx/issues/13071 |
| 765 |
assert config.translation_progress_classes == 'translated' |
| 766 |
|
| 767 |
|
| 768 |
def test_autosummary_generate_command_line_false() -> None: |
| 769 |
config = Config({}, {'autosummary_generate': '0'}) |
| 770 |
config.add('autosummary_generate', True, '', {bool, list}) |
| 771 |
|
| 772 |
# regression test for --define autosummary_generate=0 |
| 773 |
# https://github.com/sphinx-doc/sphinx/issues/13273 |
| 774 |
assert config.autosummary_generate is False |
| 775 |
|
| 776 |
|
| 777 |
def test_boolean_command_line_invalid() -> None: |
| 778 |
config = Config({}, {'rabit_of_caerbannog': ''}) |
| 779 |
config.add('rabit_of_caerbannog', True, '', {bool}) |
| 780 |
with pytest.raises( |
| 781 |
ConfigError, match="'rabit_of_caerbannog' must be '0' or '1', got ''" |
| 782 |
): |
| 783 |
_ = config.rabit_of_caerbannog |
| 784 |
|
| 785 |
|
| 786 |
def test_root_doc_and_master_doc_are_synchronized() -> None: |
| 787 |
c = Config() |
| 788 |
assert c.master_doc == 'index' |
| 789 |
assert c.root_doc == c.master_doc |
| 790 |
|
| 791 |
c = Config() |
| 792 |
c.master_doc = '1234' |
| 793 |
assert c.master_doc == '1234' |
| 794 |
assert c.root_doc == c.master_doc |
| 795 |
|
| 796 |
c = Config() |
| 797 |
c.root_doc = '1234' |
| 798 |
assert c.master_doc == '1234' |
| 799 |
assert c.root_doc == c.master_doc |
| 800 |
|
| 801 |
|
| 802 |
def test_source_encoding_deprecation(tmp_path: Path) -> None: |
| 803 |
(tmp_path / 'conf.py').touch() |
| 804 |
app = SphinxTestApp( |
| 805 |
buildername='dummy', |
| 806 |
srcdir=tmp_path, |
| 807 |
confoverrides={'source_encoding': 'latin-1'}, |
| 808 |
) |
| 809 |
expected = 'Support for source encodings other than UTF-8 is deprecated and will be removed' |
| 810 |
assert expected in app.warning.getvalue() |