Skip to content

Commit 21ae8b2

Browse files
authored
Report failed parsing (pixee#75)
* report even if no results * pygoat findings only if changset * update unit test
1 parent 6290be0 commit 21ae8b2

6 files changed

Lines changed: 29 additions & 20 deletions

File tree

‎.github/workflows/codemod_pygoat.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ jobs:
3838
- name: Run Codemodder
3939
run: codemodder --output output.codetf pygoat
4040
- name: Check PyGoat Findings
41-
run: make webgoat-test
41+
run: make pygoat-test

‎Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ test:
88
integration-test:
99
${PYTEST} integration_tests
1010

11-
webgoat-test:
12-
${PYTEST} -v ci_tests/test_webgoat_findings.py
11+
pygoat-test:
12+
${PYTEST} -v ci_tests/test_pygoat_findings.py
1313

1414
lint:
1515
pylint -v codemodder core_codemods tests integration_tests
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818

1919
@pytest.fixture(scope="session")
20-
def webgoat_findings():
20+
def pygoat_findings():
2121
with open("output.codetf") as ff:
2222
results = json.load(ff)
2323

24-
yield set([x["codemod"] for x in results["results"]])
24+
yield set([x["codemod"] for x in results["results"] if x["changeset"]])
2525

2626

27-
def test_num_webgoat_findings(webgoat_findings):
28-
assert len(webgoat_findings) == len(EXPECTED_FINDINGS)
27+
def test_num_pygoat_findings(pygoat_findings):
28+
assert len(pygoat_findings) == len(EXPECTED_FINDINGS)
2929

3030

3131
@pytest.mark.parametrize("finding", EXPECTED_FINDINGS)
32-
def test_webgoat_findings(webgoat_findings, finding):
33-
assert finding in webgoat_findings
32+
def test_pygoat_findings(pygoat_findings, finding):
33+
assert finding in pygoat_findings

‎integration_tests/base_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _run_idempotency_chec(self, command):
175175
if pathlib.Path(self.output_path).exists():
176176
with open(self.output_path, "r", encoding="utf-8") as f:
177177
codetf = json.load(f)
178-
assert codetf["results"] == []
178+
assert codetf["results"][0]["changeset"] == []
179179

180180
with open(self.code_path, "r", encoding="utf-8") as f:
181181
still_new_code = f.read()

‎src/codemodder/context.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,16 @@ def add_dependency(self, dependency: str):
7575
def compile_results(self, codemods: list[CodemodExecutorWrapper]):
7676
results = []
7777
for codemod in codemods:
78-
if not (changeset := self._results_by_codemod.get(codemod.id)):
79-
continue
80-
8178
data = {
8279
"codemod": codemod.id,
8380
"summary": codemod.summary,
8481
"description": codemod.description,
8582
"references": codemod.references,
8683
"properties": {},
87-
"failedFiles": [],
88-
"changeset": [change.to_json() for change in changeset],
84+
"failedFiles": [str(file) for file in self.get_failures(codemod.id)],
85+
"changeset": [
86+
change.to_json() for change in self.get_results(codemod.id)
87+
],
8988
}
9089

9190
results.append(data)
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,30 @@ def test_cst_parsing_fails(self, mock_reporting, mock_parse):
3232
"tests/samples/",
3333
"--output",
3434
"here.txt",
35+
"--codemod-include",
36+
"requests-verify",
37+
"--path-include",
38+
"*request.py",
3539
]
3640

3741
res = run(args)
3842

3943
assert res == 0
4044
mock_parse.assert_called()
4145

42-
# We still report for now even if parsing failed
4346
mock_reporting.assert_called_once()
4447
args_to_reporting = mock_reporting.call_args_list[0][0]
4548
assert len(args_to_reporting) == 4
4649
_, _, _, results_by_codemod = args_to_reporting
47-
assert results_by_codemod == []
50+
assert results_by_codemod != []
51+
52+
requests_report = results_by_codemod[0]
53+
assert requests_report["changeset"] == []
54+
assert len(requests_report["failedFiles"]) == 2
55+
assert sorted(requests_report["failedFiles"]) == [
56+
"tests/samples/make_request.py",
57+
"tests/samples/unverified_request.py",
58+
]
4859

4960
@mock.patch("codemodder.codemodder.update_code")
5061
@mock.patch("codemodder.codemods.base_codemod.semgrep_run", side_effect=semgrep_run)
@@ -79,10 +90,9 @@ def test_reporting(self, mock_reporting, dry_run):
7990
args_to_reporting = mock_reporting.call_args_list[0][0]
8091
assert len(args_to_reporting) == 4
8192
_, _, _, results_by_codemod = args_to_reporting
82-
# assert len(results_by_codemod) == 2
8393

84-
for codemod_results in results_by_codemod:
85-
assert len(codemod_results["changeset"]) > 0
94+
registry = load_registered_codemods()
95+
assert len(results_by_codemod) == len(registry.codemods)
8696

8797
@mock.patch("codemodder.codemods.base_codemod.semgrep_run")
8898
def test_no_codemods_to_run(self, mock_semgrep_run):

0 commit comments

Comments
 (0)