75
77
self._log_file_name = name
77
def run(self, result):
78
self.apply_redirected(None, None, None,
79
unittest.TestCase.run, self, result)
81
79
def tearDown(self):
82
80
logging.getLogger('').removeHandler(self._log_hdlr)
83
81
bzrlib.trace.enable_default_logging()
93
90
"""Return as a string the log for this test"""
94
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]
99
def run_bzr_captured(self, argv, retcode=0):
100
"""Invoke bzr and return (result, stdout, stderr).
102
Useful for code that wants to check the contents of the
103
output, the way error messages are presented, etc.
105
This should be the main method for tests that want to exercise the
106
overall behavior of the bzr application (rather than a unit test
107
or a functional test of the library.)
109
Much of the old code runs bzr by forking a new copy of Python, but
110
that is slower, harder to debug, and generally not necessary.
112
This runs bzr through the interface that catches and reports
113
errors, and with logging set to something approximating the
114
default, so that error reporting can be checked.
116
argv -- arguments to invoke bzr
117
retcode -- expected return code, or None for don't-care.
121
self.log('run bzr: %s', ' '.join(argv))
122
handler = logging.StreamHandler(stderr)
123
handler.setFormatter(bzrlib.trace.QuietFormatter())
124
handler.setLevel(logging.INFO)
125
logger = logging.getLogger('')
126
logger.addHandler(handler)
128
result = self.apply_redirected(None, stdout, stderr,
129
bzrlib.commands.run_bzr_catch_errors,
132
logger.removeHandler(handler)
133
out = stdout.getvalue()
134
err = stderr.getvalue()
136
self.log('output:\n%s', out)
138
self.log('errors:\n%s', err)
139
if retcode is not None:
140
self.assertEquals(result, retcode)
96
144
def run_bzr(self, *args, **kwargs):
97
145
"""Invoke bzr, as if it were run from the command line.
100
148
overall behavior of the bzr application (rather than a unit test
101
149
or a functional test of the library.)
103
Much of the old code runs bzr by forking a new copy of Python, but
104
that is slower, harder to debug, and generally not necessary.
151
This sends the stdout/stderr results into the test's log,
152
where it may be useful for debugging. See also run_captured.
106
retcode = kwargs.get('retcode', 0)
107
result = self.apply_redirected(None, None, None,
108
bzrlib.commands.run_bzr, args)
109
self.assertEquals(result, retcode)
154
retcode = kwargs.pop('retcode', 0)
155
return self.run_bzr_captured(args, retcode)
112
158
def check_inventory_shape(self, inv, shape):
114
160
Compare an inventory to a list of expected names.
193
238
self.fail("contents of %s not as expected")
195
240
def _make_test_root(self):
200
241
if TestCaseInTempDir.TEST_ROOT is not None:
202
TestCaseInTempDir.TEST_ROOT = os.path.abspath(
203
tempfile.mkdtemp(suffix='.tmp',
204
prefix=self._TEST_NAME + '-',
245
root = 'test%04d.tmp' % i
249
if e.errno == errno.EEXIST:
254
# successfully created
255
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
207
257
# make a fake bzr directory there to prevent any tests propagating
208
258
# up onto the source directory's real branch
209
259
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
212
262
super(TestCaseInTempDir, self).setUp()
214
263
self._make_test_root()
215
264
self._currentdir = os.getcwdu()
216
265
self.test_dir = os.path.join(self.TEST_ROOT, self.id())
218
267
os.chdir(self.test_dir)
220
269
def tearDown(self):
222
270
os.chdir(self._currentdir)
223
271
super(TestCaseInTempDir, self).tearDown()
225
def _formcmd(self, cmd):
226
if isinstance(cmd, basestring):
229
cmd[0] = self.BZRPATH
230
if self.OVERRIDE_PYTHON:
231
cmd.insert(0, self.OVERRIDE_PYTHON)
232
self.log('$ %r' % cmd)
235
def runcmd(self, cmd, retcode=0):
236
"""Run one command and check the return code.
238
Returns a tuple of (stdout,stderr) strings.
240
If a single string is based, it is split into words.
241
For commands that are not simple space-separated words, please
242
pass a list instead."""
243
cmd = self._formcmd(cmd)
244
self.log('$ ' + ' '.join(cmd))
245
actual_retcode = subprocess.call(cmd, stdout=self._log_file,
246
stderr=self._log_file)
247
if retcode != actual_retcode:
248
raise CommandFailed("test failed: %r returned %d, expected %d"
249
% (cmd, actual_retcode, retcode))
251
def backtick(self, cmd, retcode=0):
252
"""Run a command and return its output"""
253
cmd = self._formcmd(cmd)
254
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
255
outd, errd = child.communicate()
257
actual_retcode = child.wait()
259
outd = outd.replace('\r', '')
261
if retcode != actual_retcode:
262
raise CommandFailed("test failed: %r returned %d, expected %d"
263
% (cmd, actual_retcode, retcode))
269
274
def build_tree(self, shape):
270
275
"""Build a test tree according to a pattern.
299
303
def selftest(verbose=False, pattern=".*"):
300
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
304
return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
303
307
def test_suite():
305
309
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
306
310
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
307
311
from doctest import DocTestSuite
313
313
global MODULES_TO_TEST, MODULES_TO_DOCTEST
327
327
'bzrlib.selftest.blackbox',
328
328
'bzrlib.selftest.testrevisionnamespaces',
329
329
'bzrlib.selftest.testbranch',
330
'bzrlib.selftest.testremotebranch',
330
331
'bzrlib.selftest.testrevision',
331
332
'bzrlib.selftest.test_merge_core',
332
333
'bzrlib.selftest.test_smart_add',