90
92
"""Return as a string the log for this test"""
91
93
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.
95
def run_bzr(self, *args, **kwargs):
96
"""Invoke bzr, as if it were run from the command line.
104
98
This should be the main method for tests that want to exercise the
105
99
overall behavior of the bzr application (rather than a unit test
108
102
Much of the old code runs bzr by forking a new copy of Python, but
109
103
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)
142
def run_bzr(self, *args, **kwargs):
143
"""Invoke bzr, as if it were run from the command line.
145
This should be the main method for tests that want to exercise the
146
overall behavior of the bzr application (rather than a unit test
147
or a functional test of the library.)
149
This sends the stdout/stderr results into the test's log,
150
where it may be useful for debugging. See also run_captured.
152
retcode = kwargs.pop('retcode', 0)
153
return self.run_bzr_captured(args, retcode)
105
retcode = kwargs.get('retcode', 0)
106
result = self.apply_redirected(None, None, None,
107
bzrlib.commands.run_bzr, args)
108
self.assertEquals(result, retcode)
155
111
def check_inventory_shape(self, inv, shape):
156
112
"""Compare an inventory to a list of expected names.
177
133
"""Call callable with redirected std io pipes.
179
135
Returns the return code."""
136
from StringIO import StringIO
180
137
if not callable(a_callable):
181
138
raise ValueError("a_callable must be callable.")
182
139
if stdin is None:
183
140
stdin = StringIO("")
184
141
if stdout is None:
185
if hasattr(self, "_log_file"):
186
stdout = self._log_file
142
stdout = self._log_file
189
143
if stderr is None:
190
if hasattr(self, "_log_file"):
191
stderr = self._log_file
144
stderr = self._log_file
194
145
real_stdin = sys.stdin
195
146
real_stdout = sys.stdout
196
147
real_stderr = sys.stderr
231
182
if contents != expect:
232
183
self.log("expected: %r" % expect)
233
184
self.log("actually: %r" % contents)
234
self.fail("contents of %s not as expected" % filename)
185
self.fail("contents of %s not as expected")
236
187
def _make_test_root(self):
237
188
if TestCaseInTempDir.TEST_ROOT is not None:
265
217
os.chdir(self.test_dir)
267
219
def tearDown(self):
268
221
os.chdir(self._currentdir)
269
222
super(TestCaseInTempDir, self).tearDown()
224
def _formcmd(self, cmd):
225
if isinstance(cmd, basestring):
228
cmd[0] = self.BZRPATH
229
if self.OVERRIDE_PYTHON:
230
cmd.insert(0, self.OVERRIDE_PYTHON)
231
self.log('$ %r' % cmd)
234
def runcmd(self, cmd, retcode=0):
235
"""Run one command and check the return code.
237
Returns a tuple of (stdout,stderr) strings.
239
If a single string is based, it is split into words.
240
For commands that are not simple space-separated words, please
241
pass a list instead."""
242
cmd = self._formcmd(cmd)
243
self.log('$ ' + ' '.join(cmd))
244
actual_retcode = subprocess.call(cmd, stdout=self._log_file,
245
stderr=self._log_file)
246
if retcode != actual_retcode:
247
raise CommandFailed("test failed: %r returned %d, expected %d"
248
% (cmd, actual_retcode, retcode))
250
def backtick(self, cmd, retcode=0):
251
"""Run a command and return its output"""
252
cmd = self._formcmd(cmd)
253
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
254
outd, errd = child.communicate()
256
actual_retcode = child.wait()
258
outd = outd.replace('\r', '')
260
if retcode != actual_retcode:
261
raise CommandFailed("test failed: %r returned %d, expected %d"
262
% (cmd, actual_retcode, retcode))
271
268
def build_tree(self, shape):
272
269
"""Build a test tree according to a pattern.
307
306
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
308
307
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
309
308
from doctest import DocTestSuite
311
314
global MODULES_TO_TEST, MODULES_TO_DOCTEST
313
316
testmod_names = \
314
317
['bzrlib.selftest.MetaTestLog',
315
318
'bzrlib.selftest.testinv',
316
'bzrlib.selftest.test_ancestry',
317
319
'bzrlib.selftest.test_commit',
318
320
'bzrlib.selftest.test_commit_merge',
319
321
'bzrlib.selftest.versioning',
320
322
'bzrlib.selftest.testmerge3',
321
'bzrlib.selftest.testmerge',
322
323
'bzrlib.selftest.testhashcache',
323
324
'bzrlib.selftest.teststatus',
324
325
'bzrlib.selftest.testlog',
325
326
'bzrlib.selftest.testrevisionnamespaces',
326
327
'bzrlib.selftest.testbranch',
327
'bzrlib.selftest.testremotebranch',
328
328
'bzrlib.selftest.testrevision',
329
'bzrlib.selftest.test_revision_info',
330
329
'bzrlib.selftest.test_merge_core',
331
330
'bzrlib.selftest.test_smart_add',
332
'bzrlib.selftest.test_bad_files',
333
331
'bzrlib.selftest.testdiff',
334
332
'bzrlib.selftest.test_parent',
335
333
'bzrlib.selftest.test_xml',
338
336
'bzrlib.selftest.whitebox',
339
337
'bzrlib.selftest.teststore',
340
338
'bzrlib.selftest.blackbox',
341
'bzrlib.selftest.testgraph',
344
341
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,