~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
    )
63
63
from bzrlib.tests import (
64
64
    features,
 
65
    stub_sftp,
65
66
    test_lsprof,
66
 
    test_server,
67
67
    test_sftp_transport,
68
68
    TestUtil,
69
69
    )
70
70
from bzrlib.trace import note
71
 
from bzrlib.transport import memory
 
71
from bzrlib.transport.memory import MemoryServer, MemoryTransport
72
72
from bzrlib.version import _get_bzr_source_tree
73
73
 
74
74
 
208
208
    def test_scenarios(self):
209
209
        # check that constructor parameters are passed through to the adapted
210
210
        # test.
211
 
        from bzrlib.tests.per_controldir import make_scenarios
 
211
        from bzrlib.tests.per_bzrdir import make_scenarios
212
212
        vfs_factory = "v"
213
213
        server1 = "a"
214
214
        server2 = "b"
609
609
                l.attempt_lock()
610
610
        test = TestDanglingLock('test_function')
611
611
        result = test.run()
612
 
        total_failures = result.errors + result.failures
613
612
        if self._lock_check_thorough:
614
 
            self.assertLength(1, total_failures)
 
613
            self.assertEqual(1, len(result.errors))
615
614
        else:
616
615
            # When _lock_check_thorough is disabled, then we don't trigger a
617
616
            # failure
618
 
            self.assertLength(0, total_failures)
 
617
            self.assertEqual(0, len(result.errors))
619
618
 
620
619
 
621
620
class TestTestCaseWithTransport(tests.TestCaseWithTransport):
622
621
    """Tests for the convenience functions TestCaseWithTransport introduces."""
623
622
 
624
623
    def test_get_readonly_url_none(self):
 
624
        from bzrlib.transport import get_transport
 
625
        from bzrlib.transport.memory import MemoryServer
625
626
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
626
 
        self.vfs_transport_factory = memory.MemoryServer
 
627
        self.vfs_transport_factory = MemoryServer
627
628
        self.transport_readonly_server = None
628
629
        # calling get_readonly_transport() constructs a decorator on the url
629
630
        # for the server
630
631
        url = self.get_readonly_url()
631
632
        url2 = self.get_readonly_url('foo/bar')
632
 
        t = transport.get_transport(url)
633
 
        t2 = transport.get_transport(url2)
 
633
        t = get_transport(url)
 
634
        t2 = get_transport(url2)
634
635
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
635
636
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
636
637
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
637
638
 
638
639
    def test_get_readonly_url_http(self):
639
640
        from bzrlib.tests.http_server import HttpServer
 
641
        from bzrlib.transport import get_transport
 
642
        from bzrlib.transport.local import LocalURLServer
640
643
        from bzrlib.transport.http import HttpTransportBase
641
 
        self.transport_server = test_server.LocalURLServer
 
644
        self.transport_server = LocalURLServer
642
645
        self.transport_readonly_server = HttpServer
643
646
        # calling get_readonly_transport() gives us a HTTP server instance.
644
647
        url = self.get_readonly_url()
645
648
        url2 = self.get_readonly_url('foo/bar')
646
649
        # the transport returned may be any HttpTransportBase subclass
647
 
        t = transport.get_transport(url)
648
 
        t2 = transport.get_transport(url2)
 
650
        t = get_transport(url)
 
651
        t2 = get_transport(url2)
649
652
        self.failUnless(isinstance(t, HttpTransportBase))
650
653
        self.failUnless(isinstance(t2, HttpTransportBase))
651
654
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
675
678
 
676
679
    def setUp(self):
677
680
        super(TestTestCaseTransports, self).setUp()
678
 
        self.vfs_transport_factory = memory.MemoryServer
 
681
        self.vfs_transport_factory = MemoryServer
679
682
 
680
683
    def test_make_bzrdir_preserves_transport(self):
681
684
        t = self.get_transport()
682
685
        result_bzrdir = self.make_bzrdir('subdir')
683
686
        self.assertIsInstance(result_bzrdir.transport,
684
 
                              memory.MemoryTransport)
 
687
                              MemoryTransport)
685
688
        # should not be on disk, should only be in memory
686
689
        self.failIfExists('subdir')
687
690
 
689
692
class TestChrootedTest(tests.ChrootedTestCase):
690
693
 
691
694
    def test_root_is_root(self):
692
 
        t = transport.get_transport(self.get_readonly_url())
 
695
        from bzrlib.transport import get_transport
 
696
        t = get_transport(self.get_readonly_url())
693
697
        url = t.base
694
698
        self.assertEqual(url, t.clone('..').base)
695
699
 
751
755
    def _patch_get_bzr_source_tree(self):
752
756
        # Reading from the actual source tree breaks isolation, but we don't
753
757
        # want to assume that thats *all* that would happen.
754
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
 
758
        def _get_bzr_source_tree():
 
759
            return None
 
760
        orig_get_bzr_source_tree = bzrlib.version._get_bzr_source_tree
 
761
        bzrlib.version._get_bzr_source_tree = _get_bzr_source_tree
 
762
        def restore():
 
763
            bzrlib.version._get_bzr_source_tree = orig_get_bzr_source_tree
 
764
        self.addCleanup(restore)
755
765
 
756
766
    def test_assigned_benchmark_file_stores_date(self):
757
767
        self._patch_get_bzr_source_tree()
801
811
        self.requireFeature(test_lsprof.LSProfFeature)
802
812
        result_stream = StringIO()
803
813
        result = bzrlib.tests.VerboseTestResult(
804
 
            result_stream,
 
814
            unittest._WritelnDecorator(result_stream),
805
815
            descriptions=0,
806
816
            verbosity=2,
807
817
            )
862
872
        # verbose test output formatting
863
873
        result_stream = StringIO()
864
874
        result = bzrlib.tests.VerboseTestResult(
865
 
            result_stream,
 
875
            unittest._WritelnDecorator(result_stream),
866
876
            descriptions=0,
867
877
            verbosity=2,
868
878
            )
878
888
        output = result_stream.getvalue()[prefix:]
879
889
        lines = output.splitlines()
880
890
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
881
 
        if sys.version_info > (2, 7):
882
 
            self.expectFailure("_ExpectedFailure on 2.7 loses the message",
883
 
                self.assertNotEqual, lines[1], '    ')
884
891
        self.assertEqual(lines[1], '    foo')
885
892
        self.assertEqual(2, len(lines))
886
893
 
920
927
        # verbose test output formatting
921
928
        result_stream = StringIO()
922
929
        result = bzrlib.tests.VerboseTestResult(
923
 
            result_stream,
 
930
            unittest._WritelnDecorator(result_stream),
924
931
            descriptions=0,
925
932
            verbosity=2,
926
933
            )
1195
1202
        # Reading from the actual source tree breaks isolation, but we don't
1196
1203
        # want to assume that thats *all* that would happen.
1197
1204
        self._get_source_tree_calls = []
1198
 
        def new_get():
 
1205
        def _get_bzr_source_tree():
1199
1206
            self._get_source_tree_calls.append("called")
1200
1207
            return None
1201
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
 
1208
        orig_get_bzr_source_tree = bzrlib.version._get_bzr_source_tree
 
1209
        bzrlib.version._get_bzr_source_tree = _get_bzr_source_tree
 
1210
        def restore():
 
1211
            bzrlib.version._get_bzr_source_tree = orig_get_bzr_source_tree
 
1212
        self.addCleanup(restore)
1202
1213
 
1203
1214
    def test_bench_history(self):
1204
1215
        # tests that the running the benchmark passes bench_history into
1312
1323
        self.assertEqual(flags, bzrlib.debug.debug_flags)
1313
1324
 
1314
1325
    def change_selftest_debug_flags(self, new_flags):
1315
 
        self.overrideAttr(tests, 'selftest_debug_flags', set(new_flags))
 
1326
        orig_selftest_flags = tests.selftest_debug_flags
 
1327
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
 
1328
        tests.selftest_debug_flags = set(new_flags)
 
1329
 
 
1330
    def _restore_selftest_debug_flags(self, flags):
 
1331
        tests.selftest_debug_flags = flags
1316
1332
 
1317
1333
    def test_allow_debug_flag(self):
1318
1334
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1422
1438
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1423
1439
        output_stream = StringIO()
1424
1440
        result = bzrlib.tests.VerboseTestResult(
1425
 
            output_stream,
 
1441
            unittest._WritelnDecorator(output_stream),
1426
1442
            descriptions=0,
1427
1443
            verbosity=2)
1428
1444
        sample_test.run(result)
1469
1485
        # permitted.
1470
1486
        # Manually set one up (TestCase doesn't and shouldn't provide magic
1471
1487
        # machinery)
1472
 
        transport_server = memory.MemoryServer()
 
1488
        transport_server = MemoryServer()
1473
1489
        transport_server.start_server()
1474
1490
        self.addCleanup(transport_server.stop_server)
1475
1491
        t = transport.get_transport(transport_server.get_url())
1557
1573
            result.calls)
1558
1574
 
1559
1575
    def test_start_server_registers_url(self):
1560
 
        transport_server = memory.MemoryServer()
 
1576
        transport_server = MemoryServer()
1561
1577
        # A little strict, but unlikely to be changed soon.
1562
1578
        self.assertEqual([], self._bzr_selftest_roots)
1563
1579
        self.start_server(transport_server)
1619
1635
        self.assertRaises(AssertionError,
1620
1636
            self.assertListRaises, _TestException, success_generator)
1621
1637
 
1622
 
    def test_overrideAttr_without_value(self):
1623
 
        self.test_attr = 'original' # Define a test attribute
1624
 
        obj = self # Make 'obj' visible to the embedded test
1625
 
        class Test(tests.TestCase):
1626
 
 
1627
 
            def setUp(self):
1628
 
                tests.TestCase.setUp(self)
1629
 
                self.orig = self.overrideAttr(obj, 'test_attr')
1630
 
 
1631
 
            def test_value(self):
1632
 
                self.assertEqual('original', self.orig)
1633
 
                self.assertEqual('original', obj.test_attr)
1634
 
                obj.test_attr = 'modified'
1635
 
                self.assertEqual('modified', obj.test_attr)
1636
 
 
1637
 
        test = Test('test_value')
1638
 
        test.run(unittest.TestResult())
1639
 
        self.assertEqual('original', obj.test_attr)
1640
 
 
1641
 
    def test_overrideAttr_with_value(self):
1642
 
        self.test_attr = 'original' # Define a test attribute
1643
 
        obj = self # Make 'obj' visible to the embedded test
1644
 
        class Test(tests.TestCase):
1645
 
 
1646
 
            def setUp(self):
1647
 
                tests.TestCase.setUp(self)
1648
 
                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
1649
 
 
1650
 
            def test_value(self):
1651
 
                self.assertEqual('original', self.orig)
1652
 
                self.assertEqual('modified', obj.test_attr)
1653
 
 
1654
 
        test = Test('test_value')
1655
 
        test.run(unittest.TestResult())
1656
 
        self.assertEqual('original', obj.test_attr)
1657
 
 
1658
1638
 
1659
1639
# NB: Don't delete this; it's not actually from 0.11!
1660
1640
@deprecated_function(deprecated_in((0, 11, 0)))
1804
1784
        # make_branch_and_tree has to use local branch and repositories
1805
1785
        # when the vfs transport and local disk are colocated, even if
1806
1786
        # a different transport is in use for url generation.
1807
 
        self.transport_server = test_server.FakeVFATServer
 
1787
        from bzrlib.transport.fakevfat import FakeVFATServer
 
1788
        self.transport_server = FakeVFATServer
1808
1789
        self.assertFalse(self.get_url('t1').startswith('file://'))
1809
1790
        tree = self.make_branch_and_tree('t1')
1810
1791
        base = tree.bzrdir.root_transport.base
1947
1928
 
1948
1929
    def test_transport_sftp(self):
1949
1930
        self.requireFeature(features.paramiko)
1950
 
        from bzrlib.tests import stub_sftp
1951
1931
        self.check_transport_set(stub_sftp.SFTPAbsoluteServer)
1952
1932
 
1953
1933
    def test_transport_memory(self):
1954
 
        self.check_transport_set(memory.MemoryServer)
 
1934
        self.check_transport_set(bzrlib.transport.memory.MemoryServer)
1955
1935
 
1956
1936
 
1957
1937
class TestSelftestWithIdList(tests.TestCaseInTempDir, SelfTestHelper):
2340
2320
            os.chdir = orig_chdir
2341
2321
        self.assertEqual(['foo', 'current'], chdirs)
2342
2322
 
2343
 
    def test_get_bzr_path_with_cwd_bzrlib(self):
2344
 
        self.get_source_path = lambda: ""
2345
 
        self.overrideAttr(os.path, "isfile", lambda path: True)
2346
 
        self.assertEqual(self.get_bzr_path(), "bzr")
2347
 
 
2348
2323
 
2349
2324
class TestActuallyStartBzrSubprocess(tests.TestCaseWithTransport):
2350
2325
    """Tests that really need to do things with an external bzr."""
2405
2380
 
2406
2381
 
2407
2382
simple_thunk_feature = tests._CompatabilityThunkFeature(
2408
 
    deprecated_in((2, 1, 0)),
2409
 
    'bzrlib.tests.test_selftest',
2410
 
    'simple_thunk_feature','UnicodeFilename',
2411
 
    replacement_module='bzrlib.tests'
2412
 
    )
 
2383
    'bzrlib.tests', 'UnicodeFilename',
 
2384
    'bzrlib.tests.test_selftest.simple_thunk_feature',
 
2385
    deprecated_in((2,1,0)))
2413
2386
 
2414
2387
class Test_CompatibilityFeature(tests.TestCase):
2415
2388
 
2420
2393
            simple_thunk_feature.available)
2421
2394
        self.assertEqual(tests.UnicodeFilename.available(), res)
2422
2395
 
2423
 
 
 
2396
        
2424
2397
class TestModuleAvailableFeature(tests.TestCase):
2425
2398
 
2426
2399
    def test_available_module(self):
2627
2600
        # Running bzr in blackbox mode, normal/expected/user errors should be
2628
2601
        # caught in the regular way and turned into an error message plus exit
2629
2602
        # code.
2630
 
        transport_server = memory.MemoryServer()
 
2603
        transport_server = MemoryServer()
2631
2604
        transport_server.start_server()
2632
2605
        self.addCleanup(transport_server.stop_server)
2633
2606
        url = transport_server.get_url()
2779
2752
        # Test that a plausible list of modules to doctest is returned
2780
2753
        # by _test_suite_modules_to_doctest.
2781
2754
        test_list = tests._test_suite_modules_to_doctest()
2782
 
        if __doc__ is None:
2783
 
            # When docstrings are stripped, there are no modules to doctest
2784
 
            self.assertEqual([], test_list)
2785
 
            return
2786
2755
        self.assertSubset([
2787
2756
            'bzrlib.timestamp',
2788
2757
            ],
2795
2764
        # test doubles that supply a few sample tests to load, and check they
2796
2765
        # are loaded.
2797
2766
        calls = []
2798
 
        def testmod_names():
 
2767
        def _test_suite_testmod_names():
2799
2768
            calls.append("testmod_names")
2800
2769
            return [
2801
2770
                'bzrlib.tests.blackbox.test_branch',
2802
2771
                'bzrlib.tests.per_transport',
2803
2772
                'bzrlib.tests.test_selftest',
2804
2773
                ]
2805
 
        self.overrideAttr(tests, '_test_suite_testmod_names', testmod_names)
2806
 
        def doctests():
 
2774
        original_testmod_names = tests._test_suite_testmod_names
 
2775
        def _test_suite_modules_to_doctest():
2807
2776
            calls.append("modules_to_doctest")
2808
 
            if __doc__ is None:
2809
 
                return []
2810
2777
            return ['bzrlib.timestamp']
2811
 
        self.overrideAttr(tests, '_test_suite_modules_to_doctest', doctests)
 
2778
        orig_modules_to_doctest = tests._test_suite_modules_to_doctest
 
2779
        def restore_names():
 
2780
            tests._test_suite_testmod_names = original_testmod_names
 
2781
            tests._test_suite_modules_to_doctest = orig_modules_to_doctest
 
2782
        self.addCleanup(restore_names)
 
2783
        tests._test_suite_testmod_names = _test_suite_testmod_names
 
2784
        tests._test_suite_modules_to_doctest = _test_suite_modules_to_doctest
2812
2785
        expected_test_list = [
2813
2786
            # testmod_names
2814
2787
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2815
2788
            ('bzrlib.tests.per_transport.TransportTests'
2816
2789
             '.test_abspath(LocalTransport,LocalURLServer)'),
2817
2790
            'bzrlib.tests.test_selftest.TestTestSuite.test_test_suite',
 
2791
            # modules_to_doctest
 
2792
            'bzrlib.timestamp.format_highres_date',
2818
2793
            # plugins can't be tested that way since selftest may be run with
2819
2794
            # --no-plugins
2820
2795
            ]
2821
 
        if __doc__ is not None:
2822
 
            expected_test_list.extend([
2823
 
                # modules_to_doctest
2824
 
                'bzrlib.timestamp.format_highres_date',
2825
 
                ])
2826
2796
        suite = tests.test_suite()
2827
2797
        self.assertEqual(set(["testmod_names", "modules_to_doctest"]),
2828
2798
            set(calls))