The Meson Build System
http://mesonbuild.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
8.2 KiB
234 lines
8.2 KiB
12 years ago
|
#!/usr/bin/env python3
|
||
12 years ago
|
|
||
9 years ago
|
# Copyright 2013-2016 The Meson development team
|
||
12 years ago
|
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
9 years ago
|
import mesonbuild
|
||
11 years ago
|
import sys, os, subprocess, time, datetime, pickle, multiprocessing, json
|
||
11 years ago
|
import concurrent.futures as conc
|
||
10 years ago
|
import argparse
|
||
9 years ago
|
import platform
|
||
|
|
||
|
def is_windows():
|
||
|
platname = platform.system().lower()
|
||
|
return platname == 'windows' or 'mingw' in platname
|
||
12 years ago
|
|
||
9 years ago
|
tests_failed = []
|
||
11 years ago
|
|
||
10 years ago
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument('--wrapper', default=None, dest='wrapper',
|
||
10 years ago
|
help='wrapper to run tests with (e.g. valgrind)')
|
||
10 years ago
|
parser.add_argument('--wd', default=None, dest='wd',
|
||
10 years ago
|
help='directory to cd into before running')
|
||
9 years ago
|
parser.add_argument('--suite', default=None, dest='suite',
|
||
|
help='Only run tests belonging to this suite.')
|
||
10 years ago
|
parser.add_argument('args', nargs='+')
|
||
|
|
||
|
|
||
11 years ago
|
class TestRun():
|
||
10 years ago
|
def __init__(self, res, returncode, duration, stdo, stde, cmd):
|
||
11 years ago
|
self.res = res
|
||
11 years ago
|
self.returncode = returncode
|
||
11 years ago
|
self.duration = duration
|
||
|
self.stdo = stdo
|
||
|
self.stde = stde
|
||
10 years ago
|
self.cmd = cmd
|
||
11 years ago
|
|
||
9 years ago
|
def decode(stream):
|
||
|
try:
|
||
|
return stream.decode('utf-8')
|
||
|
except UnicodeDecodeError:
|
||
|
return stream.decode('iso-8859-1', errors='ignore')
|
||
|
|
||
10 years ago
|
def write_log(logfile, test_name, result_str, result):
|
||
12 years ago
|
logfile.write(result_str + '\n\n')
|
||
10 years ago
|
logfile.write('--- command ---\n')
|
||
10 years ago
|
if result.cmd is None:
|
||
|
logfile.write('NONE')
|
||
|
else:
|
||
|
logfile.write(' '.join(result.cmd))
|
||
10 years ago
|
logfile.write('\n--- "%s" stdout ---\n' % test_name)
|
||
|
logfile.write(result.stdo)
|
||
12 years ago
|
logfile.write('\n--- "%s" stderr ---\n' % test_name)
|
||
10 years ago
|
logfile.write(result.stde)
|
||
12 years ago
|
logfile.write('\n-------\n\n')
|
||
12 years ago
|
|
||
11 years ago
|
def write_json_log(jsonlogfile, test_name, result):
|
||
|
result = {'name' : test_name,
|
||
|
'stdout' : result.stdo,
|
||
|
'stderr' : result.stde,
|
||
|
'result' : result.res,
|
||
|
'duration' : result.duration,
|
||
10 years ago
|
'returncode' : result.returncode,
|
||
|
'command' : result.cmd}
|
||
11 years ago
|
jsonlogfile.write(json.dumps(result) + '\n')
|
||
11 years ago
|
|
||
10 years ago
|
def run_with_mono(fname):
|
||
9 years ago
|
if fname.endswith('.exe') and not is_windows():
|
||
10 years ago
|
return True
|
||
|
return False
|
||
|
|
||
11 years ago
|
def run_single_test(wrap, test):
|
||
11 years ago
|
global tests_failed
|
||
10 years ago
|
if test.fname[0].endswith('.jar'):
|
||
|
cmd = ['java', '-jar'] + test.fname
|
||
10 years ago
|
elif not test.is_cross and run_with_mono(test.fname[0]):
|
||
10 years ago
|
cmd = ['mono'] + test.fname
|
||
11 years ago
|
else:
|
||
11 years ago
|
if test.is_cross:
|
||
|
if test.exe_runner is None:
|
||
10 years ago
|
# Can not run test on cross compiled executable
|
||
11 years ago
|
# because there is no execute wrapper.
|
||
|
cmd = None
|
||
|
else:
|
||
10 years ago
|
cmd = [test.exe_runner] + test.fname
|
||
11 years ago
|
else:
|
||
10 years ago
|
cmd = test.fname
|
||
10 years ago
|
if len(wrap) > 0 and 'valgrind' in wrap[0]:
|
||
|
wrap += test.valgrind_args
|
||
11 years ago
|
if cmd is None:
|
||
|
res = 'SKIP'
|
||
|
duration = 0.0
|
||
|
stdo = 'Not run because can not execute cross compiled binaries.'
|
||
|
stde = ''
|
||
11 years ago
|
returncode = -1
|
||
11 years ago
|
else:
|
||
11 years ago
|
cmd = wrap + cmd + test.cmd_args
|
||
11 years ago
|
starttime = time.time()
|
||
11 years ago
|
child_env = os.environ.copy()
|
||
|
child_env.update(test.env)
|
||
9 years ago
|
if len(test.extra_paths) > 0:
|
||
|
child_env['PATH'] = child_env['PATH'] + ';'.join([''] + test.extra_paths)
|
||
11 years ago
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||
9 years ago
|
env=child_env, cwd=test.workdir)
|
||
9 years ago
|
timed_out = False
|
||
|
try:
|
||
|
(stdo, stde) = p.communicate(timeout=test.timeout)
|
||
|
except subprocess.TimeoutExpired:
|
||
|
timed_out = True
|
||
|
p.kill()
|
||
|
(stdo, stde) = p.communicate()
|
||
11 years ago
|
endtime = time.time()
|
||
|
duration = endtime - starttime
|
||
9 years ago
|
stdo = decode(stdo)
|
||
|
stde = decode(stde)
|
||
9 years ago
|
if timed_out:
|
||
|
res = 'TIMEOUT'
|
||
9 years ago
|
tests_failed.append((test.name, stdo, stde))
|
||
9 years ago
|
elif (not test.should_fail and p.returncode == 0) or \
|
||
10 years ago
|
(test.should_fail and p.returncode != 0):
|
||
11 years ago
|
res = 'OK'
|
||
|
else:
|
||
|
res = 'FAIL'
|
||
9 years ago
|
tests_failed.append((test.name, stdo, stde))
|
||
11 years ago
|
returncode = p.returncode
|
||
10 years ago
|
return TestRun(res, returncode, duration, stdo, stde, cmd)
|
||
11 years ago
|
|
||
11 years ago
|
def print_stats(numlen, tests, name, result, i, logfile, jsonlogfile):
|
||
11 years ago
|
startpad = ' '*(numlen - len('%d' % (i+1)))
|
||
|
num = '%s%d/%d' % (startpad, i+1, len(tests))
|
||
10 years ago
|
padding1 = ' '*(38-len(name))
|
||
9 years ago
|
padding2 = ' '*(8-len(result.res))
|
||
9 years ago
|
result_str = '%s %s %s%s%s%5.2f s' % \
|
||
11 years ago
|
(num, name, padding1, result.res, padding2, result.duration)
|
||
|
print(result_str)
|
||
10 years ago
|
write_log(logfile, name, result_str, result)
|
||
11 years ago
|
write_json_log(jsonlogfile, name, result)
|
||
11 years ago
|
|
||
11 years ago
|
def drain_futures(futures):
|
||
|
for i in futures:
|
||
11 years ago
|
(result, numlen, tests, name, i, logfile, jsonlogfile) = i
|
||
|
print_stats(numlen, tests, name, result.result(), i, logfile, jsonlogfile)
|
||
11 years ago
|
|
||
9 years ago
|
def filter_tests(suite, tests):
|
||
|
if suite is None:
|
||
|
return tests
|
||
9 years ago
|
return [x for x in tests if suite in x.suite]
|
||
9 years ago
|
|
||
12 years ago
|
def run_tests(options, datafilename):
|
||
12 years ago
|
logfile_base = 'meson-logs/testlog'
|
||
12 years ago
|
if options.wrapper is None:
|
||
|
wrap = []
|
||
12 years ago
|
logfilename = logfile_base + '.txt'
|
||
11 years ago
|
jsonlogfilename = logfile_base+ '.json'
|
||
12 years ago
|
else:
|
||
|
wrap = [options.wrapper]
|
||
12 years ago
|
logfilename = logfile_base + '-' + options.wrapper.replace(' ', '_') + '.txt'
|
||
11 years ago
|
jsonlogfilename = logfile_base + '-' + options.wrapper.replace(' ', '_') + '.json'
|
||
12 years ago
|
logfile = open(logfilename, 'w')
|
||
11 years ago
|
jsonlogfile = open(jsonlogfilename, 'w')
|
||
12 years ago
|
logfile.write('Log of Meson test suite run on %s.\n\n' % datetime.datetime.now().isoformat())
|
||
11 years ago
|
tests = pickle.load(open(datafilename, 'rb'))
|
||
10 years ago
|
if len(tests) == 0:
|
||
|
print('No tests defined.')
|
||
|
return
|
||
11 years ago
|
numlen = len('%d' % len(tests))
|
||
11 years ago
|
varname = 'MESON_TESTTHREADS'
|
||
|
if varname in os.environ:
|
||
|
try:
|
||
|
num_workers = int(os.environ[varname])
|
||
|
except ValueError:
|
||
10 years ago
|
print('Invalid value in %s, using 1 thread.' % varname)
|
||
11 years ago
|
num_workers = 1
|
||
11 years ago
|
else:
|
||
|
num_workers = multiprocessing.cpu_count()
|
||
11 years ago
|
executor = conc.ThreadPoolExecutor(max_workers=num_workers)
|
||
|
futures = []
|
||
9 years ago
|
filtered_tests = filter_tests(options.suite, tests)
|
||
|
for i, test in enumerate(filtered_tests):
|
||
9 years ago
|
if test.suite[0] == '':
|
||
9 years ago
|
visible_name = test.name
|
||
|
else:
|
||
9 years ago
|
if options.suite is not None:
|
||
|
visible_name = options.suite + ' / ' + test.name
|
||
|
else:
|
||
|
visible_name = test.suite[0] + ' / ' + test.name
|
||
|
|
||
11 years ago
|
if not test.is_parallel:
|
||
11 years ago
|
drain_futures(futures)
|
||
|
futures = []
|
||
11 years ago
|
res = run_single_test(wrap, test)
|
||
9 years ago
|
print_stats(numlen, filtered_tests, visible_name, res, i, logfile, jsonlogfile)
|
||
11 years ago
|
else:
|
||
11 years ago
|
f = executor.submit(run_single_test, wrap, test)
|
||
9 years ago
|
futures.append((f, numlen, filtered_tests, visible_name, i, logfile, jsonlogfile))
|
||
11 years ago
|
drain_futures(futures)
|
||
9 years ago
|
return logfilename
|
||
12 years ago
|
|
||
10 years ago
|
def run(args):
|
||
10 years ago
|
global tests_failed
|
||
10 years ago
|
options = parser.parse_args(args)
|
||
10 years ago
|
if len(options.args) != 1:
|
||
12 years ago
|
print('Test runner for Meson. Do not run on your own, mmm\'kay?')
|
||
12 years ago
|
print('%s [data file]' % sys.argv[0])
|
||
11 years ago
|
if options.wd is not None:
|
||
|
os.chdir(options.wd)
|
||
10 years ago
|
datafile = options.args[0]
|
||
9 years ago
|
logfilename = run_tests(options, datafile)
|
||
|
returncode = 0
|
||
|
if len(tests_failed) > 0:
|
||
|
print('\nOutput of failed tests (max 10):')
|
||
|
for (name, stdo, stde) in tests_failed[:10]:
|
||
|
print("{} stdout:\n".format(name))
|
||
|
print(stdo)
|
||
|
print('\n{} stderr:\n'.format(name))
|
||
|
print(stde)
|
||
|
print('\n')
|
||
|
returncode = 1
|
||
|
print('\nFull log written to %s.' % logfilename)
|
||
|
return returncode
|
||
11 years ago
|
|
||
10 years ago
|
if __name__ == '__main__':
|
||
|
sys.exit(run(sys.argv[1:]))
|