~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2005-11-28 05:13:41 UTC
  • mfrom: (1185.33.54 merge-recovered)
  • Revision ID: robertc@robertcollins.net-20051128051341-059936f2f29a12c8
Merge from Martin. Adjust check to work with HTTP again.

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
 
 
32
30
import bzrlib.commands
33
31
import bzrlib.trace
34
32
import bzrlib.osutils as osutils
35
 
from bzrlib.selftest import TestUtil
36
 
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
37
 
from bzrlib.selftest.treeshape import build_tree_contents
 
33
from bzrlib.trace import mutter
 
34
from bzrlib.tests.TestUtil import TestLoader, TestSuite
 
35
from bzrlib.tests.treeshape import build_tree_contents
38
36
from bzrlib.errors import BzrError
39
37
 
40
38
MODULES_TO_TEST = []
66
64
 
67
65
 
68
66
class _MyResult(unittest._TextTestResult):
69
 
    """
70
 
    Custom TestResult.
71
 
 
72
 
    No special behaviour for now.
73
 
    """
74
 
 
 
67
    """Custom TestResult.
 
68
 
 
69
    Shows output in a different format, including displaying runtime for tests.
 
70
    """
 
71
 
 
72
    # assumes 80-column window, less 'ERROR 99999ms' = 13ch
75
73
    def _elapsedTime(self):
76
 
        return "(Took %.3fs)" % (time.time() - self._start_time)
 
74
        return "%5dms" % (1000 * (time.time() - self._start_time))
77
75
 
78
76
    def startTest(self, test):
79
77
        unittest.TestResult.startTest(self, test)
80
 
        # TODO: Maybe show test.shortDescription somewhere?
81
 
        what = test.shortDescription() or test.id()        
 
78
        # In a short description, the important words are in
 
79
        # the beginning, but in an id, the important words are
 
80
        # at the end
 
81
        SHOW_DESCRIPTIONS = False
 
82
        what = SHOW_DESCRIPTIONS and test.shortDescription()
 
83
        if what:
 
84
            if len(what) > 65:
 
85
                what = what[:62] + '...'
 
86
        else:
 
87
            what = test.id()
 
88
            if what.startswith('bzrlib.tests.'):
 
89
                what = what[13:]
 
90
            if len(what) > 65:
 
91
                what = '...' + what[-62:]
82
92
        if self.showAll:
83
 
            self.stream.write('%-70.70s' % what)
 
93
            self.stream.write('%-65.65s' % what)
84
94
        self.stream.flush()
85
95
        self._start_time = time.time()
86
96
 
95
105
    def addFailure(self, test, err):
96
106
        unittest.TestResult.addFailure(self, test, err)
97
107
        if self.showAll:
98
 
            self.stream.writeln("FAIL %s" % self._elapsedTime())
 
108
            self.stream.writeln(" FAIL %s" % self._elapsedTime())
99
109
        elif self.dots:
100
110
            self.stream.write('F')
101
111
        self.stream.flush()
102
112
 
103
113
    def addSuccess(self, test):
104
114
        if self.showAll:
105
 
            self.stream.writeln('OK %s' % self._elapsedTime())
 
115
            self.stream.writeln('   OK %s' % self._elapsedTime())
106
116
        elif self.dots:
107
117
            self.stream.write('~')
108
118
        self.stream.flush()
224
234
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
225
235
        self._log_file = os.fdopen(fileno, 'w+')
226
236
        bzrlib.trace.enable_test_log(self._log_file)
227
 
        debug('opened log file %s', name)
228
237
        self._log_file_name = name
229
238
        self.addCleanup(self._finishLogFile)
230
239
 
298
307
            callable()
299
308
 
300
309
    def log(self, *args):
301
 
        logging.debug(*args)
 
310
        mutter(*args)
302
311
 
303
312
    def _get_log(self):
304
313
        """Return as a string the log for this test"""
306
315
            return open(self._log_file_name).read()
307
316
        else:
308
317
            return self._log_contents
 
318
        # TODO: Delete the log after it's been read in
309
319
 
310
320
    def capture(self, cmd, retcode=0):
311
321
        """Shortcut that splits cmd into words, runs, and returns stdout"""
334
344
        stdout = StringIO()
335
345
        stderr = StringIO()
336
346
        self.log('run bzr: %s', ' '.join(argv))
 
347
        # FIXME: don't call into logging here
337
348
        handler = logging.StreamHandler(stderr)
338
349
        handler.setFormatter(bzrlib.trace.QuietFormatter())
339
350
        handler.setLevel(logging.INFO)
474
485
        super(TestCaseInTempDir, self).setUp()
475
486
        self._make_test_root()
476
487
        _currentdir = os.getcwdu()
477
 
        short_id = self.id().replace('bzrlib.selftest.', '') \
 
488
        short_id = self.id().replace('bzrlib.tests.', '') \
478
489
                   .replace('__main__.', '')
479
490
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
480
491
        os.mkdir(self.test_dir)
512
523
                f.close()
513
524
 
514
525
    def build_tree_contents(self, shape):
515
 
        bzrlib.selftest.build_tree_contents(shape)
 
526
        bzrlib.tests.build_tree_contents(shape)
516
527
 
517
528
    def failUnlessExists(self, path):
518
529
        """Fail unless path, which may be abs or relative, exists."""
528
539
    def test_logging(self):
529
540
        """Test logs are captured when a test fails."""
530
541
        self.log('a test message')
 
542
        self._log_file.flush()
531
543
        self.assertContainsRe(self._get_log(), 'a test message\n')
532
544
 
533
545
 
584
596
    # exception is hidden by unittest.  Sucks.  Should either fix that or
585
597
    # perhaps import them and pass them to unittest as modules.
586
598
    testmod_names = \
587
 
                  ['bzrlib.selftest.MetaTestLog',
588
 
                   'bzrlib.selftest.testapi',
589
 
                   'bzrlib.selftest.testgpg',
590
 
                   'bzrlib.selftest.testidentitymap',
591
 
                   'bzrlib.selftest.testinv',
592
 
                   'bzrlib.selftest.test_ancestry',
593
 
                   'bzrlib.selftest.test_commit',
594
 
                   'bzrlib.selftest.test_command',
595
 
                   'bzrlib.selftest.test_commit_merge',
596
 
                   'bzrlib.selftest.testconfig',
597
 
                   'bzrlib.selftest.versioning',
598
 
                   'bzrlib.selftest.testmerge3',
599
 
                   'bzrlib.selftest.testmerge',
600
 
                   'bzrlib.selftest.testhashcache',
601
 
                   'bzrlib.selftest.teststatus',
602
 
                   'bzrlib.selftest.testlog',
603
 
                   'bzrlib.selftest.testrevisionnamespaces',
604
 
                   'bzrlib.selftest.testbranch',
605
 
                   'bzrlib.selftest.testrevision',
606
 
                   'bzrlib.selftest.test_revision_info',
607
 
                   'bzrlib.selftest.test_merge_core',
608
 
                   'bzrlib.selftest.test_smart_add',
609
 
                   'bzrlib.selftest.test_bad_files',
610
 
                   'bzrlib.selftest.testdiff',
611
 
                   'bzrlib.selftest.test_parent',
612
 
                   'bzrlib.selftest.test_xml',
613
 
                   'bzrlib.selftest.test_weave',
614
 
                   'bzrlib.selftest.testfetch',
615
 
                   'bzrlib.selftest.whitebox',
616
 
                   'bzrlib.selftest.teststore',
617
 
                   'bzrlib.selftest.blackbox',
618
 
                   'bzrlib.selftest.testsampler',
619
 
                   'bzrlib.selftest.testtransactions',
620
 
                   'bzrlib.selftest.testtransport',
621
 
                   'bzrlib.selftest.testsftp',
622
 
                   'bzrlib.selftest.testgraph',
623
 
                   'bzrlib.selftest.testworkingtree',
624
 
                   'bzrlib.selftest.test_upgrade',
625
 
                   'bzrlib.selftest.test_conflicts',
626
 
                   'bzrlib.selftest.testtestament',
627
 
                   'bzrlib.selftest.testannotate',
628
 
                   'bzrlib.selftest.testrevprops',
629
 
                   'bzrlib.selftest.testoptions',
630
 
                   'bzrlib.selftest.testhttp',
631
 
                   'bzrlib.selftest.testnonascii',
632
 
                   'bzrlib.selftest.testreweave',
633
 
                   'bzrlib.selftest.testtsort',
634
 
                   'bzrlib.selftest.testtrace',
 
599
                  ['bzrlib.tests.MetaTestLog',
 
600
                   'bzrlib.tests.test_api',
 
601
                   'bzrlib.tests.test_gpg',
 
602
                   'bzrlib.tests.test_identitymap',
 
603
                   'bzrlib.tests.test_inv',
 
604
                   'bzrlib.tests.test_ancestry',
 
605
                   'bzrlib.tests.test_commit',
 
606
                   'bzrlib.tests.test_command',
 
607
                   'bzrlib.tests.test_commit_merge',
 
608
                   'bzrlib.tests.test_config',
 
609
                   'bzrlib.tests.test_merge3',
 
610
                   'bzrlib.tests.test_merge',
 
611
                   'bzrlib.tests.test_hashcache',
 
612
                   'bzrlib.tests.test_status',
 
613
                   'bzrlib.tests.test_log',
 
614
                   'bzrlib.tests.test_revisionnamespaces',
 
615
                   'bzrlib.tests.test_branch',
 
616
                   'bzrlib.tests.test_revision',
 
617
                   'bzrlib.tests.test_revision_info',
 
618
                   'bzrlib.tests.test_merge_core',
 
619
                   'bzrlib.tests.test_smart_add',
 
620
                   'bzrlib.tests.test_bad_files',
 
621
                   'bzrlib.tests.test_diff',
 
622
                   'bzrlib.tests.test_parent',
 
623
                   'bzrlib.tests.test_xml',
 
624
                   'bzrlib.tests.test_weave',
 
625
                   'bzrlib.tests.test_fetch',
 
626
                   'bzrlib.tests.test_whitebox',
 
627
                   'bzrlib.tests.test_store',
 
628
                   'bzrlib.tests.blackbox',
 
629
                   'bzrlib.tests.blackbox.versioning',
 
630
                   'bzrlib.tests.test_sampler',
 
631
                   'bzrlib.tests.test_transactions',
 
632
                   'bzrlib.tests.test_transport',
 
633
                   'bzrlib.tests.test_sftp',
 
634
                   'bzrlib.tests.test_graph',
 
635
                   'bzrlib.tests.test_workingtree',
 
636
                   'bzrlib.tests.test_upgrade',
 
637
                   'bzrlib.tests.test_uncommit',
 
638
                   'bzrlib.tests.test_conflicts',
 
639
                   'bzrlib.tests.test_testament',
 
640
                   'bzrlib.tests.test_annotate',
 
641
                   'bzrlib.tests.test_revprops',
 
642
                   'bzrlib.tests.test_options',
 
643
                   'bzrlib.tests.test_http',
 
644
                   'bzrlib.tests.test_nonascii',
 
645
                   'bzrlib.tests.test_reweave',
 
646
                   'bzrlib.tests.test_tsort',
 
647
                   'bzrlib.tests.test_trace',
 
648
                   'bzrlib.tests.test_basicio',
635
649
                   ]
636
650
 
637
651
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,