|
| 1 | +from __future__ import print_function |
1 | 2 | import os |
2 | 3 | import tempfile |
3 | 4 | import unittest |
|
12 | 13 | import unittest2 as unittest |
13 | 14 |
|
14 | 15 | from future.utils import bind_method, PY26, PY3, PY2 |
15 | | -from future.moves.subprocess import check_output, STDOUT |
| 16 | +from future.moves.subprocess import check_output, STDOUT, CalledProcessError |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def reformat_code(code): |
@@ -76,6 +77,25 @@ def mymin(numbers): |
76 | 77 | return '\n'.join(new_lines) |
77 | 78 |
|
78 | 79 |
|
| 80 | +class FuturizeError(CalledProcessError): |
| 81 | + """This exception is raised when a process run by check_call() or |
| 82 | + check_output() returns a non-zero exit status. |
| 83 | + The exit status will be stored in the returncode attribute; |
| 84 | + check_output() will also store the output in the output attribute. |
| 85 | + """ |
| 86 | + def __init__(self, msg, returncode, cmd, output=None): |
| 87 | + self.msg = msg |
| 88 | + self.returncode = returncode |
| 89 | + self.cmd = cmd |
| 90 | + self.output = output |
| 91 | + |
| 92 | + def __str__(self): |
| 93 | + return ("Command '%s' failed with exit status %d\nMessage: %s" |
| 94 | + % (self.cmd, self.returncode, self.msg)) |
| 95 | + |
| 96 | +class PasteurizeError(FuturizeError): |
| 97 | + pass |
| 98 | + |
79 | 99 | class CodeHandler(unittest.TestCase): |
80 | 100 | """ |
81 | 101 | Handy mixin for test classes for writing / reading / futurizing / |
@@ -276,9 +296,21 @@ def _futurize_test_script(self, filename='mytestscript.py', stages=(1, 2), |
276 | 296 | params.append('--conservative') |
277 | 297 | # No extra params needed |
278 | 298 |
|
279 | | - output = check_output([sys.executable, script] + params + |
280 | | - ['-w', self.tempdir + filename], |
281 | | - stderr=STDOUT) |
| 299 | + # Absolute file path: |
| 300 | + fn = self.tempdir + filename |
| 301 | + call_args = [sys.executable, script] + params + ['-w', fn] |
| 302 | + try: |
| 303 | + output = check_output(call_args, stderr=STDOUT) |
| 304 | + except CalledProcessError as e: |
| 305 | + msg = ('Error running the command %s\n%s\nContents of file %s:\n\n%s' % |
| 306 | + (' '.join(call_args), |
| 307 | + 'PYTHONPATH=%s' % os.environ.get('PYTHONPATH'), |
| 308 | + fn, |
| 309 | + '----\n%s\n----' % open(fn).read(), |
| 310 | + ) |
| 311 | + ) |
| 312 | + ErrorClass = (FuturizeError if 'futurize' in script else PasteurizeError) |
| 313 | + raise ErrorClass(msg, e.returncode, e.cmd) |
282 | 314 | return output |
283 | 315 |
|
284 | 316 | def _run_test_script(self, filename='mytestscript.py', |
|
0 commit comments