blob: ffe033f48e3b31da8e21eabed96fddbc058a3875 [file] [log] [blame]
Christopher Dunnbd1e8952014-11-19 23:30:47 -06001from __future__ import print_function
Christopher Dunncd140b52015-01-16 13:44:27 -06002from __future__ import unicode_literals
3from io import open
4from glob import glob
Christopher Dunnf9864232007-06-14 21:01:26 +00005import sys
6import os
Christopher Dunn4ca9d252015-01-09 22:28:20 -06007import os.path
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +00008import optparse
Christopher Dunnf9864232007-06-14 21:01:26 +00009
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000010VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
Christopher Dunnf9864232007-06-14 21:01:26 +000011
Christopher Dunncd140b52015-01-16 13:44:27 -060012def getStatusOutput(cmd):
13 """
14 Return int, unicode (for both Python 2 and 3).
15 Note: os.popen().close() would return None for 0.
16 """
17 pipe = os.popen(cmd)
18 process_output = pipe.read()
19 try:
20 # We have been using os.popen(). When we read() the result
21 # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
22 # Ugh! There must be a better way to handle this.
23 process_output = process_output.decode('utf-8')
24 except AttributeError:
25 pass # python3
26 status = pipe.close()
27 return status, process_output
Christopher Dunnf9864232007-06-14 21:01:26 +000028def compareOutputs( expected, actual, message ):
29 expected = expected.strip().replace('\r','').split('\n')
30 actual = actual.strip().replace('\r','').split('\n')
31 diff_line = 0
32 max_line_to_compare = min( len(expected), len(actual) )
Christopher Dunnbd1e8952014-11-19 23:30:47 -060033 for index in range(0,max_line_to_compare):
Christopher Dunnf9864232007-06-14 21:01:26 +000034 if expected[index].strip() != actual[index].strip():
35 diff_line = index + 1
36 break
37 if diff_line == 0 and len(expected) != len(actual):
38 diff_line = max_line_to_compare+1
39 if diff_line == 0:
40 return None
41 def safeGetLine( lines, index ):
42 index += -1
43 if index >= len(lines):
44 return ''
45 return lines[index].strip()
46 return """ Difference in %s at line %d:
47 Expected: '%s'
48 Actual: '%s'
49""" % (message, diff_line,
50 safeGetLine(expected,diff_line),
51 safeGetLine(actual,diff_line) )
52
53def safeReadFile( path ):
54 try:
datadiode01aee4a2015-01-11 10:39:24 +010055 return open( path, 'rt', encoding = 'utf-8' ).read()
Christopher Dunn9aa61442014-11-19 23:10:02 -060056 except IOError as e:
Christopher Dunnf9864232007-06-14 21:01:26 +000057 return '<File "%s" is missing: %s>' % (path,e)
58
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000059def runAllTests( jsontest_executable_path, input_dir = None,
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000060 use_valgrind=False, with_json_checker=False ):
Christopher Dunnf9864232007-06-14 21:01:26 +000061 if not input_dir:
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +000062 input_dir = os.path.join( os.getcwd(), 'data' )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000063 tests = glob( os.path.join( input_dir, '*.json' ) )
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000064 if with_json_checker:
65 test_jsonchecker = glob( os.path.join( input_dir, '../jsonchecker', '*.json' ) )
Baptiste Lepilleur88681472009-11-18 21:38:54 +000066 else:
67 test_jsonchecker = []
Christopher Dunnf9864232007-06-14 21:01:26 +000068 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000069 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000070 for input_path in tests + test_jsonchecker:
Baptiste Lepilleur9c98f222011-05-01 15:40:47 +000071 expect_failure = os.path.basename( input_path ).startswith( 'fail' )
72 is_json_checker_test = (input_path in test_jsonchecker) or expect_failure
Christopher Dunnbd1e8952014-11-19 23:30:47 -060073 print('TESTING:', input_path, end=' ')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000074 options = is_json_checker_test and '--json-checker' or ''
Christopher Dunn26c52862015-01-23 11:53:16 -060075 options += ' --json-writer StyledWriter'
Christopher Dunncd140b52015-01-16 13:44:27 -060076 cmd = '%s%s %s "%s"' % (
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000077 valgrind_path, jsontest_executable_path, options,
Christopher Dunncd140b52015-01-16 13:44:27 -060078 input_path)
79 status, process_output = getStatusOutput(cmd)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000080 if is_json_checker_test:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000081 if expect_failure:
Christopher Dunncd140b52015-01-16 13:44:27 -060082 if not status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060083 print('FAILED')
Baptiste Lepilleur88681472009-11-18 21:38:54 +000084 failed_tests.append( (input_path, 'Parsing should have failed:\n%s' %
85 safeReadFile(input_path)) )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000086 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060087 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +000088 else:
Christopher Dunncd140b52015-01-16 13:44:27 -060089 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060090 print('FAILED')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000091 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
92 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060093 print('OK')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000094 else:
95 base_path = os.path.splitext(input_path)[0]
96 actual_output = safeReadFile( base_path + '.actual' )
97 actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
datadiode01aee4a2015-01-11 10:39:24 +010098 open(base_path + '.process-output', 'wt', encoding = 'utf-8').write( process_output )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000099 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600100 print('parsing failed')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000101 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
102 else:
103 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
datadiode01aee4a2015-01-11 10:39:24 +0100104 expected_output = open( expected_output_path, 'rt', encoding = 'utf-8' ).read()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000105 detail = ( compareOutputs( expected_output, actual_output, 'input' )
106 or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
107 if detail:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600108 print('FAILED')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000109 failed_tests.append( (input_path, detail) )
110 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600111 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +0000112
113 if failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600114 print()
115 print('Failure details:')
Christopher Dunnf9864232007-06-14 21:01:26 +0000116 for failed_test in failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600117 print('* Test', failed_test[0])
118 print(failed_test[1])
119 print()
120 print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
121 len(failed_tests) ))
Christopher Dunnf9864232007-06-14 21:01:26 +0000122 return 1
123 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600124 print('All %d tests passed.' % len(tests))
Christopher Dunnf9864232007-06-14 21:01:26 +0000125 return 0
126
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000127def main():
128 from optparse import OptionParser
129 parser = OptionParser( usage="%prog [options] <path to jsontestrunner.exe> [test case directory]" )
130 parser.add_option("--valgrind",
131 action="store_true", dest="valgrind", default=False,
132 help="run all the tests using valgrind to detect memory leaks")
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000133 parser.add_option("-c", "--with-json-checker",
134 action="store_true", dest="with_json_checker", default=False,
135 help="run all the tests from the official JSONChecker test suite of json.org")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000136 parser.enable_interspersed_args()
137 options, args = parser.parse_args()
138
139 if len(args) < 1 or len(args) > 2:
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000140 parser.error( 'Must provides at least path to jsontestrunner executable.' )
Christopher Dunnf9864232007-06-14 21:01:26 +0000141 sys.exit( 1 )
142
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000143 jsontest_executable_path = os.path.normpath( os.path.abspath( args[0] ) )
144 if len(args) > 1:
145 input_path = os.path.normpath( os.path.abspath( args[1] ) )
Christopher Dunnf9864232007-06-14 21:01:26 +0000146 else:
147 input_path = None
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000148 status = runAllTests( jsontest_executable_path, input_path,
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000149 use_valgrind=options.valgrind, with_json_checker=options.with_json_checker )
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000150 sys.exit( status )
151
152if __name__ == '__main__':
153 main()