Skip to content

Commit

Permalink
Let 'cve-bin-tool --version' return success
Browse files Browse the repository at this point in the history
Previously, `cve-bin-tool --version` returned an error code of '2',
as if it was an invalid argument.

With this change, it returns successfully, consistent with standard
argparse behavior.
  • Loading branch information
raboof committed Jan 11, 2023
1 parent 5ce7111 commit 6460af7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 7 additions & 3 deletions cve_bin_tool/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ class ErrorMode(Enum):
def excepthook(exc_type, exc_val, exc_tb):
trace = Traceback.from_exception(exc_type, exc_val, exc_tb)
CONSOLE.print(trace)
if ERROR_CODES.get(exc_type):
if isinstance(exc_val, SystemExit):
sys.exit(exc_val.code)
elif ERROR_CODES.get(exc_type):
sys.exit(ERROR_CODES[exc_type])


Expand Down Expand Up @@ -164,7 +166,10 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(exc_val, BaseException):
if isinstance(exc_val, SystemExit):
self.exit_code = exc_val.code
self.exc_val = exc_val
elif isinstance(exc_val, BaseException):
self.exit_code = ERROR_CODES.get(exc_type, -1)
self.exc_val = exc_val
if self.mode == ErrorMode.Ignore:
Expand All @@ -186,7 +191,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
# Error code 3 is reserved for "we found negative cves" (should be impossible)
# Error code 4-20 are reserved just in case
ERROR_CODES = {
SystemExit: 2,
FileNotFoundError: 21,
InvalidCsvError: 22,
InvalidJsonError: 22,
Expand Down
18 changes: 12 additions & 6 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def test_usage(self):
main(["cve-bin-tool"])
assert e.value.args[0] == ERROR_CODES[InsufficientArgs]

def test_version(self):
"""Test that the version returns 0"""
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--version"])
assert e.value.args[0] == 0

def test_invalid_file_or_directory(self):
"""Test behaviour with an invalid file/directory"""
with pytest.raises(SystemExit) as e:
Expand Down Expand Up @@ -136,22 +142,22 @@ def test_invalid_parameter(self):
# no directory specified
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param"])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2

# bad parameter (but good directory)
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param", self.tempdir])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2

# worse parameter
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param && cat hi", self.tempdir])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2

# bad parameter after directory
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", self.tempdir, "--bad-param;cat hi"])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2

@pytest.mark.skipif(not LONG_TESTS(), reason="Update flag tests are long tests")
@pytest.mark.skipif(
Expand Down Expand Up @@ -389,11 +395,11 @@ def test_severity(self, capsys, caplog):
# Check command line parameters - wrong case
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "-S", "HIGH", self.tempdir])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2
# Check command line parameters - wrong option
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "-S", "ALL", self.tempdir])
assert e.value.args[0] == ERROR_CODES[SystemExit]
assert e.value.args[0] == 2

my_test_filename = "sevtest.csv"
my_test_filename_pathlib = Path(my_test_filename)
Expand Down

0 comments on commit 6460af7

Please sign in to comment.