1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
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("expcted paths not found in inventory: %r" % shape)
144
self.fail("unexpected paths found in inventory: %r" % extras)
147
class InTempDir(TestBase):
148
"""Base class for tests run in a temporary branch."""
151
self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
152
os.mkdir(self.test_dir)
153
os.chdir(self.test_dir)
157
os.chdir(self.TEST_ROOT)
163
class _MyResult(TestResult):
167
No special behaviour for now.
169
def __init__(self, out):
171
TestResult.__init__(self)
173
def startTest(self, test):
174
# TODO: Maybe show test.shortDescription somewhere?
175
print >>self.out, '%-60.60s' % test.id(),
176
TestResult.startTest(self, test)
178
def stopTest(self, test):
180
TestResult.stopTest(self, test)
183
def addError(self, test, err):
184
print >>self.out, 'ERROR'
185
TestResult.addError(self, test, err)
187
def addFailure(self, test, err):
188
print >>self.out, 'FAILURE'
189
TestResult.addFailure(self, test, err)
191
def addSuccess(self, test):
192
print >>self.out, 'OK'
193
TestResult.addSuccess(self, test)
198
from unittest import TestLoader, TestSuite
200
import bzrlib.selftest.whitebox
201
import bzrlib.selftest.blackbox
202
import bzrlib.selftest.versioning
203
from doctest import DocTestSuite
216
for m in bzrlib.selftest.whitebox, \
217
bzrlib.selftest.versioning:
218
suite.addTest(tl.loadTestsFromModule(m))
220
suite.addTest(bzrlib.selftest.blackbox.suite())
222
for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
224
suite.addTest(DocTestSuite(m))
226
# save stdout & stderr so there's no leakage from code-under-test
227
real_stdout = sys.stdout
228
real_stderr = sys.stderr
229
sys.stdout = sys.stderr = TestBase.TEST_LOG
231
result = _MyResult(real_stdout)
234
sys.stdout = real_stdout
235
sys.stderr = real_stderr
237
_show_results(result)
239
return result.wasSuccessful()
244
def _setup_test_log():
248
log_filename = os.path.abspath('testbzr.log')
249
TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
251
print >>TestBase.TEST_LOG, "bzr tests run at " + time.ctime()
252
print '%-30s %s' % ('test log', log_filename)
255
def _setup_test_dir():
259
TestBase.ORIG_DIR = os.getcwdu()
260
TestBase.TEST_ROOT = os.path.abspath("testbzr.tmp")
262
print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
264
if os.path.exists(TestBase.TEST_ROOT):
265
shutil.rmtree(TestBase.TEST_ROOT)
266
os.mkdir(TestBase.TEST_ROOT)
267
os.chdir(TestBase.TEST_ROOT)
269
# make a fake bzr directory there to prevent any tests propagating
270
# up onto the source directory's real branch
271
os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
275
def _show_results(result):
276
for case, tb in result.errors:
277
_show_test_failure('ERROR', case, tb)
279
for case, tb in result.failures:
280
_show_test_failure('FAILURE', case, tb)
283
print '%4d tests run' % result.testsRun
284
print '%4d errors' % len(result.errors)
285
print '%4d failures' % len(result.failures)
289
def _show_test_failure(kind, case, tb):
290
print (kind + '! ').ljust(60, '-')
292
desc = case.shortDescription()
297
if isinstance(case, TestBase):
299
print 'log from this test:'
302
print ''.ljust(60, '-')