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