25
23
from testsweet import run_suite
26
25
import bzrlib.commands
28
26
import bzrlib.trace
32
28
MODULES_TO_TEST = []
33
29
MODULES_TO_DOCTEST = []
35
31
from logging import debug, warning, error
37
class CommandFailed(Exception):
40
34
class TestCase(unittest.TestCase):
41
35
"""Base class for bzr unit tests.
43
37
Tests that need access to disk resources should subclass
44
TestCaseInTempDir not TestCase.
38
FunctionalTestCase not TestCase.
46
40
Error and debug log messages are redirected from their usual
47
41
location into a temporary file, the contents of which can be
101
95
that is slower, harder to debug, and generally not necessary.
103
97
retcode = kwargs.get('retcode', 0)
104
result = self.apply_redirected(None, None, None,
105
bzrlib.commands.run_bzr, args)
106
self.assertEquals(result, retcode)
98
self.assertEquals(bzrlib.commands.run_bzr(args), retcode)
109
100
def check_inventory_shape(self, inv, shape):
128
119
self.fail("unexpected paths found in inventory: %r" % extras)
130
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
131
a_callable=None, *args, **kwargs):
132
"""Call callable with redirected std io pipes.
134
Returns the return code."""
135
from StringIO import StringIO
136
if not callable(a_callable):
137
raise ValueError("a_callable must be callable.")
141
stdout = self._log_file
143
stderr = self._log_file
144
real_stdin = sys.stdin
145
real_stdout = sys.stdout
146
real_stderr = sys.stderr
151
return a_callable(*args, **kwargs)
153
sys.stdout = real_stdout
154
sys.stderr = real_stderr
155
sys.stdin = real_stdin
158
121
BzrTestBase = TestCase
161
class TestCaseInTempDir(TestCase):
162
"""Derived class that runs a test within a temporary directory.
164
This is useful for tests that need to create a branch, etc.
166
The directory is created in a slightly complex way: for each
167
Python invocation, a new temporary top-level directory is created.
168
All test cases create their own directory within that. If the
169
tests complete successfully, the directory is removed.
124
class FunctionalTestCase(TestCase):
125
"""Base class for tests that perform function testing - running bzr,
126
using files on disk, and similar activities.
171
128
InTempDir is an old alias for FunctionalTestCase.
191
if TestCaseInTempDir.TEST_ROOT is not None:
148
if FunctionalTestCase.TEST_ROOT is not None:
193
TestCaseInTempDir.TEST_ROOT = os.path.abspath(
150
FunctionalTestCase.TEST_ROOT = os.path.abspath(
194
151
tempfile.mkdtemp(suffix='.tmp',
195
152
prefix=self._TEST_NAME + '-',
198
155
# make a fake bzr directory there to prevent any tests propagating
199
156
# up onto the source directory's real branch
200
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
157
os.mkdir(os.path.join(FunctionalTestCase.TEST_ROOT, '.bzr'))
203
super(TestCaseInTempDir, self).setUp()
160
super(FunctionalTestCase, self).setUp()
205
162
self._make_test_root()
206
163
self._currentdir = os.getcwdu()
211
168
def tearDown(self):
213
170
os.chdir(self._currentdir)
214
super(TestCaseInTempDir, self).tearDown()
171
super(FunctionalTestCase, self).tearDown()
216
173
def _formcmd(self, cmd):
217
174
if isinstance(cmd, basestring):
231
188
If a single string is based, it is split into words.
232
189
For commands that are not simple space-separated words, please
233
190
pass a list instead."""
193
from subprocess import call
194
except ImportError, e:
234
197
cmd = self._formcmd(cmd)
235
198
self.log('$ ' + ' '.join(cmd))
236
actual_retcode = subprocess.call(cmd, stdout=self._log_file,
237
stderr=self._log_file)
199
actual_retcode = call(cmd, stdout=self._log_file, stderr=self._log_file)
238
200
if retcode != actual_retcode:
239
201
raise CommandFailed("test failed: %r returned %d, expected %d"
240
202
% (cmd, actual_retcode, retcode))
242
204
def backtick(self, cmd, retcode=0):
243
205
"""Run a command and return its output"""
208
from subprocess import Popen, PIPE
209
except ImportError, e:
244
213
cmd = self._formcmd(cmd)
245
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
214
child = Popen(cmd, stdout=PIPE, stderr=self._log_file)
246
215
outd, errd = child.communicate()
248
217
actual_retcode = child.wait()
290
def selftest(verbose=False, pattern=".*"):
291
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
295
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
259
InTempDir = FunctionalTestCase
262
def selftest(verbose=False):
263
from unittest import TestLoader, TestSuite
296
264
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
297
265
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
298
266
from doctest import DocTestSuite
306
274
testmod_names = \
307
275
['bzrlib.selftest.MetaTestLog',
308
'bzrlib.selftest.test_parent',
309
276
'bzrlib.selftest.testinv',
310
277
'bzrlib.selftest.testfetch',
311
278
'bzrlib.selftest.versioning',
318
285
'bzrlib.selftest.testrevisionnamespaces',
319
286
'bzrlib.selftest.testbranch',
320
287
'bzrlib.selftest.testrevision',
321
'bzrlib.selftest.test_merge_core',
322
'bzrlib.selftest.test_smart_add',
323
289
'bzrlib.selftest.testdiff',
324
'bzrlib.selftest.test_xml',
326
'bzrlib.selftest.teststore',
329
292
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
334
297
TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
335
298
print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
337
302
suite = TestSuite()
338
304
suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
339
306
for m in MODULES_TO_TEST:
340
307
suite.addTest(TestLoader().loadTestsFromModule(m))
341
309
for m in (MODULES_TO_DOCTEST):
342
310
suite.addTest(DocTestSuite(m))
343
312
for p in bzrlib.plugin.all_plugins:
344
313
if hasattr(p, 'test_suite'):
345
314
suite.addTest(p.test_suite())
316
import bzrlib.merge_core
317
suite.addTest(unittest.makeSuite(bzrlib.merge_core.MergeTest, 'test_'))
319
return run_suite(suite, 'testbzr', verbose=verbose)