-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtest_cli.py
181 lines (149 loc) · 6.08 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pretend # type: ignore
import pytest
import pip_audit._cli
from pip_audit._cli import (
OutputFormatChoice,
ProgressSpinnerChoice,
VulnerabilityDescriptionChoice,
VulnerabilityServiceChoice,
)
class TestOutputFormatChoice:
def test_to_format_is_exhaustive(self):
for choice in OutputFormatChoice:
assert choice.to_format(False) is not None
assert choice.to_format(True) is not None
def test_str(self):
for choice in OutputFormatChoice:
assert str(choice) == choice.value
class TestVulnerabilityServiceChoice:
def test_to_service_is_exhaustive(self):
for choice in VulnerabilityServiceChoice:
assert choice.to_service(0, pretend.stub()) is not None
def test_str(self):
for choice in VulnerabilityServiceChoice:
assert str(choice) == choice.value
class TestVulnerabilityDescriptionChoice:
def test_to_bool_is_exhaustive(self):
for choice in VulnerabilityDescriptionChoice:
assert choice.to_bool(OutputFormatChoice.Json) in {True, False}
def test_auto_to_bool_for_json(self):
assert VulnerabilityDescriptionChoice.Auto.to_bool(OutputFormatChoice.Json) is True
def test_str(self):
for choice in VulnerabilityDescriptionChoice:
assert str(choice) == choice.value
class TestProgressSpinnerChoice:
def test_bool(self):
assert bool(ProgressSpinnerChoice.On)
assert not bool(ProgressSpinnerChoice.Off)
def test_str(self):
for choice in ProgressSpinnerChoice:
assert str(choice) == choice.value
@pytest.mark.parametrize(
"args, vuln_count, pkg_count, expected",
[
([], 1, 1, "Found 1 known vulnerability in 1 package"),
([], 2, 1, "Found 2 known vulnerabilities in 1 package"),
([], 2, 2, "Found 2 known vulnerabilities in 2 packages"),
(["--ignore-vuln", "bar"], 2, 2, "Found 2 known vulnerabilities, ignored 1 in 2 packages"),
(["--fix"], 1, 1, "fixed 1 vulnerability in 1 package"),
(["--fix"], 2, 1, "fixed 2 vulnerabilities in 1 package"),
(["--fix"], 2, 2, "fixed 2 vulnerabilities in 2 packages"),
([], 0, 0, "No known vulnerabilities found"),
(["--ignore-vuln", "bar"], 0, 1, "No known vulnerabilities found, 1 ignored"),
(["--ignore-vuln", "bar::1970-01-01"], 1, 1, "Found 2 known vulnerabilities in 1 package"),
(["--ignore-vuln", "bar::9999-01-01"], 0, 1, "No known vulnerabilities found, 1 ignored"),
],
)
def test_plurals(capsys, monkeypatch, args, vuln_count, pkg_count, expected):
dummysource = pretend.stub(fix=lambda a: None)
monkeypatch.setattr(pip_audit._cli, "PipSource", lambda *a, **kw: dummysource)
parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda x: parser.parse_args(args))
result = [
(
pretend.stub(
is_skipped=lambda: False,
name="something" + str(i),
canonical_name="something" + str(i),
version=1,
),
[pretend.stub(fix_versions=[2], id="foo", aliases=set(), has_any_id=lambda x: False)]
* (vuln_count // pkg_count),
)
for i in range(pkg_count)
]
if "--ignore-vuln" in args:
should_be_found = "bar::1970-01-01" not in args
result[0][1].append(pretend.stub(id="bar", aliases=set(), has_any_id=lambda x: "bar" in x, fix_versions="baz"))
auditor = pretend.stub(audit=lambda a: result)
monkeypatch.setattr(pip_audit._cli, "Auditor", lambda *a, **kw: auditor)
resolve_fix_versions = [
pretend.stub(is_skipped=lambda: False, dep=spec, version=2) for spec, _ in result
]
monkeypatch.setattr(pip_audit._cli, "resolve_fix_versions", lambda *a: resolve_fix_versions)
try:
pip_audit._cli.audit()
except SystemExit:
pass
captured = capsys.readouterr()
assert expected in captured.err
@pytest.mark.parametrize(
"vuln_count, pkg_count, skip_count, print_format",
[
(1, 1, 0, True),
(2, 1, 0, True),
(2, 2, 0, True),
(0, 0, 0, False),
(0, 1, 0, False),
# If there are no vulnerabilities but a dependency has been skipped, we
# should print the formatted result
(0, 0, 1, True),
],
)
def test_print_format(monkeypatch, vuln_count, pkg_count, skip_count, print_format):
dummysource = pretend.stub(fix=lambda a: None)
monkeypatch.setattr(pip_audit._cli, "PipSource", lambda *a, **kw: dummysource)
dummyformat = pretend.stub(
format=pretend.call_recorder(lambda _result, _fixes: None),
is_manifest=False,
)
monkeypatch.setattr(pip_audit._cli, "ColumnsFormat", lambda *a, **kw: dummyformat)
parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda x: parser.parse_args([]))
result = [
(
pretend.stub(
is_skipped=lambda: False,
name="something" + str(i),
canonical_name="something" + str(i),
version=1,
),
[pretend.stub(fix_versions=[2], id="foo", aliases=set(), has_any_id=lambda x: False)]
* (vuln_count // pkg_count),
)
for i in range(pkg_count)
]
result.extend(
(
pretend.stub(
is_skipped=lambda: True,
name="skipped " + str(i),
canonical_name="skipped " + str(i),
version=1,
skip_reason="reason " + str(i),
),
[],
)
for i in range(skip_count)
)
auditor = pretend.stub(audit=lambda a: result)
monkeypatch.setattr(pip_audit._cli, "Auditor", lambda *a, **kw: auditor)
resolve_fix_versions = [
pretend.stub(is_skipped=lambda: False, dep=spec, version=2) for spec, _ in result
]
monkeypatch.setattr(pip_audit._cli, "resolve_fix_versions", lambda *a: resolve_fix_versions)
try:
pip_audit._cli.audit()
except SystemExit:
pass
assert bool(dummyformat.format.calls) == print_format