From 61cf7440ddab57705b3088525f411152460c1607 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 7 May 2023 00:44:38 +0200 Subject: [PATCH] feat: sort violations per location and error code --- deptry/core.py | 19 ++- tests/functional/cli/test_cli.py | 124 +++++++++--------- tests/functional/cli/test_cli_pdm.py | 16 +-- .../cli/test_cli_requirements_txt.py | 44 +++---- 4 files changed, 105 insertions(+), 98 deletions(-) diff --git a/deptry/core.py b/deptry/core.py index a394db44..62a24944 100644 --- a/deptry/core.py +++ b/deptry/core.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import operator import sys from dataclasses import dataclass from typing import TYPE_CHECKING @@ -97,33 +98,39 @@ def run(self) -> None: def _find_violations( self, imported_modules_with_locations: list[ModuleLocations], dependencies: list[Dependency] ) -> list[Violation]: - result = [] + violations = [] if not self.skip_obsolete: - result.extend( + violations.extend( ObsoleteDependenciesFinder(imported_modules_with_locations, dependencies, self.ignore_obsolete).find() ) if not self.skip_missing: - result.extend( + violations.extend( MissingDependenciesFinder(imported_modules_with_locations, dependencies, self.ignore_missing).find() ) if not self.skip_transitive: - result.extend( + violations.extend( TransitiveDependenciesFinder( imported_modules_with_locations, dependencies, self.ignore_transitive ).find() ) if not self.skip_misplaced_dev: - result.extend( + violations.extend( MisplacedDevDependenciesFinder( imported_modules_with_locations, dependencies, self.ignore_misplaced_dev ).find() ) - return result + return self._get_sorted_violations(violations) + + @staticmethod + def _get_sorted_violations(violations: list[Violation]) -> list[Violation]: + return sorted( + violations, key=operator.attrgetter("location.file", "location.line", "location.column", "error_code") + ) def _get_dependencies(self, dependency_management_format: DependencyManagementFormat) -> DependenciesExtract: if dependency_management_format is DependencyManagementFormat.POETRY: diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py index 93a36e71..9c6ea441 100644 --- a/tests/functional/cli/test_cli.py +++ b/tests/functional/cli/test_cli.py @@ -47,25 +47,25 @@ def test_cli_returns_error(poetry_project_builder: ToolSpecificProjectBuilder) - }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -116,25 +116,25 @@ def test_cli_ignore_notebooks(project_builder: ToolSpecificProjectBuilder) -> No }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -199,25 +199,25 @@ def test_cli_exclude(project_builder: ToolSpecificProjectBuilder) -> None: }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -268,25 +268,25 @@ def test_cli_extend_exclude(project_builder: ToolSpecificProjectBuilder) -> None }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -371,25 +371,25 @@ def test_cli_not_verbose(project_builder: ToolSpecificProjectBuilder) -> None: }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -431,25 +431,25 @@ def test_cli_verbose(project_builder: ToolSpecificProjectBuilder) -> None: }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, @@ -469,9 +469,9 @@ def test_cli_with_not_json_output(project_builder: ToolSpecificProjectBuilder) - result.stderr == f"Scanning 2 files...\n\n{str(Path('pyproject.toml'))}: DEP002 isort defined as a dependency but not" f" used in the codebase\n{str(Path('pyproject.toml'))}: DEP002 requests defined as a dependency but not" - f" used in the codebase\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the" - f" dependency definitions\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev" - " dependency\nFound 4 dependency issues.\n\nFor more information, see the documentation:" + f" used in the codebase\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev" + f" dependency\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the dependency" + " definitions\nFound 4 dependency issues.\n\nFor more information, see the documentation:" " https://fpgmaas.github.io/deptry/\n" ) @@ -485,9 +485,9 @@ def test_cli_with_json_output(project_builder: ToolSpecificProjectBuilder) -> No result.stderr == f"Scanning 2 files...\n\n{str(Path('pyproject.toml'))}: DEP002 isort defined as a dependency but not" f" used in the codebase\n{str(Path('pyproject.toml'))}: DEP002 requests defined as a dependency but not" - f" used in the codebase\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the" - f" dependency definitions\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev" - " dependency\nFound 4 dependency issues.\n\nFor more information, see the documentation:" + f" used in the codebase\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev" + f" dependency\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the dependency" + " definitions\nFound 4 dependency issues.\n\nFor more information, see the documentation:" " https://fpgmaas.github.io/deptry/\n" ) assert get_issues_report("deptry.json") == [ @@ -517,25 +517,25 @@ def test_cli_with_json_output(project_builder: ToolSpecificProjectBuilder) -> No }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, diff --git a/tests/functional/cli/test_cli_pdm.py b/tests/functional/cli/test_cli_pdm.py index 453f3984..50a95482 100644 --- a/tests/functional/cli/test_cli_pdm.py +++ b/tests/functional/cli/test_cli_pdm.py @@ -44,25 +44,25 @@ def test_cli_with_pdm(pdm_project_builder: ToolSpecificProjectBuilder) -> None: }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, }, diff --git a/tests/functional/cli/test_cli_requirements_txt.py b/tests/functional/cli/test_cli_requirements_txt.py index 0eed9819..1ddcd0ef 100644 --- a/tests/functional/cli/test_cli_requirements_txt.py +++ b/tests/functional/cli/test_cli_requirements_txt.py @@ -47,25 +47,25 @@ def test_cli_single_requirements_txt(requirements_txt_project_builder: ToolSpeci }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP003", - "message": "urllib3 imported but it is a transitive dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "urllib3", + "module": "white", "location": { - "file": str(Path("src/notebook.ipynb")), - "line": 3, + "file": str(Path("src/main.py")), + "line": 6, "column": 0, }, }, @@ -83,13 +83,13 @@ def test_cli_single_requirements_txt(requirements_txt_project_builder: ToolSpeci }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP003", + "message": "urllib3 imported but it is a transitive dependency", }, - "module": "black", + "module": "urllib3", "location": { - "file": str(Path("src/main.py")), - "line": 4, + "file": str(Path("src/notebook.ipynb")), + "line": 3, "column": 0, }, }, @@ -134,25 +134,25 @@ def test_cli_multiple_requirements_txt(requirements_txt_project_builder: ToolSpe }, { "error": { - "code": "DEP001", - "message": "white imported but missing from the dependency definitions", + "code": "DEP004", + "message": "black imported but declared as a dev dependency", }, - "module": "white", + "module": "black", "location": { "file": str(Path("src/main.py")), - "line": 6, + "line": 4, "column": 0, }, }, { "error": { - "code": "DEP004", - "message": "black imported but declared as a dev dependency", + "code": "DEP001", + "message": "white imported but missing from the dependency definitions", }, - "module": "black", + "module": "white", "location": { "file": str(Path("src/main.py")), - "line": 4, + "line": 6, "column": 0, }, },