From 01aee4a0dc481aa40ed5d8d0532eea7daec43441 Mon Sep 17 00:00:00 2001 From: datadiode Date: Sun, 11 Jan 2015 10:39:24 +0100 Subject: [PATCH 1/3] Fix Python test scripts for Python 3 and Windows --- test/runjsontests.py | 6 +++--- test/rununittests.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/runjsontests.py b/test/runjsontests.py index 9422d57..62e5600 100644 --- a/test/runjsontests.py +++ b/test/runjsontests.py @@ -34,7 +34,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) @@ -77,13 +77,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..90b7bb9 100644 --- a/test/rununittests.py +++ b/test/rununittests.py @@ -31,7 +31,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=' ') From cd140b5141c457818c86bf84a8ced696374896af Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Fri, 16 Jan 2015 13:44:27 -0600 Subject: [PATCH 2/3] py2 and py3 --- test/runjsontests.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/test/runjsontests.py b/test/runjsontests.py index 62e5600..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') @@ -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: From 4bc311503cad5596c8d129db13f1c72acb47b623 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Fri, 16 Jan 2015 14:48:06 -0600 Subject: [PATCH 3/3] just in case --- test/rununittests.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/rununittests.py b/test/rununittests.py index 90b7bb9..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