Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 1 | from __future__ import print_function |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 2 | from __future__ import unicode_literals |
| 3 | from io import open |
| 4 | from glob import glob |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 5 | import sys |
| 6 | import os |
Christopher Dunn | 4ca9d25 | 2015-01-09 22:28:20 -0600 | [diff] [blame] | 7 | import os.path |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 8 | import optparse |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 9 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 10 | VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes ' |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 11 | |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 12 | def 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 Dunn | ac6bbbc | 2015-01-20 11:36:05 -0600 | [diff] [blame^] | 17 | print(cmd, file=sys.stderr) |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 18 | 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 Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 29 | def compareOutputs( expected, actual, message ): |
| 30 | expected = expected.strip().replace('\r','').split('\n') |
| 31 | actual = actual.strip().replace('\r','').split('\n') |
| 32 | diff_line = 0 |
| 33 | max_line_to_compare = min( len(expected), len(actual) ) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 34 | for index in range(0,max_line_to_compare): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 35 | 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 |
| 42 | def safeGetLine( lines, index ): |
| 43 | 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), |
| 52 | safeGetLine(actual,diff_line) ) |
| 53 | |
| 54 | def safeReadFile( path ): |
| 55 | try: |
datadiode | 01aee4a | 2015-01-11 10:39:24 +0100 | [diff] [blame] | 56 | return open( path, 'rt', encoding = 'utf-8' ).read() |
Christopher Dunn | 9aa6144 | 2014-11-19 23:10:02 -0600 | [diff] [blame] | 57 | except IOError as e: |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 58 | return '<File "%s" is missing: %s>' % (path,e) |
| 59 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 60 | def runAllTests( jsontest_executable_path, input_dir = None, |
Baptiste Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 61 | use_valgrind=False, with_json_checker=False ): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 62 | if not input_dir: |
Baptiste Lepilleur | 7dec64f | 2009-11-21 18:20:25 +0000 | [diff] [blame] | 63 | input_dir = os.path.join( os.getcwd(), 'data' ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 64 | tests = glob( os.path.join( input_dir, '*.json' ) ) |
Baptiste Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 65 | if with_json_checker: |
| 66 | test_jsonchecker = glob( os.path.join( input_dir, '../jsonchecker', '*.json' ) ) |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 67 | else: |
| 68 | test_jsonchecker = [] |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 69 | failed_tests = [] |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 70 | valgrind_path = use_valgrind and VALGRIND_CMD or '' |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 71 | for input_path in tests + test_jsonchecker: |
Baptiste Lepilleur | 9c98f22 | 2011-05-01 15:40:47 +0000 | [diff] [blame] | 72 | expect_failure = os.path.basename( input_path ).startswith( 'fail' ) |
| 73 | is_json_checker_test = (input_path in test_jsonchecker) or expect_failure |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 74 | print('TESTING:', input_path, end=' ') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 75 | options = is_json_checker_test and '--json-checker' or '' |
Christopher Dunn | 26c5286 | 2015-01-23 11:53:16 -0600 | [diff] [blame] | 76 | options += ' --json-writer StyledWriter' |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 77 | cmd = '%s%s %s "%s"' % ( |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 78 | valgrind_path, jsontest_executable_path, options, |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 79 | input_path) |
| 80 | status, process_output = getStatusOutput(cmd) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 81 | if is_json_checker_test: |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 82 | if expect_failure: |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 83 | if not status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 84 | print('FAILED') |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 85 | failed_tests.append( (input_path, 'Parsing should have failed:\n%s' % |
| 86 | safeReadFile(input_path)) ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 87 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 88 | print('OK') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 89 | else: |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 90 | if status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 91 | print('FAILED') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 92 | failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) |
| 93 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 94 | print('OK') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 95 | else: |
| 96 | base_path = os.path.splitext(input_path)[0] |
| 97 | actual_output = safeReadFile( base_path + '.actual' ) |
| 98 | actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' ) |
datadiode | 01aee4a | 2015-01-11 10:39:24 +0100 | [diff] [blame] | 99 | open(base_path + '.process-output', 'wt', encoding = 'utf-8').write( process_output ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 100 | if status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 101 | print('parsing failed') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 102 | failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) |
| 103 | else: |
| 104 | expected_output_path = os.path.splitext(input_path)[0] + '.expected' |
datadiode | 01aee4a | 2015-01-11 10:39:24 +0100 | [diff] [blame] | 105 | expected_output = open( expected_output_path, 'rt', encoding = 'utf-8' ).read() |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 106 | detail = ( compareOutputs( expected_output, actual_output, 'input' ) |
| 107 | or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) ) |
| 108 | if detail: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 109 | print('FAILED') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 110 | failed_tests.append( (input_path, detail) ) |
| 111 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 112 | print('OK') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 113 | |
| 114 | if failed_tests: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 115 | print() |
| 116 | print('Failure details:') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 117 | for failed_test in failed_tests: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 118 | 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), |
| 122 | len(failed_tests) )) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 123 | return 1 |
| 124 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 125 | print('All %d tests passed.' % len(tests)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 126 | return 0 |
| 127 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 128 | def main(): |
| 129 | from optparse import OptionParser |
| 130 | parser = OptionParser( usage="%prog [options] <path to jsontestrunner.exe> [test case directory]" ) |
| 131 | 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 Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 134 | 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 Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 137 | parser.enable_interspersed_args() |
| 138 | options, args = parser.parse_args() |
| 139 | |
| 140 | if len(args) < 1 or len(args) > 2: |
Baptiste Lepilleur | 45c499d | 2009-11-21 18:07:09 +0000 | [diff] [blame] | 141 | parser.error( 'Must provides at least path to jsontestrunner executable.' ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 142 | sys.exit( 1 ) |
| 143 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 144 | jsontest_executable_path = os.path.normpath( os.path.abspath( args[0] ) ) |
| 145 | if len(args) > 1: |
| 146 | input_path = os.path.normpath( os.path.abspath( args[1] ) ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 147 | else: |
| 148 | input_path = None |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 149 | status = runAllTests( jsontest_executable_path, input_path, |
Baptiste Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 150 | use_valgrind=options.valgrind, with_json_checker=options.with_json_checker ) |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 151 | sys.exit( status ) |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | main() |