forked from ivy-llc/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
determine_tests.py
85 lines (79 loc) · 3.17 KB
/
determine_tests.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
import pickle # noqa
from pydriller import Repository
import os # noqa
import bz2
import _pickle as cPickle
if __name__ == "__main__":
tests = bz2.BZ2File("tests.pbz2", "rb")
tests = cPickle.load(tests)
tests_to_run = set()
ref_commit_hash = tests["commit"]
for commit in Repository(".", single=ref_commit_hash).traverse_commits():
ref_commit = commit._c_object
break
for commit in Repository(".", order="reverse").traverse_commits():
tests["commit"] = commit.hash
diff_index = ref_commit.diff(commit._c_object, create_patch=True)
modified_files = commit._parse_diff(diff_index)
for file in modified_files:
try:
file_name = file.new_path + ",cover"
except: # noqa
continue
if file_name not in tests.keys():
continue
tests_file = tests[file_name]
change = file.diff_parsed
added = set([x - 1 for (x, _) in change["added"]])
deleted = set([x - 1 for (x, _) in change["deleted"]])
updated = added.intersection(deleted)
added = added.difference(updated)
deleted = deleted.difference(updated)
# Now Update the Tests and compute the tests to run
for line in deleted:
tests_file_line = tests_file[line]
if len(tests_file_line) >= 100:
continue
tests_to_run.update(tests_file_line)
for line in sorted(deleted, reverse=True):
if line < len(tests_file):
del tests_file[line]
for line in added:
top = -1
bottom = -1
if 0 <= line - 1 < len(tests_file):
top = tests_file[line - 1]
if 0 <= line + 1 < len(tests_file):
bottom = tests_file[line + 1]
tests_line = set()
if top != -1 and bottom != -1:
tests_line = top.intersection(bottom)
elif top != -1:
tests_line = top
elif bottom != -1:
tests_line = bottom
tests_file.insert(line, tests_line)
tests[file_name] = tests_file
# Now Compute the Tests to Run
for line in updated:
tests_file_line = tests_file[line]
if len(tests_file_line) >= 100:
continue
tests_to_run.update(tests_file_line)
for line in added:
tests_file_line = tests_file[line]
if len(tests_file_line) >= 100:
continue
tests_to_run.update(tests_file_line)
break
with bz2.BZ2File("tests.pbz2", "w") as f:
cPickle.dump(tests, f)
print("----- Determined Tests -----")
print(len(tests_to_run))
for test_index in tests_to_run:
print(tests["index_mapping"][test_index])
print("----------------------------")
with open("tests_to_run", "w") as f:
for test_index in tests_to_run:
test = tests["index_mapping"][test_index]
f.write(test + "\n")