Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | from glob import glob |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import os |
| 5 | import os.path |
| 6 | import subprocess |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 7 | import optparse |
| 8 | |
| 9 | VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes' |
| 10 | |
| 11 | class TestProxy(object): |
| 12 | def __init__( self, test_exe_path, use_valgrind=False ): |
| 13 | self.test_exe_path = os.path.normpath( os.path.abspath( test_exe_path ) ) |
| 14 | self.use_valgrind = use_valgrind |
| 15 | |
| 16 | def run( self, options ): |
| 17 | if self.use_valgrind: |
| 18 | cmd = VALGRIND_CMD.split() |
| 19 | else: |
| 20 | cmd = [] |
| 21 | cmd.extend( [self.test_exe_path, '--test-auto'] + options ) |
| 22 | process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) |
| 23 | stdout = process.communicate()[0] |
| 24 | if process.returncode: |
| 25 | return False, stdout |
| 26 | return True, stdout |
| 27 | |
| 28 | def runAllTests( exe_path, use_valgrind=False ): |
| 29 | test_proxy = TestProxy( exe_path, use_valgrind=use_valgrind ) |
| 30 | status, test_names = test_proxy.run( ['--list-tests'] ) |
| 31 | if not status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 32 | 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] | 33 | return 1 |
| 34 | test_names = [name.strip() for name in test_names.strip().split('\n')] |
| 35 | failures = [] |
| 36 | for name in test_names: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 37 | print('TESTING %s:' % name, end=' ') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 38 | succeed, result = test_proxy.run( ['--test', name] ) |
| 39 | if succeed: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 40 | print('OK') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 41 | else: |
| 42 | failures.append( (name, result) ) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 43 | print('FAILED') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 44 | failed_count = len(failures) |
| 45 | pass_count = len(test_names) - failed_count |
| 46 | if failed_count: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 47 | print() |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 48 | for name, result in failures: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 49 | print(result) |
| 50 | print('%d/%d tests passed (%d failure(s))' % ( |
| 51 | pass_count, len(test_names), failed_count)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 52 | return 1 |
| 53 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 54 | print('All %d tests passed' % len(test_names)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 55 | return 0 |
| 56 | |
| 57 | def main(): |
| 58 | from optparse import OptionParser |
| 59 | parser = OptionParser( usage="%prog [options] <path to test_lib_json.exe>" ) |
| 60 | parser.add_option("--valgrind", |
| 61 | action="store_true", dest="valgrind", default=False, |
| 62 | help="run all the tests using valgrind to detect memory leaks") |
| 63 | parser.enable_interspersed_args() |
| 64 | options, args = parser.parse_args() |
| 65 | |
| 66 | if len(args) != 1: |
| 67 | parser.error( 'Must provides at least path to test_lib_json executable.' ) |
| 68 | sys.exit( 1 ) |
| 69 | |
| 70 | exit_code = runAllTests( args[0], use_valgrind=options.valgrind ) |
| 71 | sys.exit( exit_code ) |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | main() |