~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge from mbp.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import bzrlib.inventory
34
34
import bzrlib.merge3
35
35
import bzrlib.osutils
36
 
import bzrlib.osutils as osutils
37
36
import bzrlib.plugin
38
37
import bzrlib.store
39
38
import bzrlib.trace
87
86
    Shows output in a different format, including displaying runtime for tests.
88
87
    """
89
88
 
90
 
    # assumes 80-column window, less 'ERROR 99999ms' = 13ch
91
89
    def _elapsedTime(self):
92
90
        return "%5dms" % (1000 * (time.time() - self._start_time))
93
91
 
97
95
        # the beginning, but in an id, the important words are
98
96
        # at the end
99
97
        SHOW_DESCRIPTIONS = False
100
 
        what = SHOW_DESCRIPTIONS and test.shortDescription()
101
 
        if what:
102
 
            if len(what) > 65:
103
 
                what = what[:62] + '...'
104
 
        else:
105
 
            what = test.id()
106
 
            if what.startswith('bzrlib.tests.'):
107
 
                what = what[13:]
108
 
            if len(what) > 65:
109
 
                what = '...' + what[-62:]
110
98
        if self.showAll:
111
 
            self.stream.write('%-65.65s' % what)
 
99
            width = bzrlib.osutils.terminal_width()
 
100
            name_width = width - 15
 
101
            what = None
 
102
            if SHOW_DESCRIPTIONS:
 
103
                what = test.shortDescription()
 
104
                if what:
 
105
                    if len(what) > name_width:
 
106
                        what = what[:name_width-3] + '...'
 
107
            if what is None:
 
108
                what = test.id()
 
109
                if what.startswith('bzrlib.tests.'):
 
110
                    what = what[13:]
 
111
                if len(what) > name_width:
 
112
                    what = '...' + what[3-name_width:]
 
113
            what = what.ljust(name_width)
 
114
            self.stream.write(what)
112
115
        self.stream.flush()
113
116
        self._start_time = time.time()
114
117
 
321
324
 
322
325
        This should only be called from TestCase.tearDown.
323
326
        """
324
 
        for callable in reversed(self._cleanups):
325
 
            callable()
 
327
        for cleanup_fn in reversed(self._cleanups):
 
328
            cleanup_fn()
326
329
 
327
330
    def log(self, *args):
328
331
        mutter(*args)
545
548
 
546
549
    def failUnlessExists(self, path):
547
550
        """Fail unless path, which may be abs or relative, exists."""
548
 
        self.failUnless(osutils.lexists(path))
 
551
        self.failUnless(bzrlib.osutils.lexists(path))
549
552
        
550
553
    def assertFileEqual(self, content, path):
551
554
        """Fail if path does not contain 'content'."""
552
 
        self.failUnless(osutils.lexists(path))
 
555
        self.failUnless(bzrlib.osutils.lexists(path))
553
556
        self.assertEqualDiff(content, open(path, 'r').read())
554
557
        
555
558
 
556
 
class MetaTestLog(TestCase):
557
 
    def test_logging(self):
558
 
        """Test logs are captured when a test fails."""
559
 
        self.log('a test message')
560
 
        self._log_file.flush()
561
 
        self.assertContainsRe(self._get_log(), 'a test message\n')
562
 
 
563
 
 
564
559
def filter_suite_by_re(suite, pattern):
565
 
    result = TestUtil.TestSuite()
 
560
    result = TestSuite()
566
561
    filter_re = re.compile(pattern)
567
562
    for test in iter_suite_tests(suite):
568
563
        if filter_re.search(test.id()):
608
603
 
609
604
    global MODULES_TO_DOCTEST
610
605
 
611
 
    # FIXME: If these fail to load, e.g. because of a syntax error, the
612
 
    # exception is hidden by unittest.  Sucks.  Should either fix that or
613
 
    # perhaps import them and pass them to unittest as modules.
614
 
    testmod_names = \
615
 
                  ['bzrlib.tests.MetaTestLog',
 
606
    testmod_names = [ \
 
607
                   'bzrlib.tests.test_ancestry',
616
608
                   'bzrlib.tests.test_api',
 
609
                   'bzrlib.tests.test_bad_files',
617
610
                   'bzrlib.tests.test_basicio',
618
 
                   'bzrlib.tests.test_gpg',
619
 
                   'bzrlib.tests.test_identitymap',
620
 
                   'bzrlib.tests.test_inv',
621
 
                   'bzrlib.tests.test_ancestry',
 
611
                   'bzrlib.tests.test_branch',
622
612
                   'bzrlib.tests.test_commit',
623
613
                   'bzrlib.tests.test_command',
624
614
                   'bzrlib.tests.test_commit_merge',
625
615
                   'bzrlib.tests.test_config',
 
616
                   'bzrlib.tests.test_gpg',
 
617
                   'bzrlib.tests.test_graph',
 
618
                   'bzrlib.tests.test_hashcache',
 
619
                   'bzrlib.tests.test_identitymap',
 
620
                   'bzrlib.tests.test_inv',
 
621
                   'bzrlib.tests.test_log',
626
622
                   'bzrlib.tests.test_merge3',
627
623
                   'bzrlib.tests.test_merge',
628
 
                   'bzrlib.tests.test_hashcache',
629
 
                   'bzrlib.tests.test_status',
630
 
                   'bzrlib.tests.test_log',
 
624
                   'bzrlib.tests.test_merge_core',
 
625
                   'bzrlib.tests.test_msgeditor',
631
626
                   'bzrlib.tests.test_revisionnamespaces',
632
 
                   'bzrlib.tests.test_branch',
633
627
                   'bzrlib.tests.test_revision',
634
628
                   'bzrlib.tests.test_revision_info',
635
 
                   'bzrlib.tests.test_merge_core',
 
629
                   'bzrlib.tests.test_status',
636
630
                   'bzrlib.tests.test_smart_add',
637
 
                   'bzrlib.tests.test_bad_files',
638
631
                   'bzrlib.tests.test_diff',
639
632
                   'bzrlib.tests.test_parent',
640
633
                   'bzrlib.tests.test_xml',
646
639
                   'bzrlib.tests.test_transactions',
647
640
                   'bzrlib.tests.test_transport',
648
641
                   'bzrlib.tests.test_sftp',
649
 
                   'bzrlib.tests.test_graph',
650
642
                   'bzrlib.tests.test_workingtree',
651
643
                   'bzrlib.tests.test_upgrade',
652
644
                   'bzrlib.tests.test_uncommit',
 
645
                   'bzrlib.tests.test_ui',
653
646
                   'bzrlib.tests.test_conflicts',
654
 
                   'bzrlib.tests.test_testament',
655
647
                   'bzrlib.tests.test_annotate',
656
 
                   'bzrlib.tests.test_revprops',
657
 
                   'bzrlib.tests.test_options',
658
648
                   'bzrlib.tests.test_http',
659
649
                   'bzrlib.tests.test_nonascii',
 
650
                   'bzrlib.tests.test_options',
660
651
                   'bzrlib.tests.test_plugins',
 
652
                   'bzrlib.tests.test_revprops',
661
653
                   'bzrlib.tests.test_reweave',
 
654
                   'bzrlib.tests.test_rio',
 
655
                   'bzrlib.tests.test_selftest',
 
656
                   'bzrlib.tests.test_testament',
 
657
                   'bzrlib.tests.test_trace',
662
658
                   'bzrlib.tests.test_tsort',
663
 
                   'bzrlib.tests.test_trace',
664
659
                   ]
665
660
 
666
 
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
667
 
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
 
661
    print '%10s: %s' % ('bzr', os.path.realpath(sys.argv[0]))
 
662
    print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
668
663
    print
669
664
    suite = TestSuite()
670
 
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
 
665
    # python2.4's TestLoader.loadTestsFromNames gives very poor 
 
666
    # errors if it fails to load a named module - no indication of what's
 
667
    # actually wrong, just "no such module".  We should probably override that
 
668
    # class, but for the moment just load them ourselves. (mbp 20051202)
 
669
    loader = TestLoader()
 
670
    for mod_name in testmod_names:
 
671
        mod = _load_module_by_name(mod_name)
 
672
        suite.addTest(loader.loadTestsFromModule(mod))
671
673
    for package in packages_to_test():
672
674
        suite.addTest(package.test_suite())
673
675
    for m in MODULES_TO_TEST:
674
 
        suite.addTest(TestLoader().loadTestsFromModule(m))
 
676
        suite.addTest(loader.loadTestsFromModule(m))
675
677
    for m in (MODULES_TO_DOCTEST):
676
678
        suite.addTest(DocTestSuite(m))
677
679
    for name, plugin in bzrlib.plugin.all_plugins().items():
679
681
            suite.addTest(plugin.test_suite())
680
682
    return suite
681
683
 
 
684
 
 
685
def _load_module_by_name(mod_name):
 
686
    parts = mod_name.split('.')
 
687
    module = __import__(mod_name)
 
688
    del parts[0]
 
689
    # for historical reasons python returns the top-level module even though
 
690
    # it loads the submodule; we need to walk down to get the one we want.
 
691
    while parts:
 
692
        module = getattr(module, parts.pop(0))
 
693
    return module