From e38d1cac489e42f4bdbecbb50f9f25dc9c36c19f Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 5 Mar 2022 08:57:00 -0300 Subject: [PATCH] Improve error message for malformed pyproject.toml files Including the file name is enough to let the user know what the problem is. The same is not needed for `.ini` files because the error message includes the path to the file by default. Fix #9730 --- 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 00000000000..df30ab7e360 --- /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 89ade5f23b9..c082e652d92 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 8013966f071..6784809e097 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -163,7 +163,17 @@ def test_ini_parse_error(self, pytester: Pytester) -> None: 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: