From b1f9e58196349fab36dccd173d04cde21f642ae2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 6 Mar 2022 10:01:13 -0500 Subject: [PATCH 1/2] [7.0.x] Improve error message for malformed pyproject.toml files --- changelog/9730.bugfix.rst | 1 + src/_pytest/config/findpaths.py | 2 +- testing/test_config.py | 12 +++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelog/9730.bugfix.rst diff --git a/changelog/9730.bugfix.rst b/changelog/9730.bugfix.rst new file mode 100644 index 000000000..df30ab7e3 --- /dev/null +++ b/changelog/9730.bugfix.rst @@ -0,0 +1 @@ +Malformed ``pyproject.toml`` files now produce a clearer error message. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 89ade5f23..c082e652d 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -70,7 +70,7 @@ def load_config_dict_from_file( try: config = tomli.loads(toml_text) except tomli.TOMLDecodeError as exc: - raise UsageError(str(exc)) from exc + raise UsageError(f"{filepath}: {exc}") from exc result = config.get("tool", {}).get("pytest", {}).get("ini_options", None) if result is not None: diff --git a/testing/test_config.py b/testing/test_config.py index 8013966f0..6784809e0 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -163,7 +163,17 @@ class TestParseIni: pytester.path.joinpath("pytest.ini").write_text("addopts = -x") result = pytester.runpytest() assert result.ret != 0 - result.stderr.fnmatch_lines(["ERROR: *pytest.ini:1: no section header defined"]) + result.stderr.fnmatch_lines("ERROR: *pytest.ini:1: no section header defined") + + def test_toml_parse_error(self, pytester: Pytester) -> None: + pytester.makepyprojecttoml( + """ + \\" + """ + ) + result = pytester.runpytest() + assert result.ret != 0 + result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*") @pytest.mark.xfail(reason="probably not needed") def test_confcutdir(self, pytester: Pytester) -> None: From 00a163c1529ea3f9fc29007a7b0bb155c0e04a3d Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 3 Mar 2022 09:01:41 -0500 Subject: [PATCH 2/2] Merge pull request #9719 from asottile/fix-setuptools-pollution prevent import-time side-effects from setuptools bundled importlib-metadata --- testing/test_doctest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 67b8ccdb7..1b4809240 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -803,8 +803,8 @@ class TestDoctests: """ p = pytester.makepyfile( setup=""" - from setuptools import setup, find_packages if __name__ == '__main__': + from setuptools import setup, find_packages setup(name='sample', version='0.0', description='description',