blob: eba84b103d0a496a01987f986042ee32774c9bb2 [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 Lepilleurf179a182009-11-18 22:25:34 +00007RUN_JSONCHECKER = False
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +00008VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
Christopher Dunnf9864232007-06-14 21:01:26 +00009
10def compareOutputs( expected, actual, message ):
11 expected = expected.strip().replace('\r','').split('\n')
12 actual = actual.strip().replace('\r','').split('\n')
13 diff_line = 0
14 max_line_to_compare = min( len(expected), len(actual) )
15 for index in xrange(0,max_line_to_compare):
16 if expected[index].strip() != actual[index].strip():
17 diff_line = index + 1
18 break
19 if diff_line == 0 and len(expected) != len(actual):
20 diff_line = max_line_to_compare+1
21 if diff_line == 0:
22 return None
23 def safeGetLine( lines, index ):
24 index += -1
25 if index >= len(lines):
26 return ''
27 return lines[index].strip()
28 return """ Difference in %s at line %d:
29 Expected: '%s'
30 Actual: '%s'
31""" % (message, diff_line,
32 safeGetLine(expected,diff_line),
33 safeGetLine(actual,diff_line) )
34
35def safeReadFile( path ):
36 try:
37 return file( path, 'rt' ).read()
38 except IOError, e:
39 return '<File "%s" is missing: %s>' % (path,e)
40
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000041def runAllTests( jsontest_executable_path, input_dir = None,
42 use_valgrind=False ):
Christopher Dunnf9864232007-06-14 21:01:26 +000043 if not input_dir:
44 input_dir = os.getcwd()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000045 tests = glob( os.path.join( input_dir, '*.json' ) )
Baptiste Lepilleur88681472009-11-18 21:38:54 +000046 if RUN_JSONCHECKER:
47 test_jsonchecker = glob( os.path.join( input_dir, 'jsonchecker', '*.json' ) )
48 else:
49 test_jsonchecker = []
Christopher Dunnf9864232007-06-14 21:01:26 +000050 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000051 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000052 for input_path in tests + test_jsonchecker:
53 is_json_checker_test = input_path in test_jsonchecker
Christopher Dunnf9864232007-06-14 21:01:26 +000054 print 'TESTING:', input_path,
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000055 options = is_json_checker_test and '--json-checker' or ''
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000056 pipe = os.popen( "%s%s %s %s" % (
57 valgrind_path, jsontest_executable_path, options,
58 input_path) )
Christopher Dunnf9864232007-06-14 21:01:26 +000059 process_output = pipe.read()
60 status = pipe.close()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000061 if is_json_checker_test:
62 expect_failure = os.path.basename( input_path ).startswith( 'fail' )
63 if expect_failure:
64 if status is None:
65 print 'FAILED'
Baptiste Lepilleur88681472009-11-18 21:38:54 +000066 failed_tests.append( (input_path, 'Parsing should have failed:\n%s' %
67 safeReadFile(input_path)) )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000068 else:
69 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000070 else:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000071 if status is not None:
72 print 'FAILED'
73 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
74 else:
75 print 'OK'
76 else:
77 base_path = os.path.splitext(input_path)[0]
78 actual_output = safeReadFile( base_path + '.actual' )
79 actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
80 file(base_path + '.process-output','wt').write( process_output )
81 if status:
82 print 'parsing failed'
83 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
84 else:
85 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
86 expected_output = file( expected_output_path, 'rt' ).read()
87 detail = ( compareOutputs( expected_output, actual_output, 'input' )
88 or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
89 if detail:
90 print 'FAILED'
91 failed_tests.append( (input_path, detail) )
92 else:
93 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000094
95 if failed_tests:
96 print
97 print 'Failure details:'
98 for failed_test in failed_tests:
99 print '* Test', failed_test[0]
100 print failed_test[1]
101 print
102 print 'Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
103 len(failed_tests) )
104 return 1
105 else:
106 print 'All %d tests passed.' % len(tests)
107 return 0
108
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000109def 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.' )
Christopher Dunnf9864232007-06-14 21:01:26 +0000120 sys.exit( 1 )
121
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000122 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] ) )
Christopher Dunnf9864232007-06-14 21:01:26 +0000125 else:
126 input_path = None
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000127 status = runAllTests( jsontest_executable_path, input_path,
128 use_valgrind=options.valgrind )
129 sys.exit( status )
130
131if __name__ == '__main__':
132 main()