68
68
OVERRIDE_PYTHON = None # to run with alternative python 'python'
71
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
72
a_callable=None, *args, **kwargs):
73
"""Call callable with redirected std io pipes.
75
Returns the return code."""
76
from StringIO import StringIO
77
if not callable(a_callable):
78
raise ValueError("a_callable must be callable.")
82
stdout = self.TEST_LOG
84
stderr = self.TEST_LOG
85
real_stdin = sys.stdin
86
real_stdout = sys.stdout
87
real_stderr = sys.stderr
93
result = a_callable(*args, **kwargs)
95
sys.stdout = real_stdout
96
sys.stderr = real_stderr
97
sys.stdin = real_stdin
72
101
super(TestCase, self).setUp()
73
102
# setup a temporary log for the test
77
104
self.TEST_LOG = tempfile.NamedTemporaryFile(mode='wt', bufsize=0)
78
# save stdout & stderr so there's no leakage from code-under-test
79
self.real_stdout = sys.stdout
80
self.real_stderr = sys.stderr
81
sys.stdout = sys.stderr = self.TEST_LOG
82
105
self.log("%s setup" % self.id())
84
107
def tearDown(self):
85
sys.stdout = self.real_stdout
86
sys.stderr = self.real_stderr
87
108
self.log("%s teardown" % self.id())
89
110
super(TestCase, self).tearDown()
210
231
except ImportError, e:
211
232
_need_subprocess()
214
234
cmd = self.formcmd(cmd)
215
235
child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
216
236
outd, errd = child.communicate()
218
238
actual_retcode = child.wait()
220
239
outd = outd.replace('\r', '')
222
240
if retcode != actual_retcode:
223
241
raise CommandFailed("test failed: %r returned %d, expected %d"
224
242
% (cmd, actual_retcode, retcode))
230
245
def build_tree(self, shape):
231
246
"""Build a test tree according to a pattern.