You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
7.9KB

  1. # Copyright 2007 Baptiste Lepilleur and The JsonCpp Authors
  2. # 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. from __future__ import print_function
  6. from __future__ import unicode_literals
  7. from io import open
  8. from glob import glob
  9. import sys
  10. import os
  11. import os.path
  12. import optparse
  13. VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
  14. def getStatusOutput(cmd):
  15. """
  16. Return int, unicode (for both Python 2 and 3).
  17. Note: os.popen().close() would return None for 0.
  18. """
  19. print(cmd, file=sys.stderr)
  20. pipe = os.popen(cmd)
  21. process_output = pipe.read()
  22. try:
  23. # We have been using os.popen(). When we read() the result
  24. # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
  25. # Ugh! There must be a better way to handle this.
  26. process_output = process_output.decode('utf-8')
  27. except AttributeError:
  28. pass # python3
  29. status = pipe.close()
  30. return status, process_output
  31. def compareOutputs(expected, actual, message):
  32. expected = expected.strip().replace('\r','').split('\n')
  33. actual = actual.strip().replace('\r','').split('\n')
  34. diff_line = 0
  35. max_line_to_compare = min(len(expected), len(actual))
  36. for index in range(0,max_line_to_compare):
  37. if expected[index].strip() != actual[index].strip():
  38. diff_line = index + 1
  39. break
  40. if diff_line == 0 and len(expected) != len(actual):
  41. diff_line = max_line_to_compare+1
  42. if diff_line == 0:
  43. return None
  44. def safeGetLine(lines, index):
  45. index += -1
  46. if index >= len(lines):
  47. return ''
  48. return lines[index].strip()
  49. return """ Difference in %s at line %d:
  50. Expected: '%s'
  51. Actual: '%s'
  52. """ % (message, diff_line,
  53. safeGetLine(expected,diff_line),
  54. safeGetLine(actual,diff_line))
  55. def safeReadFile(path):
  56. try:
  57. return open(path, 'rt', encoding = 'utf-8').read()
  58. except IOError as e:
  59. return '<File "%s" is missing: %s>' % (path,e)
  60. class FailError(Exception):
  61. def __init__(self, msg):
  62. super(Exception, self).__init__(msg)
  63. def runAllTests(jsontest_executable_path, input_dir = None,
  64. use_valgrind=False, with_json_checker=False,
  65. writerClass='StyledWriter'):
  66. if not input_dir:
  67. input_dir = os.path.join(os.getcwd(), 'data')
  68. tests = glob(os.path.join(input_dir, '*.json'))
  69. if with_json_checker:
  70. all_tests = glob(os.path.join(input_dir, '../jsonchecker', '*.json'))
  71. # These tests fail with strict json support, but pass with JsonCPP's
  72. # extra leniency features. When adding a new exclusion to this list,
  73. # remember to add the test's number and reasoning here:
  74. known = ["fail{}.json".format(n) for n in [
  75. 4, 9, # fail because we allow trailing commas
  76. 7, # fails because we allow commas after close
  77. 8, # fails because we allow extra close
  78. 10, # fails because we allow extra values after close
  79. 13, # fails because we allow leading zeroes in numbers
  80. 18, # fails because we allow deeply nested values
  81. 25, # fails because we allow tab characters in strings
  82. 27, # fails because we allow string line breaks
  83. ]]
  84. test_jsonchecker = [ test for test in all_tests
  85. if os.path.basename(test) not in known]
  86. else:
  87. test_jsonchecker = []
  88. failed_tests = []
  89. valgrind_path = use_valgrind and VALGRIND_CMD or ''
  90. for input_path in tests + test_jsonchecker:
  91. expect_failure = os.path.basename(input_path).startswith('fail')
  92. is_json_checker_test = (input_path in test_jsonchecker) or expect_failure
  93. print('TESTING:', input_path, end=' ')
  94. options = is_json_checker_test and '--json-checker' or ''
  95. options += ' --json-writer %s'%writerClass
  96. cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options,
  97. input_path)
  98. status, process_output = getStatusOutput(cmd)
  99. if is_json_checker_test:
  100. if expect_failure:
  101. if not status:
  102. print('FAILED')
  103. failed_tests.append((input_path, 'Parsing should have failed:\n%s' %
  104. safeReadFile(input_path)))
  105. else:
  106. print('OK')
  107. else:
  108. if status:
  109. print('FAILED')
  110. failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
  111. else:
  112. print('OK')
  113. else:
  114. base_path = os.path.splitext(input_path)[0]
  115. actual_output = safeReadFile(base_path + '.actual')
  116. actual_rewrite_output = safeReadFile(base_path + '.actual-rewrite')
  117. open(base_path + '.process-output', 'wt', encoding = 'utf-8').write(process_output)
  118. if status:
  119. print('parsing failed')
  120. failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
  121. else:
  122. expected_output_path = os.path.splitext(input_path)[0] + '.expected'
  123. expected_output = open(expected_output_path, 'rt', encoding = 'utf-8').read()
  124. detail = (compareOutputs(expected_output, actual_output, 'input')
  125. or compareOutputs(expected_output, actual_rewrite_output, 'rewrite'))
  126. if detail:
  127. print('FAILED')
  128. failed_tests.append((input_path, detail))
  129. else:
  130. print('OK')
  131. if failed_tests:
  132. print()
  133. print('Failure details:')
  134. for failed_test in failed_tests:
  135. print('* Test', failed_test[0])
  136. print(failed_test[1])
  137. print()
  138. print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
  139. len(failed_tests)))
  140. raise FailError(repr(failed_tests))
  141. else:
  142. print('All %d tests passed.' % len(tests))
  143. def main():
  144. from optparse import OptionParser
  145. parser = OptionParser(usage="%prog [options] <path to jsontestrunner.exe> [test case directory]")
  146. parser.add_option("--valgrind",
  147. action="store_true", dest="valgrind", default=False,
  148. help="run all the tests using valgrind to detect memory leaks")
  149. parser.add_option("-c", "--with-json-checker",
  150. action="store_true", dest="with_json_checker", default=False,
  151. help="run all the tests from the official JSONChecker test suite of json.org")
  152. parser.enable_interspersed_args()
  153. options, args = parser.parse_args()
  154. if len(args) < 1 or len(args) > 2:
  155. parser.error('Must provides at least path to jsontestrunner executable.')
  156. sys.exit(1)
  157. jsontest_executable_path = os.path.normpath(os.path.abspath(args[0]))
  158. if len(args) > 1:
  159. input_path = os.path.normpath(os.path.abspath(args[1]))
  160. else:
  161. input_path = None
  162. runAllTests(jsontest_executable_path, input_path,
  163. use_valgrind=options.valgrind,
  164. with_json_checker=options.with_json_checker,
  165. writerClass='StyledWriter')
  166. runAllTests(jsontest_executable_path, input_path,
  167. use_valgrind=options.valgrind,
  168. with_json_checker=options.with_json_checker,
  169. writerClass='StyledStreamWriter')
  170. runAllTests(jsontest_executable_path, input_path,
  171. use_valgrind=options.valgrind,
  172. with_json_checker=options.with_json_checker,
  173. writerClass='BuiltStyledStreamWriter')
  174. if __name__ == '__main__':
  175. try:
  176. main()
  177. except FailError:
  178. sys.exit(1)