Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message when pyproject.toml is broken #140

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ dependencies = [
dev = [
"aioitertools==v0.11.0",
"click==8.1.7",
"packaging==24.2",
"packaging==24.0",
"rich==13.7.1",
"tomli==2.0.1",
"trailrunner==1.4.0",
"typing_extensions == 4.12.0",
"watchdog==4.0.1",

"attribution==1.8.0",
"attribution==1.7.1",
"black==24.4.2",
"build>=1.2",
"coverage==7.6.10",
"coverage==7.5.3",
"flit==3.9.0",
"flake8==7.1.1",
"flake8==7.0.0",
"mypy==1.10.0",
"ufmt==2.8.0",
"ufmt==2.6.0",
"usort==1.0.8.post1",
]
docs = [
Expand Down
5 changes: 4 additions & 1 deletion thx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ def load_config(path: Optional[Path] = None) -> Config:
return Config(root=path)

content = pyproject.read_text()
data = tomli.loads(content).get("tool", {}).get("thx", {})
try:
data = tomli.loads(content).get("tool", {}).get("thx", {})
except tomli.TOMLDecodeError as error:
raise ConfigError(f"failure parsing {pyproject}: {error}") from error

default: List[str] = ensure_listish(data.pop("default", None), "tool.thx.default")
jobs: List[Job] = parse_jobs(data.pop("jobs", {}))
Expand Down
12 changes: 12 additions & 0 deletions thx/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ def test_no_config(self) -> None:
result = load_config(td)
self.assertEqual(expected, result)

def test_broken_config(self) -> None:
with fake_pyproject(
"""
[tool.black]
line_length = 37[] # should cause parse failure
"""
) as td:
with self.assertRaisesRegex(
ConfigError, r"failure parsing .*pyproject\.toml"
):
load_config(td)

def test_empty_config(self) -> None:
with fake_pyproject(
"""
Expand Down