15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from unittest import TestResult, TestCase
22
from subprocess import call, Popen, PIPE
23
except ImportError, e:
24
sys.stderr.write("testbzr: sorry, this test suite requires the subprocess module\n"
25
"this is shipped with python2.4 and available separately for 2.3\n")
29
class CommandFailed(Exception):
33
class TestBase(TestCase):
34
"""Base class for bzr test cases.
36
Just defines some useful helper functions; doesn't actually test
40
# TODO: Special methods to invoke bzr, so that we can run it
41
# through a specified Python intepreter
43
OVERRIDE_PYTHON = None # to run with alternative python 'python'
49
def formcmd(self, cmd):
50
if isinstance(cmd, basestring):
55
if self.OVERRIDE_PYTHON:
56
cmd.insert(0, self.OVERRIDE_PYTHON)
58
self.log('$ %r' % cmd)
63
def runcmd(self, cmd, retcode=0):
64
"""Run one command and check the return code.
66
Returns a tuple of (stdout,stderr) strings.
68
If a single string is based, it is split into words.
69
For commands that are not simple space-separated words, please
70
pass a list instead."""
71
cmd = self.formcmd(cmd)
73
self.log('$ ' + ' '.join(cmd))
74
actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
76
if retcode != actual_retcode:
77
raise CommandFailed("test failed: %r returned %d, expected %d"
78
% (cmd, actual_retcode, retcode))
81
def backtick(self, cmd, retcode=0):
82
"""Run a command and return its output"""
83
cmd = self.formcmd(cmd)
84
child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
85
outd, errd = child.communicate()
87
actual_retcode = child.wait()
89
outd = outd.replace('\r', '')
91
if retcode != actual_retcode:
92
raise CommandFailed("test failed: %r returned %d, expected %d"
93
% (cmd, actual_retcode, retcode))
99
def build_tree(self, shape):
100
"""Build a test tree according to a pattern.
102
shape is a sequence of file specifications. If the final
103
character is '/', a directory is created.
105
This doesn't add anything to a branch.
107
# XXX: It's OK to just create them using forward slashes on windows?
110
assert isinstance(name, basestring)
115
print >>f, "contents of", name
120
"""Log a message to a progress file"""
121
self._log_buf = self._log_buf + str(msg) + '\n'
122
print >>self.TEST_LOG, msg
125
def check_inventory_shape(self, inv, shape):
127
Compare an inventory to a list of expected names.
129
Fail if they are not precisely equal.
132
shape = list(shape) # copy
133
for path, ie in inv.entries():
134
name = path.replace('\\', '/')
142
self.fail("expected paths not found in inventory: %r" % shape)
144
self.fail("unexpected paths found in inventory: %r" % extras)
147
def check_file_contents(self, filename, expect):
148
self.log("check contents of file %s" % filename)
149
contents = file(filename, 'r').read()
150
if contents != expect:
151
self.log("expected: %r" % expected)
152
self.log("actually: %r" % contents)
153
self.fail("contents of %s not as expected")
157
class InTempDir(TestBase):
158
"""Base class for tests run in a temporary branch."""
161
self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
162
os.mkdir(self.test_dir)
163
os.chdir(self.test_dir)
167
os.chdir(self.TEST_ROOT)
173
class _MyResult(TestResult):
177
No special behaviour for now.
179
def __init__(self, out):
181
TestResult.__init__(self)
183
def startTest(self, test):
184
# TODO: Maybe show test.shortDescription somewhere?
185
print >>self.out, '%-60.60s' % test.id(),
187
TestResult.startTest(self, test)
189
def stopTest(self, test):
191
TestResult.stopTest(self, test)
194
def addError(self, test, err):
195
print >>self.out, 'ERROR'
196
TestResult.addError(self, test, err)
197
_show_test_failure('error', test, err, self.out)
199
def addFailure(self, test, err):
200
print >>self.out, 'FAILURE'
201
TestResult.addFailure(self, test, err)
202
_show_test_failure('failure', test, err, self.out)
204
def addSuccess(self, test):
205
print >>self.out, 'OK'
206
TestResult.addSuccess(self, test)
18
from testsweet import TestBase, run_suite, InTempDir
21
MODULES_TO_DOCTEST = []
211
24
from unittest import TestLoader, TestSuite
212
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, bzrlib.commands
25
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
26
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
27
global MODULES_TO_TEST, MODULES_TO_DOCTEST
214
29
import bzrlib.selftest.whitebox
215
30
import bzrlib.selftest.blackbox
216
31
import bzrlib.selftest.versioning
32
import bzrlib.selftest.testmerge3
33
import bzrlib.selftest.testhashcache
34
import bzrlib.selftest.testrevisionnamespaces
35
import bzrlib.selftest.testbranch
36
import bzrlib.selftest.teststatus
37
import bzrlib.selftest.testinv
38
import bzrlib.merge_core
217
39
from doctest import DocTestSuite
46
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
47
bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
48
if m not in MODULES_TO_DOCTEST:
49
MODULES_TO_DOCTEST.append(m)
51
for m in (bzrlib.selftest.whitebox,
52
bzrlib.selftest.versioning,
53
bzrlib.selftest.testinv,
54
bzrlib.selftest.testmerge3,
55
bzrlib.selftest.testhashcache,
56
bzrlib.selftest.teststatus,
57
bzrlib.selftest.blackbox,
58
bzrlib.selftest.testhashcache,
59
bzrlib.selftest.testrevisionnamespaces,
60
bzrlib.selftest.testbranch,
62
if m not in MODULES_TO_TEST:
63
MODULES_TO_TEST.append(m)
223
66
TestBase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
224
67
print '%-30s %s' % ('bzr binary', TestBase.BZRPATH)
230
71
suite = TestSuite()
233
for m in bzrlib.selftest.whitebox, \
234
bzrlib.selftest.versioning:
235
suite.addTest(tl.loadTestsFromModule(m))
237
for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
73
# should also test bzrlib.merge_core, but they seem to be out of date with
77
# XXX: python2.3's TestLoader() doesn't seem to find all the
78
# tests; don't know why
79
for m in MODULES_TO_TEST:
80
suite.addTest(TestLoader().loadTestsFromModule(m))
82
for m in (MODULES_TO_DOCTEST):
239
83
suite.addTest(DocTestSuite(m))
241
suite.addTest(bzrlib.selftest.blackbox.suite())
243
# save stdout & stderr so there's no leakage from code-under-test
244
real_stdout = sys.stdout
245
real_stderr = sys.stderr
246
sys.stdout = sys.stderr = TestBase.TEST_LOG
248
result = _MyResult(real_stdout)
251
sys.stdout = real_stdout
252
sys.stderr = real_stderr
254
_show_results(result)
256
return result.wasSuccessful()
261
def _setup_test_log():
265
log_filename = os.path.abspath('testbzr.log')
266
TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
268
print >>TestBase.TEST_LOG, "bzr tests run at " + time.ctime()
269
print '%-30s %s' % ('test log', log_filename)
272
def _setup_test_dir():
276
TestBase.ORIG_DIR = os.getcwdu()
277
TestBase.TEST_ROOT = os.path.abspath("testbzr.tmp")
279
print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
281
if os.path.exists(TestBase.TEST_ROOT):
282
shutil.rmtree(TestBase.TEST_ROOT)
283
os.mkdir(TestBase.TEST_ROOT)
284
os.chdir(TestBase.TEST_ROOT)
286
# make a fake bzr directory there to prevent any tests propagating
287
# up onto the source directory's real branch
288
os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
292
def _show_results(result):
294
print '%4d tests run' % result.testsRun
295
print '%4d errors' % len(result.errors)
296
print '%4d failures' % len(result.failures)
300
def _show_test_failure(kind, case, exc_info, out):
301
from traceback import print_exception
303
print >>out, '-' * 60
306
desc = case.shortDescription()
308
print >>out, ' (%s)' % desc
310
print_exception(exc_info[0], exc_info[1], exc_info[2], None, out)
312
if isinstance(case, TestBase):
314
print >>out, 'log from this test:'
315
print >>out, case._log_buf
317
print >>out, '-' * 60
85
for p in bzrlib.plugin.all_plugins:
86
if hasattr(p, 'test_suite'):
87
suite.addTest(p.test_suite())
89
suite.addTest(unittest.makeSuite(bzrlib.merge_core.MergeTest, 'test_'))
91
return run_suite(suite, 'testbzr')