blob: 6634e72419678ca43fc8b0876d0a9c79777a7e50 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001# Copyright 2009 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-09 18:01:33 -07002# Distributed under MIT license, or public domain if desired and
3# recognized in your jurisdiction.
4# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Christopher Dunnbd1e8952014-11-19 23:30:47 -06006from __future__ import print_function
Christopher Dunn4bc31152015-01-16 14:48:06 -06007from __future__ import unicode_literals
8from io import open
Christopher Dunnbd1e8952014-11-19 23:30:47 -06009from glob import glob
Christopher Dunndc0f7362011-06-21 21:18:49 +000010import sys
11import os
12import os.path
13import subprocess
Christopher Dunndc0f7362011-06-21 21:18:49 +000014import optparse
15
16VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes'
17
18class TestProxy(object):
Christopher Dunn494950a2015-01-24 15:29:52 -060019 def __init__(self, test_exe_path, use_valgrind=False):
20 self.test_exe_path = os.path.normpath(os.path.abspath(test_exe_path))
Christopher Dunndc0f7362011-06-21 21:18:49 +000021 self.use_valgrind = use_valgrind
22
Christopher Dunn494950a2015-01-24 15:29:52 -060023 def run(self, options):
Christopher Dunndc0f7362011-06-21 21:18:49 +000024 if self.use_valgrind:
25 cmd = VALGRIND_CMD.split()
26 else:
27 cmd = []
Christopher Dunn494950a2015-01-24 15:29:52 -060028 cmd.extend([self.test_exe_path, '--test-auto'] + options)
Christopher Dunn4bc31152015-01-16 14:48:06 -060029 try:
Christopher Dunn494950a2015-01-24 15:29:52 -060030 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Christopher Dunn4bc31152015-01-16 14:48:06 -060031 except:
32 print(cmd)
33 raise
Christopher Dunndc0f7362011-06-21 21:18:49 +000034 stdout = process.communicate()[0]
35 if process.returncode:
36 return False, stdout
37 return True, stdout
38
Christopher Dunn494950a2015-01-24 15:29:52 -060039def runAllTests(exe_path, use_valgrind=False):
40 test_proxy = TestProxy(exe_path, use_valgrind=use_valgrind)
41 status, test_names = test_proxy.run(['--list-tests'])
Christopher Dunndc0f7362011-06-21 21:18:49 +000042 if not status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060043 print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr)
Christopher Dunndc0f7362011-06-21 21:18:49 +000044 return 1
datadiode01aee4a2015-01-11 10:39:24 +010045 test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')]
Christopher Dunndc0f7362011-06-21 21:18:49 +000046 failures = []
47 for name in test_names:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060048 print('TESTING %s:' % name, end=' ')
Christopher Dunn494950a2015-01-24 15:29:52 -060049 succeed, result = test_proxy.run(['--test', name])
Christopher Dunndc0f7362011-06-21 21:18:49 +000050 if succeed:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060051 print('OK')
Christopher Dunndc0f7362011-06-21 21:18:49 +000052 else:
Christopher Dunn494950a2015-01-24 15:29:52 -060053 failures.append((name, result))
Christopher Dunnbd1e8952014-11-19 23:30:47 -060054 print('FAILED')
Christopher Dunndc0f7362011-06-21 21:18:49 +000055 failed_count = len(failures)
56 pass_count = len(test_names) - failed_count
57 if failed_count:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060058 print()
Christopher Dunndc0f7362011-06-21 21:18:49 +000059 for name, result in failures:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060060 print(result)
Christopher Dunn494950a2015-01-24 15:29:52 -060061 print('%d/%d tests passed (%d failure(s))' % ( pass_count, len(test_names), failed_count))
Christopher Dunndc0f7362011-06-21 21:18:49 +000062 return 1
63 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060064 print('All %d tests passed' % len(test_names))
Christopher Dunndc0f7362011-06-21 21:18:49 +000065 return 0
66
67def main():
68 from optparse import OptionParser
Christopher Dunn494950a2015-01-24 15:29:52 -060069 parser = OptionParser(usage="%prog [options] <path to test_lib_json.exe>")
Christopher Dunndc0f7362011-06-21 21:18:49 +000070 parser.add_option("--valgrind",
71 action="store_true", dest="valgrind", default=False,
72 help="run all the tests using valgrind to detect memory leaks")
73 parser.enable_interspersed_args()
74 options, args = parser.parse_args()
75
76 if len(args) != 1:
Christopher Dunn494950a2015-01-24 15:29:52 -060077 parser.error('Must provides at least path to test_lib_json executable.')
78 sys.exit(1)
Christopher Dunndc0f7362011-06-21 21:18:49 +000079
Christopher Dunn494950a2015-01-24 15:29:52 -060080 exit_code = runAllTests(args[0], use_valgrind=options.valgrind)
81 sys.exit(exit_code)
Christopher Dunndc0f7362011-06-21 21:18:49 +000082
83if __name__ == '__main__':
84 main()