diff --git a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py index 8cd739c..d97a776 100644 --- a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py +++ b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py @@ -7,6 +7,8 @@ import tempfile from pathlib import Path +UNREADABLE_DECLARATION_NAMES = ("setup.py", "setup.cfg", "Pipfile") + def run_deptry(deptry: str, report: Path) -> subprocess.CompletedProcess[str]: return subprocess.run( @@ -25,6 +27,36 @@ def deptry_found_no_declaration(result: subprocess.CompletedProcess[str]) -> boo return "DependencySpecificationNotFoundError" in result.stderr +def unreadable_declarations() -> list[Path]: + return [ + Path(name) for name in UNREADABLE_DECLARATION_NAMES if Path(name).is_file() + ] + + +def unchecked_dependencies(declarations: list[Path]) -> list[dict]: + return [ + { + "smell": "parse-error", + "details": {}, + "issues": [ + { + "key": str(declaration), + "details": { + "file": str(declaration), + "content": ( + f"dependencies declared in {declaration} are never " + "checked — deptry reads pyproject.toml or requirements " + "files only" + ), + "source": "deptry:no-declaration", + }, + } + for declaration in declarations + ], + } + ] + + def unused_dependencies(report: Path) -> list[dict]: entries = json.loads(report.read_text(encoding="utf-8")) return [entry for entry in entries if entry["error"]["code"] == "DEP002"] @@ -61,6 +93,10 @@ def main() -> int: result = run_deptry(deptry, report) if deptry_crashed(result, report): if deptry_found_no_declaration(result): + declarations = unreadable_declarations() + if declarations: + print(json.dumps(unchecked_dependencies(declarations))) + return 0 print(json.dumps([])) return 0 sys.stderr.write(result.stderr) diff --git a/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py new file mode 100644 index 0000000..316cf42 --- /dev/null +++ b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py @@ -0,0 +1,72 @@ + +from __future__ import annotations + +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +SENSOR = ( + Path(__file__).resolve().parents[1] + / "src/habit_hooks_python/sensors/deptry_sensor.py" +) + +DECLARATION_CONTENTS = { + "setup.py": "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", + "setup.cfg": "[metadata]\nname = demo\nversion = 0.0.0\n\n" + "[options]\ninstall_requires =\n attrs\n", + "Pipfile": "[packages]\nattrs = '*'\n", +} + + +def _run_sensor(tmp_path: Path, deptry: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + [sys.executable, str(SENSOR), deptry], + cwd=tmp_path, + capture_output=True, + encoding="utf-8", + errors="replace", + ) + + +@pytest.mark.parametrize( + "declaration", DECLARATION_CONTENTS, ids=list(DECLARATION_CONTENTS) +) +def test_dependencies_deptry_cannot_read_are_not_silently_skipped( + tmp_path: Path, deptry: str, declaration: str +) -> None: + (tmp_path / declaration).write_text( + DECLARATION_CONTENTS[declaration], encoding="utf-8" + ) + (tmp_path / "app.py").write_text("import rich\n", encoding="utf-8") + + result = _run_sensor(tmp_path, deptry) + + assert result.returncode == 0 + findings = json.loads(result.stdout) + assert [finding["smell"] for finding in findings] == ["parse-error"] + assert [issue["details"]["file"] for issue in findings[0]["issues"]] == [ + declaration + ] + assert findings[0]["issues"][0]["details"]["source"] == "deptry:no-declaration" + + +def test_every_unreadable_declaration_is_named(tmp_path: Path, deptry: str) -> None: + (tmp_path / "setup.py").write_text( + "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", + encoding="utf-8", + ) + (tmp_path / "Pipfile").write_text("[packages]\nattrs = '*'\n", encoding="utf-8") + (tmp_path / "app.py").write_text("import rich\n", encoding="utf-8") + + result = _run_sensor(tmp_path, deptry) + + assert result.returncode == 0 + findings = json.loads(result.stdout) + assert [finding["smell"] for finding in findings] == ["parse-error"] + assert [issue["details"]["file"] for issue in findings[0]["issues"]] == [ + "setup.py", + "Pipfile", + ]