|
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') |
@@ -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: |
|
0 commit comments