blob: 6634e72419678ca43fc8b0876d0a9c79777a7e50 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 10:44:361# Copyright 2009 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-10 01:01:332# 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-20 05:30:476from __future__ import print_function
Christopher Dunn4bc31152015-01-16 20:48:067from __future__ import unicode_literals
8from io import open
Christopher Dunnbd1e8952014-11-20 05:30:479from glob import glob
Christopher Dunndc0f7362011-06-21 21:18:4910import sys
11import os
12import os.path
13import subprocess
Christopher Dunndc0f7362011-06-21 21:18:4914import optparse
15
16VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes'
17
18class TestProxy(object):
Christopher Dunn494950a2015-01-24 21:29:5219 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:4921 self.use_valgrind = use_valgrind
22
Christopher Dunn494950a2015-01-24 21:29:5223 def run(self, options):
Christopher Dunndc0f7362011-06-21 21:18:4924 if self.use_valgrind:
25 cmd = VALGRIND_CMD.split()
26 else:
27 cmd = []
Christopher Dunn494950a2015-01-24 21:29:5228 cmd.extend([self.test_exe_path, '--test-auto'] + options)
Christopher Dunn4bc31152015-01-16 20:48:0629 try:
Christopher Dunn494950a2015-01-24 21:29:5230 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Christopher Dunn4bc31152015-01-16 20:48:0631 except:
32 print(cmd)
33 raise
Christopher Dunndc0f7362011-06-21 21:18:4934 stdout = process.communicate()[0]
35 if process.returncode:
36 return False, stdout
37 return True, stdout
38
Christopher Dunn494950a2015-01-24 21:29:5239def 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:4942 if not status:
Christopher Dunnbd1e8952014-11-20 05:30:4743 print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr)
Christopher Dunndc0f7362011-06-21 21:18:4944 return 1
datadiode01aee4a2015-01-11 09:39:2445 test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')]
Christopher Dunndc0f7362011-06-21 21:18:4946 failures = []
47 for name in test_names:
Christopher Dunnbd1e8952014-11-20 05:30:4748 print('TESTING %s:' % name, end=' ')
Christopher Dunn494950a2015-01-24 21:29:5249 succeed, result = test_proxy.run(['--test', name])
Christopher Dunndc0f7362011-06-21 21:18:4950 if succeed:
Christopher Dunnbd1e8952014-11-20 05:30:4751 print('OK')
Christopher Dunndc0f7362011-06-21 21:18:4952 else:
Christopher Dunn494950a2015-01-24 21:29:5253 failures.append((name, result))
Christopher Dunnbd1e8952014-11-20 05:30:4754 print('FAILED')
Christopher Dunndc0f7362011-06-21 21:18:4955 failed_count = len(failures)
56 pass_count = len(test_names) - failed_count
57 if failed_count:
Christopher Dunnbd1e8952014-11-20 05:30:4758 print()
Christopher Dunndc0f7362011-06-21 21:18:4959 for name, result in failures:
Christopher Dunnbd1e8952014-11-20 05:30:4760 print(result)
Christopher Dunn494950a2015-01-24 21:29:5261 print('%d/%d tests passed (%d failure(s))' % ( pass_count, len(test_names), failed_count))
Christopher Dunndc0f7362011-06-21 21:18:4962 return 1
63 else:
Christopher Dunnbd1e8952014-11-20 05:30:4764 print('All %d tests passed' % len(test_names))
Christopher Dunndc0f7362011-06-21 21:18:4965 return 0
66
67def main():
68 from optparse import OptionParser
Christopher Dunn494950a2015-01-24 21:29:5269 parser = OptionParser(usage="%prog [options] <path to test_lib_json.exe>")
Christopher Dunndc0f7362011-06-21 21:18:4970 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 21:29:5277 parser.error('Must provides at least path to test_lib_json executable.')
78 sys.exit(1)
Christopher Dunndc0f7362011-06-21 21:18:4979
Christopher Dunn494950a2015-01-24 21:29:5280 exit_code = runAllTests(args[0], use_valgrind=options.valgrind)
81 sys.exit(exit_code)
Christopher Dunndc0f7362011-06-21 21:18:4982
83if __name__ == '__main__':
84 main()