Skip to content

utils: port round-trip-syntax-test to windows #21884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 33 additions & 42 deletions utils/round-trip-syntax-test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import print_function, unicode_literals

import argparse
import difflib
import logging
import os
import subprocess
Expand Down Expand Up @@ -36,62 +37,52 @@ class RoundTripTask(object):
return [self.swift_syntax_test, self.action,
'-input-source-filename', self.input_filename]

@property
def diff_command(self):
return ['/usr/bin/diff', '-u', self.input_filename, '-']

def diff(self):
logging.debug(' '.join(self.diff_command))
diff = subprocess.Popen(self.diff_command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = diff.communicate(self.stdout)
if diff.returncode != 0:
return stdout
assert stdout == ''
assert stderr == ''
return None

def run(self):
command = self.test_command
logging.debug(' '.join(command))
self.output_file = tempfile.NamedTemporaryFile('w')
self.stderr_file = tempfile.NamedTemporaryFile('w')
self.output_file = tempfile.NamedTemporaryFile('w', delete=False)
self.stderr_file = tempfile.NamedTemporaryFile('w', delete=False)

process = subprocess.Popen(command, stdout=self.output_file,
stderr=self.stderr_file)
process.wait()
self.returncode = process.returncode

self.output_file.close()
self.stderr_file.close()

with open(self.output_file.name, 'r') as stdout_in:
self.stdout = stdout_in.read()
with open(self.stderr_file.name, 'r') as stderr_in:
self.stderr = stderr_in.read()

self.output_file.flush()
self.stderr_file.flush()

try:
if self.returncode != 0:
if self.skip_bad_syntax:
logging.warning('---===WARNING===--- Lex/parse had error'
' diagnostics, so not diffing. Skipping'
' this file due to -skip-bad-syntax.')
logging.error(' '.join(command))
return None
else:
logging.error('---===ERROR===--- Lex/parse had error'
' diagnostics, so not diffing.')
logging.error(' '.join(command))
logging.error(self.stdout)
logging.error(self.stderr)
raise RuntimeError()
finally:
self.output_file.close()
self.stderr_file.close()

diff = self.diff()
return diff
os.remove(self.output_file.name)
os.remove(self.stderr_file.name)

if self.returncode != 0:
if self.skip_bad_syntax:
logging.warning('---===WARNING===--- Lex/parse had error'
' diagnostics, so not diffing. Skipping'
' this file due to -skip-bad-syntax.')
logging.error(' '.join(command))
return None
else:
logging.error('---===ERROR===--- Lex/parse had error'
' diagnostics, so not diffing.')
logging.error(' '.join(command))
logging.error(self.stdout)
logging.error(self.stderr)
raise RuntimeError()

contents = ''.join(map(lambda l: l.decode('utf-8', errors='replace'),
open(self.input_filename).readlines()))
lines = difflib.unified_diff(contents,
self.stdout.decode('utf-8',
errors='replace'),
fromfile=self.input_filename,
tofile='-')
diff = '\n'.join(line for line in lines)
return diff if diff else None


def swift_files_in_dir(d):
Expand Down