15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from testsweet import TestCase, run_suite, InTempDir
18
from cStringIO import StringIO
19
31
import bzrlib.commands
32
from bzrlib.errors import BzrError
33
import bzrlib.inventory
36
import bzrlib.osutils as osutils
40
from bzrlib.trace import mutter
41
from bzrlib.tests.TestUtil import TestLoader, TestSuite
42
from bzrlib.tests.treeshape import build_tree_contents
21
44
MODULES_TO_TEST = []
22
MODULES_TO_DOCTEST = []
25
class BzrTestBase(InTempDir):
26
"""bzr-specific test base class"""
45
MODULES_TO_DOCTEST = [
54
def packages_to_test():
55
import bzrlib.tests.blackbox
61
class EarlyStoppingTestResultAdapter(object):
62
"""An adapter for TestResult to stop at the first first failure or error"""
64
def __init__(self, result):
67
def addError(self, test, err):
68
self._result.addError(test, err)
71
def addFailure(self, test, err):
72
self._result.addFailure(test, err)
75
def __getattr__(self, name):
76
return getattr(self._result, name)
78
def __setattr__(self, name, value):
80
object.__setattr__(self, name, value)
81
return setattr(self._result, name, value)
84
class _MyResult(unittest._TextTestResult):
87
Shows output in a different format, including displaying runtime for tests.
90
def _elapsedTime(self):
91
return "%5dms" % (1000 * (time.time() - self._start_time))
93
def startTest(self, test):
94
unittest.TestResult.startTest(self, test)
95
# In a short description, the important words are in
96
# the beginning, but in an id, the important words are
98
SHOW_DESCRIPTIONS = False
100
width = osutils.terminal_width()
101
name_width = width - 15
103
if SHOW_DESCRIPTIONS:
104
what = test.shortDescription()
106
if len(what) > name_width:
107
what = what[:name_width-3] + '...'
110
if what.startswith('bzrlib.tests.'):
112
if len(what) > name_width:
113
what = '...' + what[3-name_width:]
114
what = what.ljust(name_width)
115
self.stream.write(what)
117
self._start_time = time.time()
119
def addError(self, test, err):
120
unittest.TestResult.addError(self, test, err)
122
self.stream.writeln("ERROR %s" % self._elapsedTime())
124
self.stream.write('E')
127
def addFailure(self, test, err):
128
unittest.TestResult.addFailure(self, test, err)
130
self.stream.writeln(" FAIL %s" % self._elapsedTime())
132
self.stream.write('F')
135
def addSuccess(self, test):
137
self.stream.writeln(' OK %s' % self._elapsedTime())
139
self.stream.write('~')
141
unittest.TestResult.addSuccess(self, test)
143
def printErrorList(self, flavour, errors):
144
for test, err in errors:
145
self.stream.writeln(self.separator1)
146
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
147
if hasattr(test, '_get_log'):
148
self.stream.writeln()
149
self.stream.writeln('log from this test:')
150
print >>self.stream, test._get_log()
151
self.stream.writeln(self.separator2)
152
self.stream.writeln("%s" % err)
155
class TextTestRunner(unittest.TextTestRunner):
156
stop_on_failure = False
158
def _makeResult(self):
159
result = _MyResult(self.stream, self.descriptions, self.verbosity)
160
if self.stop_on_failure:
161
result = EarlyStoppingTestResultAdapter(result)
165
def iter_suite_tests(suite):
166
"""Return all tests in a suite, recursing through nested suites"""
167
for item in suite._tests:
168
if isinstance(item, unittest.TestCase):
170
elif isinstance(item, unittest.TestSuite):
171
for r in iter_suite_tests(item):
174
raise Exception('unknown object %r inside test suite %r'
178
class TestSkipped(Exception):
179
"""Indicates that a test was intentionally skipped, rather than failing."""
183
class CommandFailed(Exception):
186
class TestCase(unittest.TestCase):
187
"""Base class for bzr unit tests.
189
Tests that need access to disk resources should subclass
190
TestCaseInTempDir not TestCase.
192
Error and debug log messages are redirected from their usual
193
location into a temporary file, the contents of which can be
194
retrieved by _get_log(). We use a real OS file, not an in-memory object,
195
so that it can also capture file IO. When the test completes this file
196
is read into memory and removed from disk.
198
There are also convenience functions to invoke bzr's command-line
199
routine, and to build and check bzr trees.
201
In addition to the usual method of overriding tearDown(), this class also
202
allows subclasses to register functions into the _cleanups list, which is
203
run in order as the object is torn down. It's less likely this will be
204
accidentally overlooked.
208
_log_file_name = None
212
unittest.TestCase.setUp(self)
214
self._cleanEnvironment()
215
bzrlib.trace.disable_default_logging()
218
def _ndiff_strings(self, a, b):
219
"""Return ndiff between two strings containing lines.
221
A trailing newline is added if missing to make the strings
223
if b and b[-1] != '\n':
225
if a and a[-1] != '\n':
227
difflines = difflib.ndiff(a.splitlines(True),
229
linejunk=lambda x: False,
230
charjunk=lambda x: False)
231
return ''.join(difflines)
233
def assertEqualDiff(self, a, b):
234
"""Assert two texts are equal, if not raise an exception.
236
This is intended for use with multi-line strings where it can
237
be hard to find the differences by eye.
239
# TODO: perhaps override assertEquals to call this for strings?
242
raise AssertionError("texts not equal:\n" +
243
self._ndiff_strings(a, b))
245
def assertContainsRe(self, haystack, needle_re):
246
"""Assert that a contains something matching a regular expression."""
247
if not re.search(needle_re, haystack):
248
raise AssertionError('pattern "%s" not found in "%s"'
249
% (needle_re, haystack))
251
def _startLogFile(self):
252
"""Send bzr and test log messages to a temporary file.
254
The file is removed as the test is torn down.
256
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
257
self._log_file = os.fdopen(fileno, 'w+')
258
bzrlib.trace.enable_test_log(self._log_file)
259
self._log_file_name = name
260
self.addCleanup(self._finishLogFile)
262
def _finishLogFile(self):
263
"""Finished with the log file.
265
Read contents into memory, close, and delete.
267
bzrlib.trace.disable_test_log()
268
self._log_file.seek(0)
269
self._log_contents = self._log_file.read()
270
self._log_file.close()
271
os.remove(self._log_file_name)
272
self._log_file = self._log_file_name = None
274
def addCleanup(self, callable):
275
"""Arrange to run a callable when this case is torn down.
277
Callables are run in the reverse of the order they are registered,
278
ie last-in first-out.
280
if callable in self._cleanups:
281
raise ValueError("cleanup function %r already registered on %s"
283
self._cleanups.append(callable)
285
def _cleanEnvironment(self):
288
'APPDATA': os.getcwd(),
293
self.addCleanup(self._restoreEnvironment)
294
for name, value in new_env.iteritems():
295
self._captureVar(name, value)
298
def _captureVar(self, name, newvalue):
299
"""Set an environment variable, preparing it to be reset when finished."""
300
self.__old_env[name] = os.environ.get(name, None)
302
if name in os.environ:
305
os.environ[name] = newvalue
308
def _restoreVar(name, value):
310
if name in os.environ:
313
os.environ[name] = value
315
def _restoreEnvironment(self):
316
for name, value in self.__old_env.iteritems():
317
self._restoreVar(name, value)
321
unittest.TestCase.tearDown(self)
323
def _runCleanups(self):
324
"""Run registered cleanup functions.
326
This should only be called from TestCase.tearDown.
328
for callable in reversed(self._cleanups):
331
def log(self, *args):
335
"""Return as a string the log for this test"""
336
if self._log_file_name:
337
return open(self._log_file_name).read()
339
return self._log_contents
340
# TODO: Delete the log after it's been read in
342
def capture(self, cmd, retcode=0):
343
"""Shortcut that splits cmd into words, runs, and returns stdout"""
344
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
346
def run_bzr_captured(self, argv, retcode=0):
347
"""Invoke bzr and return (stdout, stderr).
349
Useful for code that wants to check the contents of the
350
output, the way error messages are presented, etc.
352
This should be the main method for tests that want to exercise the
353
overall behavior of the bzr application (rather than a unit test
354
or a functional test of the library.)
356
Much of the old code runs bzr by forking a new copy of Python, but
357
that is slower, harder to debug, and generally not necessary.
359
This runs bzr through the interface that catches and reports
360
errors, and with logging set to something approximating the
361
default, so that error reporting can be checked.
363
argv -- arguments to invoke bzr
364
retcode -- expected return code, or None for don't-care.
368
self.log('run bzr: %s', ' '.join(argv))
369
# FIXME: don't call into logging here
370
handler = logging.StreamHandler(stderr)
371
handler.setFormatter(bzrlib.trace.QuietFormatter())
372
handler.setLevel(logging.INFO)
373
logger = logging.getLogger('')
374
logger.addHandler(handler)
376
result = self.apply_redirected(None, stdout, stderr,
377
bzrlib.commands.run_bzr_catch_errors,
380
logger.removeHandler(handler)
381
out = stdout.getvalue()
382
err = stderr.getvalue()
384
self.log('output:\n%s', out)
386
self.log('errors:\n%s', err)
387
if retcode is not None:
388
self.assertEquals(result, retcode)
27
391
def run_bzr(self, *args, **kwargs):
28
retcode = kwargs.get('retcode', 0)
29
self.assertEquals(bzrlib.commands.run_bzr(args), retcode)
32
def selftest(verbose=False):
33
from unittest import TestLoader, TestSuite
34
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
35
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
392
"""Invoke bzr, as if it were run from the command line.
394
This should be the main method for tests that want to exercise the
395
overall behavior of the bzr application (rather than a unit test
396
or a functional test of the library.)
398
This sends the stdout/stderr results into the test's log,
399
where it may be useful for debugging. See also run_captured.
401
retcode = kwargs.pop('retcode', 0)
402
return self.run_bzr_captured(args, retcode)
404
def check_inventory_shape(self, inv, shape):
405
"""Compare an inventory to a list of expected names.
407
Fail if they are not precisely equal.
410
shape = list(shape) # copy
411
for path, ie in inv.entries():
412
name = path.replace('\\', '/')
420
self.fail("expected paths not found in inventory: %r" % shape)
422
self.fail("unexpected paths found in inventory: %r" % extras)
424
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
425
a_callable=None, *args, **kwargs):
426
"""Call callable with redirected std io pipes.
428
Returns the return code."""
429
if not callable(a_callable):
430
raise ValueError("a_callable must be callable.")
434
if hasattr(self, "_log_file"):
435
stdout = self._log_file
439
if hasattr(self, "_log_file"):
440
stderr = self._log_file
443
real_stdin = sys.stdin
444
real_stdout = sys.stdout
445
real_stderr = sys.stderr
450
return a_callable(*args, **kwargs)
452
sys.stdout = real_stdout
453
sys.stderr = real_stderr
454
sys.stdin = real_stdin
457
BzrTestBase = TestCase
460
class TestCaseInTempDir(TestCase):
461
"""Derived class that runs a test within a temporary directory.
463
This is useful for tests that need to create a branch, etc.
465
The directory is created in a slightly complex way: for each
466
Python invocation, a new temporary top-level directory is created.
467
All test cases create their own directory within that. If the
468
tests complete successfully, the directory is removed.
470
InTempDir is an old alias for FunctionalTestCase.
475
OVERRIDE_PYTHON = 'python'
477
def check_file_contents(self, filename, expect):
478
self.log("check contents of file %s" % filename)
479
contents = file(filename, 'r').read()
480
if contents != expect:
481
self.log("expected: %r" % expect)
482
self.log("actually: %r" % contents)
483
self.fail("contents of %s not as expected" % filename)
485
def _make_test_root(self):
486
if TestCaseInTempDir.TEST_ROOT is not None:
490
root = u'test%04d.tmp' % i
494
if e.errno == errno.EEXIST:
499
# successfully created
500
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
502
# make a fake bzr directory there to prevent any tests propagating
503
# up onto the source directory's real branch
504
os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
507
super(TestCaseInTempDir, self).setUp()
508
self._make_test_root()
509
_currentdir = os.getcwdu()
510
short_id = self.id().replace('bzrlib.tests.', '') \
511
.replace('__main__.', '')
512
self.test_dir = osutils.pathjoin(self.TEST_ROOT, short_id)
513
os.mkdir(self.test_dir)
514
os.chdir(self.test_dir)
515
os.environ['HOME'] = self.test_dir
516
def _leaveDirectory():
517
os.chdir(_currentdir)
518
self.addCleanup(_leaveDirectory)
520
def build_tree(self, shape, line_endings='native'):
521
"""Build a test tree according to a pattern.
523
shape is a sequence of file specifications. If the final
524
character is '/', a directory is created.
526
This doesn't add anything to a branch.
527
:param line_endings: Either 'binary' or 'native'
528
in binary mode, exact contents are written
529
in native mode, the line endings match the
530
default platform endings.
532
# XXX: It's OK to just create them using forward slashes on windows?
534
self.assert_(isinstance(name, basestring))
538
if line_endings == 'binary':
540
elif line_endings == 'native':
543
raise BzrError('Invalid line ending request %r' % (line_endings,))
544
print >>f, "contents of", name
547
def build_tree_contents(self, shape):
548
build_tree_contents(shape)
550
def failUnlessExists(self, path):
551
"""Fail unless path, which may be abs or relative, exists."""
552
self.failUnless(osutils.lexists(path))
554
def assertFileEqual(self, content, path):
555
"""Fail if path does not contain 'content'."""
556
self.failUnless(osutils.lexists(path))
557
self.assertEqualDiff(content, open(path, 'r').read())
560
class MetaTestLog(TestCase):
561
def test_logging(self):
562
"""Test logs are captured when a test fails."""
563
self.log('a test message')
564
self._log_file.flush()
565
self.assertContainsRe(self._get_log(), 'a test message\n')
568
def filter_suite_by_re(suite, pattern):
569
result = TestUtil.TestSuite()
570
filter_re = re.compile(pattern)
571
for test in iter_suite_tests(suite):
572
if filter_re.search(test.id()):
577
def run_suite(suite, name='test', verbose=False, pattern=".*",
578
stop_on_failure=False, keep_output=False):
579
TestCaseInTempDir._TEST_NAME = name
584
runner = TextTestRunner(stream=sys.stdout,
587
runner.stop_on_failure=stop_on_failure
589
suite = filter_suite_by_re(suite, pattern)
590
result = runner.run(suite)
591
# This is still a little bogus,
592
# but only a little. Folk not using our testrunner will
593
# have to delete their temp directories themselves.
594
if result.wasSuccessful() or not keep_output:
595
if TestCaseInTempDir.TEST_ROOT is not None:
596
shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
598
print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
599
return result.wasSuccessful()
602
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
604
"""Run the whole test suite under the enhanced runner"""
605
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
606
stop_on_failure=stop_on_failure, keep_output=keep_output)
610
"""Build and return TestSuite for the whole program."""
36
611
from doctest import DocTestSuite
43
global MODULES_TO_TEST, MODULES_TO_DOCTEST
613
global MODULES_TO_DOCTEST
615
# FIXME: If these fail to load, e.g. because of a syntax error, the
616
# exception is hidden by unittest. Sucks. Should either fix that or
617
# perhaps import them and pass them to unittest as modules.
46
['bzrlib.selftest.whitebox',
47
'bzrlib.selftest.versioning',
48
'bzrlib.selftest.testinv',
49
'bzrlib.selftest.testmerge3',
50
'bzrlib.selftest.testhashcache',
51
'bzrlib.selftest.teststatus',
52
'bzrlib.selftest.testlog',
53
'bzrlib.selftest.blackbox',
54
'bzrlib.selftest.testrevisionnamespaces',
55
'bzrlib.selftest.testbranch',
56
'bzrlib.selftest.testrevision',
58
'bzrlib.selftest.testdiff',
619
['bzrlib.tests.MetaTestLog',
620
'bzrlib.tests.test_api',
621
'bzrlib.tests.test_gpg',
622
'bzrlib.tests.test_identitymap',
623
'bzrlib.tests.test_inv',
624
'bzrlib.tests.test_ancestry',
625
'bzrlib.tests.test_commit',
626
'bzrlib.tests.test_command',
627
'bzrlib.tests.test_commit_merge',
628
'bzrlib.tests.test_config',
629
'bzrlib.tests.test_merge3',
630
'bzrlib.tests.test_merge',
631
'bzrlib.tests.test_hashcache',
632
'bzrlib.tests.test_status',
633
'bzrlib.tests.test_log',
634
'bzrlib.tests.test_revisionnamespaces',
635
'bzrlib.tests.test_branch',
636
'bzrlib.tests.test_revision',
637
'bzrlib.tests.test_revision_info',
638
'bzrlib.tests.test_merge_core',
639
'bzrlib.tests.test_smart_add',
640
'bzrlib.tests.test_bad_files',
641
'bzrlib.tests.test_diff',
642
'bzrlib.tests.test_parent',
643
'bzrlib.tests.test_xml',
644
'bzrlib.tests.test_weave',
645
'bzrlib.tests.test_fetch',
646
'bzrlib.tests.test_whitebox',
647
'bzrlib.tests.test_store',
648
'bzrlib.tests.test_sampler',
649
'bzrlib.tests.test_transactions',
650
'bzrlib.tests.test_transport',
651
'bzrlib.tests.test_sftp',
652
'bzrlib.tests.test_graph',
653
'bzrlib.tests.test_workingtree',
654
'bzrlib.tests.test_upgrade',
655
'bzrlib.tests.test_uncommit',
656
'bzrlib.tests.test_ui',
657
'bzrlib.tests.test_conflicts',
658
'bzrlib.tests.test_testament',
659
'bzrlib.tests.test_annotate',
660
'bzrlib.tests.test_revprops',
661
'bzrlib.tests.test_options',
662
'bzrlib.tests.test_http',
663
'bzrlib.tests.test_nonascii',
664
'bzrlib.tests.test_reweave',
665
'bzrlib.tests.test_tsort',
666
'bzrlib.tests.test_trace',
667
'bzrlib.tests.test_rio',
61
# XXX: should also test bzrlib.merge_core, but they seem to be out
62
# of date with the code.
64
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
65
bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
66
if m not in MODULES_TO_DOCTEST:
67
MODULES_TO_DOCTEST.append(m)
70
TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
670
TestCase.BZRPATH = osutils.pathjoin(
671
osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
71
672
print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
75
674
suite = TestSuite()
77
675
suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
676
for package in packages_to_test():
677
suite.addTest(package.test_suite())
79
678
for m in MODULES_TO_TEST:
80
suite.addTest(TestLoader().loadTestsFromModule(m))
679
suite.addTest(TestLoader().loadTestsFromModule(m))
82
680
for m in (MODULES_TO_DOCTEST):
83
681
suite.addTest(DocTestSuite(m))
85
682
for p in bzrlib.plugin.all_plugins:
86
683
if hasattr(p, 'test_suite'):
87
684
suite.addTest(p.test_suite())
89
import bzrlib.merge_core
90
suite.addTest(unittest.makeSuite(bzrlib.merge_core.MergeTest, 'test_'))
92
return run_suite(suite, 'testbzr', verbose=verbose)