Skip to content

Commit

Permalink
ResultSet or behavior needs update for storing tools (#944)
Browse files Browse the repository at this point in the history
* correctly or result

* only add if tool is present
  • Loading branch information
clavedeluna authored Dec 5, 2024
1 parent fa1f3d6 commit 22ea9ac
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/codemodder/codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ def from_sarif(cls, sarif_file: str | Path, truncate_rule_id: bool = False) -> S
sarif_result, sarif_run, truncate_rule_id
)
result_set.add_result(codeql_result)
result_set.store_tool_data(sarif_run.get("tool", {}))
return result_set
6 changes: 5 additions & 1 deletion src/codemodder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def add_result(self, result: Result):
self.setdefault(result.rule_id, {}).setdefault(loc.file, []).append(result)

def store_tool_data(self, tool_data: dict):
self.tools.append(tool_data)
if tool_data:
self.tools.append(tool_data)

def results_for_rule_and_file(
self, context: CodemodExecutionContext, rule_id: str, file: Path
Expand Down Expand Up @@ -231,6 +232,9 @@ def __or__(self, other):
result.results_for_rule = list_dict_or(
self.results_for_rule, other.results_for_rule
)
for tool in self.tools or other.tools:
result.store_tool_data(tool)

return result

def __ior__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/codemodder/semgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def from_sarif(cls, sarif_file: str | Path, truncate_rule_id: bool = False) -> S
result, sarif_run, truncate_rule_id
)
result_set.add_result(sarif_result)

result_set.store_tool_data(sarif_run.get("tool", {}))
return result_set


Expand Down
2 changes: 2 additions & 0 deletions tests/test_codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def test_from_file(codeql_result_set: Path):
result_set = process_codeql_findings(tuple([str(codeql_result_set)]))
assert len(result_set["py/path-injection"][Path("Path Traversal/py_ctf.py")]) == 1
assert len(result_set["py/path-injection"]) == 2
assert result_set.tools
assert result_set.tools[0]["driver"]


def test_from_duplicate_files(codeql_result_set: Path):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_sarif_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from codemodder.codemods.semgrep import process_semgrep_findings
from codemodder.sarifs import detect_sarif_tools
from codemodder.semgrep import SemgrepResult, SemgrepResultSet

Expand Down Expand Up @@ -149,3 +150,9 @@ def test_two_sarifs_different_tools(self):
assert "semgrep" in results
assert len(results["codeql"]) == 1
assert len(results["semgrep"]) == 1

def test_stores_tools(self):
sarif_file = Path("tests") / "samples" / "semgrep.sarif"
result_set = process_semgrep_findings(tuple([str(sarif_file)]))
assert result_set.tools
assert result_set.tools[0]["driver"]["rules"]

0 comments on commit 22ea9ac

Please sign in to comment.