89
89
super(TestCase, self).tearDown()
91
def formcmd(self, cmd):
92
if isinstance(cmd, basestring):
97
if self.OVERRIDE_PYTHON:
98
cmd.insert(0, self.OVERRIDE_PYTHON)
100
self.log('$ %r' % cmd)
105
def runcmd(self, cmd, retcode=0):
106
"""Run one command and check the return code.
108
Returns a tuple of (stdout,stderr) strings.
110
If a single string is based, it is split into words.
111
For commands that are not simple space-separated words, please
112
pass a list instead."""
115
from subprocess import call
116
except ImportError, e:
121
cmd = self.formcmd(cmd)
123
self.log('$ ' + ' '.join(cmd))
124
actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
126
if retcode != actual_retcode:
127
raise CommandFailed("test failed: %r returned %d, expected %d"
128
% (cmd, actual_retcode, retcode))
131
def backtick(self, cmd, retcode=0):
132
"""Run a command and return its output"""
135
from subprocess import Popen, PIPE
136
except ImportError, e:
140
cmd = self.formcmd(cmd)
141
child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
142
outd, errd = child.communicate()
144
actual_retcode = child.wait()
146
outd = outd.replace('\r', '')
148
if retcode != actual_retcode:
149
raise CommandFailed("test failed: %r returned %d, expected %d"
150
% (cmd, actual_retcode, retcode))
156
def build_tree(self, shape):
157
"""Build a test tree according to a pattern.
159
shape is a sequence of file specifications. If the final
160
character is '/', a directory is created.
162
This doesn't add anything to a branch.
164
# XXX: It's OK to just create them using forward slashes on windows?
167
assert isinstance(name, basestring)
172
print >>f, "contents of", name
176
91
def log(self, msg):
177
92
"""Log a message to a progress file"""
178
93
print >>self.TEST_LOG, msg
181
95
def check_inventory_shape(self, inv, shape):
183
97
Compare an inventory to a list of expected names.
220
class InTempDir(TestCase):
221
"""Base class for tests that use disk resources - i.e to run in a temporary
126
class FunctionalTestCase(TestCase):
127
"""Base class for tests that perform function testing - running bzr,
128
using files on disk, and similar activities.
130
InTempDir is an old alias for FunctionalTestCase.
226
134
_TEST_NAME = 'test'
136
def check_file_contents(self, filename, expect):
137
self.log("check contents of file %s" % filename)
138
contents = file(filename, 'r').read()
139
if contents != expect:
140
self.log("expected: %r" % expect)
141
self.log("actually: %r" % contents)
142
self.fail("contents of %s not as expected")
228
144
def _make_test_root(self):
233
if InTempDir.TEST_ROOT is not None:
149
if FunctionalTestCase.TEST_ROOT is not None:
235
InTempDir.TEST_ROOT = os.path.abspath(
151
FunctionalTestCase.TEST_ROOT = os.path.abspath(
236
152
tempfile.mkdtemp(suffix='.tmp',
237
153
prefix=self._TEST_NAME + '-',
240
# print '%-30s %s\n' % ('running tests in', TestCase.TEST_ROOT)
242
os.chdir(InTempDir.TEST_ROOT)
243
156
# make a fake bzr directory there to prevent any tests propagating
244
157
# up onto the source directory's real branch
245
os.mkdir(os.path.join(InTempDir.TEST_ROOT, '.bzr'))
158
os.mkdir(os.path.join(FunctionalTestCase.TEST_ROOT, '.bzr'))
248
super(InTempDir, self).setUp()
161
super(FunctionalTestCase, self).setUp()
250
163
self._make_test_root()
164
self._currentdir = os.getcwdu()
251
165
self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
252
166
os.mkdir(self.test_dir)
253
167
os.chdir(self.test_dir)
255
169
def tearDown(self):
257
os.chdir(self.TEST_ROOT)
258
super(InTempDir, self).tearDown()
171
os.chdir(self._currentdir)
172
super(FunctionalTestCase, self).tearDown()
174
def formcmd(self, cmd):
175
if isinstance(cmd, basestring):
179
cmd[0] = self.BZRPATH
180
if self.OVERRIDE_PYTHON:
181
cmd.insert(0, self.OVERRIDE_PYTHON)
183
self.log('$ %r' % cmd)
188
def runcmd(self, cmd, retcode=0):
189
"""Run one command and check the return code.
191
Returns a tuple of (stdout,stderr) strings.
193
If a single string is based, it is split into words.
194
For commands that are not simple space-separated words, please
195
pass a list instead."""
198
from subprocess import call
199
except ImportError, e:
204
cmd = self.formcmd(cmd)
206
self.log('$ ' + ' '.join(cmd))
207
actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
209
if retcode != actual_retcode:
210
raise CommandFailed("test failed: %r returned %d, expected %d"
211
% (cmd, actual_retcode, retcode))
214
def backtick(self, cmd, retcode=0):
215
"""Run a command and return its output"""
218
from subprocess import Popen, PIPE
219
except ImportError, e:
223
cmd = self.formcmd(cmd)
224
child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
225
outd, errd = child.communicate()
227
actual_retcode = child.wait()
229
outd = outd.replace('\r', '')
231
if retcode != actual_retcode:
232
raise CommandFailed("test failed: %r returned %d, expected %d"
233
% (cmd, actual_retcode, retcode))
239
def build_tree(self, shape):
240
"""Build a test tree according to a pattern.
242
shape is a sequence of file specifications. If the final
243
character is '/', a directory is created.
245
This doesn't add anything to a branch.
247
# XXX: It's OK to just create them using forward slashes on windows?
250
assert isinstance(name, basestring)
255
print >>f, "contents of", name
258
InTempDir = FunctionalTestCase
261
261
class _MyResult(unittest._TextTestResult):