blob: 49cc7a9607d0d180e1e770ceb37644bb692e6433 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 10:44:361# Copyright 2007 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-10 01:01:332# Distributed under MIT license, or public domain if desired and
3# recognized in your jurisdiction.
4# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Christopher Dunnbd1e8952014-11-20 05:30:476from __future__ import print_function
Christopher Dunncd140b52015-01-16 19:44:277from __future__ import unicode_literals
8from io import open
9from glob import glob
Christopher Dunnf9864232007-06-14 21:01:2610import sys
11import os
Christopher Dunn4ca9d252015-01-10 04:28:2012import os.path
Baptiste Lepilleur932cfc72009-11-19 20:16:5913import optparse
Christopher Dunnf9864232007-06-14 21:01:2614
Baptiste Lepilleur932cfc72009-11-19 20:16:5915VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
Christopher Dunnf9864232007-06-14 21:01:2616
Christopher Dunncd140b52015-01-16 19:44:2717def getStatusOutput(cmd):
18 """
19 Return int, unicode (for both Python 2 and 3).
20 Note: os.popen().close() would return None for 0.
21 """
Christopher Dunnac6bbbc2015-01-20 17:36:0522 print(cmd, file=sys.stderr)
Christopher Dunncd140b52015-01-16 19:44:2723 pipe = os.popen(cmd)
24 process_output = pipe.read()
25 try:
26 # We have been using os.popen(). When we read() the result
27 # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
28 # Ugh! There must be a better way to handle this.
29 process_output = process_output.decode('utf-8')
30 except AttributeError:
31 pass # python3
32 status = pipe.close()
33 return status, process_output
Christopher Dunn494950a2015-01-24 21:29:5234def compareOutputs(expected, actual, message):
Christopher Dunnf9864232007-06-14 21:01:2635 expected = expected.strip().replace('\r','').split('\n')
36 actual = actual.strip().replace('\r','').split('\n')
37 diff_line = 0
Christopher Dunn494950a2015-01-24 21:29:5238 max_line_to_compare = min(len(expected), len(actual))
Christopher Dunnbd1e8952014-11-20 05:30:4739 for index in range(0,max_line_to_compare):
Christopher Dunnf9864232007-06-14 21:01:2640 if expected[index].strip() != actual[index].strip():
41 diff_line = index + 1
42 break
43 if diff_line == 0 and len(expected) != len(actual):
44 diff_line = max_line_to_compare+1
45 if diff_line == 0:
46 return None
Christopher Dunn494950a2015-01-24 21:29:5247 def safeGetLine(lines, index):
Christopher Dunnf9864232007-06-14 21:01:2648 index += -1
49 if index >= len(lines):
50 return ''
51 return lines[index].strip()
52 return """ Difference in %s at line %d:
53 Expected: '%s'
54 Actual: '%s'
55""" % (message, diff_line,
56 safeGetLine(expected,diff_line),
Christopher Dunn494950a2015-01-24 21:29:5257 safeGetLine(actual,diff_line))
Hans Johnsona3c8e862019-01-12 18:32:1558
Christopher Dunn494950a2015-01-24 21:29:5259def safeReadFile(path):
Christopher Dunnf9864232007-06-14 21:01:2660 try:
Christopher Dunn494950a2015-01-24 21:29:5261 return open(path, 'rt', encoding = 'utf-8').read()
Christopher Dunn9aa61442014-11-20 05:10:0262 except IOError as e:
Christopher Dunnf9864232007-06-14 21:01:2663 return '<File "%s" is missing: %s>' % (path,e)
64
Christopher Dunn411d88f2020-04-24 06:45:1965class FailError(Exception):
66 def __init__(self, msg):
67 super(Exception, self).__init__(msg)
68
Christopher Dunn494950a2015-01-24 21:29:5269def runAllTests(jsontest_executable_path, input_dir = None,
Christopher Dunn70704b92015-01-23 18:04:1470 use_valgrind=False, with_json_checker=False,
71 writerClass='StyledWriter'):
Christopher Dunnf9864232007-06-14 21:01:2672 if not input_dir:
Christopher Dunn494950a2015-01-24 21:29:5273 input_dir = os.path.join(os.getcwd(), 'data')
74 tests = glob(os.path.join(input_dir, '*.json'))
Baptiste Lepilleur7c66ac22010-02-21 14:26:0875 if with_json_checker:
Jordan Bayles9e23f662019-11-14 18:41:2576 all_tests = glob(os.path.join(input_dir, '../jsonchecker', '*.json'))
77 # These tests fail with strict json support, but pass with JsonCPP's
78 # extra leniency features. When adding a new exclusion to this list,
79 # remember to add the test's number and reasoning here:
80 known = ["fail{}.json".format(n) for n in [
81 4, 9, # fail because we allow trailing commas
82 7, # fails because we allow commas after close
83 8, # fails because we allow extra close
84 10, # fails because we allow extra values after close
85 13, # fails because we allow leading zeroes in numbers
86 18, # fails because we allow deeply nested values
Jordan Baylesb3492212019-11-14 18:52:1387 25, # fails because we allow tab characters in strings
Jordan Bayles9e23f662019-11-14 18:41:2588 27, # fails because we allow string line breaks
89 ]]
90 test_jsonchecker = [ test for test in all_tests
91 if os.path.basename(test) not in known]
Hans Johnsona3c8e862019-01-12 18:32:1592
Baptiste Lepilleur88681472009-11-18 21:38:5493 else:
94 test_jsonchecker = []
Jordan Bayles9e23f662019-11-14 18:41:2595
Christopher Dunnf9864232007-06-14 21:01:2696 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:5997 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:0698 for input_path in tests + test_jsonchecker:
Christopher Dunn494950a2015-01-24 21:29:5299 expect_failure = os.path.basename(input_path).startswith('fail')
vslashg0a9b9d92024-09-10 00:30:16100 is_json_checker_test = input_path in test_jsonchecker
101 is_parse_only = is_json_checker_test or expect_failure
102 is_strict_test = ('_strict_' in os.path.basename(input_path)) or is_json_checker_test
Christopher Dunnbd1e8952014-11-20 05:30:47103 print('TESTING:', input_path, end=' ')
vslashg0a9b9d92024-09-10 00:30:16104 options = is_parse_only and '--parse-only' or ''
105 options += is_strict_test and ' --strict' or ''
Christopher Dunn70704b92015-01-23 18:04:14106 options += ' --json-writer %s'%writerClass
Christopher Dunn494950a2015-01-24 21:29:52107 cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options,
Christopher Dunncd140b52015-01-16 19:44:27108 input_path)
109 status, process_output = getStatusOutput(cmd)
vslashg0a9b9d92024-09-10 00:30:16110 if is_parse_only:
Baptiste Lepilleur64e07e52009-11-18 21:27:06111 if expect_failure:
Christopher Dunncd140b52015-01-16 19:44:27112 if not status:
Christopher Dunnbd1e8952014-11-20 05:30:47113 print('FAILED')
Christopher Dunn494950a2015-01-24 21:29:52114 failed_tests.append((input_path, 'Parsing should have failed:\n%s' %
115 safeReadFile(input_path)))
Baptiste Lepilleur64e07e52009-11-18 21:27:06116 else:
Christopher Dunnbd1e8952014-11-20 05:30:47117 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26118 else:
Christopher Dunncd140b52015-01-16 19:44:27119 if status:
Christopher Dunnbd1e8952014-11-20 05:30:47120 print('FAILED')
Christopher Dunn494950a2015-01-24 21:29:52121 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06122 else:
Christopher Dunnbd1e8952014-11-20 05:30:47123 print('OK')
Baptiste Lepilleur64e07e52009-11-18 21:27:06124 else:
125 base_path = os.path.splitext(input_path)[0]
Christopher Dunn494950a2015-01-24 21:29:52126 actual_output = safeReadFile(base_path + '.actual')
127 actual_rewrite_output = safeReadFile(base_path + '.actual-rewrite')
128 open(base_path + '.process-output', 'wt', encoding = 'utf-8').write(process_output)
Baptiste Lepilleur64e07e52009-11-18 21:27:06129 if status:
Christopher Dunnbd1e8952014-11-20 05:30:47130 print('parsing failed')
Christopher Dunn494950a2015-01-24 21:29:52131 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06132 else:
133 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
Christopher Dunn494950a2015-01-24 21:29:52134 expected_output = open(expected_output_path, 'rt', encoding = 'utf-8').read()
135 detail = (compareOutputs(expected_output, actual_output, 'input')
136 or compareOutputs(expected_output, actual_rewrite_output, 'rewrite'))
Baptiste Lepilleur64e07e52009-11-18 21:27:06137 if detail:
Christopher Dunnbd1e8952014-11-20 05:30:47138 print('FAILED')
Christopher Dunn494950a2015-01-24 21:29:52139 failed_tests.append((input_path, detail))
Baptiste Lepilleur64e07e52009-11-18 21:27:06140 else:
Christopher Dunnbd1e8952014-11-20 05:30:47141 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26142
143 if failed_tests:
Christopher Dunnbd1e8952014-11-20 05:30:47144 print()
145 print('Failure details:')
Christopher Dunnf9864232007-06-14 21:01:26146 for failed_test in failed_tests:
Christopher Dunnbd1e8952014-11-20 05:30:47147 print('* Test', failed_test[0])
148 print(failed_test[1])
149 print()
150 print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
Christopher Dunn494950a2015-01-24 21:29:52151 len(failed_tests)))
Christopher Dunn411d88f2020-04-24 06:45:19152 raise FailError(repr(failed_tests))
Christopher Dunnf9864232007-06-14 21:01:26153 else:
Christopher Dunnbd1e8952014-11-20 05:30:47154 print('All %d tests passed.' % len(tests))
Christopher Dunnf9864232007-06-14 21:01:26155
Baptiste Lepilleur932cfc72009-11-19 20:16:59156def main():
157 from optparse import OptionParser
Christopher Dunn494950a2015-01-24 21:29:52158 parser = OptionParser(usage="%prog [options] <path to jsontestrunner.exe> [test case directory]")
Baptiste Lepilleur932cfc72009-11-19 20:16:59159 parser.add_option("--valgrind",
160 action="store_true", dest="valgrind", default=False,
161 help="run all the tests using valgrind to detect memory leaks")
Baptiste Lepilleur7c66ac22010-02-21 14:26:08162 parser.add_option("-c", "--with-json-checker",
163 action="store_true", dest="with_json_checker", default=False,
164 help="run all the tests from the official JSONChecker test suite of json.org")
Baptiste Lepilleur932cfc72009-11-19 20:16:59165 parser.enable_interspersed_args()
166 options, args = parser.parse_args()
167
168 if len(args) < 1 or len(args) > 2:
Christopher Dunn494950a2015-01-24 21:29:52169 parser.error('Must provides at least path to jsontestrunner executable.')
170 sys.exit(1)
Christopher Dunnf9864232007-06-14 21:01:26171
Christopher Dunn494950a2015-01-24 21:29:52172 jsontest_executable_path = os.path.normpath(os.path.abspath(args[0]))
Baptiste Lepilleur932cfc72009-11-19 20:16:59173 if len(args) > 1:
Christopher Dunn494950a2015-01-24 21:29:52174 input_path = os.path.normpath(os.path.abspath(args[1]))
Christopher Dunnf9864232007-06-14 21:01:26175 else:
176 input_path = None
Christopher Dunn411d88f2020-04-24 06:45:19177 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 20:39:57178 use_valgrind=options.valgrind,
179 with_json_checker=options.with_json_checker,
180 writerClass='StyledWriter')
Christopher Dunn411d88f2020-04-24 06:45:19181 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 20:39:57182 use_valgrind=options.valgrind,
183 with_json_checker=options.with_json_checker,
184 writerClass='StyledStreamWriter')
Christopher Dunn411d88f2020-04-24 06:45:19185 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 20:39:57186 use_valgrind=options.valgrind,
187 with_json_checker=options.with_json_checker,
188 writerClass='BuiltStyledStreamWriter')
Baptiste Lepilleur932cfc72009-11-19 20:16:59189
190if __name__ == '__main__':
Christopher Dunn411d88f2020-04-24 06:45:19191 try:
192 main()
193 except FailError:
194 sys.exit(1)