-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
345 lines (311 loc) · 15.3 KB
/
scraper.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
import ast
import glob
import logging
import astunparse
import libraries
from src import Util
from src.lib.AssertSpec import AssertSpec
from src.lib.AssertType import AssertType
from src.lib.Test import Test
class MyVisitor(ast.NodeVisitor):
def __init__(self, filename: str, classname: str, funcname: str, asserts: list, class_mapping: dict,
assert_strings: list, loglevel=logging.ERROR):
self.count = 0
self.filename = filename
self.classname = classname
self.funcname = funcname
self.asserts = asserts
self.class_mapping = class_mapping
self.assert_strings = assert_strings
self.logger = logging.Logger("Assert Logger")
self.logger.setLevel(loglevel)
def _log(self, *args):
self.logger.log(logging.DEBUG, " ".join(args))
def _add_assert(self, node):
assert_name = None
args = []
if isinstance(node, ast.Call):
assert_name = node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id
if len(node.args) > 2:
args = node.args[2:] # save the extra arguments\
elif len(node.keywords) > 0:
args = node.keywords
elif isinstance(node, ast.Assert):
assert_name = 'assert'
else:
print("Unknown assert : %s" % astunparse.unparse(node).strip())
return
assert_spec = AssertSpec(Test(self.filename, self.classname, self.funcname),
line=node.lineno,
col_offset=node.col_offset,
assert_type=AssertType.get_assert_type(assert_name),
assert_string=astunparse.unparse(node).strip(),
args=args)
if self.filename not in self.class_mapping:
self.class_mapping[self.filename] = dict()
if self.classname not in self.class_mapping[self.filename]:
self.class_mapping[self.filename][self.classname] = []
assert_str = assert_spec.get_uniq_str()
if assert_str in self.assert_strings:
return
self.class_mapping[self.filename][self.classname].append(assert_spec)
self.asserts.append(assert_spec)
self.assert_strings.append(assert_spec.get_uniq_str())
def visit_Assert(self, node):
self._log(">>", astunparse.unparse(node).strip())
self.get_threshold(node.test, node)
self.generic_visit(node)
def visit_Call(self, node: ast.Call):
if isinstance(node.func, ast.Attribute):
if node.func.attr in ['assertTrue', 'assertFalse']:
self._log(">>", astunparse.unparse(node).strip())
if isinstance(node.args[0], ast.Compare):
self.get_threshold(node.args[0], node)
else:
self._log("Not handled:", astunparse.unparse(node).strip())
elif node.func.attr in ['assertGreater', 'assertGreaterEqual', 'assertLessEqual', 'assertLess']:
self._log(">>", astunparse.unparse(node).strip())
if self._is_fp_value(node.args[1]):
self._log(node.func.attr + ':' + astunparse.unparse(node.args[1]).strip())
self._add_assert(node) # call node
self.count += 1
elif self._is_fp_value(node.args[0]):
self._log(node.func.attr + ':' + astunparse.unparse(node.args[0]).strip())
self._add_assert(node) # call node
self.count += 1
else:
self._log("Not handled:", astunparse.unparse(node).strip())
elif node.func.attr in ['assert_almost_equal', 'assert_approx_equal', 'assert_array_almost_equal',
'assert_allclose', 'assert_array_almost_equal_nulp', 'assert_array_max_ulp',
'assert_array_less', 'assert_equal']:
# numpy asserts
self._log(">>", astunparse.unparse(node).strip())
self._add_assert(node)
self.count += 1
elif node.func.attr in ['assertAllClose']:
self._log(">>", astunparse.unparse(node).strip())
self._add_assert(node)
self.count += 1
elif isinstance(node.func, ast.Name):
if node.func.id in ['assertTrue', 'assertFalse']:
self._log(">>", astunparse.unparse(node).strip())
if isinstance(node.args[0], ast.Compare):
self.get_threshold(node.args[0], node)
else:
self._log("Not handled:", astunparse.unparse(node).strip())
elif node.func.id in ['assertGreater', 'assertGreaterEqual', 'assertLessEqual', 'assertLess']:
self._log(">>", astunparse.unparse(node).strip())
if self._is_fp_value(node.args[1]):
self._log(node.func.id + ':' + astunparse.unparse(node.args[1]).strip())
self._add_assert(node) # call node
self.count += 1
elif self._is_fp_value(node.args[0]):
self._log(node.func.attr + ':' + astunparse.unparse(node.args[0]).strip())
self._add_assert(node) # call node
self.count += 1
else:
self._log("Not handled:", astunparse.unparse(node).strip())
elif node.func.id in ['assert_almost_equal', 'assert_approx_equal', 'assert_array_almost_equal',
'assert_allclose', 'assert_array_almost_equal_nulp', 'assert_array_max_ulp',
'assert_array_less', 'assert_equal']:
# numpy asserts
self._log(">>", astunparse.unparse(node).strip())
self._add_assert(node)
self.count += 1
elif node.func.id in ['assertAllClose']:
self._log(">>", astunparse.unparse(node).strip())
self._add_assert(node)
self.count += 1
self.generic_visit(node)
@staticmethod
def _is_fp_value(node):
if isinstance(node, ast.Num):
return True
if isinstance(node, ast.UnaryOp) and isinstance(node.operand, ast.Num):
return True
def get_threshold(self, node, parent_node):
if isinstance(node, ast.Compare) \
and isinstance(node.ops[0], (ast.LtE, ast.Lt, ast.Gt, ast.GtE)) \
and len(node.comparators) == 1 \
and self._is_fp_value(node.comparators[0]): # only handling the case with single comparator operator
self._log(astunparse.unparse(node.comparators[0]).strip())
self._add_assert(parent_node)
self.count += 1
elif isinstance(node, ast.Compare) \
and isinstance(node.ops[0], (ast.LtE, ast.Lt, ast.Gt, ast.GtE)) \
and len(node.comparators) == 1 \
and self._is_fp_value(node.left): # only handling the case with single comparator operator
self._log(astunparse.unparse(node.left).strip())
self._add_assert(parent_node)
self.count += 1
else:
self._log("Not handled : ", ast.dump(node))
class Scraper:
def __init__(self, library_dir, libraryname, loglevel=logging.ERROR):
self.testfiles = glob.glob("{0}/**/*.py".format(library_dir), recursive=True)
self.assertcount = 0
self.markedflaky = 0
self.testnames = []
self.asserts = list()
self.assert_strings = list()
self.class_mapping = dict()
self.loglevel = loglevel
self.libraryname = libraryname
print(library_dir)
@staticmethod
def _has_flaky_annotation(decorator_list):
for dec in decorator_list:
if isinstance(dec, ast.Name):
if 'flaky' in dec.id:
return True
elif isinstance(dec, ast.Call):
if isinstance(dec.func, ast.Name):
if 'flaky' in dec.func.id:
return True
return False
@staticmethod
def _is_skipped(decorator_list):
for dec in decorator_list:
s = astunparse.unparse(dec).strip()
if 'skip' in s:
return True
return False
@staticmethod
def _is_parameterized(decorator_list):
for dec in decorator_list:
s = astunparse.unparse(dec).strip()
if 'parametrize' in s:
return True
return False
@staticmethod
def _valid_class(classDef):
if classDef.name.startswith('_'):
return False
return True
def _get_base_class_asserts(self, baseclassname):
for tf in self.class_mapping:
if baseclassname in self.class_mapping[tf]:
return self.class_mapping[tf][baseclassname]
return []
def _create_sub_class_asserts(self, subclassname, filename, base_class_asserts):
for a in base_class_asserts:
assert_spec = AssertSpec(Test(filename, subclassname, a.test.testname),
line=a.line,
col_offset=a.col_offset,
assert_type=a.assert_type,
assert_string=a.assert_string,
args=a.args,
base_assert=a)
self.assertcount += 1
self.asserts.append(assert_spec)
def filter_by_type(self, assert_types: list):
res = []
for a in self.asserts:
if 'numpy' in assert_types and AssertType.is_numpy_assert(a):
res.append(a)
elif 'python' in assert_types and AssertType.is_python_assert(a):
res.append(a)
elif 'unittest' in assert_types and AssertType.is_unittest_assert(a):
res.append(a)
elif 'tf' in assert_types and AssertType.is_tf_assert(a):
res.append(a)
self.asserts = res
print("Filtered asserts: %s" % len(self.asserts))
def parse_test_files(self):
allclasses = []
for tf in self.testfiles:
with open(tf) as file:
a = ast.parse(file.read())
classes = [n for n in a.body if isinstance(n, ast.ClassDef)]
for c in classes:
if not self._valid_class(c):
continue
class_decorators = c.decorator_list
if self._is_skipped(class_decorators):
continue
allclasses.append((tf, c))
functions = [n for n in c.body if isinstance(n, ast.FunctionDef)]
for f in functions:
if self._is_skipped(f.decorator_list):
continue
if self._is_parameterized(f.decorator_list):
continue
if f.name.lower().startswith("test"):
funcObj = dict()
funcObj['class'] = c.name
funcObj['name'] = f.name
funcObj['isflaky'] = self._has_flaky_annotation(
c.decorator_list) or self._has_flaky_annotation(
f.decorator_list)
self.markedflaky += funcObj['isflaky']
funcObj['filename'] = tf
self.testnames.append(funcObj)
visitor = MyVisitor(tf, c.name, f.name, self.asserts, class_mapping=self.class_mapping,
assert_strings=self.assert_strings, loglevel=self.loglevel)
visitor.visit(f)
self.assertcount += visitor.count
filefunctions = [n for n in a.body if isinstance(n, ast.FunctionDef)]
for f in filefunctions:
if self._is_skipped(f.decorator_list):
continue
if self._is_parameterized(f.decorator_list):
continue
if f.name.lower().startswith("test"):
funcObj = dict()
funcObj['class'] = "none"
funcObj['name'] = f.name
funcObj['isflaky'] = self._has_flaky_annotation(f.decorator_list)
self.markedflaky += funcObj['isflaky']
funcObj['filename'] = tf
self.testnames.append(funcObj)
visitor = MyVisitor(tf, "none", f.name, self.asserts, class_mapping=self.class_mapping,
assert_strings=self.assert_strings,
loglevel=self.loglevel)
visitor.visit(f)
self.assertcount += visitor.count
# look for sub-classes of all classes
for tf, c in allclasses:
if len(c.bases) > 0:
for base in c.bases:
cname = Util.get_name(base)
baseclassasserts = self._get_base_class_asserts(cname)
if len(baseclassasserts) > 0:
self._create_sub_class_asserts(c.name, tf, baseclassasserts)
print("Asserts found %d" % self.assertcount)
def filter_asserts(self):
with open("{0}/test_logs/{1}.txt".format(libraries.PROJECT_DIR, self.libraryname)) as ff:
tests = ff.read()
finalasserts = []
for a in self.asserts:
if a.test.classname != "none":
testname = a.test.filename.split("/")[-1] + "::" + a.test.classname + "::" + a.test.testname
else:
testname = a.test.filename.split("/")[-1] + "::" + a.test.testname
if testname in tests:
finalasserts.append(a)
print("Filtered Asserts :%d" % len(finalasserts))
self.asserts = finalasserts
self.assertcount = len(finalasserts)
def fetchassert(function: ast.FunctionDef):
print("Function name: %s" % function.name)
for stmt in function.body:
if isinstance(stmt, ast.Assert):
print(astunparse.unparse(stmt).strip())
if isinstance(stmt.test, ast.Compare):
print(stmt.test.ops)
if isinstance(stmt.test.ops[0], (ast.Lt, ast.LtE, ast.Gt, ast.GtE)):
print(stmt.test.left)
print(stmt.test.comparators)
# assuming only one comparison
if isinstance(stmt.test.comparators[0], ast.Num):
print("Compare with constant: ", stmt.test.comparators[0])
elif isinstance(stmt.test.ops[0], ast.LtE):
print(stmt.test.left)
print(stmt.test.comparators)
if isinstance(stmt.test.comparators[0], ast.Num):
print("Compare with constant: ", stmt.test.comparators[0])
elif isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call):
if isinstance(stmt.value.func, ast.Attribute):
print(">>>", stmt.value.func.attr)
for arg in stmt.value.args:
print("arg:", astunparse.unparse(arg).strip())