Skip to content

Commit 9caf084

Browse files
committed
Fixed a few more unit tests
1 parent 9a37d64 commit 9caf084

File tree

6 files changed

+108
-16
lines changed

6 files changed

+108
-16
lines changed

src/codemodder/codemods/base_visitor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ def node_is_selected(self, node) -> bool:
5858
pos_to_match
5959
)
6060

61+
def node_is_selected_by_line_only(self, node) -> bool:
62+
pos_to_match = self.node_position(node)
63+
return self.filter_by_result_line_only(
64+
pos_to_match
65+
) and self.filter_by_path_includes_or_excludes(pos_to_match)
66+
67+
def filter_by_result_line_only(self, pos_to_match) -> bool:
68+
# Codemods with detectors will only run their transformations if there are results.
69+
return self.results is None or any(
70+
pos_to_match.start.line >= location.start.line
71+
and pos_to_match.end.line <= location.end.line
72+
for r in self.results
73+
for location in r.locations
74+
)
75+
6176
def node_position(self, node):
6277
# See https://github.com/Instagram/LibCST/blob/main/libcst/_metadata_dependent.py#L112
6378
match node:

src/codemodder/codemods/test/utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from codemodder import registry
1010
from codemodder.codemods.api import BaseCodemod
11-
from codemodder.codetf import Change
11+
from codemodder.codetf.v2.codetf import ChangeSet
1212
from codemodder.context import CodemodExecutionContext
1313
from codemodder.diff import create_diff
1414
from codemodder.providers import load_providers
@@ -107,10 +107,12 @@ def run_and_assert(
107107
)
108108

109109
def assert_num_changes(self, changes, expected_num_changes, min_num_changes):
110-
print(len(changes))
111-
print(changes)
110+
print("expected_diff_per_change = [")
112111
for c in changes:
113-
print(c.diff)
112+
print('"""\\')
113+
print(c.diff.replace("\t", " "), end="")
114+
print('""",')
115+
print("]")
114116
assert len(changes) == expected_num_changes
115117

116118
actual_num = len(changes)
@@ -248,7 +250,7 @@ def run_and_assert(
248250

249251
self.assert_num_changes(changes, num_changes, min_num_changes)
250252

251-
self.assert_findings(changes[0].changes)
253+
self.assert_findings(changes)
252254

253255
self.assert_changes(
254256
tmpdir,
@@ -262,7 +264,7 @@ def run_and_assert(
262264

263265
return changes
264266

265-
def assert_findings(self, changes: list[Change]):
267+
def assert_findings(self, changes: list[ChangeSet]):
266268
assert all(
267-
x.fixedFindings for x in changes
269+
c.fixedFindings for a in changes for c in a.changes
268270
), f"Expected all changes to have findings: {changes}"

src/core_codemods/semgrep/semgrep_no_csrf_exempt.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ def leave_Decorator(
2626
self.node_position(original_node)
2727
):
2828
return updated_node
29-
30-
if (
31-
self.find_base_name(original_node.decorator)
32-
== "django.views.decorators.csrf.csrf_exempt"
33-
):
34-
self.report_change(original_node)
35-
return cst.RemovalSentinel.REMOVE
36-
return original_node
29+
# Due to semgrep's odd way of reporting the position for this (decorators + functiondef), we match by line only
30+
if self.node_is_selected_by_line_only(original_node):
31+
if (
32+
self.find_base_name(original_node.decorator)
33+
== "django.views.decorators.csrf.csrf_exempt"
34+
):
35+
self.report_change(original_node)
36+
return cst.RemovalSentinel.REMOVE
37+
return updated_node
3738

3839

3940
SemgrepNoCsrfExempt = SemgrepCodemod(

tests/codemods/semgrep/test_semgrep_no_csrf_exempt.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,31 @@ def ssrf_code_checker(request):
4848
def foo():
4949
pass
5050
"""
51-
51+
expected_diff_per_change = [
52+
"""\
53+
---
54+
+++
55+
@@ -3,7 +3,6 @@
56+
from django.dispatch import receiver
57+
from django.core.signals import request_finished
58+
59+
-@csrf_exempt
60+
def ssrf_code_checker(request):
61+
if request.user.is_authenticated:
62+
if request.method == 'POST':
63+
""",
64+
"""\
65+
---
66+
+++
67+
@@ -12,6 +12,5 @@
68+
69+
70+
@receiver(request_finished)
71+
-@csrf_exempt
72+
def foo():
73+
pass
74+
""",
75+
]
5276
results = {
5377
"runs": [
5478
{
@@ -113,6 +137,7 @@ def foo():
113137
tmpdir,
114138
input_code,
115139
expected_output,
140+
expected_diff_per_change,
116141
results=json.dumps(results),
117142
num_changes=2,
118143
)

tests/codemods/sonar/test_sonar_fix_missing_self_or_cls.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ def instance_method(self):
3030
def class_method(cls):
3131
pass
3232
"""
33+
expected_diff_per_change = [
34+
"""\
35+
---
36+
+++
37+
@@ -1,6 +1,6 @@
38+
39+
class A:
40+
- def instance_method():
41+
+ def instance_method(self):
42+
pass
43+
44+
@classmethod
45+
""",
46+
"""\
47+
---
48+
+++
49+
@@ -4,5 +4,5 @@
50+
pass
51+
52+
@classmethod
53+
- def class_method():
54+
+ def class_method(cls):
55+
pass
56+
""",
57+
]
58+
3359
issues = {
3460
"issues": [
3561
{
@@ -60,6 +86,7 @@ def class_method(cls):
6086
tmpdir,
6187
input_code,
6288
expected_output,
89+
expected_diff_per_change,
6390
results=json.dumps(issues),
6491
num_changes=2,
6592
)

tests/codemods/sonar/test_sonar_invert_boolean_check.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ def test_simple(self, tmpdir):
2020
if a != 2:
2121
b = i >= 10
2222
"""
23+
expected_diff_per_change = [
24+
"""\
25+
---
26+
+++
27+
@@ -1,3 +1,3 @@
28+
29+
-if not a == 2:
30+
+if a != 2:
31+
b = not i < 10
32+
""",
33+
"""\
34+
---
35+
+++
36+
@@ -1,3 +1,3 @@
37+
38+
if not a == 2:
39+
- b = not i < 10
40+
+ b = i >= 10
41+
""",
42+
]
43+
2344
issues = {
2445
"issues": [
2546
{
@@ -50,6 +71,7 @@ def test_simple(self, tmpdir):
5071
tmpdir,
5172
input_code,
5273
expected_output,
74+
expected_diff_per_change,
5375
results=json.dumps(issues),
5476
num_changes=2,
5577
)

0 commit comments

Comments
 (0)