Skip to content

Commit a5b4e00

Browse files
authored
Short circuit faster when no tool results are available (#483)
1 parent 5809863 commit a5b4e00

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/codemodder/codemods/base_codemod.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def _apply(
162162
else None
163163
)
164164

165+
if results is not None and not results:
166+
logger.debug("No results for %s", self.id)
167+
return
168+
165169
files_to_analyze = (
166170
[
167171
path
@@ -222,6 +226,7 @@ def _process_file(
222226
findings_for_rule.extend(
223227
results.results_for_rule_and_file(context, rule, filename)
224228
)
229+
logger.debug("%d findings for %s", len(findings_for_rule), filename)
225230

226231
file_context = FileContext(
227232
context.directory,
@@ -230,6 +235,9 @@ def _process_file(
230235
line_include,
231236
findings_for_rule,
232237
)
238+
if results is not None and not findings_for_rule:
239+
logger.debug("no findings for %s, short-circuiting analysis", filename)
240+
return file_context
233241

234242
# TODO: for SAST tools we should preemtively filter out files that are not part of the result set
235243

tests/test_codemodder.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,24 @@ def test_cst_parsing_fails(self, build_report, mock_parse, dir_structure):
106106

107107
build_report.return_value.write_report.assert_called_once()
108108

109-
@mock.patch("codemodder.codemods.libcst_transformer.update_code")
110-
@mock.patch(
111-
"codemodder.codemods.libcst_transformer.LibcstTransformerPipeline.apply",
112-
new_callable=mock.PropertyMock,
113-
)
114-
@mock.patch("codemodder.context.CodemodExecutionContext.compile_results")
115-
def test_dry_run(self, _, transform_apply, mock_update_code, dir_structure):
109+
def test_dry_run(self, mocker, dir_structure):
110+
mock_update_code = mocker.patch(
111+
"codemodder.codemods.libcst_transformer.update_code"
112+
)
113+
transform_apply = mocker.patch(
114+
"codemodder.codemods.libcst_transformer.LibcstTransformerPipeline.apply",
115+
new_callable=mock.PropertyMock,
116+
)
117+
mocker.patch("codemodder.context.CodemodExecutionContext.compile_results")
118+
116119
code_dir, codetf = dir_structure
117120
args = [
118121
str(code_dir),
119122
"--output",
120123
str(codetf),
121124
"--dry-run",
122125
# Make this test faster by restricting the number of codemods
123-
"--codemod-include=url-sandbox",
126+
"--codemod-include=use-defusedxml",
124127
]
125128

126129
assert not codetf.exists()
@@ -157,7 +160,9 @@ def test_reporting(self, mock_reporting, dry_run, dir_structure):
157160

158161
mock_reporting.return_value.write_report.assert_called_once()
159162

160-
@pytest.mark.parametrize("codemod", ["secure-random", "pixee:python/secure-random"])
163+
@pytest.mark.parametrize(
164+
"codemod", ["use-defusedxml", "pixee:python/use-defusedxml"]
165+
)
161166
@mock.patch(
162167
"codemodder.codemods.libcst_transformer.LibcstTransformerPipeline.apply",
163168
new_callable=mock.PropertyMock,

0 commit comments

Comments
 (0)