-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_cli.py
234 lines (194 loc) · 7.74 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from pathlib import Path
import pretend # type: ignore
import pytest
import pip_audit._cli
from pip_audit._cli import (
OutputFormatChoice,
ProgressSpinnerChoice,
VulnerabilityAliasChoice,
VulnerabilityDescriptionChoice,
VulnerabilityServiceChoice,
)
class TestOutputFormatChoice:
def test_to_format_is_exhaustive(self):
for choice in OutputFormatChoice:
assert choice.to_format(False, False) is not None
assert choice.to_format(True, True) is not None
assert choice.to_format(False, True) is not None
assert choice.to_format(True, False) is not None
def test_str(self):
for choice in OutputFormatChoice:
assert str(choice) == choice.value
class TestVulnerabilityServiceChoice:
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 TestVulnerabilityAliasChoice:
def test_to_bool_is_exhaustive(self):
for choice in VulnerabilityAliasChoice:
assert choice.to_bool(OutputFormatChoice.Json) in {True, False}
assert choice.to_bool(OutputFormatChoice.Markdown) in {True, False}
assert choice.to_bool(OutputFormatChoice.Columns) in {True, False}
assert choice.to_bool(OutputFormatChoice.CycloneDxJson) in {True, False}
assert choice.to_bool(OutputFormatChoice.CycloneDxXml) in {True, False}
def test_auto_to_bool_for_json(self):
assert VulnerabilityAliasChoice.Auto.to_bool(OutputFormatChoice.Json) is True
def test_str(self):
for choice in VulnerabilityAliasChoice:
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"),
],
)
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 *a: 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:
result[0][1].append(pretend.stub(id="bar", aliases=set(), has_any_id=lambda x: True))
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 *a: 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
def test_environment_variable(monkeypatch):
"""Environment variables set before execution change CLI option default."""
monkeypatch.setenv("PIP_AUDIT_DESC", "off")
monkeypatch.setenv("PIP_AUDIT_FORMAT", "markdown")
monkeypatch.setenv("PIP_AUDIT_OUTPUT", "/tmp/fake")
monkeypatch.setenv("PIP_AUDIT_PROGRESS_SPINNER", "off")
monkeypatch.setenv("PIP_AUDIT_VULNERABILITY_SERVICE", "osv")
parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda *a: parser.parse_args([]))
args = pip_audit._cli._parse_args(parser, [])
assert args.desc == VulnerabilityDescriptionChoice.Off
assert args.format == OutputFormatChoice.Markdown
assert args.output == Path("/tmp/fake")
assert not args.progress_spinner
assert args.vulnerability_service == VulnerabilityServiceChoice.Osv