|
3 | 3 | from __future__ import print_function, unicode_literals
|
4 | 4 |
|
5 | 5 | import argparse
|
| 6 | +import difflib |
6 | 7 | import logging
|
7 | 8 | import os
|
8 | 9 | import subprocess
|
@@ -36,62 +37,52 @@ class RoundTripTask(object):
|
36 | 37 | return [self.swift_syntax_test, self.action,
|
37 | 38 | '-input-source-filename', self.input_filename]
|
38 | 39 |
|
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 |
| - |
55 | 40 | def run(self):
|
56 | 41 | command = self.test_command
|
57 | 42 | 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) |
60 | 45 |
|
61 | 46 | process = subprocess.Popen(command, stdout=self.output_file,
|
62 | 47 | stderr=self.stderr_file)
|
63 | 48 | process.wait()
|
64 | 49 | self.returncode = process.returncode
|
65 | 50 |
|
| 51 | + self.output_file.close() |
| 52 | + self.stderr_file.close() |
| 53 | + |
66 | 54 | with open(self.output_file.name, 'r') as stdout_in:
|
67 | 55 | self.stdout = stdout_in.read()
|
68 | 56 | with open(self.stderr_file.name, 'r') as stderr_in:
|
69 | 57 | self.stderr = stderr_in.read()
|
70 | 58 |
|
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 |
95 | 86 |
|
96 | 87 |
|
97 | 88 | def swift_files_in_dir(d):
|
|
0 commit comments