Skip to content

Commit b93c96f

Browse files
committed
More useful reporting of test errors with futurize/pasteurize scripts
1 parent 713c70e commit b93c96f

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/future/tests/base.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import os
23
import tempfile
34
import unittest
@@ -12,7 +13,7 @@
1213
import unittest2 as unittest
1314

1415
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
1617

1718

1819
def reformat_code(code):
@@ -76,6 +77,25 @@ def mymin(numbers):
7677
return '\n'.join(new_lines)
7778

7879

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+
7999
class CodeHandler(unittest.TestCase):
80100
"""
81101
Handy mixin for test classes for writing / reading / futurizing /
@@ -276,9 +296,21 @@ def _futurize_test_script(self, filename='mytestscript.py', stages=(1, 2),
276296
params.append('--conservative')
277297
# No extra params needed
278298

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)
282314
return output
283315

284316
def _run_test_script(self, filename='mytestscript.py',

0 commit comments

Comments
 (0)