-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·91 lines (79 loc) · 2.92 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
#!/usr/bin/env python3
import sys
import os
import subprocess
import shlex
def cmd_run_echoed(cmd, **kwargs):
print("[CMD] %s" % " ".join(map(shlex.quote, cmd)))
cmd = subprocess.run(cmd, **kwargs)
if cmd.returncode != 0:
print(cmd.stdout.decode('utf-8'), file=sys.stdout)
print(cmd.stderr.decode('utf-8'), file=sys.stderr)
exit(cmd.returncode)
return cmd
def test(folder):
sim_failed = 0
com_failed = 0
for entry in os.scandir(folder):
porth_ext = '.porth'
if entry.is_file() and entry.path.endswith(porth_ext):
print('[INFO] Testing %s' % entry.path)
txt_path = entry.path[:-len(porth_ext)] + ".txt"
expected_output = None
with open(txt_path, "rb") as f:
expected_output = f.read()
sim_output = cmd_run_echoed(["./porth", "sim", entry.path], capture_output=True).stdout
if sim_output != expected_output:
sim_failed += 1
print("[ERROR] Unexpected simulation output")
print(" Expected:")
print(" %s" % expected_output)
print(" Actual:")
print(" %s" % sim_output)
# exit(1)
print()
print("Simulation failed: %d" % sim_failed)
if sim_failed != 0:
exit(1)
def record(folder):
for entry in os.scandir(folder):
porth_ext = '.porth'
if entry.is_file() and entry.path.endswith(porth_ext):
sim_output = cmd_run_echoed(["./porth", "sim", entry.path], capture_output=True).stdout
# TODO: skip the test if .txt file does not exist
txt_path = entry.path[:-len(porth_ext)] + ".txt"
print("[INFO] Saving output to %s" % txt_path)
with open(txt_path, "wb") as txt_file:
txt_file.write(sim_output)
def usage(exe_name):
print("Usage: ./test.py [OPTIONS] [SUBCOMMAND]")
print("OPTIONS:")
print(" -f <folder> Folder with the tests. (Default: ./tests/)")
print("SUBCOMMANDS:")
print(" test Run the tests. (Default when no subcommand is provided)")
print(" record Record expected output of the tests.")
print(" help Print this message to stdout and exit with 0 code.")
if __name__ == '__main__':
exe_name, *argv = sys.argv
folder = "./tests/"
subcmd = "test"
while len(argv) > 0:
arg, *argv = argv
if arg == '-f':
if len(argv) == 0:
print("[ERROR] no <folder> is provided for option `-f`")
exit(1)
folder, *argv = argv
else:
subcmd = arg
break
if subcmd == 'record':
record(folder)
elif subcmd == 'test':
test(folder)
elif subcmd == 'help':
usage(exe_name)
else:
usage(exe_name)
print("[ERROR] unknown subcommand `%s`" % subcmd, file=sys.stderr)
exit(1);