18
18
from unittest import TestResult, TestCase
22
from subprocess import call, Popen, PIPE
23
except ImportError, e:
24
sys.stderr.write("testbzr: sorry, this test suite requires the subprocess module\n"
25
"this is shipped with python2.4 and available separately for 2.3\n")
29
class CommandFailed(Exception):
21
33
class TestBase(TestCase):
22
34
"""Base class for bzr test cases.
24
36
Just defines some useful helper functions; doesn't actually test
27
# TODO: Special methods to invoke bzr
29
def runcmd(self, cmd, expected=0):
30
self.log('$ ' + ' '.join(cmd))
31
from os import spawnvp, P_WAIT
32
rc = spawnvp(P_WAIT, cmd[0], cmd)
34
self.fail("command %r returned status %d" % (cmd, rc))
37
def backtick(self, cmd):
38
"""Run a command and return its output"""
40
self.log('$ ' + ' '.join(cmd))
52
self.fail("command %r returned status %d" % (cmd, rc))
40
# TODO: Special methods to invoke bzr, so that we can run it
41
# through a specified Python intepreter
43
OVERRIDE_PYTHON = None # to run with alternative python 'python'
47
def formcmd(self, cmd):
48
if isinstance(cmd, basestring):
53
if self.OVERRIDE_PYTHON:
54
cmd.insert(0, self.OVERRIDE_PYTHON)
56
self.log('$ %r' % cmd)
61
def runcmd(self, cmd, retcode=0):
62
"""Run one command and check the return code.
64
Returns a tuple of (stdout,stderr) strings.
66
If a single string is based, it is split into words.
67
For commands that are not simple space-separated words, please
68
pass a list instead."""
69
cmd = self.formcmd(cmd)
71
self.log('$ ' + ' '.join(cmd))
72
actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
74
if retcode != actual_retcode:
75
raise CommandFailed("test failed: %r returned %d, expected %d"
76
% (cmd, actual_retcode, retcode))
57
80
def log(self, msg):
58
81
"""Log a message to a progress file"""