-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_script.py
executable file
·112 lines (82 loc) · 3.39 KB
/
test_script.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
##############################################################################################################################
# #
# Usage: python3 test_script.py #
# Requirements: 1) Adjust path in "test_dir" to reflect the path of your test directory, relative to the script's directory. #
# 2) Adjust path in "custom_exec_name" to reflect the path of your custom interpreter executable, relative to #
# the directory where your tests exist. #
# 3) [Optionally] Adjust the value of "max_timeout" to reflect the max time that the custom interpeter or ghc #
# are allowed to run before being interrupted. #
##############################################################################################################################
import glob
import subprocess
import time
test_dir = './tests'
custom_exec_name = './src/TestMain'
max_timeout = 20
OKGREEN = '\033[92m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def main():
failed_tests = []
n_failed_tests = 0
start = time.time()
print('> Begin testing ', end="", flush=True)
for testfile in glob.glob(f'{test_dir}/*'):
success = test_custom_vs_ghc(testfile)
if not success:
failed_tests.append(testfile)
n_failed_tests += 1
print(FAIL + "F" + ENDC, end="", flush=True)
else:
print(OKGREEN + "." + ENDC, end="", flush=True)
end = time.time()
print('\n> Finished testing...')
print('> Number of failed tests:', n_failed_tests)
if n_failed_tests:
print('> Failed tests:')
for test in failed_tests:
print('> ', test)
print('> Total elapsed time:', end - start)
print('> Bye!')
def check_with_custom(testfile):
exec_name = custom_exec_name
try:
res = subprocess.check_output(
f'{exec_name} < {testfile}; exit 0',
stderr=subprocess.STDOUT,
timeout=max_timeout,
shell=True
)
except:
res = b'error'
return res.decode().strip()
def check_with_ghc(testfile):
tmpname = testfile + '_tmp'
rm_cmd = f'rm -f ./{tmpname} ./{tmpname}.hi ./{tmpname}.o ./{tmpname}.hs'
try:
res = subprocess.check_output(
f'cp {testfile} {tmpname}.hs && printf "\n\nmain = print result\n\n" >> {tmpname}.hs \
&& ghc {tmpname}.hs && ./{tmpname} && {rm_cmd}; exit 0',
stderr=subprocess.STDOUT,
timeout=max_timeout,
shell=True
)
except:
subprocess.check_output(rm_cmd, shell=True) # Cleanup leftovers
res = b'error'
res = res.decode().strip()
if 'error' in res.lower():
res = 'error'
else:
res = res.split('\n')[-1].strip()
return res
def test_custom_vs_ghc(testfile):
ghc_res = check_with_ghc(testfile)
cus_res = check_with_custom(testfile)
if ghc_res == 'error' or cus_res == 'error':
return False
if '.' in ghc_res:
ghc_res = ghc_res.split('.')[0]
return ghc_res == cus_res
if __name__ == '__main__':
main()