~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Aaron Bentley
  • Date: 2005-09-29 21:07:17 UTC
  • mfrom: (1393.1.6)
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1419.
  • Revision ID: abentley@panoramicfeedback.com-20050929210717-cd73981590f17017
Merged the weave changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import errno
25
25
import subprocess
26
26
import shutil
27
 
import testsweet
28
27
 
29
28
import bzrlib.commands
30
29
import bzrlib.trace
31
30
import bzrlib.fetch
 
31
from bzrlib.selftest import TestUtil
 
32
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
32
33
 
33
34
 
34
35
MODULES_TO_TEST = []
36
37
 
37
38
from logging import debug, warning, error
38
39
 
 
40
 
 
41
 
 
42
class EarlyStoppingTestResultAdapter(object):
 
43
    """An adapter for TestResult to stop at the first first failure or error"""
 
44
 
 
45
    def __init__(self, result):
 
46
        self._result = result
 
47
 
 
48
    def addError(self, test, err):
 
49
        self._result.addError(test, err)
 
50
        self._result.stop()
 
51
 
 
52
    def addFailure(self, test, err):
 
53
        self._result.addFailure(test, err)
 
54
        self._result.stop()
 
55
 
 
56
    def __getattr__(self, name):
 
57
        return getattr(self._result, name)
 
58
 
 
59
    def __setattr__(self, name, value):
 
60
        if name == '_result':
 
61
            object.__setattr__(self, name, value)
 
62
        return setattr(self._result, name, value)
 
63
 
 
64
 
 
65
class _MyResult(unittest._TextTestResult):
 
66
    """
 
67
    Custom TestResult.
 
68
 
 
69
    No special behaviour for now.
 
70
    """
 
71
 
 
72
    def startTest(self, test):
 
73
        unittest.TestResult.startTest(self, test)
 
74
        # TODO: Maybe show test.shortDescription somewhere?
 
75
        what = test.shortDescription() or test.id()        
 
76
        if self.showAll:
 
77
            self.stream.write('%-70.70s' % what)
 
78
        self.stream.flush()
 
79
 
 
80
    def addError(self, test, err):
 
81
        super(_MyResult, self).addError(test, err)
 
82
        self.stream.flush()
 
83
 
 
84
    def addFailure(self, test, err):
 
85
        super(_MyResult, self).addFailure(test, err)
 
86
        self.stream.flush()
 
87
 
 
88
    def addSuccess(self, test):
 
89
        if self.showAll:
 
90
            self.stream.writeln('OK')
 
91
        elif self.dots:
 
92
            self.stream.write('~')
 
93
        self.stream.flush()
 
94
        unittest.TestResult.addSuccess(self, test)
 
95
 
 
96
    def printErrorList(self, flavour, errors):
 
97
        for test, err in errors:
 
98
            self.stream.writeln(self.separator1)
 
99
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
 
100
            if hasattr(test, '_get_log'):
 
101
                self.stream.writeln()
 
102
                self.stream.writeln('log from this test:')
 
103
                print >>self.stream, test._get_log()
 
104
            self.stream.writeln(self.separator2)
 
105
            self.stream.writeln("%s" % err)
 
106
 
 
107
 
 
108
class TextTestRunner(unittest.TextTestRunner):
 
109
 
 
110
    def _makeResult(self):
 
111
        result = _MyResult(self.stream, self.descriptions, self.verbosity)
 
112
        return EarlyStoppingTestResultAdapter(result)
 
113
 
 
114
 
 
115
class filteringVisitor(TestUtil.TestVisitor):
 
116
    """I accruse all the testCases I visit that pass a regexp filter on id
 
117
    into my suite
 
118
    """
 
119
 
 
120
    def __init__(self, filter):
 
121
        import re
 
122
        TestUtil.TestVisitor.__init__(self)
 
123
        self._suite=None
 
124
        self.filter=re.compile(filter)
 
125
 
 
126
    def suite(self):
 
127
        """answer the suite we are building"""
 
128
        if self._suite is None:
 
129
            self._suite=TestUtil.TestSuite()
 
130
        return self._suite
 
131
 
 
132
    def visitCase(self, aCase):
 
133
        if self.filter.match(aCase.id()):
 
134
            self.suite().addTest(aCase)
 
135
 
 
136
class TestSkipped(Exception):
 
137
    """Indicates that a test was intentionally skipped, rather than failing."""
 
138
    # XXX: Not used yet
 
139
 
 
140
 
39
141
class CommandFailed(Exception):
40
142
    pass
41
143
 
55
157
    BZRPATH = 'bzr'
56
158
 
57
159
    def setUp(self):
58
 
        # this replaces the default testsweet.TestCase; we don't want logging changed
59
160
        unittest.TestCase.setUp(self)
60
161
        bzrlib.trace.disable_default_logging()
61
162
        self._enable_file_logging()
68
169
 
69
170
        hdlr = logging.StreamHandler(self._log_file)
70
171
        hdlr.setLevel(logging.DEBUG)
71
 
        hdlr.setFormatter(logging.Formatter('%(levelname)4.4s  %(message)s'))
 
172
        hdlr.setFormatter(logging.Formatter('%(levelname)8s  %(message)s'))
72
173
        logging.getLogger('').addHandler(hdlr)
73
174
        logging.getLogger('').setLevel(logging.DEBUG)
74
175
        self._log_hdlr = hdlr
153
254
        return self.run_bzr_captured(args, retcode)
154
255
 
155
256
    def check_inventory_shape(self, inv, shape):
156
 
        """
157
 
        Compare an inventory to a list of expected names.
 
257
        """Compare an inventory to a list of expected names.
158
258
 
159
259
        Fail if they are not precisely equal.
160
260
        """
232
332
        if contents != expect:
233
333
            self.log("expected: %r" % expect)
234
334
            self.log("actually: %r" % contents)
235
 
            self.fail("contents of %s not as expected")
 
335
            self.fail("contents of %s not as expected" % filename)
236
336
 
237
337
    def _make_test_root(self):
238
338
        if TestCaseInTempDir.TEST_ROOT is not None:
259
359
        super(TestCaseInTempDir, self).setUp()
260
360
        self._make_test_root()
261
361
        self._currentdir = os.getcwdu()
262
 
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
 
362
        short_id = self.id().replace('bzrlib.selftest.', '') \
 
363
                   .replace('__main__.', '')
 
364
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
263
365
        os.mkdir(self.test_dir)
264
366
        os.chdir(self.test_dir)
265
367
        
295
397
        ##assert 0
296
398
 
297
399
 
 
400
 
 
401
def run_suite(suite, name='test', verbose=False, pattern=".*"):
 
402
    TestCaseInTempDir._TEST_NAME = name
 
403
    if verbose:
 
404
        verbosity = 2
 
405
    else:
 
406
        verbosity = 1
 
407
    runner = TextTestRunner(stream=sys.stdout,
 
408
                            descriptions=0,
 
409
                            verbosity=verbosity)
 
410
    visitor = filteringVisitor(pattern)
 
411
    suite.visit(visitor)
 
412
    result = runner.run(visitor.suite())
 
413
    # This is still a little bogus, 
 
414
    # but only a little. Folk not using our testrunner will
 
415
    # have to delete their temp directories themselves.
 
416
    if result.wasSuccessful():
 
417
        if TestCaseInTempDir.TEST_ROOT is not None:
 
418
            shutil.rmtree(TestCaseInTempDir.TEST_ROOT) 
 
419
    else:
 
420
        print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
 
421
    return result.wasSuccessful()
 
422
 
 
423
 
298
424
def selftest(verbose=False, pattern=".*"):
299
 
    return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
425
    """Run the whole test suite under the enhanced runner"""
 
426
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
300
427
 
301
428
 
302
429
def test_suite():
303
 
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
304
 
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
305
 
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
 
430
    """Build and return TestSuite for the whole program."""
 
431
    import bzrlib.store, bzrlib.inventory, bzrlib.branch
 
432
    import bzrlib.osutils, bzrlib.merge3, bzrlib.plugin
306
433
    from doctest import DocTestSuite
307
434
 
308
435
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
309
436
 
310
437
    testmod_names = \
311
438
                  ['bzrlib.selftest.MetaTestLog',
312
 
                   'bzrlib.selftest.test_parent',
313
439
                   'bzrlib.selftest.testinv',
314
 
                   'bzrlib.selftest.testfetch',
 
440
                   'bzrlib.selftest.test_ancestry',
 
441
                   'bzrlib.selftest.test_commit',
 
442
                   'bzrlib.selftest.test_commit_merge',
315
443
                   'bzrlib.selftest.versioning',
316
 
                   'bzrlib.selftest.whitebox',
317
444
                   'bzrlib.selftest.testmerge3',
318
445
                   'bzrlib.selftest.testmerge',
319
446
                   'bzrlib.selftest.testhashcache',
320
447
                   'bzrlib.selftest.teststatus',
321
448
                   'bzrlib.selftest.testlog',
322
 
                   'bzrlib.selftest.blackbox',
323
449
                   'bzrlib.selftest.testrevisionnamespaces',
324
450
                   'bzrlib.selftest.testbranch',
325
451
                   'bzrlib.selftest.testremotebranch',
329
455
                   'bzrlib.selftest.test_smart_add',
330
456
                   'bzrlib.selftest.test_bad_files',
331
457
                   'bzrlib.selftest.testdiff',
 
458
                   'bzrlib.selftest.test_parent',
332
459
                   'bzrlib.selftest.test_xml',
333
 
                   'bzrlib.fetch',
 
460
                   'bzrlib.selftest.test_weave',
 
461
                   'bzrlib.selftest.testfetch',
 
462
                   'bzrlib.selftest.whitebox',
334
463
                   'bzrlib.selftest.teststore',
 
464
                   'bzrlib.selftest.blackbox',
335
465
                   'bzrlib.selftest.testgraph',
336
466
                   ]
337
467