Skip to content

Commit

Permalink
Improve error message for malformed pyproject.toml files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicoddemus committed Mar 5, 2022
1 parent cf0a4f7 commit e38d1ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/9730.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Malformed ``pyproject.toml`` files now produce a clearer error message.
2 changes: 1 addition & 1 deletion src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e38d1ca

Please sign in to comment.