diff --git a/fawltydeps/extract_declared_dependencies.py b/fawltydeps/extract_declared_dependencies.py index f245b767..b7a7a3f7 100644 --- a/fawltydeps/extract_declared_dependencies.py +++ b/fawltydeps/extract_declared_dependencies.py @@ -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() @@ -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() diff --git a/tests/test_extract_declared_dependencies_pixi_toml.py b/tests/test_extract_declared_dependencies_pixi_toml.py index 5e3cf263..67ca1358 100644 --- a/tests/test_extract_declared_dependencies_pixi_toml.py +++ b/tests/test_extract_declared_dependencies_pixi_toml.py @@ -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.""" diff --git a/tests/test_extract_declared_dependencies_pyproject_toml.py b/tests/test_extract_declared_dependencies_pyproject_toml.py index cfe108b7..2dd083ac 100644 --- a/tests/test_extract_declared_dependencies_pyproject_toml.py +++ b/tests/test_extract_declared_dependencies_pyproject_toml.py @@ -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."""