-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_single_tests.py
191 lines (154 loc) · 4.89 KB
/
run_single_tests.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
186
187
188
189
190
191
import ast
import json
import pathlib
import queue
import re
import subprocess
import sys
import threading
import time
OMP_COMMAND = (
'./omp-server'
if sys.platform == 'linux'
else 'omp-server.exe'
# OSX can fuck off, naturally
)
PAWNCC_COMMAND = (
'./pawncc'
if sys.platform == 'linux'
else 'pawncc.exe'
# Ditto
)
DEPENDENCIES = [
path
for path in pathlib.Path('dependencies').glob('*')
if path.is_dir()
]
class ProgramOutput:
def __init__(self):
self.stdout = queue.Queue()
self.stderr = queue.Queue()
def get_stream_as_string(self, stream_name):
stream = getattr(self, stream_name)
output = ''
while True:
try:
line = stream.get_nowait()
except queue.Empty:
break
output += line
return output
class Program:
def __init__(self, command, **popen_args):
self._process = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
**popen_args
)
def run(self, timeout=None):
start_time = time.time()
output = ProgramOutput()
with self._process as process:
watch_threads = []
for stream, stream_queue in (
(process.stdout, output.stdout),
(process.stderr, output.stderr),
):
thread = threading.Thread(
target=self._watch,
args=(stream, stream_queue),
)
watch_threads.append(thread)
thread.start()
while(
timeout is None
or time.time() - start_time < timeout
):
if process.poll() is not None:
break
time.sleep(.01)
else:
process.terminate()
process.communicate()
was_terminated = True
if(
process.returncode != 0
and not was_terminated
):
process_name = ' '.join(process.args)
raise RuntimeError(
f'Process "{process_name}" failed '
f'with return code {process.returncode}'
)
for thread in watch_threads:
thread.join()
return output
def _watch(self, stream, stream_queue):
try:
for line in iter(stream.readline, ''):
stream_queue.put(line)
except ValueError: # I/O operation on closed file
pass
def main():
if len(sys.argv) != 2:
print(f'Usage: {sys.argv[0]} single_tests_folder')
return 1
folder = pathlib.Path(sys.argv[1])
if not folder.exists():
print(f'Folder {folder} does not exist.')
return 1
for source_file in sorted(folder.glob('*.pwn')):
module = source_file.stem
meta_data_path = folder / f'{module}.py'
if not meta_data_path.exists():
print(f'No test metadata found for {source_file}, skipping...')
continue
amx_path = folder / f'{module}.amx'
amx_path.unlink(missing_ok=True)
pawncc = Program([
PAWNCC_COMMAND,
source_file.resolve(),
f'-o{amx_path.resolve()}',
'-Z+',
*[
f'-i{dependency.resolve()}'
for dependency in DEPENDENCIES
]
]).run()
if not amx_path.exists():
print(f'File {source_file} failed to build, aborting.')
return 1
expected_data = ast.literal_eval(meta_data_path.read_text())
print(f'Running tests for {module}...')
omp = Program([
OMP_COMMAND,
str(amx_path)
]).run(timeout=.5)
for program_name, program in {
'omp': omp,
'pawncc': pawncc,
}.items():
for stream_name in (
'stdout',
'stderr',
):
output = program.get_stream_as_string(stream_name).rstrip('\n')
expected_pattern = expected_data[
program_name
][stream_name].strip()
expected_output_lines = expected_pattern.count('\n') + 1
useful_lines = output.split('\n')[-expected_output_lines:]
useful_output = '\n'.join(useful_lines)
if not re.fullmatch(expected_pattern, useful_output):
print(
f'Tests for {module} failed '
f'(mismatch on {program_name} {stream_name}): '
+ repr(useful_output)
)
return 1
print(f'Tests for {module} succeeded.')
if __name__ == '__main__':
sys.exit(main())