22import os
33import os .path
44from glob import glob
5+ import optparse
56
67RUN_JSONCHECKER = False
8+ VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
79
810def compareOutputs ( expected , actual , message ):
911 expected = expected .strip ().replace ('\r ' ,'' ).split ('\n ' )
@@ -36,7 +38,8 @@ def safeReadFile( path ):
3638 except IOError , e :
3739 return '<File "%s" is missing: %s>' % (path ,e )
3840
39- def runAllTests ( jsontest_executable_path , input_dir = None ):
41+ def runAllTests ( jsontest_executable_path , input_dir = None ,
42+ use_valgrind = False ):
4043 if not input_dir :
4144 input_dir = os .getcwd ()
4245 tests = glob ( os .path .join ( input_dir , '*.json' ) )
@@ -45,12 +48,14 @@ def runAllTests( jsontest_executable_path, input_dir = None ):
4548 else :
4649 test_jsonchecker = []
4750 failed_tests = []
51+ valgrind_path = use_valgrind and VALGRIND_CMD or ''
4852 for input_path in tests + test_jsonchecker :
4953 is_json_checker_test = input_path in test_jsonchecker
5054 print 'TESTING:' , input_path ,
5155 options = is_json_checker_test and '--json-checker' or ''
52- pipe = os .popen ( "%s %s %s" % (jsontest_executable_path , options ,
53- input_path ) )
56+ pipe = os .popen ( "%s%s %s %s" % (
57+ valgrind_path , jsontest_executable_path , options ,
58+ input_path ) )
5459 process_output = pipe .read ()
5560 status = pipe .close ()
5661 if is_json_checker_test :
@@ -101,15 +106,27 @@ def runAllTests( jsontest_executable_path, input_dir = None ):
101106 print 'All %d tests passed.' % len (tests )
102107 return 0
103108
104- if __name__ == '__main__' :
105- if len (sys .argv ) < 1 or len (sys .argv ) > 2 :
106- print "Usage: %s jsontest-executable-path [input-testcase-directory]" % sys .argv [0 ]
109+ def main ():
110+ from optparse import OptionParser
111+ parser = OptionParser ( usage = "%prog [options] <path to jsontestrunner.exe> [test case directory]" )
112+ parser .add_option ("--valgrind" ,
113+ action = "store_true" , dest = "valgrind" , default = False ,
114+ help = "run all the tests using valgrind to detect memory leaks" )
115+ parser .enable_interspersed_args ()
116+ options , args = parser .parse_args ()
117+
118+ if len (args ) < 1 or len (args ) > 2 :
119+ options .error ( 'Must provides at least path to jsontestrunner executable.' )
107120 sys .exit ( 1 )
108121
109- jsontest_executable_path = os .path .normpath ( os .path .abspath ( sys . argv [ 1 ] ) )
110- if len (sys . argv ) > 2 :
111- input_path = os .path .normpath ( os .path .abspath ( sys . argv [ 2 ] ) )
122+ jsontest_executable_path = os .path .normpath ( os .path .abspath ( args [ 0 ] ) )
123+ if len (args ) > 1 :
124+ input_path = os .path .normpath ( os .path .abspath ( args [ 1 ] ) )
112125 else :
113126 input_path = None
114- status = runAllTests ( jsontest_executable_path , input_path )
115- sys .exit ( status )
127+ status = runAllTests ( jsontest_executable_path , input_path ,
128+ use_valgrind = options .valgrind )
129+ sys .exit ( status )
130+
131+ if __name__ == '__main__' :
132+ main ()
0 commit comments