Skip to content

Commit

Permalink
Remove restriction on duplicate sarif tools (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella authored Nov 21, 2024
1 parent f00d4c0 commit 7ebf129
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/codemodder/codemodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from codemodder.project_analysis.file_parsers.package_store import PackageStore
from codemodder.project_analysis.python_repo_manager import PythonRepoManager
from codemodder.result import ResultSet
from codemodder.sarifs import DuplicateToolError, detect_sarif_tools
from codemodder.sarifs import detect_sarif_tools
from codemodder.semgrep import run as run_semgrep


Expand Down Expand Up @@ -235,7 +235,7 @@ def _run_cli(original_args) -> int:
tool_result_files_map: DefaultDict[str, list[Path]] = detect_sarif_tools(
[Path(name) for name in argv.sarif or []]
)
except (DuplicateToolError, FileNotFoundError) as err:
except FileNotFoundError as err:
logger.error(err)
return 1

Expand Down
11 changes: 0 additions & 11 deletions src/codemodder/sarifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def detect(cls, run_data: dict) -> bool:
pass


class DuplicateToolError(ValueError): ...


def detect_sarif_tools(filenames: list[Path]) -> DefaultDict[str, list[Path]]:
results: DefaultDict[str, list[Path]] = defaultdict(list)

Expand All @@ -42,15 +39,7 @@ def detect_sarif_tools(filenames: list[Path]) -> DefaultDict[str, list[Path]]:
try:
if det.detect(run):
logger.debug("detected %s sarif: %s", name, fname)
# According to the Codemodder spec, it is invalid to have multiple SARIF results for the same tool
# https://github.com/pixee/codemodder-specs/pull/36
if name in results:
raise DuplicateToolError(
f"duplicate tool sarif detected: {name}"
)
results[name].append(Path(fname))
except DuplicateToolError as err:
raise err
except (KeyError, AttributeError, ValueError):
continue

Expand Down
15 changes: 7 additions & 8 deletions tests/test_sarif_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from codemodder.sarifs import DuplicateToolError, detect_sarif_tools
from codemodder.sarifs import detect_sarif_tools
from codemodder.semgrep import SemgrepResult, SemgrepResultSet


Expand Down Expand Up @@ -102,15 +102,14 @@ def test_codeql_sarif_input_two_sarifs_same_tool(self, tmpdir):
check=False,
capture_output=True,
)
assert completed_process.returncode == 1
assert (
"duplicate tool sarif detected: codeql" in completed_process.stderr.decode()
)
assert completed_process.returncode == 0

def test_two_sarifs_same_tool(self):
with pytest.raises(DuplicateToolError) as exc:
detect_sarif_tools([Path("tests/samples/webgoat_v8.2.0_codeql.sarif")] * 2)
assert "duplicate tool sarif detected: codeql" in str(exc.value)
results = detect_sarif_tools(
[Path("tests/samples/webgoat_v8.2.0_codeql.sarif")] * 2
)
assert len(results) == 1
assert len(results["codeql"]) == 2

def test_bad_sarif(self, tmpdir, caplog):
sarif_file = Path("tests") / "samples" / "semgrep.sarif"
Expand Down

0 comments on commit 7ebf129

Please sign in to comment.