Repository navigation
fix(python): report dependency declarations deptry cannot read instead of skipping silently #200
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
Open
tuyentran4992
wants to merge
2
commits into
habit-hooks:main
Choose a base branch
from
tuyentran4992:fix-deptry-unreadable-declarations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
| ] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI's
ruff checkstep fails on this PR: the parametrized test takes four arguments where the repo caps at three (max-args = 3in the root pyproject). The two data arguments are one thing — a declaration file — and its contents are fixture data like any other, so a named constant keyed by filename keeps each argument meaningful and passes: