~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the Branch facility that are not interface  tests.
18
18
 
19
 
For interface tests see `tests/per_branch/*.py`.
 
19
For interface tests see tests/per_branch/*.py.
20
20
 
21
21
For concrete class tests see this file, and for meta-branch tests
22
22
also see this file.
74
74
        url = self.get_url()
75
75
        bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
76
76
        bdir.create_repository()
77
 
        branch = _mod_branch.BzrBranchFormat5().initialize(bdir)
 
77
        branch = bdir.create_branch()
78
78
        t = self.get_transport()
79
79
        self.log("branch instance is %r" % branch)
80
80
        self.assert_(isinstance(branch, _mod_branch.BzrBranch5))
112
112
        """See BzrBranchFormat.get_format_string()."""
113
113
        return "Sample branch format."
114
114
 
115
 
    def initialize(self, a_bzrdir, name=None, repository=None,
116
 
                   append_revisions_only=None):
 
115
    def initialize(self, a_bzrdir, name=None, repository=None):
117
116
        """Format 4 branches cannot be created."""
118
117
        t = a_bzrdir.get_branch_transport(self, name=name)
119
118
        t.put_bytes('format', self.get_format_string())
138
137
        """See BzrBranchFormat.get_format_string()."""
139
138
        return SampleSupportedBranchFormatString
140
139
 
141
 
    def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
 
140
    def initialize(self, a_bzrdir, name=None):
142
141
        t = a_bzrdir.get_branch_transport(self, name=name)
143
142
        t.put_bytes('format', self.get_format_string())
144
143
        return 'A branch'
178
177
            dir.create_repository()
179
178
            format.initialize(dir)
180
179
            found_format = _mod_branch.BranchFormat.find_format(dir)
181
 
            self.assertIsInstance(found_format, format.__class__)
 
180
            self.failUnless(isinstance(found_format, format.__class__))
182
181
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
183
182
 
184
183
    def test_find_format_factory(self):
323
322
 
324
323
    def test_layout(self):
325
324
        branch = self.make_branch('a', format=self.get_format_name())
326
 
        self.assertPathExists('a/.bzr/branch/last-revision')
327
 
        self.assertPathDoesNotExist('a/.bzr/branch/revision-history')
328
 
        self.assertPathDoesNotExist('a/.bzr/branch/references')
 
325
        self.failUnlessExists('a/.bzr/branch/last-revision')
 
326
        self.failIfExists('a/.bzr/branch/revision-history')
 
327
        self.failIfExists('a/.bzr/branch/references')
329
328
 
330
329
    def test_config(self):
331
330
        """Ensure that all configuration data is stored in the branch"""
332
331
        branch = self.make_branch('a', format=self.get_format_name())
333
332
        branch.set_parent('http://example.com')
334
 
        self.assertPathDoesNotExist('a/.bzr/branch/parent')
 
333
        self.failIfExists('a/.bzr/branch/parent')
335
334
        self.assertEqual('http://example.com', branch.get_parent())
336
335
        branch.set_push_location('sftp://example.com')
337
336
        config = branch.get_config()._get_branch_data_config()
338
337
        self.assertEqual('sftp://example.com',
339
338
                         config.get_user_option('push_location'))
340
339
        branch.set_bound_location('ftp://example.com')
341
 
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
 
340
        self.failIfExists('a/.bzr/branch/bound')
342
341
        self.assertEqual('ftp://example.com', branch.get_bound_location())
343
342
 
344
343
    def test_set_revision_history(self):
350
349
        branch = builder.get_branch()
351
350
        branch.lock_write()
352
351
        self.addCleanup(branch.unlock)
353
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
354
 
            branch.set_revision_history, ['foo', 'bar'])
355
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
356
 
                branch.set_revision_history, ['foo'])
 
352
        branch.set_revision_history(['foo', 'bar'])
 
353
        branch.set_revision_history(['foo'])
357
354
        self.assertRaises(errors.NotLefthandHistory,
358
 
            self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
359
 
            branch.set_revision_history, ['bar'])
 
355
                          branch.set_revision_history, ['bar'])
360
356
 
361
357
    def do_checkout_test(self, lightweight=False):
362
358
        tree = self.make_branch_and_tree('source',
375
371
        subtree.commit('a subtree file')
376
372
        subsubtree.commit('a subsubtree file')
377
373
        tree.branch.create_checkout('target', lightweight=lightweight)
378
 
        self.assertPathExists('target')
379
 
        self.assertPathExists('target/subtree')
380
 
        self.assertPathExists('target/subtree/file')
381
 
        self.assertPathExists('target/subtree/subsubtree/file')
 
374
        self.failUnlessExists('target')
 
375
        self.failUnlessExists('target/subtree')
 
376
        self.failUnlessExists('target/subtree/file')
 
377
        self.failUnlessExists('target/subtree/subsubtree/file')
382
378
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
383
379
        if lightweight:
384
380
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
653
649
        if value is not None:
654
650
            self.config.set_user_option('append_revisions_only', value)
655
651
        self.assertEqual(expected_value,
656
 
                         self.branch.get_append_revisions_only())
 
652
                         self.branch._get_append_revisions_only())
657
653
 
658
654
    def test_valid_append_revisions_only(self):
659
655
        self.assertEquals(None,
710
706
        r.new_revid = "same-revid"
711
707
        f = StringIO()
712
708
        r.report(f)
713
 
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())
714
 
 
 
709
        self.assertEqual("No revisions to pull.\n", f.getvalue())
 
710
 
 
711
 
 
712
class _StubLockable(object):
 
713
    """Helper for TestRunWithWriteLockedTarget."""
 
714
 
 
715
    def __init__(self, calls, unlock_exc=None):
 
716
        self.calls = calls
 
717
        self.unlock_exc = unlock_exc
 
718
 
 
719
    def lock_write(self):
 
720
        self.calls.append('lock_write')
 
721
 
 
722
    def unlock(self):
 
723
        self.calls.append('unlock')
 
724
        if self.unlock_exc is not None:
 
725
            raise self.unlock_exc
 
726
 
 
727
 
 
728
class _ErrorFromCallable(Exception):
 
729
    """Helper for TestRunWithWriteLockedTarget."""
 
730
 
 
731
 
 
732
class _ErrorFromUnlock(Exception):
 
733
    """Helper for TestRunWithWriteLockedTarget."""
 
734
 
 
735
 
 
736
class TestRunWithWriteLockedTarget(tests.TestCase):
 
737
    """Tests for _run_with_write_locked_target."""
 
738
 
 
739
    def setUp(self):
 
740
        tests.TestCase.setUp(self)
 
741
        self._calls = []
 
742
 
 
743
    def func_that_returns_ok(self):
 
744
        self._calls.append('func called')
 
745
        return 'ok'
 
746
 
 
747
    def func_that_raises(self):
 
748
        self._calls.append('func called')
 
749
        raise _ErrorFromCallable()
 
750
 
 
751
    def test_success_unlocks(self):
 
752
        lockable = _StubLockable(self._calls)
 
753
        result = _mod_branch._run_with_write_locked_target(
 
754
            lockable, self.func_that_returns_ok)
 
755
        self.assertEqual('ok', result)
 
756
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
 
757
 
 
758
    def test_exception_unlocks_and_propagates(self):
 
759
        lockable = _StubLockable(self._calls)
 
760
        self.assertRaises(_ErrorFromCallable,
 
761
                          _mod_branch._run_with_write_locked_target,
 
762
                          lockable, self.func_that_raises)
 
763
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
 
764
 
 
765
    def test_callable_succeeds_but_error_during_unlock(self):
 
766
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
 
767
        self.assertRaises(_ErrorFromUnlock,
 
768
                          _mod_branch._run_with_write_locked_target,
 
769
                          lockable, self.func_that_returns_ok)
 
770
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
 
771
 
 
772
    def test_error_during_unlock_does_not_mask_original_error(self):
 
773
        lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
 
774
        self.assertRaises(_ErrorFromCallable,
 
775
                          _mod_branch._run_with_write_locked_target,
 
776
                          lockable, self.func_that_raises)
 
777
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)