From eb715832db9d86d78cf447d9dcb1d7fe6ee077f9 Mon Sep 17 00:00:00 2001 From: Florian Maas Date: Mon, 19 Sep 2022 16:11:32 +0200 Subject: [PATCH] added a summary line to the logging (#120) --- deptry/cli.py | 2 +- deptry/core.py | 10 +++++++ deptry/result_logger.py | 23 ++++++++-------- tests/test_core.py | 28 ++++++++++++++++++++ tests/test_result_logger.py | 52 +++++++++++++++++++++++++------------ 5 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 tests/test_core.py diff --git a/deptry/cli.py b/deptry/cli.py index 9aab5150..81b74cd2 100644 --- a/deptry/cli.py +++ b/deptry/cli.py @@ -113,7 +113,7 @@ "--requirements-txt", "-rt", type=click.STRING, - help="""A .txt files with the project's dependencies. If a file called pyproject.toml with a [tool.poetry.dependencies] section is found, this argument is ignored + help="""A .txt file with the project's dependencies. If a file called pyproject.toml with a [tool.poetry.dependencies] section is found, this argument is ignored and the dependencies are extracted from the pyproject.toml file instead. Example use: `deptry . --requirements-txt req/prod.txt`""", default=DEFAULTS["requirements_txt"], show_default=True, diff --git a/deptry/core.py b/deptry/core.py index 3a1ee99a..cd794efd 100644 --- a/deptry/core.py +++ b/deptry/core.py @@ -1,4 +1,5 @@ import logging +import sys from pathlib import Path from typing import Dict, List @@ -64,6 +65,7 @@ def run(self) -> None: issues = self._find_issues(imported_modules, dependencies, dev_dependencies) ResultLogger(issues=issues).log_and_exit() + self._exit(issues) def _find_issues( self, imported_modules: List[Module], dependencies: List[Dependency], dev_dependencies: List[Dependency] @@ -110,3 +112,11 @@ def _log_config(self) -> None: for key, value in vars(self).items(): logging.debug(f"{key}: {value}") logging.debug("") + + @staticmethod + def _exit(issues): + total_issues_found = sum([len(v) for k, v in issues.items()]) + if total_issues_found > 0: + sys.exit(1) + else: + sys.exit(0) diff --git a/deptry/result_logger.py b/deptry/result_logger.py index 8d2f63c2..b52d6edb 100644 --- a/deptry/result_logger.py +++ b/deptry/result_logger.py @@ -1,5 +1,4 @@ import logging -import sys from typing import Dict, List @@ -12,27 +11,27 @@ def __init__(self, issues: Dict[str, List[str]]): self.issues = issues def log_and_exit(self) -> None: - issue_found = False + total_issues_found = sum([len(v) for k, v in self.issues.items()]) + self._log_total_number_of_issues_found(total_issues_found) if "obsolete" in self.issues and self.issues["obsolete"]: - issue_found = True self._log_obsolete_dependencies(self.issues["obsolete"]) if "missing" in self.issues and self.issues["missing"]: - issue_found = True self._log_missing_dependencies(self.issues["missing"]) if "transitive" in self.issues and self.issues["transitive"]: - issue_found = True self._log_transitive_dependencies(self.issues["transitive"]) if "misplaced_dev" in self.issues and self.issues["misplaced_dev"]: - issue_found = True self._log_misplaced_develop_dependencies(self.issues["misplaced_dev"]) - - if issue_found: + if total_issues_found > 0: self._log_additional_info() - sys.exit(1) + + @staticmethod + def _log_total_number_of_issues_found(number): + if number == 0: + logging.info("Success! No dependency issues found.") + elif number == 1: + logging.info("There was 1 dependency issue found.") else: - # TODO: adapt message below; e.g. if only checking for obsolete and transitive, display 'No obsolete or transitive dependencies found' etc - logging.info("Success! No obsolete, missing, or transitive dependencies found.") - sys.exit(0) + logging.info(f"There were {number} dependency issues found.") def _log_obsolete_dependencies(self, dependencies: List[str], sep: str = "\n\t") -> None: logging.info("\n-----------------------------------------------------\n") diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 00000000..f2ab268e --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,28 @@ +import logging + +import pytest + +from deptry.core import Core + + +def test_simple(): + issues = { + "missing": ["foo"], + "obsolete": ["foo"], + "transitive": ["foo"], + "misplaced_dev": ["foo"], + } + with pytest.raises(SystemExit) as e: + Core._exit(issues) + + assert e.type == SystemExit + assert e.value.code == 1 + + +def test_no_issues(): + issues = {} + with pytest.raises(SystemExit) as e: + Core._exit(issues) + + assert e.type == SystemExit + assert e.value.code == 0 diff --git a/tests/test_result_logger.py b/tests/test_result_logger.py index cba114c7..35fb1d10 100644 --- a/tests/test_result_logger.py +++ b/tests/test_result_logger.py @@ -1,26 +1,46 @@ +import logging + import pytest from deptry.result_logger import ResultLogger -def test_simple(): - issues = { - "missing": ["foo"], - "obsolete": ["foo"], - "transitive": ["foo"], - "misplaced_dev": ["foo"], - } - with pytest.raises(SystemExit) as e: +def test_logging_number_multiple(caplog): + with caplog.at_level(logging.INFO): + issues = { + "missing": ["foo"], + "obsolete": ["foo"], + "transitive": ["foo"], + "misplaced_dev": ["foo"], + } ResultLogger(issues).log_and_exit() - - assert e.type == SystemExit - assert e.value.code == 1 + assert "There were 4 dependency issues found" in caplog.text + assert "The project contains obsolete dependencies" in caplog.text + assert "There are dependencies missing from the project's list of dependencies" in caplog.text + assert "There are transitive dependencies that should be explicitly defined as dependencies" in caplog.text + assert "There are imported modules from development dependencies detected" in caplog.text + assert "For more information, see the documentation" in caplog.text -def test_no_issues(): - issues = {} - with pytest.raises(SystemExit) as e: +def test_logging_number_single(caplog): + with caplog.at_level(logging.INFO): + issues = { + "missing": ["foo"], + } ResultLogger(issues).log_and_exit() + assert "There was 1 dependency issue found" in caplog.text - assert e.type == SystemExit - assert e.value.code == 0 + +def test_logging_number_none(caplog): + with caplog.at_level(logging.INFO): + issues = { + "missing": [], + } + ResultLogger(issues).log_and_exit() + assert "No dependency issues found" in caplog.text + assert "There were 4 dependency issues found" not in caplog.text + assert "The project contains obsolete dependencies" not in caplog.text + assert "There are dependencies missing from the project's list of dependencies" not in caplog.text + assert "There are transitive dependencies that should be explicitly defined as dependencies" not in caplog.text + assert "There are imported modules from development dependencies detected" not in caplog.text + assert "For more information, see the documentation" not in caplog.text