-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_linter.py
415 lines (296 loc) · 14.8 KB
/
test_linter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import pycodestyle
from flake8_debugger import DebuggerChecker
import pytest
class CaptureReport(pycodestyle.BaseReport):
"""Collect the results of the checks."""
def __init__(self, options):
self._results = []
super(CaptureReport, self).__init__(options)
def error(self, line_number, offset, text, check):
"""Store each error."""
code = super(CaptureReport, self).error(line_number, offset, text, check)
if code:
record = {"line": line_number, "col": offset, "message": "{0} {1}".format(code, text[5:])}
self._results.append(record)
return code
class DebuggerTestStyleGuide(pycodestyle.StyleGuide):
logical_checks = []
physical_checks = []
ast_checks = [("debugger_usage", DebuggerChecker, ["tree", "filename", "lines"])]
max_line_length = None
max_doc_length = None
hang_closing = False
verbose = False
benchmark_keys = {"files": 0, "physical lines": 0, "logical lines": 0}
indent_size = 4
_debugger_test_style = DebuggerTestStyleGuide()
def check_code_for_debugger_statements(code):
"""Process code using pycodestyle Checker and return all errors."""
from tempfile import NamedTemporaryFile
test_file = NamedTemporaryFile(delete=False)
test_file.write(code.encode())
test_file.flush()
report = CaptureReport(options=_debugger_test_style)
lines = [line + "\n" for line in code.split("\n")]
checker = pycodestyle.Checker(filename=test_file.name, lines=lines, options=_debugger_test_style, report=report)
checker.check_all()
return report._results
class TestQA(object):
def test_catches_simple_debugger(self):
result = check_code_for_debugger_statements("from ipdb import set_trace as r\nr()")
expected_result = [
{"line": 2, "message": "T100 trace found: set_trace used as r", "col": 0},
{"line": 1, "message": "T100 import for set_trace found as r", "col": 0},
]
assert result == expected_result
def test_catches_simple_debugger_when_called_off_lib(self):
result = check_code_for_debugger_statements("import ipdb\nipdb.set_trace()")
expected_result = [
{"line": 2, "message": "T100 trace found: ipdb.set_trace used", "col": 0},
{"line": 1, "message": "T100 import for ipdb found", "col": 0},
]
assert result == expected_result
def test_catches_simple_debugger_when_called_off_global(self):
result = check_code_for_debugger_statements("__import__('ipdb').set_trace()")
expected_result = [{"line": 1, "message": "T100 trace found: set_trace used", "col": 0}]
assert result == expected_result
@pytest.mark.skipif(True, reason="Not supported just yet")
def test_catches_simple_debugger_when_called_off_var(self):
result = check_code_for_debugger_statements("import ipdb\ntest = ipdb.set_trace\ntest()")
expected_result = [
{"line": 1, "message": "T100 import for ipdb found", "col": 0},
{"line": 3, "message": "T100 trace found: ipdb.set_trace used", "col": 0},
]
assert result == expected_result
class TestBreakpoint(object):
def test_catches_breakpoint_call_for_python_3_7_and_above(self):
result = check_code_for_debugger_statements("breakpoint()")
expected_result = [{"line": 1, "message": "T100 trace found: breakpoint used", "col": 0}]
assert result == expected_result
def test_catches_breakpoint_import(self):
result = check_code_for_debugger_statements("from builtins import breakpoint")
expected_result = [{"line": 1, "message": "T100 import for breakpoint found", "col": 0}]
assert result == expected_result
def test_allows_builtins_import(self):
result = check_code_for_debugger_statements("import builtins")
expected_result = []
assert result == expected_result
def test_catches_breakpoint_usage_from_builtins(self):
result = check_code_for_debugger_statements("import builtins\nbuiltins.breakpoint()")
expected_result = [{"col": 0, "line": 2, "message": "T100 trace found: breakpoint used"}]
assert result == expected_result
def test_catches_breakpoint_imported_as_other_name(self):
result = check_code_for_debugger_statements("from builtins import breakpoint as b\nb()")
expected_result = [
{"line": 2, "message": "T100 trace found: breakpoint used as b", "col": 0},
{"line": 1, "message": "T100 import for breakpoint found as b", "col": 0},
]
assert result == expected_result
class TestNoQA(object):
def test_skip_import(self):
result = check_code_for_debugger_statements("from ipdb import set_trace as r # noqa\nr()")
expected_result = [{"line": 2, "message": "T100 trace found: set_trace used as r", "col": 0}]
assert result == expected_result
def test_skip_usage(self):
result = check_code_for_debugger_statements("from ipdb import set_trace as r\nr() # noqa")
expected_result = [{"line": 1, "message": "T100 import for set_trace found as r", "col": 0}]
assert result == expected_result
def test_skip_import_and_usage(self):
result = check_code_for_debugger_statements("from ipdb import set_trace as r # noqa\nr() # noqa")
expected_result = []
assert result == expected_result
class TestImportCases(object):
def test_import_multiple(self):
result = check_code_for_debugger_statements("import math, ipdb, collections")
assert result == [{"col": 0, "line": 1, "message": "T100 import for ipdb found"}]
def test_import(self):
result = check_code_for_debugger_statements("import pdb")
assert result == [{"col": 0, "line": 1, "message": "T100 import for pdb found"}]
def test_import_interactive_shell_embed(self):
result = check_code_for_debugger_statements("from IPython.terminal.embed import InteractiveShellEmbed")
assert result == [{"col": 0, "line": 1, "message": "T100 import for InteractiveShellEmbed found"}]
def test_import_both_same_line(self):
result = check_code_for_debugger_statements("import pdb, ipdb")
result = sorted(result, key=lambda debugger: debugger["message"])
expected_result = [
{"col": 0, "line": 1, "message": "T100 import for ipdb found"},
{"col": 0, "line": 1, "message": "T100 import for pdb found"},
]
assert result == expected_result
def test_import_math(self):
result = check_code_for_debugger_statements("import math")
assert result == []
def test_import_noqa(self):
result = check_code_for_debugger_statements("import ipdb # noqa")
assert result == []
class TestModuleSetTraceCases(object):
def test_import_ipython_terminal_embed_use_InteractiveShellEmbed(self):
result = check_code_for_debugger_statements(
"from IPython.terminal.embed import InteractiveShellEmbed; InteractiveShellEmbed()()"
)
expected_result = [
{"col": 58, "line": 1, "message": "T100 trace found: InteractiveShellEmbed used"},
{"col": 0, "line": 1, "message": "T100 import for InteractiveShellEmbed found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_ipdb_use_set_trace(self):
result = check_code_for_debugger_statements("import ipdb;ipdb.set_trace();")
expected_result = [
{"col": 12, "line": 1, "message": "T100 trace found: ipdb.set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for ipdb found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_pdb_use_set_trace(self):
result = check_code_for_debugger_statements("import pdb;pdb.set_trace();")
expected_result = [
{"col": 11, "line": 1, "message": "T100 trace found: pdb.set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for pdb found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_pdb_use_set_trace_twice(self):
result = check_code_for_debugger_statements("import pdb;pdb.set_trace() and pdb.set_trace();")
expected_result = [
{"col": 11, "line": 1, "message": "T100 trace found: pdb.set_trace used"},
{"col": 31, "line": 1, "message": "T100 trace found: pdb.set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for pdb found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_other_module_as_set_trace_and_use_it(self):
result = check_code_for_debugger_statements("from math import Max as set_trace\nset_trace()")
assert result == []
def test_import_rdb_use_set_trace(self):
result = check_code_for_debugger_statements("from celery.contrib import rdb;rdb.set_trace();")
expected_result = [
{"col": 31, "line": 1, "message": "T100 trace found: set_trace used"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_from_celery_import_rdb_use_set_trace(self):
result = check_code_for_debugger_statements("import celery.contrib.rdb;celery.contrib.rdb.set_trace();")
expected_result = [
{"col": 26, "line": 1, "message": "T100 trace found: set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for celery.contrib.rdb found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
class TestImportAsCases(object):
def test_import_ipdb_as(self):
result = check_code_for_debugger_statements("import math, ipdb as sif, collections")
assert result == [{"col": 0, "line": 1, "message": "T100 import for ipdb found as sif"}]
class TestModuleASSetTraceCases(object):
def test_import_ipdb_as_use_set_trace(self):
result = check_code_for_debugger_statements("import ipdb as sif;sif.set_trace();")
expected_result = [
{"col": 19, "line": 1, "message": "T100 trace found: sif.set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for ipdb found as sif"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
class TestImportSetTraceCases(object):
def test_import_set_trace_ipdb(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace;set_trace();")
expected_result = [
{"col": 32, "line": 1, "message": "T100 trace found: set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for set_trace found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_set_trace_pdb(self):
result = check_code_for_debugger_statements("from pdb import set_trace; set_trace();")
expected_result = [
{"col": 27, "line": 1, "message": "T100 trace found: set_trace used"},
{"col": 0, "line": 1, "message": "T100 import for set_trace found"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_set_trace_ipdb_as_and_use(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace as sif; sif();")
expected_result = [
{"col": 40, "line": 1, "message": "T100 trace found: set_trace used as sif"},
{"col": 0, "line": 1, "message": "T100 import for set_trace found as sif"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_set_trace_ipdb_as_and_use_with_conjunction_and(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace as sif; True and sif();")
expected_result = [
{"col": 49, "line": 1, "message": "T100 trace found: set_trace used as sif"},
{"col": 0, "line": 1, "message": "T100 import for set_trace found as sif"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_set_trace_ipdb_as_and_use_with_conjunction_or(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace as sif; True or sif();")
expected_result = [
{"col": 48, "line": 1, "message": "T100 trace found: set_trace used as sif"},
{"col": 0, "line": 1, "message": "T100 import for set_trace found as sif"},
]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result
def test_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqa(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace as sif; True or sif(); # noqa")
try:
assert result == []
except AssertionError:
pass
def test_import_set_trace_ipdb_as_and_use_with_conjunction_or_noqa_import_only(self):
result = check_code_for_debugger_statements("from ipdb import run, set_trace as sif # noqa\nTrue or sif()")
expected_result = [{"col": 8, "line": 2, "message": "T100 trace found: set_trace used as sif"}]
try:
assert result == expected_result
except AssertionError:
for item in expected_result:
item["col"] = 0
assert result == expected_result