~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Robert Collins
  • Date: 2005-11-22 21:28:30 UTC
  • mfrom: (1185.33.32 bzr.dev)
  • Revision ID: robertc@robertcollins.net-20051122212830-885c284847f0b17b
Merge from mpool.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import unittest
28
28
import time
29
29
 
 
30
from logging import debug, warning, error
 
31
 
30
32
import bzrlib.commands
31
33
import bzrlib.trace
32
 
import bzrlib.fetch
33
34
import bzrlib.osutils as osutils
34
35
from bzrlib.selftest import TestUtil
35
36
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
38
39
MODULES_TO_TEST = []
39
40
MODULES_TO_DOCTEST = []
40
41
 
41
 
from logging import debug, warning, error
42
42
 
43
43
 
44
44
class EarlyStoppingTestResultAdapter(object):
222
222
        """
223
223
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
224
224
        self._log_file = os.fdopen(fileno, 'w+')
225
 
        hdlr = logging.StreamHandler(self._log_file)
226
 
        hdlr.setLevel(logging.DEBUG)
227
 
        hdlr.setFormatter(logging.Formatter('%(levelname)8s  %(message)s'))
228
 
        logging.getLogger('').addHandler(hdlr)
229
 
        logging.getLogger('').setLevel(logging.DEBUG)
230
 
        self._log_hdlr = hdlr
 
225
        bzrlib.trace.enable_test_log(self._log_file)
231
226
        debug('opened log file %s', name)
232
227
        self._log_file_name = name
233
228
        self.addCleanup(self._finishLogFile)
237
232
 
238
233
        Read contents into memory, close, and delete.
239
234
        """
 
235
        bzrlib.trace.disable_test_log()
240
236
        self._log_file.seek(0)
241
237
        self._log_contents = self._log_file.read()
242
238
        self._log_file.close()
277
273
            os.environ['EMAIL'] = self.email
278
274
 
279
275
    def tearDown(self):
280
 
        logging.getLogger('').removeHandler(self._log_hdlr)
281
 
        bzrlib.trace.enable_default_logging()
282
 
        logging.debug('%s teardown', self.id())
283
276
        self._runCleanups()
284
277
        unittest.TestCase.tearDown(self)
285
278
 
512
505
class MetaTestLog(TestCase):
513
506
    def test_logging(self):
514
507
        """Test logs are captured when a test fails."""
515
 
        logging.info('an info message')
516
 
        warning('something looks dodgy...')
517
 
        logging.debug('hello, test is running')
 
508
        self.log('a test message')
 
509
        self.assertContainsRe(self._get_log(), 'a test message\n')
518
510
 
519
511
 
520
512
def filter_suite_by_re(suite, pattern):
527
519
 
528
520
 
529
521
def run_suite(suite, name='test', verbose=False, pattern=".*",
530
 
              stop_on_failure=False):
 
522
              stop_on_failure=False, keep_output=False):
531
523
    TestCaseInTempDir._TEST_NAME = name
532
524
    if verbose:
533
525
        verbosity = 2
543
535
    # This is still a little bogus, 
544
536
    # but only a little. Folk not using our testrunner will
545
537
    # have to delete their temp directories themselves.
546
 
    if result.wasSuccessful():
 
538
    if result.wasSuccessful() or not keep_output:
547
539
        if TestCaseInTempDir.TEST_ROOT is not None:
548
540
            shutil.rmtree(TestCaseInTempDir.TEST_ROOT) 
549
541
    else:
551
543
    return result.wasSuccessful()
552
544
 
553
545
 
554
 
def selftest(verbose=False, pattern=".*", stop_on_failure=True):
 
546
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
 
547
             keep_output=False):
555
548
    """Run the whole test suite under the enhanced runner"""
556
549
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
557
 
                     stop_on_failure=stop_on_failure)
 
550
                     stop_on_failure=stop_on_failure, keep_output=keep_output)
558
551
 
559
552
 
560
553
def test_suite():
565
558
 
566
559
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
567
560
 
 
561
    # FIXME: If these fail to load, e.g. because of a syntax error, the
 
562
    # exception is hidden by unittest.  Sucks.  Should either fix that or
 
563
    # perhaps import them and pass them to unittest as modules.
568
564
    testmod_names = \
569
565
                  ['bzrlib.selftest.MetaTestLog',
570
566
                   'bzrlib.selftest.testapi',
613
609
                   'bzrlib.selftest.testnonascii',
614
610
                   'bzrlib.selftest.testreweave',
615
611
                   'bzrlib.selftest.testtsort',
 
612
                   'bzrlib.selftest.testtrace',
616
613
                   ]
617
614
 
618
615
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,