Skip to content

Commit 230bee4

Browse files
authored
Merge pull request #21884 from compnerd/round-trip-flight
utils: port round-trip-syntax-test to windows
2 parents d2095a8 + 7d07ae4 commit 230bee4

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

utils/round-trip-syntax-test

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

55
import argparse
6+
import difflib
67
import logging
78
import os
89
import subprocess
@@ -36,62 +37,52 @@ class RoundTripTask(object):
3637
return [self.swift_syntax_test, self.action,
3738
'-input-source-filename', self.input_filename]
3839

39-
@property
40-
def diff_command(self):
41-
return ['/usr/bin/diff', '-u', self.input_filename, '-']
42-
43-
def diff(self):
44-
logging.debug(' '.join(self.diff_command))
45-
diff = subprocess.Popen(self.diff_command, stdin=subprocess.PIPE,
46-
stdout=subprocess.PIPE,
47-
stderr=subprocess.PIPE)
48-
stdout, stderr = diff.communicate(self.stdout)
49-
if diff.returncode != 0:
50-
return stdout
51-
assert stdout == ''
52-
assert stderr == ''
53-
return None
54-
5540
def run(self):
5641
command = self.test_command
5742
logging.debug(' '.join(command))
58-
self.output_file = tempfile.NamedTemporaryFile('w')
59-
self.stderr_file = tempfile.NamedTemporaryFile('w')
43+
self.output_file = tempfile.NamedTemporaryFile('w', delete=False)
44+
self.stderr_file = tempfile.NamedTemporaryFile('w', delete=False)
6045

6146
process = subprocess.Popen(command, stdout=self.output_file,
6247
stderr=self.stderr_file)
6348
process.wait()
6449
self.returncode = process.returncode
6550

51+
self.output_file.close()
52+
self.stderr_file.close()
53+
6654
with open(self.output_file.name, 'r') as stdout_in:
6755
self.stdout = stdout_in.read()
6856
with open(self.stderr_file.name, 'r') as stderr_in:
6957
self.stderr = stderr_in.read()
7058

71-
self.output_file.flush()
72-
self.stderr_file.flush()
73-
74-
try:
75-
if self.returncode != 0:
76-
if self.skip_bad_syntax:
77-
logging.warning('---===WARNING===--- Lex/parse had error'
78-
' diagnostics, so not diffing. Skipping'
79-
' this file due to -skip-bad-syntax.')
80-
logging.error(' '.join(command))
81-
return None
82-
else:
83-
logging.error('---===ERROR===--- Lex/parse had error'
84-
' diagnostics, so not diffing.')
85-
logging.error(' '.join(command))
86-
logging.error(self.stdout)
87-
logging.error(self.stderr)
88-
raise RuntimeError()
89-
finally:
90-
self.output_file.close()
91-
self.stderr_file.close()
92-
93-
diff = self.diff()
94-
return diff
59+
os.remove(self.output_file.name)
60+
os.remove(self.stderr_file.name)
61+
62+
if self.returncode != 0:
63+
if self.skip_bad_syntax:
64+
logging.warning('---===WARNING===--- Lex/parse had error'
65+
' diagnostics, so not diffing. Skipping'
66+
' this file due to -skip-bad-syntax.')
67+
logging.error(' '.join(command))
68+
return None
69+
else:
70+
logging.error('---===ERROR===--- Lex/parse had error'
71+
' diagnostics, so not diffing.')
72+
logging.error(' '.join(command))
73+
logging.error(self.stdout)
74+
logging.error(self.stderr)
75+
raise RuntimeError()
76+
77+
contents = ''.join(map(lambda l: l.decode('utf-8', errors='replace'),
78+
open(self.input_filename).readlines()))
79+
lines = difflib.unified_diff(contents,
80+
self.stdout.decode('utf-8',
81+
errors='replace'),
82+
fromfile=self.input_filename,
83+
tofile='-')
84+
diff = '\n'.join(line for line in lines)
85+
return diff if diff else None
9586

9687

9788
def swift_files_in_dir(d):

0 commit comments

Comments
 (0)