Skip to content

Commit

Permalink
added a summary line to the logging (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Maas authored Sep 19, 2022
1 parent 2b356ce commit eb71583
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 29 deletions.
2 changes: 1 addition & 1 deletion deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions deptry/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys
from pathlib import Path
from typing import Dict, List

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
23 changes: 11 additions & 12 deletions deptry/result_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys
from typing import Dict, List


Expand All @@ -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")
Expand Down
28 changes: 28 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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
52 changes: 36 additions & 16 deletions tests/test_result_logger.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb71583

Please sign in to comment.