2017-04-24 19:49:00 +02:00
|
|
|
# Copyright 2009 The JsonCpp Authors
|
2015-04-10 03:01:33 +02:00
|
|
|
# Distributed under MIT license, or public domain if desired and
|
|
|
|
# recognized in your jurisdiction.
|
|
|
|
# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
|
|
|
|
2014-11-20 06:30:47 +01:00
|
|
|
from __future__ import print_function
|
2015-01-16 21:48:06 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from io import open
|
2014-11-20 06:30:47 +01:00
|
|
|
from glob import glob
|
2011-06-21 23:18:49 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import subprocess
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes'
|
|
|
|
|
|
|
|
class TestProxy(object):
|
2015-01-24 22:29:52 +01:00
|
|
|
def __init__(self, test_exe_path, use_valgrind=False):
|
|
|
|
self.test_exe_path = os.path.normpath(os.path.abspath(test_exe_path))
|
2011-06-21 23:18:49 +02:00
|
|
|
self.use_valgrind = use_valgrind
|
|
|
|
|
2015-01-24 22:29:52 +01:00
|
|
|
def run(self, options):
|
2011-06-21 23:18:49 +02:00
|
|
|
if self.use_valgrind:
|
|
|
|
cmd = VALGRIND_CMD.split()
|
|
|
|
else:
|
|
|
|
cmd = []
|
2015-01-24 22:29:52 +01:00
|
|
|
cmd.extend([self.test_exe_path, '--test-auto'] + options)
|
2015-01-16 21:48:06 +01:00
|
|
|
try:
|
2015-01-24 22:29:52 +01:00
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2015-01-16 21:48:06 +01:00
|
|
|
except:
|
|
|
|
print(cmd)
|
|
|
|
raise
|
2011-06-21 23:18:49 +02:00
|
|
|
stdout = process.communicate()[0]
|
|
|
|
if process.returncode:
|
|
|
|
return False, stdout
|
|
|
|
return True, stdout
|
|
|
|
|
2015-01-24 22:29:52 +01:00
|
|
|
def runAllTests(exe_path, use_valgrind=False):
|
|
|
|
test_proxy = TestProxy(exe_path, use_valgrind=use_valgrind)
|
|
|
|
status, test_names = test_proxy.run(['--list-tests'])
|
2011-06-21 23:18:49 +02:00
|
|
|
if not status:
|
2014-11-20 06:30:47 +01:00
|
|
|
print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr)
|
2011-06-21 23:18:49 +02:00
|
|
|
return 1
|
2015-01-11 10:39:24 +01:00
|
|
|
test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')]
|
2011-06-21 23:18:49 +02:00
|
|
|
failures = []
|
|
|
|
for name in test_names:
|
2014-11-20 06:30:47 +01:00
|
|
|
print('TESTING %s:' % name, end=' ')
|
2015-01-24 22:29:52 +01:00
|
|
|
succeed, result = test_proxy.run(['--test', name])
|
2011-06-21 23:18:49 +02:00
|
|
|
if succeed:
|
2014-11-20 06:30:47 +01:00
|
|
|
print('OK')
|
2011-06-21 23:18:49 +02:00
|
|
|
else:
|
2015-01-24 22:29:52 +01:00
|
|
|
failures.append((name, result))
|
2014-11-20 06:30:47 +01:00
|
|
|
print('FAILED')
|
2011-06-21 23:18:49 +02:00
|
|
|
failed_count = len(failures)
|
|
|
|
pass_count = len(test_names) - failed_count
|
|
|
|
if failed_count:
|
2014-11-20 06:30:47 +01:00
|
|
|
print()
|
2011-06-21 23:18:49 +02:00
|
|
|
for name, result in failures:
|
2014-11-20 06:30:47 +01:00
|
|
|
print(result)
|
2015-01-24 22:29:52 +01:00
|
|
|
print('%d/%d tests passed (%d failure(s))' % ( pass_count, len(test_names), failed_count))
|
2011-06-21 23:18:49 +02:00
|
|
|
return 1
|
|
|
|
else:
|
2014-11-20 06:30:47 +01:00
|
|
|
print('All %d tests passed' % len(test_names))
|
2011-06-21 23:18:49 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
def main():
|
|
|
|
from optparse import OptionParser
|
2015-01-24 22:29:52 +01:00
|
|
|
parser = OptionParser(usage="%prog [options] <path to test_lib_json.exe>")
|
2011-06-21 23:18:49 +02:00
|
|
|
parser.add_option("--valgrind",
|
|
|
|
action="store_true", dest="valgrind", default=False,
|
|
|
|
help="run all the tests using valgrind to detect memory leaks")
|
|
|
|
parser.enable_interspersed_args()
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) != 1:
|
2015-01-24 22:29:52 +01:00
|
|
|
parser.error('Must provides at least path to test_lib_json executable.')
|
|
|
|
sys.exit(1)
|
2011-06-21 23:18:49 +02:00
|
|
|
|
2015-01-24 22:29:52 +01:00
|
|
|
exit_code = runAllTests(args[0], use_valgrind=options.valgrind)
|
|
|
|
sys.exit(exit_code)
|
2011-06-21 23:18:49 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|