Devin Jeanpierre | 19fc55f | 2017-04-24 10:49:00 -0700 | [diff] [blame] | 1 | # Copyright 2009 The JsonCpp Authors |
Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 2 | # 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 Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 6 | from __future__ import print_function |
Christopher Dunn | 4bc3115 | 2015-01-16 14:48:06 -0600 | [diff] [blame] | 7 | from __future__ import unicode_literals |
| 8 | from io import open |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 9 | from glob import glob |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 10 | import sys |
| 11 | import os |
| 12 | import os.path |
| 13 | import subprocess |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 14 | import optparse |
| 15 | |
| 16 | VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes' |
| 17 | |
| 18 | class TestProxy(object): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 19 | def __init__(self, test_exe_path, use_valgrind=False): |
| 20 | self.test_exe_path = os.path.normpath(os.path.abspath(test_exe_path)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 21 | self.use_valgrind = use_valgrind |
| 22 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 23 | def run(self, options): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 24 | if self.use_valgrind: |
| 25 | cmd = VALGRIND_CMD.split() |
| 26 | else: |
| 27 | cmd = [] |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 28 | cmd.extend([self.test_exe_path, '--test-auto'] + options) |
Christopher Dunn | 4bc3115 | 2015-01-16 14:48:06 -0600 | [diff] [blame] | 29 | try: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 30 | process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
Christopher Dunn | 4bc3115 | 2015-01-16 14:48:06 -0600 | [diff] [blame] | 31 | except: |
| 32 | print(cmd) |
| 33 | raise |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 34 | stdout = process.communicate()[0] |
| 35 | if process.returncode: |
| 36 | return False, stdout |
| 37 | return True, stdout |
| 38 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 39 | def 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 Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 42 | if not status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 43 | print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 44 | return 1 |
datadiode | 01aee4a | 2015-01-11 10:39:24 +0100 | [diff] [blame] | 45 | test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')] |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 46 | failures = [] |
| 47 | for name in test_names: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 48 | print('TESTING %s:' % name, end=' ') |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 49 | succeed, result = test_proxy.run(['--test', name]) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 50 | if succeed: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 51 | print('OK') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 52 | else: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 53 | failures.append((name, result)) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 54 | print('FAILED') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 55 | failed_count = len(failures) |
| 56 | pass_count = len(test_names) - failed_count |
| 57 | if failed_count: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 58 | print() |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 59 | for name, result in failures: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 60 | print(result) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 61 | print('%d/%d tests passed (%d failure(s))' % ( pass_count, len(test_names), failed_count)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 62 | return 1 |
| 63 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 64 | print('All %d tests passed' % len(test_names)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 65 | return 0 |
| 66 | |
| 67 | def main(): |
| 68 | from optparse import OptionParser |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 69 | parser = OptionParser(usage="%prog [options] <path to test_lib_json.exe>") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 70 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 77 | parser.error('Must provides at least path to test_lib_json executable.') |
| 78 | sys.exit(1) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 79 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 80 | exit_code = runAllTests(args[0], use_valgrind=options.valgrind) |
| 81 | sys.exit(exit_code) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 82 | |
| 83 | if __name__ == '__main__': |
| 84 | main() |