Skip to content

Commit e38d1ca

Browse files
committed
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
1 parent cf0a4f7 commit e38d1ca

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

changelog/9730.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Malformed ``pyproject.toml`` files now produce a clearer error message.

src/_pytest/config/findpaths.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def load_config_dict_from_file(
7070
try:
7171
config = tomli.loads(toml_text)
7272
except tomli.TOMLDecodeError as exc:
73-
raise UsageError(str(exc)) from exc
73+
raise UsageError(f"{filepath}: {exc}") from exc
7474

7575
result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
7676
if result is not None:

testing/test_config.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,17 @@ def test_ini_parse_error(self, pytester: Pytester) -> None:
163163
pytester.path.joinpath("pytest.ini").write_text("addopts = -x")
164164
result = pytester.runpytest()
165165
assert result.ret != 0
166-
result.stderr.fnmatch_lines(["ERROR: *pytest.ini:1: no section header defined"])
166+
result.stderr.fnmatch_lines("ERROR: *pytest.ini:1: no section header defined")
167+
168+
def test_toml_parse_error(self, pytester: Pytester) -> None:
169+
pytester.makepyprojecttoml(
170+
"""
171+
\\"
172+
"""
173+
)
174+
result = pytester.runpytest()
175+
assert result.ret != 0
176+
result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*")
167177

168178
@pytest.mark.xfail(reason="probably not needed")
169179
def test_confcutdir(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)