blob: 800337d4c38f332a1b5bf6f2322c3afe23efcc45 [file] [log] [blame]
Christopher Dunnf9864232007-06-14 21:01:26 +00001import sys
2import os
3import os.path
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +00004from glob import glob
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +00005import optparse
Christopher Dunnf9864232007-06-14 21:01:26 +00006
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +00007VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
Christopher Dunnf9864232007-06-14 21:01:26 +00008
9def compareOutputs( expected, actual, message ):
10 expected = expected.strip().replace('\r','').split('\n')
11 actual = actual.strip().replace('\r','').split('\n')
12 diff_line = 0
13 max_line_to_compare = min( len(expected), len(actual) )
14 for index in xrange(0,max_line_to_compare):
15 if expected[index].strip() != actual[index].strip():
16 diff_line = index + 1
17 break
18 if diff_line == 0 and len(expected) != len(actual):
19 diff_line = max_line_to_compare+1
20 if diff_line == 0:
21 return None
22 def safeGetLine( lines, index ):
23 index += -1
24 if index >= len(lines):
25 return ''
26 return lines[index].strip()
27 return """ Difference in %s at line %d:
28 Expected: '%s'
29 Actual: '%s'
30""" % (message, diff_line,
31 safeGetLine(expected,diff_line),
32 safeGetLine(actual,diff_line) )
33
34def safeReadFile( path ):
35 try:
36 return file( path, 'rt' ).read()
37 except IOError, e:
38 return '<File "%s" is missing: %s>' % (path,e)
39
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000040def runAllTests( jsontest_executable_path, input_dir = None,
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000041 use_valgrind=False, with_json_checker=False ):
Christopher Dunnf9864232007-06-14 21:01:26 +000042 if not input_dir:
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +000043 input_dir = os.path.join( os.getcwd(), 'data' )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000044 tests = glob( os.path.join( input_dir, '*.json' ) )
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000045 if with_json_checker:
46 test_jsonchecker = glob( os.path.join( input_dir, '../jsonchecker', '*.json' ) )
Baptiste Lepilleur88681472009-11-18 21:38:54 +000047 else:
48 test_jsonchecker = []
Christopher Dunnf9864232007-06-14 21:01:26 +000049 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000050 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000051 for input_path in tests + test_jsonchecker:
52 is_json_checker_test = input_path in test_jsonchecker
Christopher Dunnf9864232007-06-14 21:01:26 +000053 print 'TESTING:', input_path,
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000054 options = is_json_checker_test and '--json-checker' or ''
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000055 pipe = os.popen( "%s%s %s %s" % (
56 valgrind_path, jsontest_executable_path, options,
57 input_path) )
Christopher Dunnf9864232007-06-14 21:01:26 +000058 process_output = pipe.read()
59 status = pipe.close()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000060 if is_json_checker_test:
61 expect_failure = os.path.basename( input_path ).startswith( 'fail' )
62 if expect_failure:
63 if status is None:
64 print 'FAILED'
Baptiste Lepilleur88681472009-11-18 21:38:54 +000065 failed_tests.append( (input_path, 'Parsing should have failed:\n%s' %
66 safeReadFile(input_path)) )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000067 else:
68 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000069 else:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000070 if status is not None:
71 print 'FAILED'
72 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
73 else:
74 print 'OK'
75 else:
76 base_path = os.path.splitext(input_path)[0]
77 actual_output = safeReadFile( base_path + '.actual' )
78 actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
79 file(base_path + '.process-output','wt').write( process_output )
80 if status:
81 print 'parsing failed'
82 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
83 else:
84 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
85 expected_output = file( expected_output_path, 'rt' ).read()
86 detail = ( compareOutputs( expected_output, actual_output, 'input' )
87 or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
88 if detail:
89 print 'FAILED'
90 failed_tests.append( (input_path, detail) )
91 else:
92 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000093
94 if failed_tests:
95 print
96 print 'Failure details:'
97 for failed_test in failed_tests:
98 print '* Test', failed_test[0]
99 print failed_test[1]
100 print
101 print 'Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
102 len(failed_tests) )
103 return 1
104 else:
105 print 'All %d tests passed.' % len(tests)
106 return 0
107
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000108def main():
109 from optparse import OptionParser
110 parser = OptionParser( usage="%prog [options] <path to jsontestrunner.exe> [test case directory]" )
111 parser.add_option("--valgrind",
112 action="store_true", dest="valgrind", default=False,
113 help="run all the tests using valgrind to detect memory leaks")
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000114 parser.add_option("-c", "--with-json-checker",
115 action="store_true", dest="with_json_checker", default=False,
116 help="run all the tests from the official JSONChecker test suite of json.org")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000117 parser.enable_interspersed_args()
118 options, args = parser.parse_args()
119
120 if len(args) < 1 or len(args) > 2:
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000121 parser.error( 'Must provides at least path to jsontestrunner executable.' )
Christopher Dunnf9864232007-06-14 21:01:26 +0000122 sys.exit( 1 )
123
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000124 jsontest_executable_path = os.path.normpath( os.path.abspath( args[0] ) )
125 if len(args) > 1:
126 input_path = os.path.normpath( os.path.abspath( args[1] ) )
Christopher Dunnf9864232007-06-14 21:01:26 +0000127 else:
128 input_path = None
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000129 status = runAllTests( jsontest_executable_path, input_path,
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000130 use_valgrind=options.valgrind, with_json_checker=options.with_json_checker )
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000131 sys.exit( status )
132
133if __name__ == '__main__':
134 main()