1
1
from __future__ import print_function
2
+ from __future__ import unicode_literals
3
+ from io import open
4
+ from glob import glob
2
5
import sys
3
6
import os
4
7
import os .path
5
- from glob import glob
6
8
import optparse
7
9
8
10
VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
9
11
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
+ """
17
+ pipe = os .popen (cmd )
18
+ process_output = pipe .read ()
19
+ try :
20
+ # We have been using os.popen(). When we read() the result
21
+ # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
22
+ # Ugh! There must be a better way to handle this.
23
+ process_output = process_output .decode ('utf-8' )
24
+ except AttributeError :
25
+ pass # python3
26
+ status = pipe .close ()
27
+ return status , process_output
10
28
def compareOutputs ( expected , actual , message ):
11
29
expected = expected .strip ().replace ('\r ' ,'' ).split ('\n ' )
12
30
actual = actual .strip ().replace ('\r ' ,'' ).split ('\n ' )
@@ -34,7 +52,7 @@ def safeGetLine( lines, index ):
34
52
35
53
def safeReadFile ( path ):
36
54
try :
37
- return file ( path , 'rt' ).read ()
55
+ return open ( path , 'rt' , encoding = 'utf-8 ' ).read ()
38
56
except IOError as e :
39
57
return '<File "%s" is missing: %s>' % (path ,e )
40
58
@@ -54,21 +72,20 @@ def runAllTests( jsontest_executable_path, input_dir = None,
54
72
is_json_checker_test = (input_path in test_jsonchecker ) or expect_failure
55
73
print ('TESTING:' , input_path , end = ' ' )
56
74
options = is_json_checker_test and '--json-checker' or ''
57
- pipe = os . popen ( '%s%s %s "%s"' % (
75
+ cmd = '%s%s %s "%s"' % (
58
76
valgrind_path , jsontest_executable_path , options ,
59
- input_path ) )
60
- process_output = pipe .read ()
61
- status = pipe .close ()
77
+ input_path )
78
+ status , process_output = getStatusOutput (cmd )
62
79
if is_json_checker_test :
63
80
if expect_failure :
64
- if status is None :
81
+ if not status :
65
82
print ('FAILED' )
66
83
failed_tests .append ( (input_path , 'Parsing should have failed:\n %s' %
67
84
safeReadFile (input_path )) )
68
85
else :
69
86
print ('OK' )
70
87
else :
71
- if status is not None :
88
+ if status :
72
89
print ('FAILED' )
73
90
failed_tests .append ( (input_path , 'Parsing failed:\n ' + process_output ) )
74
91
else :
@@ -77,13 +94,13 @@ def runAllTests( jsontest_executable_path, input_dir = None,
77
94
base_path = os .path .splitext (input_path )[0 ]
78
95
actual_output = safeReadFile ( base_path + '.actual' )
79
96
actual_rewrite_output = safeReadFile ( base_path + '.actual-rewrite' )
80
- file (base_path + '.process-output' ,'wt' ).write ( process_output )
97
+ open (base_path + '.process-output' , 'wt' , encoding = 'utf-8 ' ).write ( process_output )
81
98
if status :
82
99
print ('parsing failed' )
83
100
failed_tests .append ( (input_path , 'Parsing failed:\n ' + process_output ) )
84
101
else :
85
102
expected_output_path = os .path .splitext (input_path )[0 ] + '.expected'
86
- expected_output = file ( expected_output_path , 'rt' ).read ()
103
+ expected_output = open ( expected_output_path , 'rt' , encoding = 'utf-8 ' ).read ()
87
104
detail = ( compareOutputs ( expected_output , actual_output , 'input' )
88
105
or compareOutputs ( expected_output , actual_rewrite_output , 'rewrite' ) )
89
106
if detail :
0 commit comments