66
85
class _MyResult(unittest._TextTestResult):
70
No special behaviour for now.
88
Shows output in a different format, including displaying runtime for tests.
91
def _elapsedTime(self):
92
return "%5dms" % (1000 * (time.time() - self._start_time))
73
94
def startTest(self, test):
74
95
unittest.TestResult.startTest(self, test)
75
# TODO: Maybe show test.shortDescription somewhere?
76
what = test.shortDescription() or test.id()
96
# In a short description, the important words are in
97
# the beginning, but in an id, the important words are
99
SHOW_DESCRIPTIONS = False
78
self.stream.write('%-70.70s' % what)
101
width = osutils.terminal_width()
102
name_width = width - 15
104
if SHOW_DESCRIPTIONS:
105
what = test.shortDescription()
107
if len(what) > name_width:
108
what = what[:name_width-3] + '...'
111
if what.startswith('bzrlib.tests.'):
113
if len(what) > name_width:
114
what = '...' + what[3-name_width:]
115
what = what.ljust(name_width)
116
self.stream.write(what)
79
117
self.stream.flush()
118
self._start_time = time.time()
81
120
def addError(self, test, err):
82
super(_MyResult, self).addError(test, err)
121
if isinstance(err[1], TestSkipped):
122
return self.addSkipped(test, err)
123
unittest.TestResult.addError(self, test, err)
125
self.stream.writeln("ERROR %s" % self._elapsedTime())
127
self.stream.write('E')
83
128
self.stream.flush()
85
130
def addFailure(self, test, err):
86
super(_MyResult, self).addFailure(test, err)
131
unittest.TestResult.addFailure(self, test, err)
133
self.stream.writeln(" FAIL %s" % self._elapsedTime())
135
self.stream.write('F')
87
136
self.stream.flush()
89
138
def addSuccess(self, test):
91
self.stream.writeln('OK')
140
self.stream.writeln(' OK %s' % self._elapsedTime())
93
142
self.stream.write('~')
94
143
self.stream.flush()
95
144
unittest.TestResult.addSuccess(self, test)
146
def addSkipped(self, test, skip_excinfo):
148
print >>self.stream, ' SKIP %s' % self._elapsedTime()
149
print >>self.stream, ' %s' % skip_excinfo[1]
151
self.stream.write('S')
153
# seems best to treat this as success from point-of-view of unittest
154
# -- it actually does nothing so it barely matters :)
155
unittest.TestResult.addSuccess(self, test)
97
157
def printErrorList(self, flavour, errors):
98
158
for test, err in errors:
99
159
self.stream.writeln(self.separator1)
100
160
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
101
161
if hasattr(test, '_get_log'):
102
self.stream.writeln()
103
self.stream.writeln('log from this test:')
163
print >>self.stream, \
164
('vvvv[log from %s]' % test).ljust(78,'-')
104
165
print >>self.stream, test._get_log()
166
print >>self.stream, \
167
('^^^^[log from %s]' % test).ljust(78,'-')
105
168
self.stream.writeln(self.separator2)
106
169
self.stream.writeln("%s" % err)
109
172
class TextTestRunner(unittest.TextTestRunner):
173
stop_on_failure = False
111
175
def _makeResult(self):
112
176
result = _MyResult(self.stream, self.descriptions, self.verbosity)
113
return EarlyStoppingTestResultAdapter(result)
177
if self.stop_on_failure:
178
result = EarlyStoppingTestResultAdapter(result)
116
182
def iter_suite_tests(suite):
143
209
Error and debug log messages are redirected from their usual
144
210
location into a temporary file, the contents of which can be
145
retrieved by _get_log().
211
retrieved by _get_log(). We use a real OS file, not an in-memory object,
212
so that it can also capture file IO. When the test completes this file
213
is read into memory and removed from disk.
147
215
There are also convenience functions to invoke bzr's command-line
148
routine, and to build and check bzr trees."""
216
routine, and to build and check bzr trees.
218
In addition to the usual method of overriding tearDown(), this class also
219
allows subclasses to register functions into the _cleanups list, which is
220
run in order as the object is torn down. It's less likely this will be
221
accidentally overlooked.
225
_log_file_name = None
153
229
unittest.TestCase.setUp(self)
231
self._cleanEnvironment()
154
232
bzrlib.trace.disable_default_logging()
155
self._enable_file_logging()
158
def _enable_file_logging(self):
235
def _ndiff_strings(self, a, b):
236
"""Return ndiff between two strings containing lines.
238
A trailing newline is added if missing to make the strings
240
if b and b[-1] != '\n':
242
if a and a[-1] != '\n':
244
difflines = difflib.ndiff(a.splitlines(True),
246
linejunk=lambda x: False,
247
charjunk=lambda x: False)
248
return ''.join(difflines)
250
def assertEqualDiff(self, a, b):
251
"""Assert two texts are equal, if not raise an exception.
253
This is intended for use with multi-line strings where it can
254
be hard to find the differences by eye.
256
# TODO: perhaps override assertEquals to call this for strings?
259
raise AssertionError("texts not equal:\n" +
260
self._ndiff_strings(a, b))
262
def assertStartsWith(self, s, prefix):
263
if not s.startswith(prefix):
264
raise AssertionError('string %r does not start with %r' % (s, prefix))
266
def assertEndsWith(self, s, suffix):
267
if not s.endswith(prefix):
268
raise AssertionError('string %r does not end with %r' % (s, suffix))
270
def assertContainsRe(self, haystack, needle_re):
271
"""Assert that a contains something matching a regular expression."""
272
if not re.search(needle_re, haystack):
273
raise AssertionError('pattern "%s" not found in "%s"'
274
% (needle_re, haystack))
276
def AssertSubset(self, sublist, superlist):
277
"""Assert that every entry in sublist is present in superlist."""
279
for entry in sublist:
280
if entry not in superlist:
281
missing.append(entry)
283
raise AssertionError("value(s) %r not present in container %r" %
284
(missing, superlist))
286
def _startLogFile(self):
287
"""Send bzr and test log messages to a temporary file.
289
The file is removed as the test is torn down.
159
291
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
161
self._log_file = os.fdopen(fileno, 'w+')
163
hdlr = logging.StreamHandler(self._log_file)
164
hdlr.setLevel(logging.DEBUG)
165
hdlr.setFormatter(logging.Formatter('%(levelname)8s %(message)s'))
166
logging.getLogger('').addHandler(hdlr)
167
logging.getLogger('').setLevel(logging.DEBUG)
168
self._log_hdlr = hdlr
169
debug('opened log file %s', name)
292
encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
293
self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
294
bzrlib.trace.enable_test_log(self._log_file)
171
295
self._log_file_name = name
174
logging.getLogger('').removeHandler(self._log_hdlr)
175
bzrlib.trace.enable_default_logging()
176
logging.debug('%s teardown', self.id())
296
self.addCleanup(self._finishLogFile)
298
def _finishLogFile(self):
299
"""Finished with the log file.
301
Read contents into memory, close, and delete.
303
bzrlib.trace.disable_test_log()
304
self._log_file.seek(0)
305
self._log_contents = self._log_file.read()
177
306
self._log_file.close()
307
os.remove(self._log_file_name)
308
self._log_file = self._log_file_name = None
310
def addCleanup(self, callable):
311
"""Arrange to run a callable when this case is torn down.
313
Callables are run in the reverse of the order they are registered,
314
ie last-in first-out.
316
if callable in self._cleanups:
317
raise ValueError("cleanup function %r already registered on %s"
319
self._cleanups.append(callable)
321
def _cleanEnvironment(self):
324
'APPDATA': os.getcwd(),
329
self.addCleanup(self._restoreEnvironment)
330
for name, value in new_env.iteritems():
331
self._captureVar(name, value)
334
def _captureVar(self, name, newvalue):
335
"""Set an environment variable, preparing it to be reset when finished."""
336
self.__old_env[name] = os.environ.get(name, None)
338
if name in os.environ:
341
os.environ[name] = newvalue
344
def _restoreVar(name, value):
346
if name in os.environ:
349
os.environ[name] = value
351
def _restoreEnvironment(self):
352
for name, value in self.__old_env.iteritems():
353
self._restoreVar(name, value)
178
357
unittest.TestCase.tearDown(self)
359
def _runCleanups(self):
360
"""Run registered cleanup functions.
362
This should only be called from TestCase.tearDown.
364
for cleanup_fn in reversed(self._cleanups):
180
367
def log(self, *args):
183
370
def _get_log(self):
184
371
"""Return as a string the log for this test"""
185
return open(self._log_file_name).read()
188
def capture(self, cmd):
372
if self._log_file_name:
373
return open(self._log_file_name).read()
375
return self._log_contents
376
# TODO: Delete the log after it's been read in
378
def capture(self, cmd, retcode=0):
189
379
"""Shortcut that splits cmd into words, runs, and returns stdout"""
190
return self.run_bzr_captured(cmd.split())[0]
380
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
192
382
def run_bzr_captured(self, argv, retcode=0):
193
"""Invoke bzr and return (result, stdout, stderr).
383
"""Invoke bzr and return (stdout, stderr).
195
385
Useful for code that wants to check the contents of the
196
386
output, the way error messages are presented, etc.
344
535
# successfully created
345
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
536
TestCaseInTempDir.TEST_ROOT = osutils.abspath(root)
347
538
# make a fake bzr directory there to prevent any tests propagating
348
539
# up onto the source directory's real branch
349
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
540
os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
352
543
super(TestCaseInTempDir, self).setUp()
353
544
self._make_test_root()
354
self._currentdir = os.getcwdu()
355
short_id = self.id().replace('bzrlib.selftest.', '') \
545
_currentdir = os.getcwdu()
546
short_id = self.id().replace('bzrlib.tests.', '') \
356
547
.replace('__main__.', '')
357
self.test_dir = os.path.join(self.TEST_ROOT, short_id)
548
self.test_dir = osutils.pathjoin(self.TEST_ROOT, short_id)
358
549
os.mkdir(self.test_dir)
359
550
os.chdir(self.test_dir)
551
os.environ['HOME'] = self.test_dir
552
os.environ['APPDATA'] = self.test_dir
553
def _leaveDirectory():
554
os.chdir(_currentdir)
555
self.addCleanup(_leaveDirectory)
362
os.chdir(self._currentdir)
363
super(TestCaseInTempDir, self).tearDown()
365
def build_tree(self, shape):
557
def build_tree(self, shape, line_endings='native'):
366
558
"""Build a test tree according to a pattern.
368
560
shape is a sequence of file specifications. If the final
369
561
character is '/', a directory is created.
371
563
This doesn't add anything to a branch.
564
:param line_endings: Either 'binary' or 'native'
565
in binary mode, exact contents are written
566
in native mode, the line endings match the
567
default platform endings.
373
569
# XXX: It's OK to just create them using forward slashes on windows?
374
570
for name in shape:
375
assert isinstance(name, basestring)
571
self.assert_(isinstance(name, basestring))
376
572
if name[-1] == '/':
377
573
os.mkdir(name[:-1])
575
if line_endings == 'binary':
577
elif line_endings == 'native':
580
raise BzrError('Invalid line ending request %r' % (line_endings,))
380
581
print >>f, "contents of", name
584
def build_tree_contents(self, shape):
585
build_tree_contents(shape)
383
587
def failUnlessExists(self, path):
384
588
"""Fail unless path, which may be abs or relative, exists."""
385
self.failUnless(os.path.exists(path))
589
self.failUnless(osutils.lexists(path))
591
def failIfExists(self, path):
592
"""Fail if path, which may be abs or relative, exists."""
593
self.failIf(osutils.lexists(path))
388
class MetaTestLog(TestCase):
389
def test_logging(self):
390
"""Test logs are captured when a test fails."""
391
logging.info('an info message')
392
warning('something looks dodgy...')
393
logging.debug('hello, test is running')
595
def assertFileEqual(self, content, path):
596
"""Fail if path does not contain 'content'."""
597
self.failUnless(osutils.lexists(path))
598
self.assertEqualDiff(content, open(path, 'r').read())
397
601
def filter_suite_by_re(suite, pattern):
398
result = TestUtil.TestSuite()
399
603
filter_re = re.compile(pattern)
400
604
for test in iter_suite_tests(suite):
401
if filter_re.match(test.id()):
605
if filter_re.search(test.id()):
402
606
result.addTest(test)
406
def filter_suite_by_names(suite, wanted_names):
407
"""Return a new suite containing only selected tests.
409
Names are considered to match if any name is a substring of the
410
fully-qualified test id (i.e. the class ."""
412
for test in iter_suite_tests(suite):
414
for p in wanted_names:
415
if this_id.find(p) != -1:
420
def run_suite(suite, name='test', verbose=False, pattern=".*", testnames=None):
610
def run_suite(suite, name='test', verbose=False, pattern=".*",
611
stop_on_failure=False, keep_output=False):
421
612
TestCaseInTempDir._TEST_NAME = name
442
632
return result.wasSuccessful()
445
def selftest(verbose=False, pattern=".*", testnames=None):
635
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
446
637
"""Run the whole test suite under the enhanced runner"""
447
638
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
639
stop_on_failure=stop_on_failure, keep_output=keep_output)
451
642
def test_suite():
452
643
"""Build and return TestSuite for the whole program."""
453
import bzrlib.store, bzrlib.inventory, bzrlib.branch
454
import bzrlib.osutils, bzrlib.merge3, bzrlib.plugin
455
644
from doctest import DocTestSuite
457
global MODULES_TO_TEST, MODULES_TO_DOCTEST
646
global MODULES_TO_DOCTEST
460
['bzrlib.selftest.MetaTestLog',
461
'bzrlib.selftest.testinv',
462
'bzrlib.selftest.test_ancestry',
463
'bzrlib.selftest.test_commit',
464
'bzrlib.selftest.test_commit_merge',
465
'bzrlib.selftest.versioning',
466
'bzrlib.selftest.testmerge3',
467
'bzrlib.selftest.testmerge',
468
'bzrlib.selftest.testhashcache',
469
'bzrlib.selftest.teststatus',
470
'bzrlib.selftest.testlog',
471
'bzrlib.selftest.testrevisionnamespaces',
472
'bzrlib.selftest.testbranch',
473
'bzrlib.selftest.testrevision',
474
'bzrlib.selftest.test_revision_info',
475
'bzrlib.selftest.test_merge_core',
476
'bzrlib.selftest.test_smart_add',
477
'bzrlib.selftest.test_bad_files',
478
'bzrlib.selftest.testdiff',
479
'bzrlib.selftest.test_parent',
480
'bzrlib.selftest.test_xml',
481
'bzrlib.selftest.test_weave',
482
'bzrlib.selftest.testfetch',
483
'bzrlib.selftest.whitebox',
484
'bzrlib.selftest.teststore',
485
'bzrlib.selftest.blackbox',
486
'bzrlib.selftest.testsampler',
487
'bzrlib.selftest.testtransport',
488
'bzrlib.selftest.testgraph',
489
'bzrlib.selftest.testworkingtree',
490
'bzrlib.selftest.test_upgrade',
491
'bzrlib.selftest.test_conflicts',
649
'bzrlib.tests.test_ancestry',
650
'bzrlib.tests.test_annotate',
651
'bzrlib.tests.test_api',
652
'bzrlib.tests.test_bad_files',
653
'bzrlib.tests.test_basis_inventory',
654
'bzrlib.tests.test_bound_sftp',
655
'bzrlib.tests.test_branch',
656
'bzrlib.tests.test_command',
657
'bzrlib.tests.test_commit',
658
'bzrlib.tests.test_commit_merge',
659
'bzrlib.tests.test_config',
660
'bzrlib.tests.test_conflicts',
661
'bzrlib.tests.test_diff',
662
'bzrlib.tests.test_fetch',
663
'bzrlib.tests.test_gpg',
664
'bzrlib.tests.test_graph',
665
'bzrlib.tests.test_hashcache',
666
'bzrlib.tests.test_http',
667
'bzrlib.tests.test_identitymap',
668
'bzrlib.tests.test_inv',
669
'bzrlib.tests.test_log',
670
'bzrlib.tests.test_merge',
671
'bzrlib.tests.test_merge3',
672
'bzrlib.tests.test_merge_core',
673
'bzrlib.tests.test_missing',
674
'bzrlib.tests.test_msgeditor',
675
'bzrlib.tests.test_nonascii',
676
'bzrlib.tests.test_options',
677
'bzrlib.tests.test_osutils',
678
'bzrlib.tests.test_parent',
679
'bzrlib.tests.test_permissions',
680
'bzrlib.tests.test_plugins',
681
'bzrlib.tests.test_remove',
682
'bzrlib.tests.test_revision',
683
'bzrlib.tests.test_revisionnamespaces',
684
'bzrlib.tests.test_revprops',
685
'bzrlib.tests.test_reweave',
686
'bzrlib.tests.test_rio',
687
'bzrlib.tests.test_sampler',
688
'bzrlib.tests.test_selftest',
689
'bzrlib.tests.test_setup',
690
'bzrlib.tests.test_sftp_transport',
691
'bzrlib.tests.test_smart_add',
692
'bzrlib.tests.test_source',
693
'bzrlib.tests.test_status',
694
'bzrlib.tests.test_store',
695
'bzrlib.tests.test_testament',
696
'bzrlib.tests.test_trace',
697
'bzrlib.tests.test_transactions',
698
'bzrlib.tests.test_transport',
699
'bzrlib.tests.test_tsort',
700
'bzrlib.tests.test_ui',
701
'bzrlib.tests.test_uncommit',
702
'bzrlib.tests.test_upgrade',
703
'bzrlib.tests.test_weave',
704
'bzrlib.tests.test_whitebox',
705
'bzrlib.tests.test_workingtree',
706
'bzrlib.tests.test_xml',
494
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
495
bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
496
if m not in MODULES_TO_DOCTEST:
497
MODULES_TO_DOCTEST.append(m)
499
TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
500
print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
709
TestCase.BZRPATH = osutils.pathjoin(
710
osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
711
print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
712
print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
502
714
suite = TestSuite()
503
suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
715
# python2.4's TestLoader.loadTestsFromNames gives very poor
716
# errors if it fails to load a named module - no indication of what's
717
# actually wrong, just "no such module". We should probably override that
718
# class, but for the moment just load them ourselves. (mbp 20051202)
719
loader = TestLoader()
720
for mod_name in testmod_names:
721
mod = _load_module_by_name(mod_name)
722
suite.addTest(loader.loadTestsFromModule(mod))
723
for package in packages_to_test():
724
suite.addTest(package.test_suite())
504
725
for m in MODULES_TO_TEST:
505
suite.addTest(TestLoader().loadTestsFromModule(m))
726
suite.addTest(loader.loadTestsFromModule(m))
506
727
for m in (MODULES_TO_DOCTEST):
507
728
suite.addTest(DocTestSuite(m))
508
for p in bzrlib.plugin.all_plugins:
509
if hasattr(p, 'test_suite'):
510
suite.addTest(p.test_suite())
729
for name, plugin in bzrlib.plugin.all_plugins().items():
730
if hasattr(plugin, 'test_suite'):
731
suite.addTest(plugin.test_suite())
735
def _load_module_by_name(mod_name):
736
parts = mod_name.split('.')
737
module = __import__(mod_name)
739
# for historical reasons python returns the top-level module even though
740
# it loads the submodule; we need to walk down to get the one we want.
742
module = getattr(module, parts.pop(0))