diff options
Diffstat (limited to '')
-rw-r--r-- | tests/test_config/__init__.py | 0 | ||||
-rw-r--r-- | tests/test_config/test_config.py (renamed from tests/test_config.py) | 342 | ||||
-rw-r--r-- | tests/test_config/test_correct_year.py (renamed from tests/test_correct_year.py) | 0 |
3 files changed, 315 insertions, 27 deletions
diff --git a/tests/test_config/__init__.py b/tests/test_config/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/test_config/__init__.py diff --git a/tests/test_config.py b/tests/test_config/test_config.py index 0be0a58..e1cb1b0 100644 --- a/tests/test_config.py +++ b/tests/test_config/test_config.py @@ -1,15 +1,81 @@ """Test the sphinx.config.Config class.""" +from __future__ import annotations +import pickle import time +from collections import Counter from pathlib import Path +from typing import TYPE_CHECKING, Any from unittest import mock import pytest import sphinx -from sphinx.config import ENUM, Config, check_confval_types +from sphinx.builders.gettext import _gettext_compact_validator +from sphinx.config import ( + ENUM, + Config, + _Opt, + check_confval_types, + correct_copyright_year, + is_serializable, +) +from sphinx.deprecation import RemovedInSphinx90Warning from sphinx.errors import ConfigError, ExtensionError, VersionRequirementError +if TYPE_CHECKING: + from collections.abc import Iterable + from typing import Union + + CircularList = list[Union[int, 'CircularList']] + CircularDict = dict[str, Union[int, 'CircularDict']] + + +def check_is_serializable(subject: object, *, circular: bool) -> None: + assert is_serializable(subject) + + if circular: + class UselessGuard(frozenset[int]): + def __or__(self, other: object, /) -> UselessGuard: + # do nothing + return self + + def union(self, *args: Iterable[object]) -> UselessGuard: + # do nothing + return self + + # check that without recursive guards, a recursion error occurs + with pytest.raises(RecursionError): + assert is_serializable(subject, _seen=UselessGuard()) + + +def test_is_serializable() -> None: + subject = [1, [2, {3, 'a'}], {'x': {'y': frozenset((4, 5))}}] + check_is_serializable(subject, circular=False) + + a, b = [1], [2] # type: (CircularList, CircularList) + a.append(b) + b.append(a) + check_is_serializable(a, circular=True) + check_is_serializable(b, circular=True) + + x: CircularDict = {'a': 1, 'b': {'c': 1}} + x['b'] = x + check_is_serializable(x, circular=True) + + +def test_config_opt_deprecated(recwarn): + opt = _Opt('default', '', ()) + + with pytest.warns(RemovedInSphinx90Warning): + default, rebuild, valid_types = opt + + with pytest.warns(RemovedInSphinx90Warning): + _ = opt[0] + + with pytest.warns(RemovedInSphinx90Warning): + _ = list(opt) + @pytest.mark.sphinx(testroot='config', confoverrides={ 'root_doc': 'root', @@ -30,7 +96,7 @@ def test_core_config(app, status, warning): assert cfg.modindex_common_prefix == ['path1', 'path2'] # simple default values - assert 'locale_dirs' not in cfg.__dict__ + assert 'locale_dirs' in cfg.__dict__ assert cfg.locale_dirs == ['locales'] assert cfg.trim_footnote_reference_space is False @@ -71,6 +137,161 @@ def test_config_not_found(tmp_path): Config.read(tmp_path) +@pytest.mark.parametrize("protocol", list(range(pickle.HIGHEST_PROTOCOL))) +def test_config_pickle_protocol(tmp_path, protocol: int): + config = Config() + + pickled_config = pickle.loads(pickle.dumps(config, protocol)) + + assert list(config._options) == list(pickled_config._options) + assert repr(config) == repr(pickled_config) + + +def test_config_pickle_circular_reference_in_list(): + a, b = [1], [2] # type: (CircularList, CircularList) + a.append(b) + b.append(a) + + check_is_serializable(a, circular=True) + check_is_serializable(b, circular=True) + + config = Config() + config.add('a', [], '', types=list) + config.add('b', [], '', types=list) + config.a, config.b = a, b + + actual = pickle.loads(pickle.dumps(config)) + assert isinstance(actual.a, list) + check_is_serializable(actual.a, circular=True) + + assert isinstance(actual.b, list) + check_is_serializable(actual.b, circular=True) + + assert actual.a[0] == 1 + assert actual.a[1][0] == 2 + assert actual.a[1][1][0] == 1 + assert actual.a[1][1][1][0] == 2 + + assert actual.b[0] == 2 + assert actual.b[1][0] == 1 + assert actual.b[1][1][0] == 2 + assert actual.b[1][1][1][0] == 1 + + assert len(actual.a) == 2 + assert len(actual.a[1]) == 2 + assert len(actual.a[1][1]) == 2 + assert len(actual.a[1][1][1]) == 2 + assert len(actual.a[1][1][1][1]) == 2 + + assert len(actual.b) == 2 + assert len(actual.b[1]) == 2 + assert len(actual.b[1][1]) == 2 + assert len(actual.b[1][1][1]) == 2 + assert len(actual.b[1][1][1][1]) == 2 + + def check( + u: list[list[object] | int], + v: list[list[object] | int], + *, + counter: Counter[type, int] | None = None, + guard: frozenset[int] = frozenset(), + ) -> Counter[type, int]: + counter = Counter() if counter is None else counter + + if id(u) in guard and id(v) in guard: + return counter + + if isinstance(u, int): + assert v.__class__ is u.__class__ + assert u == v + counter[type(u)] += 1 + return counter + + assert isinstance(u, list) + assert v.__class__ is u.__class__ + assert len(u) == len(v) + + for u_i, v_i in zip(u, v): + counter[type(u)] += 1 + check(u_i, v_i, counter=counter, guard=guard | {id(u), id(v)}) + + return counter + + counter = check(actual.a, a) + # check(actual.a, a) + # check(actual.a[0], a[0]) -> ++counter[dict] + # ++counter[int] (a[0] is an int) + # check(actual.a[1], a[1]) -> ++counter[dict] + # check(actual.a[1][0], a[1][0]) -> ++counter[dict] + # ++counter[int] (a[1][0] is an int) + # check(actual.a[1][1], a[1][1]) -> ++counter[dict] + # recursive guard since a[1][1] == a + assert counter[type(a[0])] == 2 + assert counter[type(a[1])] == 4 + + # same logic as above + counter = check(actual.b, b) + assert counter[type(b[0])] == 2 + assert counter[type(b[1])] == 4 + + +def test_config_pickle_circular_reference_in_dict(): + x: CircularDict = {'a': 1, 'b': {'c': 1}} + x['b'] = x + check_is_serializable(x, circular=True) + + config = Config() + config.add('x', [], '', types=dict) + config.x = x + + actual = pickle.loads(pickle.dumps(config)) + check_is_serializable(actual.x, circular=True) + assert isinstance(actual.x, dict) + + assert actual.x['a'] == 1 + assert actual.x['b']['a'] == 1 + + assert len(actual.x) == 2 + assert len(actual.x['b']) == 2 + assert len(actual.x['b']['b']) == 2 + + def check( + u: dict[str, dict[str, object] | int], + v: dict[str, dict[str, object] | int], + *, + counter: Counter[type, int] | None = None, + guard: frozenset[int] = frozenset(), + ) -> Counter: + counter = Counter() if counter is None else counter + + if id(u) in guard and id(v) in guard: + return counter + + if isinstance(u, int): + assert v.__class__ is u.__class__ + assert u == v + counter[type(u)] += 1 + return counter + + assert isinstance(u, dict) + assert v.__class__ is u.__class__ + assert len(u) == len(v) + + for u_i, v_i in zip(u, v): + counter[type(u)] += 1 + check(u[u_i], v[v_i], counter=counter, guard=guard | {id(u), id(v)}) + return counter + + counters = check(actual.x, x, counter=Counter()) + # check(actual.x, x) + # check(actual.x['a'], x['a']) -> ++counter[dict] + # ++counter[int] (x['a'] is an int) + # check(actual.x['b'], x['b']) -> ++counter[dict] + # recursive guard since x['b'] == x + assert counters[type(x['a'])] == 1 + assert counters[type(x['b'])] == 2 + + def test_extension_values(): config = Config() @@ -104,7 +325,6 @@ def test_overrides(): config.add('value6', {'default': 0}, 'env', ()) config.add('value7', None, 'env', ()) config.add('value8', [], 'env', ()) - config.init_values() assert config.value1 == '1' assert config.value2 == 999 @@ -123,7 +343,6 @@ def test_overrides_boolean(): config.add('value1', None, 'env', [bool]) config.add('value2', None, 'env', [bool]) config.add('value3', True, 'env', ()) - config.init_values() assert config.value1 is True assert config.value2 is False @@ -131,6 +350,37 @@ def test_overrides_boolean(): @mock.patch("sphinx.config.logger") +def test_overrides_dict_str(logger): + config = Config({}, {'spam': 'lobster'}) + + config.add('spam', {'ham': 'eggs'}, 'env', {dict, str}) + + assert config.spam == {'ham': 'eggs'} + + # assert len(caplog.records) == 1 + # msg = caplog.messages[0] + assert logger.method_calls + msg = str(logger.method_calls[0].args[1]) + assert msg == ("cannot override dictionary config setting 'spam', " + "ignoring (use 'spam.key=value' to set individual elements)") + + +def test_callable_defer(): + config = Config() + config.add('alias', lambda c: c.master_doc, '', str) + + assert config.master_doc == 'index' + assert config.alias == 'index' + + config.master_doc = 'contents' + assert config.alias == 'contents' + + config.master_doc = 'master_doc' + config.alias = 'spam' + assert config.alias == 'spam' + + +@mock.patch("sphinx.config.logger") def test_errors_warnings(logger, tmp_path): # test the error for syntax errors in the config file (tmp_path / 'conf.py').write_text('project = \n', encoding='ascii') @@ -141,7 +391,6 @@ def test_errors_warnings(logger, tmp_path): # test the automatic conversion of 2.x only code in configs (tmp_path / 'conf.py').write_text('project = u"Jägermeister"\n', encoding='utf8') cfg = Config.read(tmp_path, {}, None) - cfg.init_values() assert cfg.project == 'Jägermeister' assert logger.called is False @@ -193,7 +442,6 @@ def test_config_eol(logger, tmp_path): for eol in (b'\n', b'\r\n'): configfile.write_bytes(b'project = "spam"' + eol) cfg = Config.read(tmp_path, {}, None) - cfg.init_values() assert cfg.project == 'spam' assert logger.called is False @@ -248,7 +496,6 @@ TYPECHECK_WARNINGS = [ def test_check_types(logger, name, default, annotation, actual, warned): config = Config({name: actual}) config.add(name, default, 'env', annotation or ()) - config.init_values() check_confval_types(None, config) assert logger.warning.called == warned @@ -257,9 +504,9 @@ TYPECHECK_WARNING_MESSAGES = [ ('value1', 'string', [str], ['foo', 'bar'], "The config value `value1' has type `list'; expected `str'."), ('value1', 'string', [str, int], ['foo', 'bar'], - "The config value `value1' has type `list'; expected `str' or `int'."), + "The config value `value1' has type `list'; expected `int' or `str'."), ('value1', 'string', [str, int, tuple], ['foo', 'bar'], - "The config value `value1' has type `list'; expected `str', `int', or `tuple'."), + "The config value `value1' has type `list'; expected `int', `str', or `tuple'."), ] @@ -268,7 +515,6 @@ TYPECHECK_WARNING_MESSAGES = [ def test_conf_warning_message(logger, name, default, annotation, actual, message): config = Config({name: actual}) config.add(name, default, False, annotation or ()) - config.init_values() check_confval_types(None, config) assert logger.warning.called assert logger.warning.call_args[0][0] == message @@ -278,7 +524,6 @@ def test_conf_warning_message(logger, name, default, annotation, actual, message def test_check_enum(logger): config = Config() config.add('value', 'default', False, ENUM('default', 'one', 'two')) - config.init_values() check_confval_types(None, config) logger.warning.assert_not_called() # not warned @@ -287,7 +532,6 @@ def test_check_enum(logger): def test_check_enum_failed(logger): config = Config({'value': 'invalid'}) config.add('value', 'default', False, ENUM('default', 'one', 'two')) - config.init_values() check_confval_types(None, config) assert logger.warning.called @@ -296,7 +540,6 @@ def test_check_enum_failed(logger): def test_check_enum_for_list(logger): config = Config({'value': ['one', 'two']}) config.add('value', 'default', False, ENUM('default', 'one', 'two')) - config.init_values() check_confval_types(None, config) logger.warning.assert_not_called() # not warned @@ -305,11 +548,18 @@ def test_check_enum_for_list(logger): def test_check_enum_for_list_failed(logger): config = Config({'value': ['one', 'two', 'invalid']}) config.add('value', 'default', False, ENUM('default', 'one', 'two')) - config.init_values() check_confval_types(None, config) assert logger.warning.called +@mock.patch("sphinx.config.logger") +def test_check_any(logger): + config = Config({'value': None}) + config.add('value', 'default', '', Any) + check_confval_types(None, config) + logger.warning.assert_not_called() # not warned + + nitpick_warnings = [ "WARNING: py:const reference target not found: prefix.anything.postfix", "WARNING: py:class reference target not found: prefix.anything", @@ -320,7 +570,7 @@ nitpick_warnings = [ @pytest.mark.sphinx(testroot='nitpicky-warnings') def test_nitpick_base(app, status, warning): - app.builder.build_all() + app.build(force_all=True) warning = warning.getvalue().strip().split('\n') assert len(warning) == len(nitpick_warnings) @@ -337,7 +587,7 @@ def test_nitpick_base(app, status, warning): }, }) def test_nitpick_ignore(app, status, warning): - app.builder.build_all() + app.build(force_all=True) assert not len(warning.getvalue().strip()) @@ -348,7 +598,7 @@ def test_nitpick_ignore(app, status, warning): ], }) def test_nitpick_ignore_regex1(app, status, warning): - app.builder.build_all() + app.build(force_all=True) assert not len(warning.getvalue().strip()) @@ -359,7 +609,7 @@ def test_nitpick_ignore_regex1(app, status, warning): ], }) def test_nitpick_ignore_regex2(app, status, warning): - app.builder.build_all() + app.build(force_all=True) assert not len(warning.getvalue().strip()) @@ -376,7 +626,7 @@ def test_nitpick_ignore_regex2(app, status, warning): ], }) def test_nitpick_ignore_regex_fullmatch(app, status, warning): - app.builder.build_all() + app.build(force_all=True) warning = warning.getvalue().strip().split('\n') assert len(warning) == len(nitpick_warnings) @@ -386,13 +636,11 @@ def test_nitpick_ignore_regex_fullmatch(app, status, warning): def test_conf_py_language_none(tmp_path): """Regression test for #10474.""" - # Given a conf.py file with language = None (tmp_path / 'conf.py').write_text("language = None", encoding='utf-8') # When we load conf.py into a Config object cfg = Config.read(tmp_path, {}, None) - cfg.init_values() # Then the language is coerced to English assert cfg.language == "en" @@ -401,7 +649,6 @@ def test_conf_py_language_none(tmp_path): @mock.patch("sphinx.config.logger") def test_conf_py_language_none_warning(logger, tmp_path): """Regression test for #10474.""" - # Given a conf.py file with language = None (tmp_path / 'conf.py').write_text("language = None", encoding='utf-8') @@ -418,13 +665,11 @@ def test_conf_py_language_none_warning(logger, tmp_path): def test_conf_py_no_language(tmp_path): """Regression test for #10474.""" - # Given a conf.py file with no language attribute (tmp_path / 'conf.py').write_text("", encoding='utf-8') # When we load conf.py into a Config object cfg = Config.read(tmp_path, {}, None) - cfg.init_values() # Then the language is coerced to English assert cfg.language == "en" @@ -432,13 +677,11 @@ def test_conf_py_no_language(tmp_path): def test_conf_py_nitpick_ignore_list(tmp_path): """Regression test for #11355.""" - # Given a conf.py file with no language attribute (tmp_path / 'conf.py').write_text("", encoding='utf-8') # When we load conf.py into a Config object cfg = Config.read(tmp_path, {}, None) - cfg.init_values() # Then the default nitpick_ignore[_regex] is an empty list assert cfg.nitpick_ignore == [] @@ -465,7 +708,7 @@ def source_date_year(request, monkeypatch): @pytest.mark.sphinx(testroot='copyright-multiline') def test_multi_line_copyright(source_date_year, app, monkeypatch): - app.builder.build_all() + app.build(force_all=True) content = (app.outdir / 'index.html').read_text(encoding='utf-8') @@ -515,3 +758,48 @@ def test_multi_line_copyright(source_date_year, app, monkeypatch): f' \n' f' © Copyright 2022-{source_date_year}, Eve.' ) in content + + +@pytest.mark.parametrize(('conf_copyright', 'expected_copyright'), [ + ('1970', '{current_year}'), + # https://github.com/sphinx-doc/sphinx/issues/11913 + ('1970-1990', '1970-{current_year}'), + ('1970-1990 Alice', '1970-{current_year} Alice'), +]) +def test_correct_copyright_year(conf_copyright, expected_copyright, source_date_year): + config = Config({}, {'copyright': conf_copyright}) + correct_copyright_year(_app=None, config=config) + actual_copyright = config['copyright'] + + if source_date_year is None: + expected_copyright = conf_copyright + else: + expected_copyright = expected_copyright.format(current_year=source_date_year) + assert actual_copyright == expected_copyright + + +def test_gettext_compact_command_line_true(): + config = Config({}, {'gettext_compact': '1'}) + config.add('gettext_compact', True, '', {bool, str}) + _gettext_compact_validator(..., config) + + # regression test for #8549 (-D gettext_compact=1) + assert config.gettext_compact is True + + +def test_gettext_compact_command_line_false(): + config = Config({}, {'gettext_compact': '0'}) + config.add('gettext_compact', True, '', {bool, str}) + _gettext_compact_validator(..., config) + + # regression test for #8549 (-D gettext_compact=0) + assert config.gettext_compact is False + + +def test_gettext_compact_command_line_str(): + config = Config({}, {'gettext_compact': 'spam'}) + config.add('gettext_compact', True, '', {bool, str}) + _gettext_compact_validator(..., config) + + # regression test for #8549 (-D gettext_compact=spam) + assert config.gettext_compact == 'spam' diff --git a/tests/test_correct_year.py b/tests/test_config/test_correct_year.py index 4ef77a6..4ef77a6 100644 --- a/tests/test_correct_year.py +++ b/tests/test_config/test_correct_year.py |