-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·370 lines (283 loc) · 10.3 KB
/
Copy pathtest.py
File metadata and controls
executable file
·370 lines (283 loc) · 10.3 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
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
#!/usr/bin/env python3
import re
import sys
from os import listdir
from os.path import abspath, dirname, isdir, join, realpath, relpath, splitext
from subprocess import PIPE, Popen
# Runs the tests.
REPO_DIR = dirname(realpath(__file__))
OUTPUT_EXPECT = re.compile(r"// expect: ?(.*)")
ERROR_EXPECT = re.compile(r"// (Error.*)")
ERROR_LINE_EXPECT = re.compile(r"// \[((java|c) )?line: (\d+)\] (Error.*)")
RUNTIME_ERROR_EXPECT = re.compile(r"// expect runtime error: (.+)")
SYNTAX_ERROR_RE = re.compile(r"\[.*line: (\d+)\] (Error.+)")
STACK_TRACE_RE = re.compile(r"\[line: (\d+)\]")
passed = 0
failed = 0
expectations = 0
filter_path = None
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 expectations
# Get the path components.
parts = self.path.split("/")
subpath = ""
state = "pass"
# Figure out the state of the test. We don't break 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 not state:
print('Unknown test state for "{}".'.format(self.path))
# TODO: State for tests that should be run but are expected to fail?
line_num = 1
with open(self.path, "r") 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 and not self.compile_errors:
self.compile_errors.add(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:
if not self.compile_errors:
self.compile_errors.add(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
line_num += 1
# If we got here, it's a valid test.
return True
def run(self):
# Invoke the interpreter and run the test with the file path as an argument.
args = ["./rune", "run", self.path]
proc = Popen(args, stdout=PIPE, stderr=PIPE, text=True)
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
out = out.replace("\r\n", "\n")
err = err.replace("\r\n", "\n")
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:]
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:
pass
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 = 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('Got output "{0}" when none was expected.', line)
elif self.output[index][0] != line:
self.fail(
'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(
'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 supports_ansi():
return sys.platform != "win32" and sys.stdout.isatty()
def color_text(text, color):
"""Converts text to a string and wraps it in the ANSI escape sequence for
color, if supported."""
if not supports_ansi():
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 sorted(listdir(dir)):
nfile = join(dir, file)
if isdir(nfile):
walk(nfile, callback)
else:
callback(nfile)
def print_line(line=None):
if supports_ansi():
# Erase the line.
print("\033[2K", end="")
# Move the cursor to the beginning.
print("\r", end="")
else:
print()
if line:
print(line, end="")
sys.stdout.flush()
def run_script(path):
if "benchmark" in path:
return
global passed
global failed
if splitext(path)[1] != ".rn":
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("\\", "/")
# 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 not test.failures:
passed += 1
print_line(f"{green('PASS')}: {path}")
else:
failed += 1
print_line(f"{red('FAIL')}: {path}\n")
for failure in test.failures:
print(f" {pink(failure)}")
def run_suite():
global passed
global failed
global expectations
passed = 0
failed = 0
expectations = 0
walk(join(REPO_DIR, "test"), run_script)
print_line()
summary = (
f"All {green(passed)} tests passed ({expectations} expectations)."
if failed == 0
else f"{green(passed)} tests passed. {red(failed)} tests failed."
)
print(summary)
return failed == 0
def main(argv):
global filter_path
if len(argv) < 1 or len(argv) > 2:
print("Usage: test.py [filter]")
sys.exit(1)
if len(argv) == 2:
filter_path = argv[1]
if not run_suite():
sys.exit(1)
if __name__ == "__main__":
main(sys.argv)