Skip to content

Commit ca5e94c

Browse files
authored
refactor(package-list-parser): Make invalid packages log warning instead of throwing error (#1415)
* fixes #1414 * refactor(package-list-parser): Invalid packages will throw warning instead of error * feat(package-list-parser): Make sure the scanned file is .txt * test(package-list-parser): error to warning
1 parent 274a307 commit ca5e94c

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

cve_bin_tool/package_list_parser/__init__.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ def check_file(self):
182182
with ErrorHandler(mode=error_mode):
183183
raise EmptyTxtError(input_file)
184184

185+
if not input_file.endswith(".txt"):
186+
with ErrorHandler(mode=error_mode):
187+
raise InvalidListError(
188+
"Invalid Package list file format (should be .txt)"
189+
)
190+
185191
if not input_file.endswith("requirements.txt"):
186192
if distro.id() not in SUPPORTED_DISTROS:
187193
LOGGER.warning(
@@ -201,10 +207,13 @@ def check_file(self):
201207
)
202208

203209
if output.returncode != 0:
204-
with ErrorHandler(mode=error_mode):
205-
raise InvalidListError(
206-
f"Invalid Package list\n{output.stderr.decode('utf-8')}"
207-
)
210+
invalid_packages = re.findall(
211+
r"E: Unable to locate package (.+)",
212+
output.stderr.decode("utf-8"),
213+
)
214+
LOGGER.warning(
215+
f"Invalid Package found: {','.join(invalid_packages)}"
216+
)
208217
elif distro.id() in RPM_DISTROS:
209218
output = run(
210219
["xargs", "-a", input_file, "rpm", "-qi"],
@@ -216,10 +225,9 @@ def check_file(self):
216225
r"package (.+) is not installed", output.stdout.decode("utf-8")
217226
)
218227
if not_installed_packages:
219-
with ErrorHandler(mode=error_mode):
220-
raise InvalidListError(
221-
f"The packages {','.join(not_installed_packages)} seems to be not installed.\nIt is either an invalid package or not installed.\nUse `sudo yum install $(cat package-list)` to install all packages"
222-
)
228+
LOGGER.warning(
229+
f"The packages {','.join(not_installed_packages)} seems to be not installed.\nIt is either an invalid package or not installed.\nUse `sudo yum install $(cat package-list)` to install all packages"
230+
)
223231
elif distro.id() in PACMAN_DISTROS:
224232
output = run(
225233
["xargs", "-a", input_file, "pacman", "-Qk"],
@@ -233,10 +241,9 @@ def check_file(self):
233241
)
234242

235243
if not_installed_packages:
236-
with ErrorHandler(mode=error_mode):
237-
raise InvalidListError(
238-
f"The packages {','.join(not_installed_packages)} seems to be not installed.\nIt is either an invalid package or not installed.\nUse `sudo pacman -S $(cat package-list)` to install all packages"
239-
)
244+
LOGGER.warning(
245+
f"The packages {','.join(not_installed_packages)} seems to be not installed.\nIt is either an invalid package or not installed.\nUse `sudo pacman -S $(cat package-list)` to install all packages"
246+
)
240247
else:
241248
# TODO: Replace below error handling with a proper pip install dry run
242249
# See: https://github.com/pypa/pip/issues/53

test/test_package_list_parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ def test_valid_requirements(self, filepath, parsed_data):
9292
reason=f"Test for {','.join(SUPPORTED_DISTROS)} systems",
9393
)
9494
@pytest.mark.parametrize(
95-
"filepath, exception",
96-
[(join(TXT_PATH, "test_broken_linux_list.txt"), InvalidListError)],
95+
"filepath",
96+
[(join(TXT_PATH, "test_broken_linux_list.txt"))],
9797
)
98-
def test_invalid_linux_list(self, filepath, exception):
98+
def test_invalid_linux_list(self, filepath, caplog):
9999
package_list = PackageListParser(filepath, error_mode=ErrorMode.FullTrace)
100-
with pytest.raises(exception):
101-
package_list.parse_list()
100+
package_list.check_file()
101+
expected_output = ["Invalid Package found: br0s"]
102+
103+
assert expected_output == [rec.message for rec in caplog.records]
102104

103105
@pytest.mark.skipif(
104106
"ACTIONS" not in environ

0 commit comments

Comments
 (0)