92
90
"""Return as a string the log for this test"""
93
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)
95
142
def run_bzr(self, *args, **kwargs):
96
143
"""Invoke bzr, as if it were run from the command line.
99
146
overall behavior of the bzr application (rather than a unit test
100
147
or a functional test of the library.)
102
Much of the old code runs bzr by forking a new copy of Python, but
103
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.
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)
152
retcode = kwargs.pop('retcode', 0)
153
return self.run_bzr_captured(args, retcode)
111
155
def check_inventory_shape(self, inv, shape):
112
156
"""Compare an inventory to a list of expected names.
133
177
"""Call callable with redirected std io pipes.
135
179
Returns the return code."""
136
from StringIO import StringIO
137
180
if not callable(a_callable):
138
181
raise ValueError("a_callable must be callable.")
139
182
if stdin is None:
140
183
stdin = StringIO("")
141
184
if stdout is None:
142
stdout = self._log_file
185
if hasattr(self, "_log_file"):
186
stdout = self._log_file
143
189
if stderr is None:
144
stderr = self._log_file
190
if hasattr(self, "_log_file"):
191
stderr = self._log_file
145
194
real_stdin = sys.stdin
146
195
real_stdout = sys.stdout
147
196
real_stderr = sys.stderr
182
231
if contents != expect:
183
232
self.log("expected: %r" % expect)
184
233
self.log("actually: %r" % contents)
185
self.fail("contents of %s not as expected")
234
self.fail("contents of %s not as expected" % filename)
187
236
def _make_test_root(self):
188
237
if TestCaseInTempDir.TEST_ROOT is not None:
217
265
os.chdir(self.test_dir)
219
267
def tearDown(self):
221
268
os.chdir(self._currentdir)
222
269
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))
268
271
def build_tree(self, shape):
269
272
"""Build a test tree according to a pattern.
306
307
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
307
308
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
308
309
from doctest import DocTestSuite
314
311
global MODULES_TO_TEST, MODULES_TO_DOCTEST
316
313
testmod_names = \
317
314
['bzrlib.selftest.MetaTestLog',
318
315
'bzrlib.selftest.testinv',
316
'bzrlib.selftest.test_ancestry',
319
317
'bzrlib.selftest.test_commit',
320
318
'bzrlib.selftest.test_commit_merge',
321
319
'bzrlib.selftest.versioning',
322
320
'bzrlib.selftest.testmerge3',
321
'bzrlib.selftest.testmerge',
323
322
'bzrlib.selftest.testhashcache',
324
323
'bzrlib.selftest.teststatus',
325
324
'bzrlib.selftest.testlog',
326
325
'bzrlib.selftest.testrevisionnamespaces',
327
326
'bzrlib.selftest.testbranch',
327
'bzrlib.selftest.testremotebranch',
328
328
'bzrlib.selftest.testrevision',
329
'bzrlib.selftest.test_revision_info',
329
330
'bzrlib.selftest.test_merge_core',
330
331
'bzrlib.selftest.test_smart_add',
332
'bzrlib.selftest.test_bad_files',
331
333
'bzrlib.selftest.testdiff',
332
334
'bzrlib.selftest.test_parent',
333
335
'bzrlib.selftest.test_xml',
336
338
'bzrlib.selftest.whitebox',
337
339
'bzrlib.selftest.teststore',
338
340
'bzrlib.selftest.blackbox',
341
'bzrlib.selftest.testgraph',
341
344
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,