Skip to content

Commit

Permalink
extract_declared_dependencies: Handle exceptions due to invalid toml
Browse files Browse the repository at this point in the history
When parsing a pyproject.toml or pixi.toml with invalid TOML data, the
tomllib parser will raise an exception that is not properly caught or
handled by FawltyDeps.

Fix this by logging an error message and returning from the parse
function. This allows FawltyDeps to continue parsing other files and
reporting undeclared/unused dependencies, whereas before it would abort
with a traceback.
  • Loading branch information
jherland committed Sep 20, 2024
1 parent f3b6b0c commit 8198e99
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions fawltydeps/extract_declared_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ def parse_pyproject_toml(path: Path) -> Iterator[DeclaredDependency]:
"""
source = Location(path)
with path.open("rb") as tomlfile:
parsed_contents = tomllib.load(tomlfile)
try:
parsed_contents = tomllib.load(tomlfile)
except tomllib.TOMLDecodeError as e:
logger.error(f"Failed to parse {source}: {e}")
return

skip = set()

Expand Down Expand Up @@ -457,7 +461,11 @@ def parse_pixi_toml(path: Path) -> Iterator[DeclaredDependency]:
"""
source = Location(path)
with path.open("rb") as tomlfile:
parsed_contents = tomllib.load(tomlfile)
try:
parsed_contents = tomllib.load(tomlfile)
except tomllib.TOMLDecodeError as e:
logger.error(f"Failed to parse {source}: {e}")
return

skip = set()

Expand Down
12 changes: 12 additions & 0 deletions tests/test_extract_declared_dependencies_pixi_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ def test_parse_pixi_toml__wellformed_dependencies__yields_dependencies(
assert result == expected


def test_parse_pixi_toml__invalid_toml__yields_no_deps_and_error_message(
write_tmp_files, caplog
):
tmp_path = write_tmp_files({"pixi.toml": "[this is not valid toml\n"})
path = tmp_path / "pixi.toml"

caplog.set_level(logging.ERROR)
result = list(parse_pixi_toml(path))
assert result == []
assert f"Failed to parse {path}:" in caplog.text


@dataclass
class PixiTestVector:
"""Test vectors for parsing of malformed pixi.toml."""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_extract_declared_dependencies_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ def test_parse_pyproject_toml__wellformed_dependencies__yields_dependencies(
assert result == expected


def test_parse_pyproject_toml__invalid_toml__yields_no_deps_and_error_message(
write_tmp_files, caplog
):
tmp_path = write_tmp_files({"pyproject.toml": "[this is not valid toml\n"})
path = tmp_path / "pyproject.toml"

caplog.set_level(logging.ERROR)
result = list(parse_pyproject_toml(path))
assert result == []
assert f"Failed to parse {path}:" in caplog.text


@dataclass
class PyprojectTestVector:
"""Test vectors for parsing of malformed pyproject.toml."""
Expand Down

0 comments on commit 8198e99

Please sign in to comment.