Skip to content

Commit 9cd8ba3

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

File tree

4 files changed

+92
-32
lines changed

4 files changed

+92
-32
lines changed

src/codemodder/codemods/test/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ def assert_changes(
164164
dedent(expected).splitlines(keepends=True),
165165
)
166166
try:
167-
assert expected_diff == changes.diff
167+
assert expected_diff == changes[0].diff
168168
except AssertionError:
169-
raise DiffError(expected_diff, changes.diff)
169+
raise DiffError(expected_diff, changes[0].diff)
170170

171171
output_code = file_path.read_bytes().decode("utf-8")
172172

tests/codemods/semgrep/test_semgrep_nan_injection.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def home(request):
6666
input_code,
6767
expected_output,
6868
results=json.dumps(results),
69-
num_changes=4,
7069
)
7170

7271
def test_multiple(self, tmpdir):
@@ -226,7 +225,6 @@ def view(request):
226225
input_code,
227226
expected_output,
228227
results=json.dumps(results),
229-
num_changes=16,
230228
)
231229

232230
def test_once_nested(self, tmpdir):
@@ -284,7 +282,6 @@ def view(request):
284282
input_code,
285283
expected_output,
286284
results=json.dumps(results),
287-
num_changes=4,
288285
)
289286

290287
def test_twice_nested(self, tmpdir):
@@ -341,7 +338,6 @@ def view(request):
341338
input_code,
342339
expected_output,
343340
results=json.dumps(results),
344-
num_changes=4,
345341
)
346342

347343
def test_direct_source(self, tmpdir):
@@ -396,7 +392,6 @@ def view(request):
396392
input_code,
397393
expected_output,
398394
results=json.dumps(results),
399-
num_changes=4,
400395
)
401396

402397
def test_binop(self, tmpdir):
@@ -453,7 +448,6 @@ def view(request):
453448
input_code,
454449
expected_output,
455450
results=json.dumps(results),
456-
num_changes=4,
457451
)
458452

459453

tests/codemods/semgrep/test_semgrep_sql_parametrization.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ def f():
4646
conn = sqlite3.connect("example")
4747
conn.cursor().execute(sql, ((user), ))
4848
'''
49+
expected_diff_per_change = [
50+
'''\
51+
---
52+
+++
53+
@@ -8,7 +8,7 @@
54+
@app.route("/example")
55+
def f():
56+
user = request.args["user"]
57+
- sql = """SELECT user FROM users WHERE user = '%s'"""
58+
+ sql = """SELECT user FROM users WHERE user = ?"""
59+
60+
conn = sqlite3.connect("example")
61+
- conn.cursor().execute(sql % (user))
62+
+ conn.cursor().execute(sql, ((user), ))
63+
''',
64+
'''\
65+
---
66+
+++
67+
@@ -8,7 +8,7 @@
68+
@app.route("/example")
69+
def f():
70+
user = request.args["user"]
71+
- sql = """SELECT user FROM users WHERE user = '%s'"""
72+
+ sql = """SELECT user FROM users WHERE user = ?"""
73+
74+
conn = sqlite3.connect("example")
75+
- conn.cursor().execute(sql % (user))
76+
+ conn.cursor().execute(sql, ((user), ))
77+
''',
78+
]
79+
4980
results = {
5081
"runs": [
5182
{
@@ -189,12 +220,11 @@ def f():
189220
}
190221
]
191222
}
192-
changes = self.run_and_assert(
223+
self.run_and_assert(
193224
tmpdir,
194225
input_code,
195226
expexted_output,
227+
expected_diff_per_change,
196228
results=json.dumps(results),
197-
)
198-
assert len(changes[0].changes[0].fixedFindings) == len(
199-
results["runs"][0]["results"]
229+
num_changes=2,
200230
)

tests/codemods/test_with_threading_lock.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class TestThreadingNameResolution(BaseCodemodTest):
9393
codemod = WithThreadingLock
9494

9595
@pytest.mark.parametrize(
96-
"input_code,expected_code,num_changes",
96+
"input_code,expected_code,expected_diff_per_change,num_changes",
9797
[
9898
(
9999
"""
@@ -111,6 +111,7 @@ class TestThreadingNameResolution(BaseCodemodTest):
111111
with lock_1:
112112
...
113113
""",
114+
[],
114115
1,
115116
),
116117
(
@@ -127,6 +128,7 @@ class TestThreadingNameResolution(BaseCodemodTest):
127128
with lock_1:
128129
...
129130
""",
131+
[],
130132
1,
131133
),
132134
(
@@ -147,6 +149,7 @@ def f(l):
147149
with lock_2:
148150
return [lock_1 for lock_1 in l]
149151
""",
152+
[],
150153
1,
151154
),
152155
(
@@ -173,25 +176,49 @@ def f(l):
173176
with lock_2:
174177
print()
175178
""",
179+
[
180+
"""\
181+
---
182+
+++
183+
@@ -1,6 +1,7 @@
184+
185+
import threading
186+
-with threading.Lock():
187+
+lock = threading.Lock()
188+
+with lock:
189+
int("1")
190+
with threading.Lock():
191+
print()
192+
""",
193+
"""\
194+
---
195+
+++
196+
@@ -2,7 +2,8 @@
197+
import threading
198+
with threading.Lock():
199+
int("1")
200+
-with threading.Lock():
201+
+lock = threading.Lock()
202+
+with lock:
203+
print()
204+
var = 1
205+
with threading.Lock():
206+
""",
207+
"""\
208+
---
209+
+++
210+
@@ -5,5 +5,6 @@
211+
with threading.Lock():
212+
print()
213+
var = 1
214+
-with threading.Lock():
215+
+lock = threading.Lock()
216+
+with lock:
217+
print()
218+
""",
219+
],
176220
3,
177221
),
178-
(
179-
"""
180-
import threading
181-
with threading.Lock():
182-
with threading.Lock():
183-
print()
184-
""",
185-
"""
186-
import threading
187-
lock_1 = threading.Lock()
188-
with lock_1:
189-
lock = threading.Lock()
190-
with lock:
191-
print()
192-
""",
193-
2,
194-
),
195222
(
196223
"""
197224
import threading
@@ -210,9 +237,18 @@ def my_func():
210237
with lock_1:
211238
foo()
212239
""",
240+
[],
213241
1,
214242
),
215243
],
216244
)
217-
def test_name_resolution(self, tmpdir, input_code, expected_code, num_changes):
218-
self.run_and_assert(tmpdir, input_code, expected_code, num_changes=num_changes)
245+
def test_name_resolution(
246+
self, tmpdir, input_code, expected_code, expected_diff_per_change, num_changes
247+
):
248+
self.run_and_assert(
249+
tmpdir,
250+
input_code,
251+
expected_code,
252+
expected_diff_per_change,
253+
num_changes=num_changes,
254+
)

0 commit comments

Comments
 (0)