blob: 597bf2f06da05828904f550c1ca32946652dedf6 [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 """
Christopher Dunnac6bbbc2015-01-20 11:36:05 -060017 print(cmd, file=sys.stderr)
Christopher Dunncd140b52015-01-16 13:44:27 -060018 pipe = os.popen(cmd)
19 process_output = pipe.read()
20 try:
21 # We have been using os.popen(). When we read() the result
22 # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
23 # Ugh! There must be a better way to handle this.
24 process_output = process_output.decode('utf-8')
25 except AttributeError:
26 pass # python3
27 status = pipe.close()
28 return status, process_output
Christopher Dunn494950a2015-01-24 15:29:52 -060029def compareOutputs(expected, actual, message):
Christopher Dunnf9864232007-06-14 21:01:26 +000030 expected = expected.strip().replace('\r','').split('\n')
31 actual = actual.strip().replace('\r','').split('\n')
32 diff_line = 0
Christopher Dunn494950a2015-01-24 15:29:52 -060033 max_line_to_compare = min(len(expected), len(actual))
Christopher Dunnbd1e8952014-11-19 23:30:47 -060034 for index in range(0,max_line_to_compare):
Christopher Dunnf9864232007-06-14 21:01:26 +000035 if expected[index].strip() != actual[index].strip():
36 diff_line = index + 1
37 break
38 if diff_line == 0 and len(expected) != len(actual):
39 diff_line = max_line_to_compare+1
40 if diff_line == 0:
41 return None
Christopher Dunn494950a2015-01-24 15:29:52 -060042 def safeGetLine(lines, index):
Christopher Dunnf9864232007-06-14 21:01:26 +000043 index += -1
44 if index >= len(lines):
45 return ''
46 return lines[index].strip()
47 return """ Difference in %s at line %d:
48 Expected: '%s'
49 Actual: '%s'
50""" % (message, diff_line,
51 safeGetLine(expected,diff_line),
Christopher Dunn494950a2015-01-24 15:29:52 -060052 safeGetLine(actual,diff_line))
Christopher Dunnf9864232007-06-14 21:01:26 +000053
Christopher Dunn494950a2015-01-24 15:29:52 -060054def safeReadFile(path):
Christopher Dunnf9864232007-06-14 21:01:26 +000055 try:
Christopher Dunn494950a2015-01-24 15:29:52 -060056 return open(path, 'rt', encoding = 'utf-8').read()
Christopher Dunn9aa61442014-11-19 23:10:02 -060057 except IOError as e:
Christopher Dunnf9864232007-06-14 21:01:26 +000058 return '<File "%s" is missing: %s>' % (path,e)
59
Christopher Dunn494950a2015-01-24 15:29:52 -060060def runAllTests(jsontest_executable_path, input_dir = None,
Christopher Dunn70704b92015-01-23 12:04:14 -060061 use_valgrind=False, with_json_checker=False,
62 writerClass='StyledWriter'):
Christopher Dunnf9864232007-06-14 21:01:26 +000063 if not input_dir:
Christopher Dunn494950a2015-01-24 15:29:52 -060064 input_dir = os.path.join(os.getcwd(), 'data')
65 tests = glob(os.path.join(input_dir, '*.json'))
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000066 if with_json_checker:
Christopher Dunn494950a2015-01-24 15:29:52 -060067 test_jsonchecker = glob(os.path.join(input_dir, '../jsonchecker', '*.json'))
Baptiste Lepilleur88681472009-11-18 21:38:54 +000068 else:
69 test_jsonchecker = []
Christopher Dunnf9864232007-06-14 21:01:26 +000070 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000071 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000072 for input_path in tests + test_jsonchecker:
Christopher Dunn494950a2015-01-24 15:29:52 -060073 expect_failure = os.path.basename(input_path).startswith('fail')
Baptiste Lepilleur9c98f222011-05-01 15:40:47 +000074 is_json_checker_test = (input_path in test_jsonchecker) or expect_failure
Christopher Dunnbd1e8952014-11-19 23:30:47 -060075 print('TESTING:', input_path, end=' ')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000076 options = is_json_checker_test and '--json-checker' or ''
Christopher Dunn70704b92015-01-23 12:04:14 -060077 options += ' --json-writer %s'%writerClass
Christopher Dunn494950a2015-01-24 15:29:52 -060078 cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options,
Christopher Dunncd140b52015-01-16 13:44:27 -060079 input_path)
80 status, process_output = getStatusOutput(cmd)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000081 if is_json_checker_test:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000082 if expect_failure:
Christopher Dunncd140b52015-01-16 13:44:27 -060083 if not status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060084 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -060085 failed_tests.append((input_path, 'Parsing should have failed:\n%s' %
86 safeReadFile(input_path)))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000087 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060088 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +000089 else:
Christopher Dunncd140b52015-01-16 13:44:27 -060090 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060091 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -060092 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000093 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060094 print('OK')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000095 else:
96 base_path = os.path.splitext(input_path)[0]
Christopher Dunn494950a2015-01-24 15:29:52 -060097 actual_output = safeReadFile(base_path + '.actual')
98 actual_rewrite_output = safeReadFile(base_path + '.actual-rewrite')
99 open(base_path + '.process-output', 'wt', encoding = 'utf-8').write(process_output)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000100 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600101 print('parsing failed')
Christopher Dunn494950a2015-01-24 15:29:52 -0600102 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000103 else:
104 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
Christopher Dunn494950a2015-01-24 15:29:52 -0600105 expected_output = open(expected_output_path, 'rt', encoding = 'utf-8').read()
106 detail = (compareOutputs(expected_output, actual_output, 'input')
107 or compareOutputs(expected_output, actual_rewrite_output, 'rewrite'))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000108 if detail:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600109 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -0600110 failed_tests.append((input_path, detail))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000111 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600112 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +0000113
114 if failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600115 print()
116 print('Failure details:')
Christopher Dunnf9864232007-06-14 21:01:26 +0000117 for failed_test in failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600118 print('* Test', failed_test[0])
119 print(failed_test[1])
120 print()
121 print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
Christopher Dunn494950a2015-01-24 15:29:52 -0600122 len(failed_tests)))
Christopher Dunnf9864232007-06-14 21:01:26 +0000123 return 1
124 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600125 print('All %d tests passed.' % len(tests))
Christopher Dunnf9864232007-06-14 21:01:26 +0000126 return 0
127
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000128def main():
129 from optparse import OptionParser
Christopher Dunn494950a2015-01-24 15:29:52 -0600130 parser = OptionParser(usage="%prog [options] <path to jsontestrunner.exe> [test case directory]")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000131 parser.add_option("--valgrind",
132 action="store_true", dest="valgrind", default=False,
133 help="run all the tests using valgrind to detect memory leaks")
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000134 parser.add_option("-c", "--with-json-checker",
135 action="store_true", dest="with_json_checker", default=False,
136 help="run all the tests from the official JSONChecker test suite of json.org")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000137 parser.enable_interspersed_args()
138 options, args = parser.parse_args()
139
140 if len(args) < 1 or len(args) > 2:
Christopher Dunn494950a2015-01-24 15:29:52 -0600141 parser.error('Must provides at least path to jsontestrunner executable.')
142 sys.exit(1)
Christopher Dunnf9864232007-06-14 21:01:26 +0000143
Christopher Dunn494950a2015-01-24 15:29:52 -0600144 jsontest_executable_path = os.path.normpath(os.path.abspath(args[0]))
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000145 if len(args) > 1:
Christopher Dunn494950a2015-01-24 15:29:52 -0600146 input_path = os.path.normpath(os.path.abspath(args[1]))
Christopher Dunnf9864232007-06-14 21:01:26 +0000147 else:
148 input_path = None
Christopher Dunn494950a2015-01-24 15:29:52 -0600149 status = runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 14:39:57 -0600150 use_valgrind=options.valgrind,
151 with_json_checker=options.with_json_checker,
152 writerClass='StyledWriter')
Christopher Dunn70704b92015-01-23 12:04:14 -0600153 if status:
Christopher Dunn494950a2015-01-24 15:29:52 -0600154 sys.exit(status)
155 status = runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 14:39:57 -0600156 use_valgrind=options.valgrind,
157 with_json_checker=options.with_json_checker,
158 writerClass='StyledStreamWriter')
159 if status:
160 sys.exit(status)
161 status = runAllTests(jsontest_executable_path, input_path,
162 use_valgrind=options.valgrind,
163 with_json_checker=options.with_json_checker,
164 writerClass='BuiltStyledStreamWriter')
165 if status:
166 sys.exit(status)
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000167
168if __name__ == '__main__':
169 main()