Skip to content

Commit

Permalink
Fix crash when using --check switch (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
debonte authored Oct 4, 2024
1 parent 161a926 commit 7f96025
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
3 changes: 1 addition & 2 deletions sarif/cmdline/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ def _create_arg_parser():
def _check(input_files: sarif_file.SarifFileSet, check_level):
ret = 0
if check_level:
counts = input_files.get_result_count_by_severity()
for severity in sarif_file.SARIF_SEVERITIES_WITH_NONE:
ret += counts.get(severity, 0)
ret += input_files.get_report().get_issue_count_for_severity(severity)
if severity == check_level:
break
if ret > 0:
Expand Down
15 changes: 0 additions & 15 deletions sarif/sarif_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,21 +477,6 @@ def get_result_count(self) -> int:
"""
return sum(run.get_result_count() for run in self.runs)

def get_result_count_by_severity(self, severities=None) -> Dict[str, int]:
"""
Return a dict from SARIF severity to number of records.
"""
severities = severities or self.get_severities()
result_count_by_severity_per_run = [
run.get_result_count_by_severity(severities) for run in self.runs
]
return {
severity: sum(
rc.get(severity, 0) for rc in result_count_by_severity_per_run
)
for severity in severities
}

def get_filter_stats(self) -> Optional[FilterStats]:
"""
Get the number of records that were included or excluded by the filter.
Expand Down
28 changes: 28 additions & 0 deletions tests/test_check_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import datetime
from sarif.cmdline.main import _check
from sarif import sarif_file

SARIF = {
"runs": [
{
"tool": {"driver": {"name": "Tool"}},
"results": [{"level": "warning", "ruleId": "rule"}],
}
]
}


def test_check():
fileSet = sarif_file.SarifFileSet()
fileSet.add_file(
sarif_file.SarifFile("SARIF", SARIF, mtime=datetime.datetime.now())
)

result = _check(fileSet, "error")
assert result == 0

result = _check(fileSet, "warning")
assert result == 1

result = _check(fileSet, "note")
assert result == 1

0 comments on commit 7f96025

Please sign in to comment.