forked from ivy-llc/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
determine_tests.py
79 lines (72 loc) · 2.82 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
import pickle # noqa
from pydriller import Repository
import os
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:
file_name = file.new_path + ",cover"
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_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 line - 1 < len(tests_file):
top = tests_file[line - 1]
if 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_to_run.update(tests_file[line])
for line in added:
tests_to_run.update(tests_file[line])
break
with bz2.BZ2File("tests.pbz2", "w") as f:
cPickle.dump(tests, f)
# with open("tests_to_run", "w") as f:
# for test in tests_to_run:
# f.write(test + "\n")
# Run Tests
failed = False
for test_index in tests_to_run:
test = tests["index_mapping"][test_index]
ret = os.system(
f'docker run --rm -v "$(pwd)":/ivy -v "$(pwd)"/.hypothesis:/.hypothesis unifyai/ivy:latest python3 -m pytest {test}' # noqa
)
if ret != 0:
failed = True
if failed:
exit(1)