Skip to content

Commit ded5713

Browse files
committed
Fixed more unit tests
1 parent f00ae35 commit ded5713

File tree

5 files changed

+125
-14
lines changed

5 files changed

+125
-14
lines changed

src/codemodder/codemods/test/utils.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def run_and_assert(
9494
assert not changes
9595
return
9696

97-
self.assert_num_changes(changes, num_changes, min_num_changes)
97+
self.assert_num_changes(
98+
changes, num_changes, expected_diff_per_change, min_num_changes
99+
)
98100

99101
self.assert_changes(
100102
tmpdir,
@@ -106,16 +108,25 @@ def run_and_assert(
106108
changes,
107109
)
108110

109-
def assert_num_changes(self, changes, expected_num_changes, min_num_changes):
111+
def assert_num_changes(
112+
self, changes, expected_num_changes, expected_diff_per_change, min_num_changes
113+
):
110114
print("expected_diff_per_change = [")
111115
for c in changes:
112116
print('"""\\')
113-
print(c.diff.replace("\t", " "), end="")
117+
diff_lines = c.diff.replace("\t", " ").splitlines()
118+
for line in diff_lines:
119+
print(line)
114120
print('""",')
115121
print("]")
116-
assert len(changes) == expected_num_changes
122+
if expected_diff_per_change:
123+
assert len(changes) == expected_num_changes
124+
actual_num = len(changes)
125+
else:
126+
assert len(changes[0].changes) == expected_num_changes
127+
actual_num = len(changes[0].changes)
117128

118-
actual_num = len(changes)
129+
# actual_num = len(changes)
119130

120131
if min_num_changes is not None:
121132
assert (
@@ -142,7 +153,7 @@ def assert_changes(
142153
assert all(c.description for change in changes for c in change.changes)
143154

144155
# assert each change individually
145-
if num_changes > 1:
156+
if expected_diff_per_change and num_changes > 1:
146157
assert num_changes == len(expected_diff_per_change)
147158
for change, diff in zip(changes, expected_diff_per_change):
148159
print(change.diff)
@@ -248,7 +259,9 @@ def run_and_assert(
248259
assert not changes
249260
return
250261

251-
self.assert_num_changes(changes, num_changes, min_num_changes)
262+
self.assert_num_changes(
263+
changes, num_changes, expected_diff_per_change, min_num_changes
264+
)
252265

253266
self.assert_findings(changes)
254267

tests/codemods/semgrep/test_semgrep_nan_injection.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def home(request):
6565
tmpdir,
6666
input_code,
6767
expected_output,
68+
num_changes=4,
6869
results=json.dumps(results),
6970
)
7071

@@ -107,6 +108,68 @@ def view(request):
107108
else:
108109
return [1, 2, float(tid), 3]
109110
"""
111+
expected_diff_per_change = [
112+
"""\
113+
---
114+
+++
115+
@@ -2,7 +2,10 @@
116+
tid = request.POST.get("tid")
117+
some_list = [1, 2, 3, float('nan')]
118+
119+
- float(tid) in some_list
120+
+ if tid.lower() == "nan":
121+
+ raise ValueError
122+
+ else:
123+
+ float(tid) in some_list
124+
125+
z = [1, 2, complex(tid), 3]
126+
127+
""",
128+
"""\
129+
---
130+
+++
131+
@@ -4,7 +4,10 @@
132+
133+
float(tid) in some_list
134+
135+
- z = [1, 2, complex(tid), 3]
136+
+ if tid.lower() == "nan":
137+
+ raise ValueError
138+
+ else:
139+
+ z = [1, 2, complex(tid), 3]
140+
141+
x = [float(tid), 1.0, 2.0]
142+
143+
""",
144+
"""\
145+
---
146+
+++
147+
@@ -6,6 +6,9 @@
148+
149+
z = [1, 2, complex(tid), 3]
150+
151+
- x = [float(tid), 1.0, 2.0]
152+
+ if tid.lower() == "nan":
153+
+ raise ValueError
154+
+ else:
155+
+ x = [float(tid), 1.0, 2.0]
156+
157+
return [1, 2, float(tid), 3]
158+
""",
159+
"""\
160+
---
161+
+++
162+
@@ -8,4 +8,7 @@
163+
164+
x = [float(tid), 1.0, 2.0]
165+
166+
- return [1, 2, float(tid), 3]
167+
+ if tid.lower() == "nan":
168+
+ raise ValueError
169+
+ else:
170+
+ return [1, 2, float(tid), 3]
171+
""",
172+
]
110173

111174
results = {
112175
"runs": [
@@ -224,6 +287,8 @@ def view(request):
224287
tmpdir,
225288
input_code,
226289
expected_output,
290+
expected_diff_per_change,
291+
num_changes=4,
227292
results=json.dumps(results),
228293
)
229294

@@ -281,6 +346,7 @@ def view(request):
281346
tmpdir,
282347
input_code,
283348
expected_output,
349+
num_changes=4,
284350
results=json.dumps(results),
285351
)
286352

@@ -337,6 +403,7 @@ def view(request):
337403
tmpdir,
338404
input_code,
339405
expected_output,
406+
num_changes=4,
340407
results=json.dumps(results),
341408
)
342409

@@ -391,6 +458,7 @@ def view(request):
391458
tmpdir,
392459
input_code,
393460
expected_output,
461+
num_changes=4,
394462
results=json.dumps(results),
395463
)
396464

@@ -447,6 +515,7 @@ def view(request):
447515
tmpdir,
448516
input_code,
449517
expected_output,
518+
num_changes=4,
450519
results=json.dumps(results),
451520
)
452521

tests/codemods/sonar/test_sonar_django_receiver_on_top.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def test_name(self):
1313

1414
def assert_findings(self, changes):
1515
# For now we can only link the finding to the line with the receiver decorator
16-
assert changes[0].fixedFindings
17-
assert not changes[1].fixedFindings
16+
assert changes[0].changes[0].fixedFindings
17+
assert not changes[0].changes[1].fixedFindings
1818

1919
def test_simple(self, tmpdir):
2020
input_code = """

tests/codemods/sonar/test_sonar_fix_assert_tuple.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def test_name(self):
1313

1414
def assert_findings(self, changes):
1515
# For now we can only link the finding to the first line changed
16-
assert changes[0].fixedFindings
17-
assert not changes[1].fixedFindings
18-
assert not changes[2].fixedFindings
16+
assert changes[0].changes[0].fixedFindings
17+
assert not changes[0].changes[1].fixedFindings
18+
assert not changes[0].changes[2].fixedFindings
1919

2020
def test_simple(self, tmpdir):
2121
input_code = """

tests/codemods/sonar/test_sonar_timezone_aware_datetime.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from core_codemods.sonar.sonar_timezone_aware_datetime import SonarTimezoneAwareDatetime
55

66

7-
class TestSonarSQLParameterization(BaseSASTCodemodTest):
7+
class TestSonarTimezoneAwareDatetime(BaseSASTCodemodTest):
88
codemod = SonarTimezoneAwareDatetime
99
tool = "sonar"
1010

@@ -26,6 +26,30 @@ def test_simple(self, tmpdir):
2626
timestamp = 1571595618.0
2727
datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
2828
"""
29+
expected_diff_per_change = [
30+
"""\
31+
---
32+
+++
33+
@@ -1,5 +1,5 @@
34+
import datetime
35+
36+
-datetime.datetime.utcnow()
37+
+datetime.datetime.now(tz=datetime.timezone.utc)
38+
timestamp = 1571595618.0
39+
datetime.datetime.utcfromtimestamp(timestamp)
40+
""",
41+
"""\
42+
---
43+
+++
44+
@@ -2,4 +2,4 @@
45+
46+
datetime.datetime.utcnow()
47+
timestamp = 1571595618.0
48+
-datetime.datetime.utcfromtimestamp(timestamp)
49+
+datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
50+
""",
51+
]
52+
2953
issues = {
3054
"issues": [
3155
{
@@ -60,5 +84,10 @@ def test_simple(self, tmpdir):
6084
]
6185
}
6286
self.run_and_assert(
63-
tmpdir, input_code, expected, results=json.dumps(issues), num_changes=2
87+
tmpdir,
88+
input_code,
89+
expected,
90+
expected_diff_per_change,
91+
results=json.dumps(issues),
92+
num_changes=2,
6493
)

0 commit comments

Comments
 (0)