-
Notifications
You must be signed in to change notification settings - Fork 53
/
test.py
executable file
·302 lines (239 loc) · 9.06 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
###########################################################################
#
# Copyright 2015-2018 Jose Fonseca
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sub license, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. NO EVENT SHALL
# THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OF OR CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS THE SOFTWARE.
#
# The above copyright notice and this permission notice (including the
# next paragraph) shall be included in all copies or substantial portions
# of the Software.
#
###########################################################################
import glob
import sys
import subprocess
import os.path
import re
import optparse
import tempfile
import threading
import multiprocessing.dummy as multiprocessing
from multiprocessing import cpu_count
assert sys.version_info.major >= 3
stdoutLock = threading.Lock()
def writeStdout(s):
with stdoutLock:
sys.stdout.write(s)
sys.stdout.flush()
def checkString(needle, haystack):
if needle.startswith('/'):
assert needle.endswith('/')
needle = needle[1:-1]
else:
needle = '^' + re.escape(needle) + '$'
needleRe = re.compile(needle, re.MULTILINE)
return needleRe.search(haystack) is not None
def haveAnsiEscapes():
if sys.platform != 'win32':
return True
if os.environ.get('APPVEYOR', 'False') == 'True':
# https://help.appveyor.com/discussions/suggestions/197-support-ansi-color-codes
return True;
import ctypes.wintypes
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
# Set output mode to handle virtual terminal sequences
# https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032.aspx
hConsoleHandle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
dwMode = ctypes.wintypes.DWORD()
if ctypes.windll.kernel32.GetConsoleMode(hConsoleHandle, ctypes.byref(dwMode)):
dwMode = ctypes.wintypes.DWORD(dwMode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
if ctypes.windll.kernel32.SetConsoleMode(hConsoleHandle, dwMode):
return True
return False
if haveAnsiEscapes():
_csi = '\33['
NORMAL = _csi + '0m'
RED = _csi + '31m'
GREEN = _csi + '32m'
else:
NORMAL = ''
RED = ''
GREEN = ''
def test(args):
catchsegvExe, testExe, testSrc = args
result = True
cmd = [
catchsegvExe,
'-v',
'-t', '30',
testExe
]
if sys.platform != 'win32':
cmd = ['wine'] + cmd
if options.verbose:
os.environ['WINEDEBUG'] = '+debugstr'
writeStdout('# ' + ' '.join(cmd) + '\n')
# XXX: Popen.communicate takes a lot of time with wine, so avoid it
stdout = tempfile.TemporaryFile()
stderr = tempfile.TemporaryFile()
# Isolate this python script from console events
creationflags = 0
if sys.platform == 'win32':
testName, _ = os.path.splitext(os.path.basename(testSrc))
if testName.startswith('ctrl_'):
creationflags |= subprocess.CREATE_NEW_CONSOLE
p = subprocess.Popen(cmd, stdout=stdout, stderr=stderr, creationflags=creationflags)
p.wait()
stdout.seek(0)
stderr.seek(0)
stdout = stdout.read()
stderr = stderr.read()
stdout = stdout.replace(b'\r\n', b'\n')
stderr = stderr.replace(b'\r\n', b'\n')
stdout = stdout.decode()
stderr = stderr.decode()
if options.verbose:
with stdoutLock:
sys.stderr.write(stderr)
sys.stderr.write(stdout)
# Adjust return code
exitCode = p.returncode
if exitCode < 0:
exitCode += (1 << 32)
if exitCode == 0x4000001f:
exitCode = 0x80000003
if exitCode == 125:
# skip
writeStdout('%sok - %s # skip%s\n' % (GREEN, testExe, NORMAL))
else:
# Search the source file for '// CHECK_...' annotations and process
# them.
checkCommentRe = re.compile(r'^// CHECK_([_0-9A-Z]+):\s+(.*)$')
for line in open(testSrc, 'rt'):
line = line.rstrip('\n')
mo = checkCommentRe.match(line)
if mo:
checkName = mo.group(1)
checkExpr = mo.group(2)
if checkName == 'EXIT_CODE':
inverse = False
if checkExpr.startswith('!'):
inverse = True
checkExpr = checkExpr[1:]
if checkExpr.startswith('0x'):
checkExitCode = int(checkExpr, 16)
else:
checkExitCode = int(checkExpr)
if sys.platform != 'win32':
checkExitCode = checkExitCode % 256
ok = exitCode == checkExitCode
if inverse:
ok = not ok
if not ok:
writeStdout('# exit code was 0x%08x\n' % exitCode)
elif checkName == 'STDOUT':
ok = checkString(checkExpr, stdout)
elif checkName == 'STDERR':
ok = checkString(checkExpr, stderr)
else:
assert False
ok_or_not = [RED + 'not ok', GREEN + 'ok']
checkExpr = mo.group(2)
writeStdout('%s - %s %s %s%s\n' % (ok_or_not[int(bool(ok))], testExe, 'CHECK_' + checkName, checkExpr, NORMAL))
if not ok:
result = False
if not result and not options.verbose:
sys.stderr.write(stderr)
sys.stderr.write(stdout)
return testExe, result
def main():
optparser = optparse.OptionParser( usage="%prog [options] [path/to/catchsegv.exe] [path/to/test/apps] ..")
optparser.add_option(
'-R', '--regex', metavar='REGEX',
type="string", dest="regex",
default = '.*')
optparser.add_option(
'-v', '--verbose',
action="store_true",
dest="verbose", default=False)
global options
(options, args) = optparser.parse_args(sys.argv[1:])
if len(args) >= 1:
catchsegvExe = args[0]
else:
catchsegvExe = os.path.join('bin', 'catchsegv.exe')
if not os.path.isfile(catchsegvExe):
optparser.error('error: %s does not exist\n' % catchsegvExe)
sys.exit(1)
if len(args) >= 2:
testsExeDirs = args[1:]
else:
testsExeDirs = [os.path.normpath(os.path.join(os.path.dirname(catchsegvExe), '..', 'tests', 'apps'))]
testsSrcDir = os.path.dirname(__file__)
testNameRe = re.compile(options.regex)
if sys.platform == 'win32':
import ctypes
SEM_FAILCRITICALERRORS = 0x0001
SEM_NOALIGNMENTFAULTEXCEPT = 0x0004
SEM_NOGPFAULTERRORBOX = 0x0002
SEM_NOOPENFILEERRORBOX = 0x8000
uMode = ctypes.windll.kernel32.SetErrorMode(0)
uMode |= SEM_FAILCRITICALERRORS \
| SEM_NOALIGNMENTFAULTEXCEPT \
| SEM_NOGPFAULTERRORBOX \
| SEM_NOOPENFILEERRORBOX
ctypes.windll.kernel32.SetErrorMode(uMode)
failedTests = []
numJobs = cpu_count()
pool = multiprocessing.Pool(numJobs)
testSrcFiles = os.listdir(testsSrcDir)
testSrcFiles.sort()
testArgs = []
for testSrcFile in testSrcFiles:
testName, ext = os.path.splitext(testSrcFile)
if ext not in ('.c', '.cpp'):
continue
if not testNameRe.search(testName):
continue
if sys.platform != 'win32' and testName in ('ctrl_break', 'ctrl_c'):
continue
testSrc = os.path.join(testsSrcDir, testSrcFile)
for testsExeDir in testsExeDirs:
testExe = os.path.join(testsExeDir, testName + '.exe')
if not os.path.isfile(testExe):
sys.stderr.write('fatal: %s does not exist\n' % testExe)
sys.exit(1)
testArgs.append((catchsegvExe, testExe, testSrc))
if numJobs <= 1 or len(testArgs) <= 1:
imap = map
else:
imap = pool.imap_unordered
for testName, testResult in imap(test, testArgs):
if not testResult:
failedTests.append(testName)
#sys.stdout.write('1..%u\n' % numTests)
if failedTests:
sys.stdout.write('# %u tests failed\n' % len(failedTests))
for failedTest in failedTests:
sys.stdout.write('# - %s\n' % failedTest)
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
main()