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