-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntests.py
More file actions
145 lines (124 loc) · 5.45 KB
/
runtests.py
File metadata and controls
145 lines (124 loc) · 5.45 KB
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
# THIS SOFTWARE IS DISTRBUTED AS IS
import argparse
import glob
import json
import re
import sys
import time
from subprocess import PIPE, Popen, run
success_count, fail_count = 0, 0
parser = argparse.ArgumentParser(description='This is the compiler test running script!')
parser.add_argument('--ignore-warnings', default=False, dest='ignore_Warnings')
parser.add_argument('-n', '--test-name', default='all', action="store")
parser.add_argument('--silent', default=False, dest='none-verbose')
parser.add_argument('--clean', default=False, dest='clean-first')
parser.add_argument('-f', '--failed',
help="Run failed tests from previous invocation",
action="store_true")
parser.add_argument('--write-pprint', default=False,
help="Write PPrint output from each test to a corresponding"
" file in tests/pprint_output/\{\}+pprintout.ul",
action="store_true")
parser.add_argument('--check-pprint', default=False,
help="Insure all outputed pretty print files are valid",
action="store_true")
parser.add_argument('--check-ast', default=False,
help="Don't compare the existing, stored PPrint output to the latest one generated",
action="store_true")
parser.add_argument('--custom-test-dir', default=None,
help="Specify a different directory of tests than ./tests to run on")
parser.add_argument('--store-ir', default=False, action="store_true")
parser.add_argument('--fail-fast', default=False, action="store_true")
class FailedToCompileError(Exception):
pass
def collect_files(testdir):
accepts, rejects = [], []
for f in glob.glob('{}/*.ul'.format(testdir)):
if 'accept' in f:
accepts.append(f)
else:
rejects.append(f)
return accepts, rejects
def run_tests(test_list, write_pprint=False, ignore_ast=False):
failed_tests = []
for test in test_list:
failed = run_on_test_file(test, 'reject' in test or 'invalid' in test, write_pprint, ignore_ast)
if failed is not None:
failed_tests.append(failed)
if failed_tests:
with open('.tests-cache', 'w') as f:
f.write(json.dumps({'failed_tests': failed_tests}))
print("=" * 70)
print("Wrote failed tests to Cache. Rerun with --failed")
def run_on_test_file(test, reject, write_pprint, check_pprint):
global success_count, fail_count
prog = run(['java', 'Compiler', test], stdout=PIPE, stderr=PIPE)
err = prog.stderr.decode('utf-8')
out = prog.stdout.decode('utf-8')
if ((err or 'Error' in out) and not reject):
print("FAILED: {} - FAILURE TO ACCEPT. Output:".format(test))
print(out if 'Error' in out else err)
fail_count += 1
return test
elif reject and not err and 'Error' not in out:
print("FAILED: {} - FAILURE TO REJECT".format(test))
fail_count += 1
else:
print("Correctly {} {}".format('rejected' if reject else 'accepted', test.split('/')[-1]))
success_count += 1
test_name = re.search(r'/(.*)\.ul', test).group(1)
if not reject and write_pprint:
with open('tests/pprint_output/pprintout-{}.ul'.format(test_name), 'w') as f:
f.write(out)
print("---> Successfully wrote", f.name)
elif check_pprint and not reject:
with open('tests/pprint_output/pprintout-{}.ul'.format(test_name), 'r') as f:
val = f.read()
assert val == out, "PPrint output was different from existing PPrint.\
Insure you have initialized the tests/pprint_output/ dir by running with --write-pprint"
def compile_proj():
global success_count, fail_count
make = run(['make'], stdout=PIPE, stderr=PIPE)
err = make.stderr.lower()
if b'err' in err:
raise FailedToCompileError(
"Antlr wasn't able to compile your grammar\n {}".format(err))
if b'warn' in err:
raise FailedToCompileError(
"Antlr Compiled with warnings: {}\n. Rerun with -f to override this\n".format(err))
def collect_failed_files(a, r):
tests = []
try:
with open('.tests-cache', 'r') as f:
tests = json.loads(f.read())
except FileNotFoundError:
print("=" * 70)
print("No Test Cache found. Run without --failed first")
tests = tests['failed_tests']
return [t for t in tests if t in a], [t for t in tests if t in r]
def main(args):
global success_count, fail_count
start = time.time()
try:
compile_proj()
except FailedToCompileError as e:
print("FailedToCompileErr: {}".format(e))
return
if args.test_name != 'all': # run a specific test
run_on_test_file(
'tests/{}{}'.format(args.test_name, '.ul' if '.ul' not in args.test_name else ''),
reject='reject' in args.test_name)
return
print("Javac Compiled with no Errors or warnings.\n")
accept, reject = collect_files('tests/pprint_output' if args.check_pprint else 'tests')
if args.failed:
accept, reject = collect_failed_files(accept, reject)
accept.extend(reject)
run_tests(accept, args.write_pprint, args.check_ast)
end = time.time()
num_tests = str(len(accept))
print("=" * 70)
print("All Tests completed. Ran {} tests with {} failures in {} seconds.".format(num_tests, str(fail_count), end - start))
if __name__ == "__main__":
args = parser.parse_args()
main(args)