~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2009-09-17 11:54:41 UTC
  • mto: This revision was merged to the branch mainline in revision 4700.
  • Revision ID: robertc@robertcollins.net-20090917115441-2ug57z6eyrnb6zim
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    repository,
39
39
    symbol_versioning,
40
40
    tests,
 
41
    transport,
41
42
    workingtree,
42
43
    )
43
44
from bzrlib.repofmt import (
576
577
                         self.get_transport().get_bytes(
577
578
                            'dir/.bzr/repository/format'))
578
579
 
579
 
    def test_safety_net(self):
580
 
        """No test should modify the safety .bzr directory.
581
 
 
582
 
        We just test that the _check_safety_net private method raises
583
 
        AssertionError, it's easier than building a test suite with the same
584
 
        test.
585
 
        """
586
 
        # Oops, a commit in the current directory (i.e. without local .bzr
587
 
        # directory) will crawl up the hierarchy to find a .bzr directory.
588
 
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
589
 
        # But we have a safety net in place.
590
 
        self.assertRaises(AssertionError, self._check_safety_net)
591
 
 
592
580
    def test_dangling_locks_cause_failures(self):
593
581
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
594
582
            def test_function(self):
739
727
        self.check_timing(ShortDelayTestCase('test_short_delay'),
740
728
                          r"^ +[0-9]+ms$")
741
729
 
 
730
    def _patch_get_bzr_source_tree(self):
 
731
        # Reading from the actual source tree breaks isolation, but we don't
 
732
        # want to assume that thats *all* that would happen.
 
733
        def _get_bzr_source_tree():
 
734
            return None
 
735
        orig_get_bzr_source_tree = bzrlib.version._get_bzr_source_tree
 
736
        bzrlib.version._get_bzr_source_tree = _get_bzr_source_tree
 
737
        def restore():
 
738
            bzrlib.version._get_bzr_source_tree = orig_get_bzr_source_tree
 
739
        self.addCleanup(restore)
 
740
 
742
741
    def test_assigned_benchmark_file_stores_date(self):
 
742
        self._patch_get_bzr_source_tree()
743
743
        output = StringIO()
744
744
        result = bzrlib.tests.TextTestResult(self._log_file,
745
745
                                        descriptions=0,
753
753
        self.assertContainsRe(output_string, "--date [0-9.]+")
754
754
 
755
755
    def test_benchhistory_records_test_times(self):
 
756
        self._patch_get_bzr_source_tree()
756
757
        result_stream = StringIO()
757
758
        result = bzrlib.tests.TextTestResult(
758
759
            self._log_file,
1165
1166
            ],
1166
1167
            lines[-3:])
1167
1168
 
 
1169
    def _patch_get_bzr_source_tree(self):
 
1170
        # Reading from the actual source tree breaks isolation, but we don't
 
1171
        # want to assume that thats *all* that would happen.
 
1172
        self._get_source_tree_calls = []
 
1173
        def _get_bzr_source_tree():
 
1174
            self._get_source_tree_calls.append("called")
 
1175
            return None
 
1176
        orig_get_bzr_source_tree = bzrlib.version._get_bzr_source_tree
 
1177
        bzrlib.version._get_bzr_source_tree = _get_bzr_source_tree
 
1178
        def restore():
 
1179
            bzrlib.version._get_bzr_source_tree = orig_get_bzr_source_tree
 
1180
        self.addCleanup(restore)
 
1181
 
1168
1182
    def test_bench_history(self):
1169
 
        # tests that the running the benchmark produces a history file
1170
 
        # containing a timestamp and the revision id of the bzrlib source which
1171
 
        # was tested.
1172
 
        workingtree = _get_bzr_source_tree()
 
1183
        # tests that the running the benchmark passes bench_history into
 
1184
        # the test result object. We can tell that happens if
 
1185
        # _get_bzr_source_tree is called.
 
1186
        self._patch_get_bzr_source_tree()
1173
1187
        test = TestRunner('dummy_test')
1174
1188
        output = StringIO()
1175
1189
        runner = tests.TextTestRunner(stream=self._log_file,
1177
1191
        result = self.run_test_runner(runner, test)
1178
1192
        output_string = output.getvalue()
1179
1193
        self.assertContainsRe(output_string, "--date [0-9.]+")
1180
 
        if workingtree is not None:
1181
 
            revision_id = workingtree.get_parent_ids()[0]
1182
 
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
1194
        self.assertLength(1, self._get_source_tree_calls)
1183
1195
 
1184
1196
    def assertLogDeleted(self, test):
1185
1197
        log = test._get_log()
1548
1560
        """Self.knownFailure() should raise a KnownFailure exception."""
1549
1561
        self.assertRaises(tests.KnownFailure, self.knownFailure, "A Failure")
1550
1562
 
 
1563
    def test_open_bzrdir_safe_roots(self):
 
1564
        # even a memory transport should fail to open when its url isn't 
 
1565
        # permitted.
 
1566
        # Manually set one up (TestCase doesn't and shouldn't provide magic
 
1567
        # machinery)
 
1568
        transport_server = MemoryServer()
 
1569
        transport_server.setUp()
 
1570
        self.addCleanup(transport_server.tearDown)
 
1571
        t = transport.get_transport(transport_server.get_url())
 
1572
        bzrdir.BzrDir.create(t.base)
 
1573
        self.assertRaises(errors.BzrError,
 
1574
            bzrdir.BzrDir.open_from_transport, t)
 
1575
        # But if we declare this as safe, we can open the bzrdir.
 
1576
        self.permit_url(t.base)
 
1577
        self._bzr_selftest_roots.append(t.base)
 
1578
        bzrdir.BzrDir.open_from_transport(t)
 
1579
 
1551
1580
    def test_requireFeature_available(self):
1552
1581
        """self.requireFeature(available) is a no-op."""
1553
1582
        class Available(tests.Feature):
1620
1649
            ],
1621
1650
            result.calls)
1622
1651
 
 
1652
    def test_start_server_registers_url(self):
 
1653
        transport_server = MemoryServer()
 
1654
        # A little strict, but unlikely to be changed soon.
 
1655
        self.assertEqual([], self._bzr_selftest_roots)
 
1656
        self.start_server(transport_server)
 
1657
        self.assertSubset([transport_server.get_url()],
 
1658
            self._bzr_selftest_roots)
 
1659
 
1623
1660
    def test_assert_list_raises_on_generator(self):
1624
1661
        def generator_which_will_raise():
1625
1662
            # This will not raise until after the first yield
2632
2669
        # Running bzr in blackbox mode, normal/expected/user errors should be
2633
2670
        # caught in the regular way and turned into an error message plus exit
2634
2671
        # code.
2635
 
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
 
2672
        transport_server = MemoryServer()
 
2673
        transport_server.setUp()
 
2674
        self.addCleanup(transport_server.tearDown)
 
2675
        url = transport_server.get_url()
 
2676
        self.permit_url(url)
 
2677
        out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)
2636
2678
        self.assertEqual(out, '')
2637
2679
        self.assertContainsRe(err,
2638
2680
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')