Skip to content
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

_cli, _format: Print skipped packages even when no vulns are found #240

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pip_audit/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,15 @@ def audit() -> None:
result = {}
pkg_count = 0
vuln_count = 0
skip_count = 0
for (spec, vulns) in auditor.audit(source):
if spec.is_skipped():
spec = cast(SkippedDependency, spec)
if args.strict:
_fatal(f"{spec.name}: {spec.skip_reason}")
else:
state.update_state(f"Skipping {spec.name}: {spec.skip_reason}")
skip_count += 1
else:
spec = cast(ResolvedDependency, spec)
state.update_state(f"Auditing {spec.name} ({spec.version})")
Expand Down Expand Up @@ -390,3 +392,5 @@ def audit() -> None:
sys.exit(1)
else:
print("No known vulnerabilities found", file=sys.stderr)
if skip_count > 0:
print(formatter.format(result, fixes))
25 changes: 15 additions & 10 deletions pip_audit/_format/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ def format(
for vuln in vulns:
vuln_data.append(self._format_vuln(dep, vuln, applied_fix))

vuln_strings, sizes = tabulate(vuln_data)
columns_string = str()

# Create and add a separator.
if len(vuln_data) > 0:
vuln_strings.insert(1, " ".join(map(lambda x: "-" * x, sizes)))
# If it's just a header, don't bother adding it to the output
if len(vuln_data) > 1:
vuln_strings, sizes = tabulate(vuln_data)

columns_string = str()
for row in vuln_strings:
if columns_string:
columns_string += "\n"
columns_string += row
# Create and add a separator.
if len(vuln_data) > 0:
vuln_strings.insert(1, " ".join(map(lambda x: "-" * x, sizes)))

for row in vuln_strings:
if columns_string:
columns_string += "\n"
columns_string += row

# Now display the skipped dependencies
skip_data: List[List[Any]] = []
Expand All @@ -99,7 +102,9 @@ def format(
skip_strings.insert(1, " ".join(map(lambda x: "-" * x, sizes)))

for row in skip_strings:
columns_string += "\n" + row
if columns_string:
columns_string += "\n"
columns_string += row

return columns_string

Expand Down
21 changes: 21 additions & 0 deletions test/format/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
_SKIPPED_DEP: [],
}

_TEST_NO_VULN_DATA: Dict[service.Dependency, List[service.VulnerabilityResult]] = {
_RESOLVED_DEP_FOO: [],
_RESOLVED_DEP_BAR: [],
}

_TEST_NO_VULN_DATA_SKIPPED_DEP: Dict[service.Dependency, List[service.VulnerabilityResult]] = {
_RESOLVED_DEP_FOO: [],
_RESOLVED_DEP_BAR: [],
_SKIPPED_DEP: [],
}

_TEST_FIX_DATA: List[fix.FixVersion] = [
fix.ResolvedFixVersion(dep=_RESOLVED_DEP_FOO, version=Version("1.8")),
fix.ResolvedFixVersion(dep=_RESOLVED_DEP_BAR, version=Version("0.3")),
Expand All @@ -74,6 +85,16 @@ def vuln_data_skipped_dep():
return _TEST_VULN_DATA_SKIPPED_DEP


@pytest.fixture(autouse=True)
def no_vuln_data():
return _TEST_NO_VULN_DATA


@pytest.fixture(autouse=True)
def no_vuln_data_skipped_dep():
return _TEST_NO_VULN_DATA_SKIPPED_DEP


@pytest.fixture(autouse=True)
def fix_data():
return _TEST_FIX_DATA
Expand Down
14 changes: 14 additions & 0 deletions test/format/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ def test_columns_skipped_dep(vuln_data_skipped_dep):
assert columns_format.format(vuln_data_skipped_dep, list()) == expected_columns


def test_columns_no_vuln_data(no_vuln_data):
columns_format = format.ColumnsFormat(False)
expected_columns = str()
assert columns_format.format(no_vuln_data, list()) == expected_columns


def test_column_no_vuln_data_skipped_dep(no_vuln_data_skipped_dep):
columns_format = format.ColumnsFormat(False)
expected_columns = """Name Skip Reason
---- -----------
bar skip-reason"""
assert columns_format.format(no_vuln_data_skipped_dep, list()) == expected_columns


def test_columns_fix(vuln_data, fix_data):
columns_format = format.ColumnsFormat(False)
expected_columns = """Name Version ID Fix Versions Applied Fix
Expand Down