-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnnc_gen_test.py
148 lines (128 loc) · 4.47 KB
/
nnc_gen_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
import os
import sys
import subprocess
from typing import List
class nnc_test_case(object):
def __init__(self, src_path: str) -> None:
self.__cwd: str = os.getcwd()
self.__source: str = src_path
self.__init_rest()
def __init_rest(self) -> None:
self.pure: str = self.__source.split('.')[0]
self.__argv: str = f'{self.pure}.argv'
self.__stdout: str = f'{self.pure}.stdout'
self.__retcode: str = f'{self.pure}.retcode'
self.__chk_permissions()
def __read_file(self, file: str) -> str:
import io
if not os.path.exists(file):
return ''
fd: io.TextIOWrapper = open(file, 'r')
c: str = fd.read()
fd.close()
return c
def __get_argv(self) -> List[str]:
contents: str = self.__read_file(self.__argv)
assert contents != None
if contents.strip() == '':
return []
return contents.split(' ')
def __get_retcode(self) -> int:
contents: str = self.__read_file(self.__retcode)
assert contents != None
try:
return int(contents)
except:
return 0
def __get_stdout(self) -> str:
contents: str = self.__read_file(self.__stdout)
assert contents != None
return contents
def __lt__(self, other) -> bool:
return self.pure < other.pure
def __str__(self) -> str:
return f'{self.pure}'
def __chk_permissions(self) -> None:
chk_list: List[str] = [
f'{self.__cwd}/compile.sh',
f'{self.__cwd}/cleanup.sh',
f'{self.__cwd}/prepare.sh',
]
for f in chk_list:
if not os.access(f, os.X_OK):
os.chmod(f, 0o777)
def __cleanup(self) -> None:
subprocess.run([f'./cleanup.sh'])
def __compile(self) -> str:
p: subprocess.Popen = subprocess.Popen(
args=[f'./compile.sh', f'{self.__source}'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
err: str = p.communicate()[1]
ret: int = p.wait()
if ret != 0:
return '\ncannot compile, see error:' + f'{err}\n'
return ''
def __run_compiled(self) -> str:
p: subprocess.Popen = subprocess.Popen(
args=[f'./compiled'] + self.__get_argv(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
report: str = ''
out, err = p.communicate()
ret: int = p.wait()
e_out: str = self.__get_stdout()
e_ret: int = self.__get_retcode()
if e_ret != ret:
report += f'\n\tbad retcode: expected `{e_ret}`, got `{ret}`\n'
if e_out != out:
report += f'\n\tbad stdout: expected `{e_out}`, got `{out}`\n'
return report
def run(self) -> str:
self.__cleanup()
report: str = ''
report: str = self.__compile()
if report != '':
return report
report = self.__run_compiled()
if report != '':
return report
return 'ok'
class nnc_tester_stat(object):
def __init__(self, whole: int) -> None:
self.whole: int = whole
self.failed: int = 0
self.succeeded: int = 0
class nnc_tester(object):
def __init__(self, test_path: str) -> None:
if not os.path.exists(test_path):
raise Exception(f'{test_path} does not exists.')
self.__path: str = test_path
def __prepare(self) -> None:
subprocess.run([f'./prepare.sh'])
def discover(self) -> List[nnc_test_case]:
tests: List[nnc_test_case] = []
print(f'discovering tests at [{os.path.abspath(self.__path)}]', end='')
for f in os.listdir(self.__path):
if f.endswith('.source'):
tests.append(nnc_test_case(os.path.join(self.__path, f)))
tests.sort()
print(f', found={len(tests)}')
return tests
def execute(self, tests: List[nnc_test_case] = None) -> nnc_tester_stat:
if tests == None:
tests = self.discover()
self.__prepare()
stat: nnc_tester_stat = nnc_tester_stat(len(tests))
for i, test in zip(range(len(tests)), tests):
print(f'{i+1}) {test}: {test.run()}')
return stat
if __name__ == '__main__':
if len(sys.argv) != 2:
print(f'usage: python {os.path.basename(__file__)} [path_to_test_folder]')
else:
nnc_tester(sys.argv[1]).execute()