diff --git a/test/runjsontests.py b/test/runjsontests.py index 9422d57..7243063 100644 --- a/test/runjsontests.py +++ b/test/runjsontests.py @@ -1,12 +1,30 @@ from __future__ import print_function +from __future__ import unicode_literals +from io import open +from glob import glob import sys import os import os.path -from glob import glob import optparse VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes ' +def getStatusOutput(cmd): + """ + Return int, unicode (for both Python 2 and 3). + Note: os.popen().close() would return None for 0. + """ + pipe = os.popen(cmd) + process_output = pipe.read() + try: + # We have been using os.popen(). When we read() the result + # we get 'str' (bytes) in py2, and 'str' (unicode) in py3. + # Ugh! There must be a better way to handle this. + process_output = process_output.decode('utf-8') + except AttributeError: + pass # python3 + status = pipe.close() + return status, process_output def compareOutputs( expected, actual, message ): expected = expected.strip().replace('\r','').split('\n') actual = actual.strip().replace('\r','').split('\n') @@ -34,7 +52,7 @@ def compareOutputs( expected, actual, message ): def safeReadFile( path ): try: - return file( path, 'rt' ).read() + return open( path, 'rt', encoding = 'utf-8' ).read() except IOError as e: return '' % (path,e) @@ -54,21 +72,20 @@ def runAllTests( jsontest_executable_path, input_dir = None, is_json_checker_test = (input_path in test_jsonchecker) or expect_failure print('TESTING:', input_path, end=' ') options = is_json_checker_test and '--json-checker' or '' - pipe = os.popen( '%s%s %s "%s"' % ( + cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options, - input_path) ) - process_output = pipe.read() - status = pipe.close() + input_path) + status, process_output = getStatusOutput(cmd) if is_json_checker_test: if expect_failure: - if status is None: + if not status: print('FAILED') failed_tests.append( (input_path, 'Parsing should have failed:\n%s' % safeReadFile(input_path)) ) else: print('OK') else: - if status is not None: + if status: print('FAILED') failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) else: @@ -77,13 +94,13 @@ def runAllTests( jsontest_executable_path, input_dir = None, base_path = os.path.splitext(input_path)[0] actual_output = safeReadFile( base_path + '.actual' ) actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' ) - file(base_path + '.process-output','wt').write( process_output ) + open(base_path + '.process-output', 'wt', encoding = 'utf-8').write( process_output ) if status: print('parsing failed') failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) else: expected_output_path = os.path.splitext(input_path)[0] + '.expected' - expected_output = file( expected_output_path, 'rt' ).read() + expected_output = open( expected_output_path, 'rt', encoding = 'utf-8' ).read() detail = ( compareOutputs( expected_output, actual_output, 'input' ) or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) ) if detail: diff --git a/test/rununittests.py b/test/rununittests.py index 6279f80..54c4da4 100644 --- a/test/rununittests.py +++ b/test/rununittests.py @@ -1,4 +1,6 @@ from __future__ import print_function +from __future__ import unicode_literals +from io import open from glob import glob import sys import os @@ -19,7 +21,11 @@ class TestProxy(object): else: cmd = [] cmd.extend( [self.test_exe_path, '--test-auto'] + options ) - process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) + try: + process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) + except: + print(cmd) + raise stdout = process.communicate()[0] if process.returncode: return False, stdout @@ -31,7 +37,7 @@ def runAllTests( exe_path, use_valgrind=False ): if not status: print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr) return 1 - test_names = [name.strip() for name in test_names.strip().split('\n')] + test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')] failures = [] for name in test_names: print('TESTING %s:' % name, end=' ')