-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
executable file
·274 lines (238 loc) · 9.86 KB
/
test.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
#!/usr/bin/env python3
import difflib
import json
import os
import os.path
import subprocess
import sys
import traceback
# XXX NOTE: THIS CLASS IS EVIL
# The dict must not be mutated after using this
class hack_hashable_dict(dict):
def __hash__(self):
return hash(tuple(sorted(self.items())))
def do_set_hack(x):
if type(x) == dict:
return hack_hashable_dict({k: do_set_hack(v) for k, v in x.items()})
elif type(x) == list:
if len(x) == 0:
return x
if x[0] != "__is_a_set":
return [do_set_hack(x) for x in x]
# We actually have a set now
return set((do_set_hack(x) for x in x[1:]))
else:
return x
def do_parser_tests():
print("*" * 80)
print("Running parser tests...")
print("*" * 80)
# Gather tests
test_files = os.listdir("parser_tests")
test_files_real = []
test_files_set = set()
for f in sorted(test_files):
name, ext = os.path.basename(f).rsplit(".", 1)
vhd_name = "parser_tests/" + name + ".vhd"
json_name = "parser_tests/" + name + ".json"
fail_name = "parser_tests/" + name + ".fail"
if (os.path.isfile(vhd_name) and
(os.path.isfile(json_name) or os.path.isfile(fail_name))):
if os.path.isfile(json_name):
this_test = (vhd_name, json_name, name)
else:
this_test = (vhd_name, None, name)
if name not in test_files_set:
print("Found test \"" + name + "\"")
test_files_real.append(this_test)
test_files_set.add(name)
print("Found " + str(len(test_files_real)) + " tests")
# Run each test
failures = False
for vhd_file, json_file, base_name in test_files_real:
if json_file:
print(base_name + ": ", end='')
else:
print(base_name + " (expect fail): ", end='')
# Run parser
subp = subprocess.run(['./vhdl_parser', vhd_file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if json_file:
# Load reference
with open(json_file, 'r') as inf:
reference = json.load(inf)
if subp.returncode != 0:
failures = True
print("\x1b[31m✗")
print("Executing parser failed!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
continue
# Load parser result
try:
prog_output = json.loads(subp.stdout.decode('ascii'))
except Exception as e:
failures = True
print("\x1b[31m✗")
print("Bad parser output!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
print("\x1b[33m----- exception -----\x1b[0m")
print(traceback.format_exc())
continue
# Compare
if prog_output != reference:
failures = True
print("\x1b[31m✗")
print("Test output mismatch!\x1b[0m")
# Re-encode in sorted order for proper diffing
ref_json = json.dumps(reference, indent=4,
separators=(',', ': '), sort_keys=True)
out_json = json.dumps(prog_output, indent=4,
separators=(',', ': '), sort_keys=True)
print("\x1b[33m----- expected -----\x1b[0m")
print(ref_json)
print("\x1b[33m----- actual -----\x1b[0m")
print(out_json)
print("\x1b[33m----- diff -----\x1b[0m")
udiff = difflib.unified_diff(ref_json.split('\n'),
out_json.split('\n'),
fromfile="expected_output",
tofile="parser_output",
lineterm='')
print('\n'.join(udiff))
else:
print("\x1b[32m✓\x1b[0m")
else:
# Expect failure
if subp.returncode == 0:
failures = True
print("\x1b[31m✗")
print("Executing parser succeeded when it should not!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
continue
print("\x1b[32m✓\x1b[0m")
return failures
# FIXME: Fix copypasta
def do_analyser_json_tests():
print("*" * 80)
print("Running analyser JSON tests...")
print("*" * 80)
# Gather tests
test_files = os.listdir("analyser_json_tests")
test_files_real = []
test_files_set = set()
for f in sorted(test_files):
name, ext = os.path.basename(f).rsplit(".", 1)
vhd_name = "analyser_json_tests/" + name + ".vhd"
json_name = "analyser_json_tests/" + name + ".json"
fail_name = "analyser_json_tests/" + name + ".fail"
if (os.path.isfile(vhd_name) and
(os.path.isfile(json_name) or os.path.isfile(fail_name))):
if os.path.isfile(json_name):
this_test = (vhd_name, json_name, name)
else:
this_test = (vhd_name, None, name)
if name not in test_files_set:
print("Found test \"" + name + "\"")
test_files_real.append(this_test)
test_files_set.add(name)
print("Found " + str(len(test_files_real)) + " tests")
# Run each test
failures = False
for vhd_file, json_file, base_name in test_files_real:
if json_file:
print(base_name + ": ", end='')
else:
print(base_name + " (expect fail): ", end='')
# Run parser
subp = subprocess.run(['./vhdl_analyzer', 'worklib', vhd_file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if json_file:
# Load reference
with open(json_file, 'r') as inf:
reference = json.load(inf)
if subp.returncode != 0:
failures = True
print("\x1b[31m✗")
print("Executing parser failed!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
continue
# Load parser result
try:
last_line = subp.stdout.strip().split(b'\n')[-1]
prog_output = json.loads(last_line.decode('ascii'))
except Exception as e:
failures = True
print("\x1b[31m✗")
print("Bad parser output!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
print("\x1b[33m----- exception -----\x1b[0m")
print(traceback.format_exc())
continue
# Compare
if do_set_hack(prog_output) != do_set_hack(reference):
failures = True
print("\x1b[31m✗")
print("Test output mismatch!\x1b[0m")
# Re-encode in sorted order for proper diffing
ref_json = json.dumps(reference, indent=4,
separators=(',', ': '), sort_keys=True)
out_json = json.dumps(prog_output, indent=4,
separators=(',', ': '), sort_keys=True)
print("\x1b[33m----- expected -----\x1b[0m")
print(ref_json)
print("\x1b[33m----- actual -----\x1b[0m")
print(out_json)
print("\x1b[33m----- diff -----\x1b[0m")
udiff = difflib.unified_diff(ref_json.split('\n'),
out_json.split('\n'),
fromfile="expected_output",
tofile="parser_output",
lineterm='')
print('\n'.join(udiff))
else:
print("\x1b[32m✓\x1b[0m")
else:
# Expect failure
if subp.returncode == 0:
failures = True
print("\x1b[31m✗")
print("Executing parser succeeded when it should not!\x1b[0m")
print("\x1b[33m----- stdout -----\x1b[0m")
sys.stdout.buffer.write(subp.stdout)
print("\x1b[33m----- stderr -----\x1b[0m")
sys.stdout.buffer.write(subp.stderr)
continue
print("\x1b[32m✓\x1b[0m")
return failures
def main():
# I have been burned too many times by this flakiness, so we first set our
# CWD to "definitely where this file is" which also must be in the root
# of the repo
os.chdir(os.path.dirname(__file__))
failures = False
failures = failures or do_parser_tests()
failures = failures or do_analyser_json_tests()
if failures:
print("\x1b[31mThere were test failures!\x1b[0m")
sys.exit(1)
else:
print("\x1b[32mAll tests pass!\x1b[0m")
if __name__ == '__main__':
main()