Skip to content

Commit

Permalink
Better error message when pyproject.toml is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
amyreese committed Feb 3, 2025
1 parent 8d27d73 commit 017efa1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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.toml: {str(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
10 changes: 10 additions & 0 deletions thx/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ 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, "failure parsing pyproject.toml"):
load_config(td)

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

0 comments on commit 017efa1

Please sign in to comment.