Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
| 3 | import os.path |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 4 | from glob import glob |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 5 | |
Baptiste Lepilleur | f179a18 | 2009-11-18 22:25:34 +0000 | [diff] [blame] | 6 | RUN_JSONCHECKER = False |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 7 | |
| 8 | def compareOutputs( expected, actual, message ): |
| 9 | expected = expected.strip().replace('\r','').split('\n') |
| 10 | actual = actual.strip().replace('\r','').split('\n') |
| 11 | diff_line = 0 |
| 12 | max_line_to_compare = min( len(expected), len(actual) ) |
| 13 | for index in xrange(0,max_line_to_compare): |
| 14 | if expected[index].strip() != actual[index].strip(): |
| 15 | diff_line = index + 1 |
| 16 | break |
| 17 | if diff_line == 0 and len(expected) != len(actual): |
| 18 | diff_line = max_line_to_compare+1 |
| 19 | if diff_line == 0: |
| 20 | return None |
| 21 | def safeGetLine( lines, index ): |
| 22 | index += -1 |
| 23 | if index >= len(lines): |
| 24 | return '' |
| 25 | return lines[index].strip() |
| 26 | return """ Difference in %s at line %d: |
| 27 | Expected: '%s' |
| 28 | Actual: '%s' |
| 29 | """ % (message, diff_line, |
| 30 | safeGetLine(expected,diff_line), |
| 31 | safeGetLine(actual,diff_line) ) |
| 32 | |
| 33 | def safeReadFile( path ): |
| 34 | try: |
| 35 | return file( path, 'rt' ).read() |
| 36 | except IOError, e: |
| 37 | return '<File "%s" is missing: %s>' % (path,e) |
| 38 | |
| 39 | def runAllTests( jsontest_executable_path, input_dir = None ): |
| 40 | if not input_dir: |
| 41 | input_dir = os.getcwd() |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 42 | tests = glob( os.path.join( input_dir, '*.json' ) ) |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 43 | if RUN_JSONCHECKER: |
| 44 | test_jsonchecker = glob( os.path.join( input_dir, 'jsonchecker', '*.json' ) ) |
| 45 | else: |
| 46 | test_jsonchecker = [] |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 47 | failed_tests = [] |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 48 | for input_path in tests + test_jsonchecker: |
| 49 | is_json_checker_test = input_path in test_jsonchecker |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 50 | print 'TESTING:', input_path, |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 51 | options = is_json_checker_test and '--json-checker' or '' |
| 52 | pipe = os.popen( "%s %s %s" % (jsontest_executable_path, options, |
| 53 | input_path) ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 54 | process_output = pipe.read() |
| 55 | status = pipe.close() |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 56 | if is_json_checker_test: |
| 57 | expect_failure = os.path.basename( input_path ).startswith( 'fail' ) |
| 58 | if expect_failure: |
| 59 | if status is None: |
| 60 | print 'FAILED' |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 61 | failed_tests.append( (input_path, 'Parsing should have failed:\n%s' % |
| 62 | safeReadFile(input_path)) ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 63 | else: |
| 64 | print 'OK' |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 65 | else: |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 66 | if status is not None: |
| 67 | print 'FAILED' |
| 68 | failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) |
| 69 | else: |
| 70 | print 'OK' |
| 71 | else: |
| 72 | base_path = os.path.splitext(input_path)[0] |
| 73 | actual_output = safeReadFile( base_path + '.actual' ) |
| 74 | actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' ) |
| 75 | file(base_path + '.process-output','wt').write( process_output ) |
| 76 | if status: |
| 77 | print 'parsing failed' |
| 78 | failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) |
| 79 | else: |
| 80 | expected_output_path = os.path.splitext(input_path)[0] + '.expected' |
| 81 | expected_output = file( expected_output_path, 'rt' ).read() |
| 82 | detail = ( compareOutputs( expected_output, actual_output, 'input' ) |
| 83 | or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) ) |
| 84 | if detail: |
| 85 | print 'FAILED' |
| 86 | failed_tests.append( (input_path, detail) ) |
| 87 | else: |
| 88 | print 'OK' |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 89 | |
| 90 | if failed_tests: |
| 91 | print |
| 92 | print 'Failure details:' |
| 93 | for failed_test in failed_tests: |
| 94 | print '* Test', failed_test[0] |
| 95 | print failed_test[1] |
| 96 | print |
| 97 | print 'Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests), |
| 98 | len(failed_tests) ) |
| 99 | return 1 |
| 100 | else: |
| 101 | print 'All %d tests passed.' % len(tests) |
| 102 | return 0 |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | if len(sys.argv) < 1 or len(sys.argv) > 2: |
| 106 | print "Usage: %s jsontest-executable-path [input-testcase-directory]" % sys.argv[0] |
| 107 | sys.exit( 1 ) |
| 108 | |
| 109 | jsontest_executable_path = os.path.normpath( os.path.abspath( sys.argv[1] ) ) |
| 110 | if len(sys.argv) > 2: |
| 111 | input_path = os.path.normpath( os.path.abspath( sys.argv[2] ) ) |
| 112 | else: |
| 113 | input_path = None |
| 114 | status = runAllTests( jsontest_executable_path, input_path ) |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 115 | sys.exit( status ) |