diff options
author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-08-06 19:16:10 +0100 |
---|---|---|
committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-08-06 19:16:10 +0100 |
commit | 33fe15c322605b570187724760e3cd08519e8297 (patch) | |
tree | 1d9b5a358d146ba07287b8cc572b7b879c11f96f | |
parent | ef6fd974aa10237fda8bac84de54259ef86e186d (diff) | |
parent | 7119ff19a5526c113cbe60d63ed9262c502958f9 (diff) | |
download | python-setuptools-git-33fe15c322605b570187724760e3cd08519e8297.tar.gz |
Improve pyproject.toml validation messages (#3487)
-rw-r--r-- | changelog.d/3487.misc.rst | 2 | ||||
-rw-r--r-- | setuptools/config/pyprojecttoml.py | 12 | ||||
-rw-r--r-- | setuptools/tests/config/test_pyprojecttoml.py | 23 |
3 files changed, 15 insertions, 22 deletions
diff --git a/changelog.d/3487.misc.rst b/changelog.d/3487.misc.rst new file mode 100644 index 00000000..9dbbb61c --- /dev/null +++ b/changelog.d/3487.misc.rst @@ -0,0 +1,2 @@ +Modified ``pyproject.toml`` validation exception handling to +make relevant debugging information easier to spot. diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index 0e9e3c9c..9ff0c87f 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -41,10 +41,14 @@ def validate(config: dict, filepath: _Path) -> bool: try: return validator.validate(config) except validator.ValidationError as ex: - _logger.error(f"configuration error: {ex.summary}") # type: ignore - _logger.debug(ex.details) # type: ignore - error = ValueError(f"invalid pyproject.toml config: {ex.name}") # type: ignore - raise error from None + summary = f"configuration error: {ex.summary}" + if ex.name.strip("`") != "project": + # Probably it is just a field missing/misnamed, not worthy the verbosity... + _logger.debug(summary) + _logger.debug(ex.details) + + error = f"invalid pyproject.toml config: {ex.name}." + raise ValueError(f"{error}\n{summary}") from None def apply_configuration( diff --git a/setuptools/tests/config/test_pyprojecttoml.py b/setuptools/tests/config/test_pyprojecttoml.py index 200312b5..811328f5 100644 --- a/setuptools/tests/config/test_pyprojecttoml.py +++ b/setuptools/tests/config/test_pyprojecttoml.py @@ -1,4 +1,3 @@ -import logging import re from configparser import ConfigParser from inspect import cleandoc @@ -307,7 +306,7 @@ def test_ignore_unrelated_config(tmp_path, example): @pytest.mark.parametrize( - "example, error_msg, value_shown_in_debug", + "example, error_msg", [ ( """ @@ -316,30 +315,18 @@ def test_ignore_unrelated_config(tmp_path, example): version = "1.2" requires = ['pywin32; platform_system=="Windows"' ] """, - "configuration error: `project` must not contain {'requires'} properties", - '"requires": ["pywin32; platform_system==\\"Windows\\""]', + "configuration error: .project. must not contain ..requires.. properties", ), ], ) -def test_invalid_example(tmp_path, caplog, example, error_msg, value_shown_in_debug): - caplog.set_level(logging.DEBUG) +def test_invalid_example(tmp_path, example, error_msg): pyproject = tmp_path / "pyproject.toml" pyproject.write_text(cleandoc(example)) - caplog.clear() - with pytest.raises(ValueError, match="invalid pyproject.toml"): + pattern = re.compile(f"invalid pyproject.toml.*{error_msg}.*", re.M | re.S) + with pytest.raises(ValueError, match=pattern): read_configuration(pyproject) - # Make sure the logs give guidance to the user - error_log = caplog.record_tuples[0] - assert error_log[1] == logging.ERROR - assert error_msg in error_log[2] - - debug_log = caplog.record_tuples[1] - assert debug_log[1] == logging.DEBUG - debug_msg = "".join(line.strip() for line in debug_log[2].splitlines()) - assert value_shown_in_debug in debug_msg - @pytest.mark.parametrize("config", ("", "[tool.something]\nvalue = 42")) def test_empty(tmp_path, config): |