~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merged the new bzr missing command

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import tempfile
27
27
import unittest
28
28
import time
 
29
import codecs
29
30
 
 
31
import bzrlib.branch
30
32
import bzrlib.commands
 
33
from bzrlib.errors import BzrError
 
34
import bzrlib.inventory
 
35
import bzrlib.merge3
 
36
import bzrlib.osutils
 
37
import bzrlib.plugin
 
38
import bzrlib.store
31
39
import bzrlib.trace
32
 
import bzrlib.osutils as osutils
33
40
from bzrlib.trace import mutter
34
41
from bzrlib.tests.TestUtil import TestLoader, TestSuite
35
42
from bzrlib.tests.treeshape import build_tree_contents
36
 
from bzrlib.errors import BzrError
37
43
 
38
44
MODULES_TO_TEST = []
39
 
MODULES_TO_DOCTEST = []
40
 
 
 
45
MODULES_TO_DOCTEST = [
 
46
                      bzrlib.branch,
 
47
                      bzrlib.commands,
 
48
                      bzrlib.errors,
 
49
                      bzrlib.inventory,
 
50
                      bzrlib.merge3,
 
51
                      bzrlib.osutils,
 
52
                      bzrlib.store,
 
53
                      ]
 
54
def packages_to_test():
 
55
    import bzrlib.tests.blackbox
 
56
    return [
 
57
            bzrlib.tests.blackbox
 
58
            ]
41
59
 
42
60
 
43
61
class EarlyStoppingTestResultAdapter(object):
69
87
    Shows output in a different format, including displaying runtime for tests.
70
88
    """
71
89
 
72
 
    # assumes 80-column window, less 'ERROR 99999ms' = 13ch
73
90
    def _elapsedTime(self):
74
91
        return "%5dms" % (1000 * (time.time() - self._start_time))
75
92
 
79
96
        # the beginning, but in an id, the important words are
80
97
        # at the end
81
98
        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:]
92
99
        if self.showAll:
93
 
            self.stream.write('%-65.65s' % what)
 
100
            width = bzrlib.osutils.terminal_width()
 
101
            name_width = width - 15
 
102
            what = None
 
103
            if SHOW_DESCRIPTIONS:
 
104
                what = test.shortDescription()
 
105
                if what:
 
106
                    if len(what) > name_width:
 
107
                        what = what[:name_width-3] + '...'
 
108
            if what is None:
 
109
                what = test.id()
 
110
                if what.startswith('bzrlib.tests.'):
 
111
                    what = what[13:]
 
112
                if len(what) > name_width:
 
113
                    what = '...' + what[3-name_width:]
 
114
            what = what.ljust(name_width)
 
115
            self.stream.write(what)
94
116
        self.stream.flush()
95
117
        self._start_time = time.time()
96
118
 
232
254
        The file is removed as the test is torn down.
233
255
        """
234
256
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
235
 
        self._log_file = os.fdopen(fileno, 'w+')
 
257
        encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
 
258
        self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
236
259
        bzrlib.trace.enable_test_log(self._log_file)
237
260
        self._log_file_name = name
238
261
        self.addCleanup(self._finishLogFile)
303
326
 
304
327
        This should only be called from TestCase.tearDown.
305
328
        """
306
 
        for callable in reversed(self._cleanups):
307
 
            callable()
 
329
        for cleanup_fn in reversed(self._cleanups):
 
330
            cleanup_fn()
308
331
 
309
332
    def log(self, *args):
310
333
        mutter(*args)
523
546
                f.close()
524
547
 
525
548
    def build_tree_contents(self, shape):
526
 
        bzrlib.tests.build_tree_contents(shape)
 
549
        build_tree_contents(shape)
527
550
 
528
551
    def failUnlessExists(self, path):
529
552
        """Fail unless path, which may be abs or relative, exists."""
530
 
        self.failUnless(osutils.lexists(path))
 
553
        self.failUnless(bzrlib.osutils.lexists(path))
531
554
        
532
555
    def assertFileEqual(self, content, path):
533
556
        """Fail if path does not contain 'content'."""
534
 
        self.failUnless(osutils.lexists(path))
 
557
        self.failUnless(bzrlib.osutils.lexists(path))
535
558
        self.assertEqualDiff(content, open(path, 'r').read())
536
559
        
537
560
 
538
 
class MetaTestLog(TestCase):
539
 
    def test_logging(self):
540
 
        """Test logs are captured when a test fails."""
541
 
        self.log('a test message')
542
 
        self._log_file.flush()
543
 
        self.assertContainsRe(self._get_log(), 'a test message\n')
544
 
 
545
 
 
546
561
def filter_suite_by_re(suite, pattern):
547
 
    result = TestUtil.TestSuite()
 
562
    result = TestSuite()
548
563
    filter_re = re.compile(pattern)
549
564
    for test in iter_suite_tests(suite):
550
565
        if filter_re.search(test.id()):
586
601
 
587
602
def test_suite():
588
603
    """Build and return TestSuite for the whole program."""
589
 
    import bzrlib.store, bzrlib.inventory, bzrlib.branch
590
 
    import bzrlib.osutils, bzrlib.merge3, bzrlib.plugin
591
604
    from doctest import DocTestSuite
592
605
 
593
 
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
 
606
    global MODULES_TO_DOCTEST
594
607
 
595
 
    # FIXME: If these fail to load, e.g. because of a syntax error, the
596
 
    # exception is hidden by unittest.  Sucks.  Should either fix that or
597
 
    # perhaps import them and pass them to unittest as modules.
598
 
    testmod_names = \
599
 
                  ['bzrlib.tests.MetaTestLog',
 
608
    testmod_names = [ \
600
609
                   'bzrlib.tests.test_api',
601
610
                   'bzrlib.tests.test_gpg',
602
611
                   'bzrlib.tests.test_identitymap',
625
634
                   'bzrlib.tests.test_fetch',
626
635
                   'bzrlib.tests.test_whitebox',
627
636
                   'bzrlib.tests.test_store',
628
 
                   'bzrlib.tests.blackbox',
629
 
                   'bzrlib.tests.blackbox.versioning',
630
637
                   'bzrlib.tests.test_sampler',
631
638
                   'bzrlib.tests.test_transactions',
632
639
                   'bzrlib.tests.test_transport',
635
642
                   'bzrlib.tests.test_workingtree',
636
643
                   'bzrlib.tests.test_upgrade',
637
644
                   'bzrlib.tests.test_uncommit',
 
645
                   'bzrlib.tests.test_ui',
638
646
                   'bzrlib.tests.test_conflicts',
639
647
                   'bzrlib.tests.test_testament',
640
648
                   'bzrlib.tests.test_annotate',
642
650
                   'bzrlib.tests.test_options',
643
651
                   'bzrlib.tests.test_http',
644
652
                   'bzrlib.tests.test_nonascii',
 
653
                   'bzrlib.tests.test_plugins',
645
654
                   'bzrlib.tests.test_reweave',
646
655
                   'bzrlib.tests.test_tsort',
647
656
                   'bzrlib.tests.test_trace',
648
 
                   'bzrlib.tests.test_basicio',
 
657
                   'bzrlib.tests.test_rio',
 
658
                   'bzrlib.tests.test_msgeditor',
 
659
                   'bzrlib.tests.test_selftest',
 
660
                   'bzrlib.tests.test_missing',
649
661
                   ]
650
662
 
651
 
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
652
 
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3,
653
 
              bzrlib.errors,
654
 
              ):
655
 
        if m not in MODULES_TO_DOCTEST:
656
 
            MODULES_TO_DOCTEST.append(m)
657
 
 
658
 
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
659
 
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
 
663
    print '%10s: %s' % ('bzr', os.path.realpath(sys.argv[0]))
 
664
    print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
660
665
    print
661
666
    suite = TestSuite()
662
 
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
 
667
    # python2.4's TestLoader.loadTestsFromNames gives very poor 
 
668
    # errors if it fails to load a named module - no indication of what's
 
669
    # actually wrong, just "no such module".  We should probably override that
 
670
    # class, but for the moment just load them ourselves. (mbp 20051202)
 
671
    loader = TestLoader()
 
672
    for mod_name in testmod_names:
 
673
        mod = _load_module_by_name(mod_name)
 
674
        suite.addTest(loader.loadTestsFromModule(mod))
 
675
    for package in packages_to_test():
 
676
        suite.addTest(package.test_suite())
663
677
    for m in MODULES_TO_TEST:
664
 
         suite.addTest(TestLoader().loadTestsFromModule(m))
 
678
        suite.addTest(loader.loadTestsFromModule(m))
665
679
    for m in (MODULES_TO_DOCTEST):
666
680
        suite.addTest(DocTestSuite(m))
667
 
    for p in bzrlib.plugin.all_plugins:
668
 
        if hasattr(p, 'test_suite'):
669
 
            suite.addTest(p.test_suite())
 
681
    for name, plugin in bzrlib.plugin.all_plugins().items():
 
682
        if hasattr(plugin, 'test_suite'):
 
683
            suite.addTest(plugin.test_suite())
670
684
    return suite
671
685
 
 
686
 
 
687
def _load_module_by_name(mod_name):
 
688
    parts = mod_name.split('.')
 
689
    module = __import__(mod_name)
 
690
    del parts[0]
 
691
    # for historical reasons python returns the top-level module even though
 
692
    # it loads the submodule; we need to walk down to get the one we want.
 
693
    while parts:
 
694
        module = getattr(module, parts.pop(0))
 
695
    return module