15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
25
from testsweet import run_suite
32
26
import bzrlib.commands
33
from bzrlib.errors import BzrError
34
import bzrlib.inventory
39
28
import bzrlib.trace
40
from bzrlib.trace import mutter
41
from bzrlib.tests.TestUtil import TestLoader, TestSuite
42
from bzrlib.tests.treeshape import build_tree_contents
44
32
MODULES_TO_TEST = []
45
MODULES_TO_DOCTEST = [
54
def packages_to_test():
55
"""Return a list of packages to test.
57
The packages are not globally imported so that import failures are
58
triggered when running selftest, not when importing the command.
61
import bzrlib.tests.blackbox
68
class EarlyStoppingTestResultAdapter(object):
69
"""An adapter for TestResult to stop at the first first failure or error"""
71
def __init__(self, result):
74
def addError(self, test, err):
75
self._result.addError(test, err)
78
def addFailure(self, test, err):
79
self._result.addFailure(test, err)
82
def __getattr__(self, name):
83
return getattr(self._result, name)
85
def __setattr__(self, name, value):
87
object.__setattr__(self, name, value)
88
return setattr(self._result, name, value)
91
class _MyResult(unittest._TextTestResult):
94
Shows output in a different format, including displaying runtime for tests.
97
def _elapsedTime(self):
98
return "%5dms" % (1000 * (time.time() - self._start_time))
100
def startTest(self, test):
101
unittest.TestResult.startTest(self, test)
102
# In a short description, the important words are in
103
# the beginning, but in an id, the important words are
105
SHOW_DESCRIPTIONS = False
107
width = bzrlib.osutils.terminal_width()
108
name_width = width - 15
110
if SHOW_DESCRIPTIONS:
111
what = test.shortDescription()
113
if len(what) > name_width:
114
what = what[:name_width-3] + '...'
117
if what.startswith('bzrlib.tests.'):
119
if len(what) > name_width:
120
what = '...' + what[3-name_width:]
121
what = what.ljust(name_width)
122
self.stream.write(what)
124
self._start_time = time.time()
126
def addError(self, test, err):
127
if isinstance(err[1], TestSkipped):
128
return self.addSkipped(test, err)
129
unittest.TestResult.addError(self, test, err)
131
self.stream.writeln("ERROR %s" % self._elapsedTime())
133
self.stream.write('E')
136
def addFailure(self, test, err):
137
unittest.TestResult.addFailure(self, test, err)
139
self.stream.writeln(" FAIL %s" % self._elapsedTime())
141
self.stream.write('F')
144
def addSuccess(self, test):
146
self.stream.writeln(' OK %s' % self._elapsedTime())
148
self.stream.write('~')
150
unittest.TestResult.addSuccess(self, test)
152
def addSkipped(self, test, skip_excinfo):
154
print >>self.stream, ' SKIP %s' % self._elapsedTime()
155
print >>self.stream, ' %s' % skip_excinfo[1]
157
self.stream.write('S')
159
# seems best to treat this as success from point-of-view of unittest
160
# -- it actually does nothing so it barely matters :)
161
unittest.TestResult.addSuccess(self, test)
163
def printErrorList(self, flavour, errors):
164
for test, err in errors:
165
self.stream.writeln(self.separator1)
166
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
167
if hasattr(test, '_get_log'):
169
print >>self.stream, \
170
('vvvv[log from %s]' % test).ljust(78,'-')
171
print >>self.stream, test._get_log()
172
print >>self.stream, \
173
('^^^^[log from %s]' % test).ljust(78,'-')
174
self.stream.writeln(self.separator2)
175
self.stream.writeln("%s" % err)
178
class TextTestRunner(unittest.TextTestRunner):
179
stop_on_failure = False
181
def _makeResult(self):
182
result = _MyResult(self.stream, self.descriptions, self.verbosity)
183
if self.stop_on_failure:
184
result = EarlyStoppingTestResultAdapter(result)
188
def iter_suite_tests(suite):
189
"""Return all tests in a suite, recursing through nested suites"""
190
for item in suite._tests:
191
if isinstance(item, unittest.TestCase):
193
elif isinstance(item, unittest.TestSuite):
194
for r in iter_suite_tests(item):
197
raise Exception('unknown object %r inside test suite %r'
201
class TestSkipped(Exception):
202
"""Indicates that a test was intentionally skipped, rather than failing."""
33
MODULES_TO_DOCTEST = []
35
from logging import debug, warning, error
206
37
class CommandFailed(Exception):
215
46
Error and debug log messages are redirected from their usual
216
47
location into a temporary file, the contents of which can be
217
retrieved by _get_log(). We use a real OS file, not an in-memory object,
218
so that it can also capture file IO. When the test completes this file
219
is read into memory and removed from disk.
48
retrieved by _get_log().
221
50
There are also convenience functions to invoke bzr's command-line
222
routine, and to build and check bzr trees.
224
In addition to the usual method of overriding tearDown(), this class also
225
allows subclasses to register functions into the _cleanups list, which is
226
run in order as the object is torn down. It's less likely this will be
227
accidentally overlooked.
51
routine, and to build and check bzr trees."""
231
_log_file_name = None
56
# this replaces the default testsweet.TestCase; we don't want logging changed
235
57
unittest.TestCase.setUp(self)
237
self._cleanEnvironment()
238
58
bzrlib.trace.disable_default_logging()
241
def _ndiff_strings(self, a, b):
242
"""Return ndiff between two strings containing lines.
244
A trailing newline is added if missing to make the strings
246
if b and b[-1] != '\n':
248
if a and a[-1] != '\n':
250
difflines = difflib.ndiff(a.splitlines(True),
252
linejunk=lambda x: False,
253
charjunk=lambda x: False)
254
return ''.join(difflines)
256
def assertEqualDiff(self, a, b):
257
"""Assert two texts are equal, if not raise an exception.
259
This is intended for use with multi-line strings where it can
260
be hard to find the differences by eye.
262
# TODO: perhaps override assertEquals to call this for strings?
265
raise AssertionError("texts not equal:\n" +
266
self._ndiff_strings(a, b))
268
def assertContainsRe(self, haystack, needle_re):
269
"""Assert that a contains something matching a regular expression."""
270
if not re.search(needle_re, haystack):
271
raise AssertionError('pattern "%s" not found in "%s"'
272
% (needle_re, haystack))
274
def AssertSubset(self, sublist, superlist):
275
"""Assert that every entry in sublist is present in superlist."""
277
for entry in sublist:
278
if entry not in superlist:
279
missing.append(entry)
281
raise AssertionError("value(s) %r not present in container %r" %
282
(missing, superlist))
284
def _startLogFile(self):
285
"""Send bzr and test log messages to a temporary file.
287
The file is removed as the test is torn down.
59
self._enable_file_logging()
62
def _enable_file_logging(self):
289
63
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
290
encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
291
self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
292
bzrlib.trace.enable_test_log(self._log_file)
65
self._log_file = os.fdopen(fileno, 'w+')
67
hdlr = logging.StreamHandler(self._log_file)
68
hdlr.setLevel(logging.DEBUG)
69
hdlr.setFormatter(logging.Formatter('%(levelname)4.4s %(message)s'))
70
logging.getLogger('').addHandler(hdlr)
71
logging.getLogger('').setLevel(logging.DEBUG)
73
debug('opened log file %s', name)
293
75
self._log_file_name = name
294
self.addCleanup(self._finishLogFile)
296
def _finishLogFile(self):
297
"""Finished with the log file.
299
Read contents into memory, close, and delete.
301
bzrlib.trace.disable_test_log()
302
self._log_file.seek(0)
303
self._log_contents = self._log_file.read()
79
logging.getLogger('').removeHandler(self._log_hdlr)
80
bzrlib.trace.enable_default_logging()
81
logging.debug('%s teardown', self.id())
304
82
self._log_file.close()
305
os.remove(self._log_file_name)
306
self._log_file = self._log_file_name = None
308
def addCleanup(self, callable):
309
"""Arrange to run a callable when this case is torn down.
311
Callables are run in the reverse of the order they are registered,
312
ie last-in first-out.
314
if callable in self._cleanups:
315
raise ValueError("cleanup function %r already registered on %s"
317
self._cleanups.append(callable)
319
def _cleanEnvironment(self):
322
'APPDATA': os.getcwd(),
327
self.addCleanup(self._restoreEnvironment)
328
for name, value in new_env.iteritems():
329
self._captureVar(name, value)
332
def _captureVar(self, name, newvalue):
333
"""Set an environment variable, preparing it to be reset when finished."""
334
self.__old_env[name] = os.environ.get(name, None)
336
if name in os.environ:
339
os.environ[name] = newvalue
342
def _restoreVar(name, value):
344
if name in os.environ:
347
os.environ[name] = value
349
def _restoreEnvironment(self):
350
for name, value in self.__old_env.iteritems():
351
self._restoreVar(name, value)
355
83
unittest.TestCase.tearDown(self)
357
def _runCleanups(self):
358
"""Run registered cleanup functions.
360
This should only be called from TestCase.tearDown.
362
for cleanup_fn in reversed(self._cleanups):
365
86
def log(self, *args):
368
89
def _get_log(self):
369
90
"""Return as a string the log for this test"""
370
if self._log_file_name:
371
return open(self._log_file_name).read()
373
return self._log_contents
374
# TODO: Delete the log after it's been read in
376
def capture(self, cmd, retcode=0):
377
"""Shortcut that splits cmd into words, runs, and returns stdout"""
378
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
380
def run_bzr_captured(self, argv, retcode=0):
381
"""Invoke bzr and return (stdout, stderr).
383
Useful for code that wants to check the contents of the
384
output, the way error messages are presented, etc.
91
return open(self._log_file_name).read()
93
def run_bzr(self, *args, **kwargs):
94
"""Invoke bzr, as if it were run from the command line.
386
96
This should be the main method for tests that want to exercise the
387
97
overall behavior of the bzr application (rather than a unit test
514
181
if contents != expect:
515
182
self.log("expected: %r" % expect)
516
183
self.log("actually: %r" % contents)
517
self.fail("contents of %s not as expected" % filename)
184
self.fail("contents of %s not as expected")
519
186
def _make_test_root(self):
520
191
if TestCaseInTempDir.TEST_ROOT is not None:
524
root = u'test%04d.tmp' % i
528
if e.errno == errno.EEXIST:
533
# successfully created
534
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
193
TestCaseInTempDir.TEST_ROOT = os.path.abspath(
194
tempfile.mkdtemp(suffix='.tmp',
195
prefix=self._TEST_NAME + '-',
536
198
# make a fake bzr directory there to prevent any tests propagating
537
199
# up onto the source directory's real branch
538
200
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
541
203
super(TestCaseInTempDir, self).setUp()
542
205
self._make_test_root()
543
_currentdir = os.getcwdu()
544
short_id = self.id().replace('bzrlib.tests.', '') \
545
.replace('__main__.', '')
546
self.test_dir = os.path.join(self.TEST_ROOT, short_id)
206
self._currentdir = os.getcwdu()
207
self.test_dir = os.path.join(self.TEST_ROOT, self.id())
547
208
os.mkdir(self.test_dir)
548
209
os.chdir(self.test_dir)
549
os.environ['HOME'] = self.test_dir
550
def _leaveDirectory():
551
os.chdir(_currentdir)
552
self.addCleanup(_leaveDirectory)
554
def build_tree(self, shape, line_endings='native'):
213
os.chdir(self._currentdir)
214
super(TestCaseInTempDir, self).tearDown()
216
def _formcmd(self, cmd):
217
if isinstance(cmd, basestring):
220
cmd[0] = self.BZRPATH
221
if self.OVERRIDE_PYTHON:
222
cmd.insert(0, self.OVERRIDE_PYTHON)
223
self.log('$ %r' % cmd)
226
def runcmd(self, cmd, retcode=0):
227
"""Run one command and check the return code.
229
Returns a tuple of (stdout,stderr) strings.
231
If a single string is based, it is split into words.
232
For commands that are not simple space-separated words, please
233
pass a list instead."""
234
cmd = self._formcmd(cmd)
235
self.log('$ ' + ' '.join(cmd))
236
actual_retcode = subprocess.call(cmd, stdout=self._log_file,
237
stderr=self._log_file)
238
if retcode != actual_retcode:
239
raise CommandFailed("test failed: %r returned %d, expected %d"
240
% (cmd, actual_retcode, retcode))
242
def backtick(self, cmd, retcode=0):
243
"""Run a command and return its output"""
244
cmd = self._formcmd(cmd)
245
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
246
outd, errd = child.communicate()
248
actual_retcode = child.wait()
250
outd = outd.replace('\r', '')
252
if retcode != actual_retcode:
253
raise CommandFailed("test failed: %r returned %d, expected %d"
254
% (cmd, actual_retcode, retcode))
260
def build_tree(self, shape):
555
261
"""Build a test tree according to a pattern.
557
263
shape is a sequence of file specifications. If the final
558
264
character is '/', a directory is created.
560
266
This doesn't add anything to a branch.
561
:param line_endings: Either 'binary' or 'native'
562
in binary mode, exact contents are written
563
in native mode, the line endings match the
564
default platform endings.
566
268
# XXX: It's OK to just create them using forward slashes on windows?
567
270
for name in shape:
568
self.assert_(isinstance(name, basestring))
271
assert isinstance(name, basestring)
569
272
if name[-1] == '/':
570
273
os.mkdir(name[:-1])
572
if line_endings == 'binary':
574
elif line_endings == 'native':
577
raise BzrError('Invalid line ending request %r' % (line_endings,))
578
276
print >>f, "contents of", name
581
def build_tree_contents(self, shape):
582
build_tree_contents(shape)
584
def failUnlessExists(self, path):
585
"""Fail unless path, which may be abs or relative, exists."""
586
self.failUnless(bzrlib.osutils.lexists(path))
588
def assertFileEqual(self, content, path):
589
"""Fail if path does not contain 'content'."""
590
self.failUnless(bzrlib.osutils.lexists(path))
591
self.assertEqualDiff(content, open(path, 'r').read())
594
def filter_suite_by_re(suite, pattern):
596
filter_re = re.compile(pattern)
597
for test in iter_suite_tests(suite):
598
if filter_re.search(test.id()):
603
def run_suite(suite, name='test', verbose=False, pattern=".*",
604
stop_on_failure=False, keep_output=False):
605
TestCaseInTempDir._TEST_NAME = name
610
runner = TextTestRunner(stream=sys.stdout,
613
runner.stop_on_failure=stop_on_failure
615
suite = filter_suite_by_re(suite, pattern)
616
result = runner.run(suite)
617
# This is still a little bogus,
618
# but only a little. Folk not using our testrunner will
619
# have to delete their temp directories themselves.
620
if result.wasSuccessful() or not keep_output:
621
if TestCaseInTempDir.TEST_ROOT is not None:
622
shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
624
print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
625
return result.wasSuccessful()
628
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
630
"""Run the whole test suite under the enhanced runner"""
631
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
632
stop_on_failure=stop_on_failure, keep_output=keep_output)
281
class MetaTestLog(TestCase):
282
def test_logging(self):
283
"""Test logs are captured when a test fails."""
284
logging.info('an info message')
285
warning('something looks dodgy...')
286
logging.debug('hello, test is running')
290
def selftest(verbose=False, pattern=".*"):
291
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
635
294
def test_suite():
636
"""Build and return TestSuite for the whole program."""
295
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
296
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
297
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
637
298
from doctest import DocTestSuite
639
global MODULES_TO_DOCTEST
642
'bzrlib.tests.test_ancestry',
643
'bzrlib.tests.test_annotate',
644
'bzrlib.tests.test_api',
645
'bzrlib.tests.test_bad_files',
646
'bzrlib.tests.test_branch',
647
'bzrlib.tests.test_command',
648
'bzrlib.tests.test_commit',
649
'bzrlib.tests.test_commit_merge',
650
'bzrlib.tests.test_config',
651
'bzrlib.tests.test_conflicts',
652
'bzrlib.tests.test_diff',
653
'bzrlib.tests.test_fetch',
654
'bzrlib.tests.test_gpg',
655
'bzrlib.tests.test_graph',
656
'bzrlib.tests.test_hashcache',
657
'bzrlib.tests.test_http',
658
'bzrlib.tests.test_identitymap',
659
'bzrlib.tests.test_inv',
660
'bzrlib.tests.test_log',
661
'bzrlib.tests.test_merge',
662
'bzrlib.tests.test_merge3',
663
'bzrlib.tests.test_merge_core',
664
'bzrlib.tests.test_missing',
665
'bzrlib.tests.test_msgeditor',
666
'bzrlib.tests.test_nonascii',
667
'bzrlib.tests.test_options',
668
'bzrlib.tests.test_parent',
669
'bzrlib.tests.test_plugins',
670
'bzrlib.tests.test_remove',
671
'bzrlib.tests.test_revision',
672
'bzrlib.tests.test_revision_info',
673
'bzrlib.tests.test_revisionnamespaces',
674
'bzrlib.tests.test_revprops',
675
'bzrlib.tests.test_reweave',
676
'bzrlib.tests.test_rio',
677
'bzrlib.tests.test_sampler',
678
'bzrlib.tests.test_selftest',
679
'bzrlib.tests.test_setup',
680
'bzrlib.tests.test_sftp',
681
'bzrlib.tests.test_smart_add',
682
'bzrlib.tests.test_source',
683
'bzrlib.tests.test_status',
684
'bzrlib.tests.test_store',
685
'bzrlib.tests.test_testament',
686
'bzrlib.tests.test_trace',
687
'bzrlib.tests.test_transactions',
688
'bzrlib.tests.test_transport',
689
'bzrlib.tests.test_tsort',
690
'bzrlib.tests.test_ui',
691
'bzrlib.tests.test_uncommit',
692
'bzrlib.tests.test_upgrade',
693
'bzrlib.tests.test_weave',
694
'bzrlib.tests.test_whitebox',
695
'bzrlib.tests.test_workingtree',
696
'bzrlib.tests.test_xml',
304
global MODULES_TO_TEST, MODULES_TO_DOCTEST
307
['bzrlib.selftest.MetaTestLog',
308
'bzrlib.selftest.test_parent',
309
'bzrlib.selftest.testinv',
310
'bzrlib.selftest.testfetch',
311
'bzrlib.selftest.versioning',
312
'bzrlib.selftest.whitebox',
313
'bzrlib.selftest.testmerge3',
314
'bzrlib.selftest.testhashcache',
315
'bzrlib.selftest.teststatus',
316
'bzrlib.selftest.testlog',
317
'bzrlib.selftest.blackbox',
318
'bzrlib.selftest.testrevisionnamespaces',
319
'bzrlib.selftest.testbranch',
320
'bzrlib.selftest.testrevision',
321
'bzrlib.selftest.test_merge_core',
322
'bzrlib.selftest.test_smart_add',
323
'bzrlib.selftest.testdiff',
324
'bzrlib.selftest.test_xml',
326
'bzrlib.selftest.teststore',
699
print '%10s: %s' % ('bzr', os.path.realpath(sys.argv[0]))
700
print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
329
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
330
bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
331
if m not in MODULES_TO_DOCTEST:
332
MODULES_TO_DOCTEST.append(m)
334
TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
335
print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
702
337
suite = TestSuite()
703
# python2.4's TestLoader.loadTestsFromNames gives very poor
704
# errors if it fails to load a named module - no indication of what's
705
# actually wrong, just "no such module". We should probably override that
706
# class, but for the moment just load them ourselves. (mbp 20051202)
707
loader = TestLoader()
708
for mod_name in testmod_names:
709
mod = _load_module_by_name(mod_name)
710
suite.addTest(loader.loadTestsFromModule(mod))
711
for package in packages_to_test():
712
suite.addTest(package.test_suite())
338
suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
713
339
for m in MODULES_TO_TEST:
714
suite.addTest(loader.loadTestsFromModule(m))
340
suite.addTest(TestLoader().loadTestsFromModule(m))
715
341
for m in (MODULES_TO_DOCTEST):
716
342
suite.addTest(DocTestSuite(m))
717
for name, plugin in bzrlib.plugin.all_plugins().items():
718
if hasattr(plugin, 'test_suite'):
719
suite.addTest(plugin.test_suite())
343
for p in bzrlib.plugin.all_plugins:
344
if hasattr(p, 'test_suite'):
345
suite.addTest(p.test_suite())
723
def _load_module_by_name(mod_name):
724
parts = mod_name.split('.')
725
module = __import__(mod_name)
727
# for historical reasons python returns the top-level module even though
728
# it loads the submodule; we need to walk down to get the one we want.
730
module = getattr(module, parts.pop(0))