Skip to content

Commit

Permalink
Fix case where no semgrep codemods are available
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Oct 27, 2023
1 parent a58f737 commit 611565f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/codemodder/codemodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ def find_semgrep_results(
codemods: list[CodemodExecutorWrapper],
) -> set[str]:
"""Run semgrep once with all configuration files from all codemods and return a set of applicable rule IDs"""
yaml_files = itertools.chain.from_iterable(
[codemod.yaml_files for codemod in codemods if codemod.yaml_files]
yaml_files = list(
itertools.chain.from_iterable(
[codemod.yaml_files for codemod in codemods if codemod.yaml_files]
)
)
if not yaml_files:
return set()

results = run_semgrep(context, yaml_files)
return {rule_id for file_changes in results.values() for rule_id in file_changes}

Expand Down
17 changes: 16 additions & 1 deletion tests/test_codemodder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mock
import pytest
from codemodder.codemodder import run
from codemodder.codemodder import run, find_semgrep_results
from codemodder.semgrep import run as semgrep_run
from codemodder.registry import load_registered_codemods

Expand Down Expand Up @@ -174,3 +174,18 @@ def test_bad_codemod_name(self):
with pytest.raises(SystemExit) as err:
run(args)
assert err.value.args[0] == 3

def test_find_semgrep_results(self, mocker):
run_semgrep = mocker.patch("codemodder.codemodder.run_semgrep")
codemods = load_registered_codemods()
find_semgrep_results(mocker.MagicMock(), codemods.codemods)
assert run_semgrep.call_count == 1

def test_find_semgrep_results_no_yaml(self, mocker):
run_semgrep = mocker.patch("codemodder.codemodder.run_semgrep")
codemods = load_registered_codemods().match_codemods(
codemod_include=["use-defusedxml"]
)
result = find_semgrep_results(mocker.MagicMock(), codemods)
assert result == set()
assert run_semgrep.call_count == 0

0 comments on commit 611565f

Please sign in to comment.