-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
407 lines (315 loc) · 12.3 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python3
# Test script adapted from lit (https://raw.githubusercontent.com/egordorichev/lit)
# Where it was adapted from lox (https://github.com/munificent/craftinginterpreters)
from __future__ import print_function
from __future__ import unicode_literals
from collections import defaultdict
from os import listdir
from os.path import abspath, basename, dirname, isdir, isfile, join, realpath, relpath, splitext
import re
import io
from subprocess import Popen, PIPE
import sys
import os
# Runs the tests.
REPO_DIR = dirname(realpath(__file__))
OUTPUT_EXPECT = re.compile(r'// Expected: ?(.*)')
ERROR_EXPECT = re.compile(r'// (Error.*)')
ERROR_LINE_EXPECT = re.compile(r'// \[((java|c) )?line (\d+)\] (Error.*)')
RUNTIME_ERROR_EXPECT = re.compile(r'// Exception: (.+)')
SYNTAX_ERROR_RE = re.compile(r'\[.*line (\d+)\] (Error.+)')
STACK_TRACE_RE = re.compile(r'\[line (\d+)\]')
NONTEST_RE = re.compile(r'// Ignore')
passed = 0
failed = 0
num_skipped = 0
expectations = 0
interpreter = None
filter_path = None
INTERPRETERS = {}
C_SUITES = []
class Interpreter:
def __init__(self, name, language, args, tests):
self.name = name
self.language = language
self.args = args
self.tests = tests
def c_interpreter(name, tests):
path = name
INTERPRETERS[name] = Interpreter(name, 'c', [path], tests)
C_SUITES.append(name)
c_interpreter('funk', {
'test': 'pass'
})
class Test:
def __init__(self, path):
self.path = path
self.output = []
self.compile_errors = set()
self.runtime_error_line = 0
self.runtime_error_message = None
self.exit_code = 0
self.failures = []
def parse(self):
global num_skipped
global expectations
# Get the path components.
parts = self.path.split('/')
subpath = ""
state = None
# Figure out the state of the test. We don't break.lit out of this loop because
# we want lines for more specific paths to override more general ones.
for part in parts:
if subpath: subpath += '/'
subpath += part
if subpath in interpreter.tests:
state = interpreter.tests[subpath]
if state and state == 'skip':
num_skipped += 1
return False
# TODO: State for tests that should be run but are expected to fail?
line_num = 1
with io.open(self.path, mode='r', encoding='utf-8') as file:
for line in file:
match = OUTPUT_EXPECT.search(line)
if match:
self.output.append((match.group(1), line_num))
expectations += 1
match = ERROR_EXPECT.search(line)
if match:
self.compile_errors.add("[{0}] {1}".format(line_num, match.group(1)))
# If we expect a compile error, it should exit with EX_DATAERR.
self.exit_code = 65
expectations += 1
match = ERROR_LINE_EXPECT.search(line)
if match:
# The two interpreters are slightly different in terms of which
# cascaded errors may appear after an initial compile error because
# their panic mode recovery is a little different. To handle that,
# the tests can indicate if an error line should only appear for a
# certain interpreter.
language = match.group(2)
if not language or language == interpreter.language:
self.compile_errors.add("[{0}] {1}".format(
match.group(3), match.group(4)))
# If we expect a compile error, it should exit with EX_DATAERR.
self.exit_code = 65
expectations += 1
match = RUNTIME_ERROR_EXPECT.search(line)
if match:
self.runtime_error_line = line_num
self.runtime_error_message = match.group(1)
# If we expect a runtime error, it should exit with EX_SOFTWARE.
self.exit_code = 70
expectations += 1
match = NONTEST_RE.search(line)
if match:
# Not a test file at all, so ignore it.
return False
line_num += 1
# If we got here, it's a valid test.
return True
def run(self):
# Invoke the interpreter and run the test.
args = ["./dist/funk", self.path]
proc = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
self.validate(proc.returncode, out, err)
def validate(self, exit_code, out, err):
if self.compile_errors and self.runtime_error_message:
self.fail("Test error: Cannot expect both compile and runtime errors.")
return
try:
out = out.decode("utf-8").replace('\r\n', '\n')
err = err.decode("utf-8").replace('\r\n', '\n')
except:
self.fail('Error decoding output.')
error_lines = err.split('\n')
# Validate that an expected runtime error occurred.
if self.runtime_error_message:
self.validate_runtime_error(error_lines)
else:
self.validate_compile_errors(error_lines)
self.validate_exit_code(exit_code, error_lines)
self.validate_output(out)
def validate_runtime_error(self, error_lines):
if len(error_lines) < 2:
self.fail('Expected runtime error "{0}" and got none.',
self.runtime_error_message)
return
# Skip any compile errors. This can happen if there is a compile error in
# a module loaded by the module being tested.
line = 0
while SYNTAX_ERROR_RE.search(error_lines[line]):
line += 1
if error_lines[line] != self.runtime_error_message:
self.fail('Expected runtime error "{0}" and got:',
self.runtime_error_message)
self.fail(error_lines[line])
# Make sure the stack trace has the right line. Skip over any lines that
# come from builtin libraries.
match = False
stack_lines = error_lines[line + 1:]
for stack_line in stack_lines:
match = STACK_TRACE_RE.search(stack_line)
if match: break
if not match:
self.fail('Expected stack trace and got:')
for stack_line in stack_lines:
self.fail(stack_line)
else:
stack_line = int(match.group(1))
if stack_line != self.runtime_error_line:
self.fail('Expected runtime error on line {0} but was on line {1}.',
self.runtime_error_line, stack_line)
def validate_compile_errors(self, error_lines):
# Validate that every compile error was expected.
found_errors = set()
num_unexpected = 0
for line in error_lines:
match = SYNTAX_ERROR_RE.search(line)
if match:
error = "[{0}] {1}".format(match.group(1), match.group(2))
if error in self.compile_errors:
found_errors.add(error)
else:
if num_unexpected < 10:
self.fail('Unexpected error:')
self.fail(line)
num_unexpected += 1
elif line != '':
if num_unexpected < 10:
self.fail('Unexpected output on stderr:')
self.fail(line)
num_unexpected += 1
if num_unexpected > 10:
self.fail('(truncated ' + str(num_unexpected - 10) + ' more...)')
# Validate that every expected error occurred.
for error in self.compile_errors - found_errors:
self.fail('Missing expected error: {0}', error)
def validate_exit_code(self, exit_code, error_lines):
if exit_code == self.exit_code: return
if len(error_lines) > 10:
error_lines = error_lines[0:10]
error_lines.append('(truncated...)')
self.fail('Expected return code {0} and got {1}. Stderr:',
self.exit_code, exit_code)
self.failures += error_lines
def validate_output(self, out):
# Remove the trailing last empty line.
out_lines = out.split('\n')
if out_lines[-1] == '':
del out_lines[-1]
index = 0
for line in out_lines:
if index >= len(self.output):
self.fail(u'Got output "{0}" when none was expected.', line)
elif self.output[index][0] != line:
self.fail(u'Expected output "{0}" on line {1} and got "{2}".',
self.output[index][0], self.output[index][1], line)
index += 1
while index < len(self.output):
self.fail(u'Missing expected output "{0}" on line {1}.',
self.output[index][0], self.output[index][1])
index += 1
def fail(self, message, *args):
if args:
message = message.format(*args)
self.failures.append(message)
def color_text(text, color):
"""Converts text to a string and wraps it in the ANSI escape sequence for
color, if supported."""
# No ANSI escapes on Windows.
if sys.platform == 'win32':
return str(text)
return color + str(text) + '\033[0m'
def green(text): return color_text(text, '\033[32m')
def pink(text): return color_text(text, '\033[91m')
def red(text): return color_text(text, '\033[31m')
def yellow(text): return color_text(text, '\033[33m')
def gray(text): return color_text(text, '\033[1;30m')
def walk(dir, callback):
"""
Walks [dir], and executes [callback] on each file.
"""
dir = abspath(dir)
for file in listdir(dir):
nfile = join(dir, file)
if isdir(nfile):
walk(nfile, callback)
else:
callback(nfile)
def print_line(line=None):
# Erase the line.
print('\033[2K', end='')
# Move the cursor to the beginning.
print('\r', end='')
if line:
print(line, end='')
sys.stdout.flush()
def run_script(path):
if "benchmark" in path: return
global passed
global failed
global num_skipped
if (splitext(path)[1] != '.funk'):
return
# Check if we are just running a subset of the tests.
if filter_path:
this_test = relpath(path, join(REPO_DIR, 'test'))
if not this_test.startswith(filter_path):
return
# Make a nice short path relative to the working directory.
# Normalize it to use "/" since, among other things, the interpreters expect
# the argument to use that.
path = relpath(path).replace("\\", "/")
# Update the status line.
print_line('Passed: ' + green(passed) +
' Failed: ' + red(failed) +
' Skipped: ' + yellow(num_skipped) +
gray(' (' + path + ')'))
# Read the test and parse out the expectations.
test = Test(path)
if not test.parse():
# It's a skipped or non-test file.
return
test.run()
# Display the results.
if len(test.failures) == 0:
passed += 1
else:
failed += 1
print_line(red('FAIL') + ': ' + path)
print('')
for failure in test.failures:
print(' ' + pink(failure))
print('')
def run_suite(name):
global interpreter
global passed
global failed
global num_skipped
global expectations
interpreter = INTERPRETERS[name]
passed = 0
failed = 0
num_skipped = 0
expectations = 0
walk(join(REPO_DIR, 'tests'), run_script)
print_line()
if failed == 0:
print('All ' + green(passed) + ' tests passed (' + str(expectations) +
' expectations).')
else:
print(green(passed) + ' tests passed. ' + red(failed) + ' tests failed.')
return failed == 0
def run_suites(names):
any_failed = False
for name in names:
print('=== {} ==='.format(name))
if not run_suite(name):
any_failed = True
if any_failed:
sys.exit(1)
if __name__ == '__main__':
run_suites(C_SUITES)