-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·103 lines (80 loc) · 2.81 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
#!/usr/bin/env python3
import os
import re
import sys
import subprocess
import multiprocessing
import functools
import platform
def get_name():
if platform.system() == 'Linux': return 'diamond'
if platform.system() == 'Darwin': return 'diamond'
elif platform.system() == 'Windows': return 'diamond' + '.exe'
else: assert False
def get_command():
if platform.system() == 'Linux': return './' + get_name()
if platform.system() == 'Darwin': return './' + get_name()
elif platform.system() == 'Windows': return '.\\' + get_name()
else: assert False
# Helper functions
# ----------------
def get_max_path_len(files):
max_len = 0
for file in files:
if len(file) > max_len:
max_len = len(file)
return max_len
def get_all_files(folder):
files = []
for file in os.listdir(folder):
path = os.path.join(folder, file)
if os.path.isdir(path):
files += get_all_files(path)
else:
files.append(path)
return files
def test(file, expected, max_file_path_len):
# Run program and check output
result = subprocess.run([get_command(), 'run', file], stdout=subprocess.PIPE, text=True, encoding=os.device_encoding(1))
result = result.stdout
result = re.sub("\\x1b\\[.+?m", "", result) # Remove escape sequences for colored text
result = result == expected
# Print result
status = '\u001b[32mOK\u001b[0m' if result == True else '\u001b[31mFailed\u001b[0m'
spacing = " " * (max_file_path_len - len(file) + 1)
print(f"{file}{spacing}{status}", flush=True)
# Return result
return result
def read_file_and_test(file, max_file_path_len):
with open(file, encoding=os.device_encoding(1)) as content:
content = content.read()
try:
expected = re.search("(?<=--- Output\n)(.|\n)*(?=---)", content).group(0)
return test(file, expected, max_file_path_len)
except:
return True
# Main
# ----
def main():
folder = 'test'
if len(sys.argv) > 2:
print("Too many arguments :/")
return sys.exit(1)
if not os.path.exists(get_name()):
print("diamond not found :(")
return sys.exit(1)
if len(sys.argv) > 1:
folder = sys.argv[1]
if os.path.isdir(folder):
file_paths = get_all_files(folder)
max_file_path_len = get_max_path_len(file_paths)
num_cores = multiprocessing.cpu_count()
with multiprocessing.Pool(num_cores) as pool:
results = pool.map(functools.partial(read_file_and_test, max_file_path_len=max_file_path_len), file_paths)
for result in results:
if result == False:
sys.exit(1)
else:
read_file_and_test(folder, get_max_path_len([folder]))
if __name__ == "__main__":
main()