88
90
"""Return as a string the log for this test"""
89
91
return open(self._log_file_name).read()
94
def capture(self, cmd):
95
"""Shortcut that splits cmd into words, runs, and returns stdout"""
96
return self.run_bzr_captured(cmd.split())[0]
98
def run_bzr_captured(self, argv, retcode=0):
99
"""Invoke bzr and return (result, stdout, stderr).
101
Useful for code that wants to check the contents of the
102
output, the way error messages are presented, etc.
104
This should be the main method for tests that want to exercise the
105
overall behavior of the bzr application (rather than a unit test
106
or a functional test of the library.)
108
Much of the old code runs bzr by forking a new copy of Python, but
109
that is slower, harder to debug, and generally not necessary.
111
This runs bzr through the interface that catches and reports
112
errors, and with logging set to something approximating the
113
default, so that error reporting can be checked.
115
argv -- arguments to invoke bzr
116
retcode -- expected return code, or None for don't-care.
120
self.log('run bzr: %s', ' '.join(argv))
121
handler = logging.StreamHandler(stderr)
122
handler.setFormatter(bzrlib.trace.QuietFormatter())
123
handler.setLevel(logging.INFO)
124
logger = logging.getLogger('')
125
logger.addHandler(handler)
127
result = self.apply_redirected(None, stdout, stderr,
128
bzrlib.commands.run_bzr_catch_errors,
131
logger.removeHandler(handler)
132
out = stdout.getvalue()
133
err = stderr.getvalue()
135
self.log('output:\n%s', out)
137
self.log('errors:\n%s', err)
138
if retcode is not None:
139
self.assertEquals(result, retcode)
91
142
def run_bzr(self, *args, **kwargs):
92
143
"""Invoke bzr, as if it were run from the command line.
95
146
overall behavior of the bzr application (rather than a unit test
96
147
or a functional test of the library.)
98
Much of the old code runs bzr by forking a new copy of Python, but
99
that is slower, harder to debug, and generally not necessary.
149
This sends the stdout/stderr results into the test's log,
150
where it may be useful for debugging. See also run_captured.
101
retcode = kwargs.get('retcode', 0)
102
result = self.apply_redirected(None, None, None,
103
bzrlib.commands.run_bzr, args)
104
self.assertEquals(result, retcode)
152
retcode = kwargs.pop('retcode', 0)
153
return self.run_bzr_captured(args, retcode)
107
155
def check_inventory_shape(self, inv, shape):
109
157
Compare an inventory to a list of expected names.
185
232
if contents != expect:
186
233
self.log("expected: %r" % expect)
187
234
self.log("actually: %r" % contents)
188
self.fail("contents of %s not as expected")
235
self.fail("contents of %s not as expected" % filename)
190
237
def _make_test_root(self):
195
238
if TestCaseInTempDir.TEST_ROOT is not None:
197
TestCaseInTempDir.TEST_ROOT = os.path.abspath(
198
tempfile.mkdtemp(suffix='.tmp',
199
prefix=self._TEST_NAME + '-',
242
root = 'test%04d.tmp' % i
246
if e.errno == errno.EEXIST:
251
# successfully created
252
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
202
254
# make a fake bzr directory there to prevent any tests propagating
203
255
# up onto the source directory's real branch
204
256
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
207
259
super(TestCaseInTempDir, self).setUp()
209
260
self._make_test_root()
210
261
self._currentdir = os.getcwdu()
211
262
self.test_dir = os.path.join(self.TEST_ROOT, self.id())
213
264
os.chdir(self.test_dir)
215
266
def tearDown(self):
217
267
os.chdir(self._currentdir)
218
268
super(TestCaseInTempDir, self).tearDown()
220
def _formcmd(self, cmd):
221
if isinstance(cmd, basestring):
224
cmd[0] = self.BZRPATH
225
if self.OVERRIDE_PYTHON:
226
cmd.insert(0, self.OVERRIDE_PYTHON)
227
self.log('$ %r' % cmd)
230
def runcmd(self, cmd, retcode=0):
231
"""Run one command and check the return code.
233
Returns a tuple of (stdout,stderr) strings.
235
If a single string is based, it is split into words.
236
For commands that are not simple space-separated words, please
237
pass a list instead."""
238
cmd = self._formcmd(cmd)
239
self.log('$ ' + ' '.join(cmd))
240
actual_retcode = subprocess.call(cmd, stdout=self._log_file,
241
stderr=self._log_file)
242
if retcode != actual_retcode:
243
raise CommandFailed("test failed: %r returned %d, expected %d"
244
% (cmd, actual_retcode, retcode))
246
def backtick(self, cmd, retcode=0):
247
"""Run a command and return its output"""
248
cmd = self._formcmd(cmd)
249
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
250
outd, errd = child.communicate()
252
actual_retcode = child.wait()
254
outd = outd.replace('\r', '')
256
if retcode != actual_retcode:
257
raise CommandFailed("test failed: %r returned %d, expected %d"
258
% (cmd, actual_retcode, retcode))
264
270
def build_tree(self, shape):
265
271
"""Build a test tree according to a pattern.
294
298
def selftest(verbose=False, pattern=".*"):
295
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
299
return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
298
302
def test_suite():
300
304
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
301
305
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
302
306
from doctest import DocTestSuite
308
308
global MODULES_TO_TEST, MODULES_TO_DOCTEST
315
315
'bzrlib.selftest.versioning',
316
316
'bzrlib.selftest.whitebox',
317
317
'bzrlib.selftest.testmerge3',
318
'bzrlib.selftest.testmerge',
318
319
'bzrlib.selftest.testhashcache',
319
320
'bzrlib.selftest.teststatus',
320
321
'bzrlib.selftest.testlog',
321
322
'bzrlib.selftest.blackbox',
322
323
'bzrlib.selftest.testrevisionnamespaces',
323
324
'bzrlib.selftest.testbranch',
325
'bzrlib.selftest.testremotebranch',
324
326
'bzrlib.selftest.testrevision',
327
'bzrlib.selftest.test_revision_info',
325
328
'bzrlib.selftest.test_merge_core',
326
329
'bzrlib.selftest.test_smart_add',
330
'bzrlib.selftest.test_bad_files',
327
331
'bzrlib.selftest.testdiff',
328
332
'bzrlib.selftest.test_xml',