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
# TODO: Perhaps there should be an API to find out if bzr running under the
19
# test suite -- some plugins might want to avoid making intrusive changes if
20
# this is the case. However, we want behaviour under to test to diverge as
21
# little as possible, so this should be used rarely if it's added at all.
22
# (Suggestion from j-a-meinel, 2005-11-24)
24
from cStringIO import StringIO
39
import bzrlib.commands
40
from bzrlib.errors import BzrError
41
import bzrlib.inventory
44
import bzrlib.osutils as osutils
48
from bzrlib.transport import urlescape
49
from bzrlib.trace import mutter
50
from bzrlib.tests.TestUtil import TestLoader, TestSuite
51
from bzrlib.tests.treeshape import build_tree_contents
54
MODULES_TO_DOCTEST = [
63
def packages_to_test():
64
import bzrlib.tests.blackbox
70
class EarlyStoppingTestResultAdapter(object):
71
"""An adapter for TestResult to stop at the first first failure or error"""
73
def __init__(self, result):
76
def addError(self, test, err):
77
self._result.addError(test, err)
80
def addFailure(self, test, err):
81
self._result.addFailure(test, err)
84
def __getattr__(self, name):
85
return getattr(self._result, name)
87
def __setattr__(self, name, value):
89
object.__setattr__(self, name, value)
90
return setattr(self._result, name, value)
93
class _MyResult(unittest._TextTestResult):
96
Shows output in a different format, including displaying runtime for tests.
99
def _elapsedTime(self):
100
return "%5dms" % (1000 * (time.time() - self._start_time))
102
def startTest(self, test):
103
unittest.TestResult.startTest(self, test)
104
# In a short description, the important words are in
105
# the beginning, but in an id, the important words are
107
SHOW_DESCRIPTIONS = False
109
width = osutils.terminal_width()
110
name_width = width - 15
112
if SHOW_DESCRIPTIONS:
113
what = test.shortDescription()
115
if len(what) > name_width:
116
what = what[:name_width-3] + '...'
119
if what.startswith('bzrlib.tests.'):
121
if len(what) > name_width:
122
what = '...' + what[3-name_width:]
123
what = what.ljust(name_width)
124
self.stream.write(what)
126
self._start_time = time.time()
128
def addError(self, test, err):
129
if isinstance(err[1], TestSkipped):
130
return self.addSkipped(test, err)
131
unittest.TestResult.addError(self, test, err)
133
self.stream.writeln("ERROR %s" % self._elapsedTime())
135
self.stream.write('E')
138
def addFailure(self, test, err):
139
unittest.TestResult.addFailure(self, test, err)
141
self.stream.writeln(" FAIL %s" % self._elapsedTime())
143
self.stream.write('F')
146
def addSuccess(self, test):
148
self.stream.writeln(' OK %s' % self._elapsedTime())
150
self.stream.write('~')
152
unittest.TestResult.addSuccess(self, test)
154
def addSkipped(self, test, skip_excinfo):
156
print >>self.stream, ' SKIP %s' % self._elapsedTime()
157
print >>self.stream, ' %s' % skip_excinfo[1]
159
self.stream.write('S')
161
# seems best to treat this as success from point-of-view of unittest
162
# -- it actually does nothing so it barely matters :)
163
unittest.TestResult.addSuccess(self, test)
165
def printErrorList(self, flavour, errors):
166
for test, err in errors:
167
self.stream.writeln(self.separator1)
168
self.stream.writeln("%s: %s" % (flavour, self.getDescription(test)))
169
if hasattr(test, '_get_log'):
171
print >>self.stream, \
172
('vvvv[log from %s]' % test.id()).ljust(78,'-')
173
print >>self.stream, test._get_log()
174
print >>self.stream, \
175
('^^^^[log from %s]' % test.id()).ljust(78,'-')
176
self.stream.writeln(self.separator2)
177
self.stream.writeln("%s" % err)
180
class TextTestRunner(unittest.TextTestRunner):
181
stop_on_failure = False
183
def _makeResult(self):
184
result = _MyResult(self.stream, self.descriptions, self.verbosity)
185
if self.stop_on_failure:
186
result = EarlyStoppingTestResultAdapter(result)
190
def iter_suite_tests(suite):
191
"""Return all tests in a suite, recursing through nested suites"""
192
for item in suite._tests:
193
if isinstance(item, unittest.TestCase):
195
elif isinstance(item, unittest.TestSuite):
196
for r in iter_suite_tests(item):
199
raise Exception('unknown object %r inside test suite %r'
203
class TestSkipped(Exception):
204
"""Indicates that a test was intentionally skipped, rather than failing."""
208
class CommandFailed(Exception):
211
class TestCase(unittest.TestCase):
212
"""Base class for bzr unit tests.
214
Tests that need access to disk resources should subclass
215
TestCaseInTempDir not TestCase.
217
Error and debug log messages are redirected from their usual
218
location into a temporary file, the contents of which can be
219
retrieved by _get_log(). We use a real OS file, not an in-memory object,
220
so that it can also capture file IO. When the test completes this file
221
is read into memory and removed from disk.
223
There are also convenience functions to invoke bzr's command-line
224
routine, and to build and check bzr trees.
226
In addition to the usual method of overriding tearDown(), this class also
227
allows subclasses to register functions into the _cleanups list, which is
228
run in order as the object is torn down. It's less likely this will be
229
accidentally overlooked.
233
_log_file_name = None
237
unittest.TestCase.setUp(self)
239
self._cleanEnvironment()
240
bzrlib.trace.disable_default_logging()
243
def _ndiff_strings(self, a, b):
244
"""Return ndiff between two strings containing lines.
246
A trailing newline is added if missing to make the strings
248
if b and b[-1] != '\n':
250
if a and a[-1] != '\n':
252
difflines = difflib.ndiff(a.splitlines(True),
254
linejunk=lambda x: False,
255
charjunk=lambda x: False)
256
return ''.join(difflines)
258
def assertEqualDiff(self, a, b):
259
"""Assert two texts are equal, if not raise an exception.
261
This is intended for use with multi-line strings where it can
262
be hard to find the differences by eye.
264
# TODO: perhaps override assertEquals to call this for strings?
267
raise AssertionError("texts not equal:\n" +
268
self._ndiff_strings(a, b))
270
def assertStartsWith(self, s, prefix):
271
if not s.startswith(prefix):
272
raise AssertionError('string %r does not start with %r' % (s, prefix))
274
def assertEndsWith(self, s, suffix):
275
if not s.endswith(prefix):
276
raise AssertionError('string %r does not end with %r' % (s, suffix))
278
def assertContainsRe(self, haystack, needle_re):
279
"""Assert that a contains something matching a regular expression."""
280
if not re.search(needle_re, haystack):
281
raise AssertionError('pattern "%s" not found in "%s"'
282
% (needle_re, haystack))
284
def AssertSubset(self, sublist, superlist):
285
"""Assert that every entry in sublist is present in superlist."""
287
for entry in sublist:
288
if entry not in superlist:
289
missing.append(entry)
291
raise AssertionError("value(s) %r not present in container %r" %
292
(missing, superlist))
294
def assertTransportMode(self, transport, path, mode):
295
"""Fail if a path does not have mode mode.
297
If modes are not supported on this platform, the test is skipped.
299
if sys.platform == 'win32':
301
path_stat = transport.stat(path)
302
actual_mode = stat.S_IMODE(path_stat.st_mode)
303
self.assertEqual(mode, actual_mode,
304
'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
306
def _startLogFile(self):
307
"""Send bzr and test log messages to a temporary file.
309
The file is removed as the test is torn down.
311
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
312
encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
313
self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
314
bzrlib.trace.enable_test_log(self._log_file)
315
self._log_file_name = name
316
self.addCleanup(self._finishLogFile)
318
def _finishLogFile(self):
319
"""Finished with the log file.
321
Read contents into memory, close, and delete.
323
bzrlib.trace.disable_test_log()
324
self._log_file.seek(0)
325
self._log_contents = self._log_file.read()
326
self._log_file.close()
327
os.remove(self._log_file_name)
328
self._log_file = self._log_file_name = None
330
def addCleanup(self, callable):
331
"""Arrange to run a callable when this case is torn down.
333
Callables are run in the reverse of the order they are registered,
334
ie last-in first-out.
336
if callable in self._cleanups:
337
raise ValueError("cleanup function %r already registered on %s"
339
self._cleanups.append(callable)
341
def _cleanEnvironment(self):
344
'APPDATA': os.getcwd(),
349
self.addCleanup(self._restoreEnvironment)
350
for name, value in new_env.iteritems():
351
self._captureVar(name, value)
354
def _captureVar(self, name, newvalue):
355
"""Set an environment variable, preparing it to be reset when finished."""
356
self.__old_env[name] = os.environ.get(name, None)
358
if name in os.environ:
361
os.environ[name] = newvalue
364
def _restoreVar(name, value):
366
if name in os.environ:
369
os.environ[name] = value
371
def _restoreEnvironment(self):
372
for name, value in self.__old_env.iteritems():
373
self._restoreVar(name, value)
377
unittest.TestCase.tearDown(self)
379
def _runCleanups(self):
380
"""Run registered cleanup functions.
382
This should only be called from TestCase.tearDown.
384
# TODO: Perhaps this should keep running cleanups even if
386
for cleanup_fn in reversed(self._cleanups):
389
def log(self, *args):
393
"""Return as a string the log for this test"""
394
if self._log_file_name:
395
return open(self._log_file_name).read()
397
return self._log_contents
398
# TODO: Delete the log after it's been read in
400
def capture(self, cmd, retcode=0):
401
"""Shortcut that splits cmd into words, runs, and returns stdout"""
402
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
404
def run_bzr_captured(self, argv, retcode=0):
405
"""Invoke bzr and return (stdout, stderr).
407
Useful for code that wants to check the contents of the
408
output, the way error messages are presented, etc.
410
This should be the main method for tests that want to exercise the
411
overall behavior of the bzr application (rather than a unit test
412
or a functional test of the library.)
414
Much of the old code runs bzr by forking a new copy of Python, but
415
that is slower, harder to debug, and generally not necessary.
417
This runs bzr through the interface that catches and reports
418
errors, and with logging set to something approximating the
419
default, so that error reporting can be checked.
421
argv -- arguments to invoke bzr
422
retcode -- expected return code, or None for don't-care.
426
self.log('run bzr: %s', ' '.join(argv))
427
# FIXME: don't call into logging here
428
handler = logging.StreamHandler(stderr)
429
handler.setFormatter(bzrlib.trace.QuietFormatter())
430
handler.setLevel(logging.INFO)
431
logger = logging.getLogger('')
432
logger.addHandler(handler)
434
result = self.apply_redirected(None, stdout, stderr,
435
bzrlib.commands.run_bzr_catch_errors,
438
logger.removeHandler(handler)
439
out = stdout.getvalue()
440
err = stderr.getvalue()
442
self.log('output:\n%s', out)
444
self.log('errors:\n%s', err)
445
if retcode is not None:
446
self.assertEquals(result, retcode)
449
def run_bzr(self, *args, **kwargs):
450
"""Invoke bzr, as if it were run from the command line.
452
This should be the main method for tests that want to exercise the
453
overall behavior of the bzr application (rather than a unit test
454
or a functional test of the library.)
456
This sends the stdout/stderr results into the test's log,
457
where it may be useful for debugging. See also run_captured.
459
retcode = kwargs.pop('retcode', 0)
460
return self.run_bzr_captured(args, retcode)
462
def check_inventory_shape(self, inv, shape):
463
"""Compare an inventory to a list of expected names.
465
Fail if they are not precisely equal.
468
shape = list(shape) # copy
469
for path, ie in inv.entries():
470
name = path.replace('\\', '/')
478
self.fail("expected paths not found in inventory: %r" % shape)
480
self.fail("unexpected paths found in inventory: %r" % extras)
482
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
483
a_callable=None, *args, **kwargs):
484
"""Call callable with redirected std io pipes.
486
Returns the return code."""
487
if not callable(a_callable):
488
raise ValueError("a_callable must be callable.")
492
if hasattr(self, "_log_file"):
493
stdout = self._log_file
497
if hasattr(self, "_log_file"):
498
stderr = self._log_file
501
real_stdin = sys.stdin
502
real_stdout = sys.stdout
503
real_stderr = sys.stderr
508
return a_callable(*args, **kwargs)
510
sys.stdout = real_stdout
511
sys.stderr = real_stderr
512
sys.stdin = real_stdin
515
BzrTestBase = TestCase
518
class TestCaseInTempDir(TestCase):
519
"""Derived class that runs a test within a temporary directory.
521
This is useful for tests that need to create a branch, etc.
523
The directory is created in a slightly complex way: for each
524
Python invocation, a new temporary top-level directory is created.
525
All test cases create their own directory within that. If the
526
tests complete successfully, the directory is removed.
528
InTempDir is an old alias for FunctionalTestCase.
533
OVERRIDE_PYTHON = 'python'
535
def check_file_contents(self, filename, expect):
536
self.log("check contents of file %s" % filename)
537
contents = file(filename, 'r').read()
538
if contents != expect:
539
self.log("expected: %r" % expect)
540
self.log("actually: %r" % contents)
541
self.fail("contents of %s not as expected" % filename)
543
def _make_test_root(self):
544
if TestCaseInTempDir.TEST_ROOT is not None:
548
root = u'test%04d.tmp' % i
552
if e.errno == errno.EEXIST:
557
# successfully created
558
TestCaseInTempDir.TEST_ROOT = osutils.abspath(root)
560
# make a fake bzr directory there to prevent any tests propagating
561
# up onto the source directory's real branch
562
os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
565
super(TestCaseInTempDir, self).setUp()
566
self._make_test_root()
567
_currentdir = os.getcwdu()
568
short_id = self.id().replace('bzrlib.tests.', '') \
569
.replace('__main__.', '')
570
self.test_dir = osutils.pathjoin(self.TEST_ROOT, short_id)
571
os.mkdir(self.test_dir)
572
os.chdir(self.test_dir)
573
os.environ['HOME'] = self.test_dir
574
os.environ['APPDATA'] = self.test_dir
575
def _leaveDirectory():
576
os.chdir(_currentdir)
577
self.addCleanup(_leaveDirectory)
579
def build_tree(self, shape, line_endings='native', transport=None):
580
"""Build a test tree according to a pattern.
582
shape is a sequence of file specifications. If the final
583
character is '/', a directory is created.
585
This doesn't add anything to a branch.
586
:param line_endings: Either 'binary' or 'native'
587
in binary mode, exact contents are written
588
in native mode, the line endings match the
589
default platform endings.
591
:param transport: A transport to write to, for building trees on
592
VFS's. If the transport is readonly or None,
593
"." is opened automatically.
595
# XXX: It's OK to just create them using forward slashes on windows?
596
if transport is None or transport.is_readonly():
597
transport = bzrlib.transport.get_transport(".")
599
self.assert_(isinstance(name, basestring))
601
transport.mkdir(urlescape(name[:-1]))
603
if line_endings == 'binary':
605
elif line_endings == 'native':
608
raise BzrError('Invalid line ending request %r' % (line_endings,))
609
content = "contents of %s%s" % (name, end)
610
transport.put(urlescape(name), StringIO(content))
612
def build_tree_contents(self, shape):
613
build_tree_contents(shape)
615
def failUnlessExists(self, path):
616
"""Fail unless path, which may be abs or relative, exists."""
617
self.failUnless(osutils.lexists(path))
619
def failIfExists(self, path):
620
"""Fail if path, which may be abs or relative, exists."""
621
self.failIf(osutils.lexists(path))
623
def assertFileEqual(self, content, path):
624
"""Fail if path does not contain 'content'."""
625
self.failUnless(osutils.lexists(path))
626
self.assertEqualDiff(content, open(path, 'r').read())
629
def filter_suite_by_re(suite, pattern):
631
filter_re = re.compile(pattern)
632
for test in iter_suite_tests(suite):
633
if filter_re.search(test.id()):
638
def run_suite(suite, name='test', verbose=False, pattern=".*",
639
stop_on_failure=False, keep_output=False):
640
TestCaseInTempDir._TEST_NAME = name
645
runner = TextTestRunner(stream=sys.stdout,
648
runner.stop_on_failure=stop_on_failure
650
suite = filter_suite_by_re(suite, pattern)
651
result = runner.run(suite)
652
# This is still a little bogus,
653
# but only a little. Folk not using our testrunner will
654
# have to delete their temp directories themselves.
655
if result.wasSuccessful() or not keep_output:
656
if TestCaseInTempDir.TEST_ROOT is not None:
657
shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
659
print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
660
return result.wasSuccessful()
663
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
665
"""Run the whole test suite under the enhanced runner"""
666
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
667
stop_on_failure=stop_on_failure, keep_output=keep_output)
671
"""Build and return TestSuite for the whole program."""
672
from doctest import DocTestSuite
674
global MODULES_TO_DOCTEST
677
'bzrlib.tests.test_ancestry',
678
'bzrlib.tests.test_annotate',
679
'bzrlib.tests.test_api',
680
'bzrlib.tests.test_bad_files',
681
'bzrlib.tests.test_basis_inventory',
682
'bzrlib.tests.test_branch',
683
'bzrlib.tests.test_command',
684
'bzrlib.tests.test_commit',
685
'bzrlib.tests.test_commit_merge',
686
'bzrlib.tests.test_config',
687
'bzrlib.tests.test_conflicts',
688
'bzrlib.tests.test_diff',
689
'bzrlib.tests.test_fetch',
690
'bzrlib.tests.test_gpg',
691
'bzrlib.tests.test_graph',
692
'bzrlib.tests.test_hashcache',
693
'bzrlib.tests.test_http',
694
'bzrlib.tests.test_identitymap',
695
'bzrlib.tests.test_inv',
696
'bzrlib.tests.test_log',
697
'bzrlib.tests.test_merge',
698
'bzrlib.tests.test_merge3',
699
'bzrlib.tests.test_merge_core',
700
'bzrlib.tests.test_missing',
701
'bzrlib.tests.test_msgeditor',
702
'bzrlib.tests.test_nonascii',
703
'bzrlib.tests.test_options',
704
'bzrlib.tests.test_osutils',
705
'bzrlib.tests.test_parent',
706
'bzrlib.tests.test_permissions',
707
'bzrlib.tests.test_plugins',
708
'bzrlib.tests.test_remove',
709
'bzrlib.tests.test_revision',
710
'bzrlib.tests.test_revisionnamespaces',
711
'bzrlib.tests.test_revprops',
712
'bzrlib.tests.test_reweave',
713
'bzrlib.tests.test_rio',
714
'bzrlib.tests.test_sampler',
715
'bzrlib.tests.test_selftest',
716
'bzrlib.tests.test_setup',
717
'bzrlib.tests.test_sftp_transport',
718
'bzrlib.tests.test_smart_add',
719
'bzrlib.tests.test_source',
720
'bzrlib.tests.test_status',
721
'bzrlib.tests.test_store',
722
'bzrlib.tests.test_symbol_versioning',
723
'bzrlib.tests.test_testament',
724
'bzrlib.tests.test_trace',
725
'bzrlib.tests.test_transactions',
726
'bzrlib.tests.test_transport',
727
'bzrlib.tests.test_tsort',
728
'bzrlib.tests.test_ui',
729
'bzrlib.tests.test_uncommit',
730
'bzrlib.tests.test_upgrade',
731
'bzrlib.tests.test_weave',
732
'bzrlib.tests.test_whitebox',
733
'bzrlib.tests.test_workingtree',
734
'bzrlib.tests.test_xml',
736
test_transport_implementations = [
737
'bzrlib.tests.test_transport_implementations']
739
TestCase.BZRPATH = osutils.pathjoin(
740
osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
741
print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
742
print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
745
# python2.4's TestLoader.loadTestsFromNames gives very poor
746
# errors if it fails to load a named module - no indication of what's
747
# actually wrong, just "no such module". We should probably override that
748
# class, but for the moment just load them ourselves. (mbp 20051202)
749
loader = TestLoader()
750
from bzrlib.transport import TransportTestProviderAdapter
751
adapter = TransportTestProviderAdapter()
752
for mod_name in test_transport_implementations:
753
mod = _load_module_by_name(mod_name)
754
for test in iter_suite_tests(loader.loadTestsFromModule(mod)):
755
suite.addTests(adapter.adapt(test))
756
for mod_name in testmod_names:
757
mod = _load_module_by_name(mod_name)
758
suite.addTest(loader.loadTestsFromModule(mod))
759
for package in packages_to_test():
760
suite.addTest(package.test_suite())
761
for m in MODULES_TO_TEST:
762
suite.addTest(loader.loadTestsFromModule(m))
763
for m in (MODULES_TO_DOCTEST):
764
suite.addTest(DocTestSuite(m))
765
for name, plugin in bzrlib.plugin.all_plugins().items():
766
if hasattr(plugin, 'test_suite'):
767
suite.addTest(plugin.test_suite())
771
def _load_module_by_name(mod_name):
772
parts = mod_name.split('.')
773
module = __import__(mod_name)
775
# for historical reasons python returns the top-level module even though
776
# it loads the submodule; we need to walk down to get the one we want.
778
module = getattr(module, parts.pop(0))