-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·185 lines (153 loc) · 5.36 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
#!/usr/bin/python3
import os
import subprocess
import time
from sys import stdout
cargo_test = ['cargo', 'test']
build = ['cargo', 'build']
eye_path = './target/debug/eye'
run_cmd = [eye_path, 'run']
tmp_file = 'tmp_test.eye'
GREEN = '\033[1;32m'
RED = '\033[1;31m'
CYAN = '\033[1;36m'
R = '\033[1;0m'
def main():
if subprocess.run(cargo_test).returncode != 0:
print(f'{RED}Test(s) failed{R}')
exit(1)
start_time = time.time()
total, errors = test_files()
total2, errors2 = test_readme()
total += total2
errors += errors2
elapsed = time.time() - start_time
if errors > 0:
print(f'{RED}{errors}/{total} tests failed!{R} in {elapsed:.2f}s')
exit(1)
else:
print(f'{GREEN}All {total} tests passed!{R} in {elapsed:.2f}s')
def test_files():
print(f'{CYAN}Compiling...{R}')
if subprocess.run(build).returncode != 0:
print(f'{RED}An error occurred while compiling{R}')
exit(1)
print(f'{CYAN}Running tests ...{R}')
total = 1
errors = 0
if not check_std(): errors += 1
tmp_total, tmp_errors = walk('eye')
total += tmp_total
errors += tmp_errors
return total, errors
def test_readme():
print(f'{CYAN}Running README tests ...{R}')
file = open('README.md', encoding="utf8")
# contents = file.read()
current_source = ''
in_code_block = False
block_count = 0
errors = 0
for line in file:
if line.find('```') != -1:
if in_code_block:
temp_code_file = open(tmp_file, 'w', encoding="utf8")
temp_code_file.write(current_source)
temp_code_file.close()
print(f'Testing README block #{block_count} ...', end = '')
out, stderr, exit_code = run(tmp_file)
padding = ' ' * max(0, 28 - len(f'{block_count}'))
if exit_code != 0:
print(f'{padding}{RED}[ERR]{R}')
print(f'{RED}Output{R}:\n{out}')
print(f'{RED}Stderr{R}:\n{stderr}')
errors += 1
else:
print(f'{padding}{GREEN}[OK]{R}')
current_source = ''
else: block_count += 1
in_code_block = not in_code_block
elif in_code_block:
current_source += line
os.remove(tmp_file)
return block_count, errors
def walk(walkdir):
errors = 0
total = 0
with os.scandir(walkdir) as it:
for entry in it:
if entry.name.endswith('.eye') and entry.is_file():
res = test(entry.path)
if res is OK:
total += 1
elif res is ERR:
total += 1
errors += 1
else: assert res is SKIP
elif entry.is_dir():
if os.path.exists(entry.path + "/main.eye"):
res = test(entry.path)
if res is OK:
total += 1
elif res is ERR:
total += 1
errors += 1
else: assert res is SKIP
else:
total2, errors2 = walk(entry.path)
total += total2
errors += errors2
return total, errors
OK = "OK"
SKIP = "SKIP"
ERR = "ERR"
def test(eye_file) -> str:
print(eye_file, '...', end='')
stdout.flush()
no_ext = eye_file.rsplit('.', maxsplit=1)[0]
if os.path.exists(no_ext + '.out'):
expected = no_ext + '.out'
elif os.path.exists(no_ext + '.err'):
expected = no_ext + '.err'
else:
t = 'No .out or .err file'
padding = ' ' * max(0, 50 - len(eye_file) - len(t))
print(f'{t}{padding}{CYAN}[SKIP]{R}')
return SKIP
with open(expected, encoding="utf8") as file:
expected_output = file.read()
input = None
if os.path.exists(no_ext + '.in'):
with open(no_ext + '.in', encoding="utf8") as file:
input = memoryview(bytes(file.read(), 'utf-8'))
out, stderr, exit_code = run(eye_file, input)
padding = ' ' * max(0, 50 - len(eye_file))
out = out.replace('\r\n', '\n')
expected_output = expected_output.replace('\r\n', '\n')
if expected_output == out:
print(f'{padding}{GREEN}[OK]{R}')
return OK
else:
print(f'{padding}{RED}[ERR]{R}\nFailed with status: {exit_code}, Output:')
print(f'--------------------\n{out}\n--------------------')
print('... but expected:')
print(f'--------------------\n{expected_output}\n--------------------')
print(f'Stderr:\n--------------------\n{stderr}\n--------------------')
return ERR
def run(eye_file, input = ''):
res = subprocess.run(run_cmd + [eye_file], capture_output=True, input=input)
return res.stdout.decode('utf-8', 'replace'), res.stderr.decode('utf-8', 'replace'), res.returncode
def check_std():
text = 'std library ...'
pad = ' ' * (54 - len(text))
print(f'{text}{pad}', end='')
res = subprocess.run([eye_path, 'check', 'std', '--lib'], capture_output=True)
if res.returncode == 0:
print(f'{GREEN}[OK]{R}')
return True
else:
print(f'{RED}[ERR]{R}')
print(res.stdout.decode('utf-8', 'replace'))
return False
if __name__ == '__main__':
main()