Skip to content

Commit 2fad025

Browse files
committed
test: make test_util more Python 3 friendly
This uses `io.open` to allow `test_util` to open with the encoding and newline handling across python 2 and python 3. With this change, the test suite failures are within the single digits locally.
1 parent a11c06c commit 2fad025

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

utils/incrparse/test_util.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import print_function
44

55
import argparse
6+
import io
67
import os
78
import re
89
import subprocess
@@ -21,6 +22,7 @@ def escapeCmdArg(arg):
2122

2223

2324
def run_command(cmd):
25+
cmd = list(map(lambda s: s.encode('utf-8'), cmd))
2426
print(' '.join([escapeCmdArg(arg) for arg in cmd]))
2527
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
2628

@@ -56,7 +58,7 @@ def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args,
5658
# Compute the -incremental-edit argument for swift-syntax-test
5759
column = len(pre_edit_line) + len(prefix) + 1
5860
edit_arg = '%d:%d-%d:%d=%s' % \
59-
(line_no, column, line_no, column + len(pre_edit),
61+
(line_no, column, line_no, column + len(pre_edit.encode('utf-8')),
6062
post_edit)
6163
incremental_edit_args.append('-incremental-edit')
6264
incremental_edit_args.append(edit_arg)
@@ -102,14 +104,14 @@ def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args,
102104
# Nothing more to do
103105
line = ''
104106

105-
return (pre_edit_line, post_edit_line, current_reparse_start)
107+
return (pre_edit_line.encode('utf-8'), post_edit_line.encode('utf-8'), current_reparse_start)
106108

107109

108110
def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
109111
incremental_edit_args, reparse_args):
110-
with open(test_file, mode='r') as test_file_handle, \
111-
open(pre_edit_file, mode='w+b') as pre_edit_file_handle, \
112-
open(post_edit_file, mode='w+b') as post_edit_file_handle:
112+
with io.open(test_file, mode='r', encoding='utf-8', newline='\n') as test_file_handle, \
113+
io.open(pre_edit_file, mode='w+', encoding='utf-8', newline='\n') as pre_edit_file_handle, \
114+
io.open(post_edit_file, mode='w+', encoding='utf-8', newline='\n') as post_edit_file_handle:
113115

114116
current_reparse_start = None
115117

@@ -121,8 +123,8 @@ def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
121123
(pre_edit_line, post_edit_line, current_reparse_start) = \
122124
parseLineRes
123125

124-
pre_edit_file_handle.write(pre_edit_line)
125-
post_edit_file_handle.write(post_edit_line)
126+
pre_edit_file_handle.write(pre_edit_line.decode('utf-8'))
127+
post_edit_file_handle.write(post_edit_line.decode('utf-8'))
126128

127129
line_no += 1
128130

@@ -231,7 +233,7 @@ def serializeIncrParseMarkupFile(test_file, test_case, mode,
231233
if print_visual_reuse_info:
232234
print(output)
233235
except subprocess.CalledProcessError as e:
234-
raise TestFailedError(e.output)
236+
raise TestFailedError(e.output.decode('utf-8'))
235237

236238

237239
def main():

0 commit comments

Comments
 (0)