~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testbranch.py

  • Committer: Aaron Bentley
  • Date: 2005-10-18 18:48:27 UTC
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1474.
  • Revision ID: abentley@panoramicfeedback.com-20051018184827-2cc69376beb1cdf3
Switched to ConfigObj

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
18
 
 
19
 
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
 
18
from bzrlib.branch import Branch
20
19
from bzrlib.clone import copy_branch
21
20
from bzrlib.commit import commit
22
21
import bzrlib.errors as errors
23
22
from bzrlib.errors import NoSuchRevision, UnlistableBranch, NotBranchError
24
 
import bzrlib.gpg
25
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
26
 
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
 
23
from bzrlib.selftest import TestCaseInTempDir
27
24
from bzrlib.trace import mutter
28
25
import bzrlib.transactions as transactions
 
26
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
29
27
 
30
28
# TODO: Make a branch using basis branch, and check that it 
31
29
# doesn't request any files that could have been avoided, by 
62
60
        tree = b2.revision_tree('revision-1')
63
61
        eq(tree.get_file_text('foo-id'), 'hello')
64
62
 
65
 
    def get_unbalanced_branch_pair(self):
66
 
        """Return two branches, a and b, with one file in a."""
 
63
    def test_push_stores(self):
 
64
        """Copy the stores from one branch to another"""
67
65
        os.mkdir('a')
68
66
        br_a = Branch.initialize("a")
69
67
        file('a/b', 'wb').write('b')
70
68
        br_a.add('b')
71
 
        commit(br_a, "silly commit", rev_id='A')
 
69
        commit(br_a, "silly commit")
 
70
 
72
71
        os.mkdir('b')
73
72
        br_b = Branch.initialize("b")
74
 
        return br_a, br_b
75
 
 
76
 
    def get_balanced_branch_pair(self):
77
 
        """Returns br_a, br_b as with one commit in a, and b has a's stores."""
78
 
        br_a, br_b = self.get_unbalanced_branch_pair()
79
 
        br_a.push_stores(br_b)
80
 
        return br_a, br_b
81
 
 
82
 
    def test_push_stores(self):
83
 
        """Copy the stores from one branch to another"""
84
 
        br_a, br_b = self.get_unbalanced_branch_pair()
85
 
        # ensure the revision is missing.
86
73
        self.assertRaises(NoSuchRevision, br_b.get_revision, 
87
74
                          br_a.revision_history()[0])
88
75
        br_a.push_stores(br_b)
89
 
        # check that b now has all the data from a's first commit.
90
76
        rev = br_b.get_revision(br_a.revision_history()[0])
91
77
        tree = br_b.revision_tree(br_a.revision_history()[0])
92
78
        for file_id in tree:
96
82
 
97
83
    def test_copy_branch(self):
98
84
        """Copy the stores from one branch to another"""
99
 
        br_a, br_b = self.get_balanced_branch_pair()
 
85
        br_a, br_b = self.test_push_stores()
100
86
        commit(br_b, "silly commit")
101
87
        os.mkdir('c')
102
88
        br_c = copy_branch(br_a, 'c', basis_branch=br_b)
116
102
        self.assertTrue(os.path.exists('b/one'))
117
103
        self.assertFalse(os.path.exists('b/two'))
118
104
        
 
105
 
119
106
    def test_record_initial_ghost_merge(self):
120
107
        """A pending merge with no revision present is still a merge."""
121
108
        branch = Branch.initialize('.')
155
142
        # list should be cleared when we do a commit
156
143
        self.assertEquals(b.pending_merges(), [])
157
144
 
158
 
    def test_sign_existing_revision(self):
159
 
        branch = Branch.initialize('.')
160
 
        branch.commit("base", allow_pointless=True, rev_id='A')
161
 
        from bzrlib.testament import Testament
162
 
        branch.sign_revision('A', bzrlib.gpg.LoopbackGPGStrategy(None))
163
 
        self.assertEqual(Testament.from_revision(branch, 'A').as_short_text(),
164
 
                         branch.revision_store.get('A', 'sig').read())
165
 
 
166
 
    def test_store_signature(self):
167
 
        branch = Branch.initialize('.')
168
 
        branch.store_revision_signature(bzrlib.gpg.LoopbackGPGStrategy(None),
169
 
                                        'FOO', 'A')
170
 
        self.assertEqual('FOO', branch.revision_store.get('A', 'sig').read())
171
 
 
172
 
    def test__relcontrolfilename(self):
173
 
        branch = Branch.initialize('.')
174
 
        self.assertEqual('.bzr/%25', branch._rel_controlfilename('%'))
175
 
        
176
 
    def test__relcontrolfilename_empty(self):
177
 
        branch = Branch.initialize('.')
178
 
        self.assertEqual('.bzr', branch._rel_controlfilename(''))
179
 
 
180
145
 
181
146
class TestRemote(TestCaseWithWebserver):
182
147
 
186
151
        self.assertRaises(NotBranchError, Branch.open_containing,
187
152
                          self.get_remote_url('g/p/q'))
188
153
        b = Branch.initialize('.')
189
 
        branch, relpath = Branch.open_containing(self.get_remote_url(''))
190
 
        self.assertEqual('', relpath)
191
 
        branch, relpath = Branch.open_containing(self.get_remote_url('g/p/q'))
192
 
        self.assertEqual('g/p/q', relpath)
 
154
        Branch.open_containing(self.get_remote_url(''))
 
155
        Branch.open_containing(self.get_remote_url('g/p/q'))
193
156
        
194
157
# TODO: rewrite this as a regular unittest, without relying on the displayed output        
195
158
#         >>> from bzrlib.commit import commit
219
182
        self.calls = []
220
183
 
221
184
 
222
 
class TestDecorator(object):
223
 
 
224
 
    def __init__(self):
225
 
        self._calls = []
226
 
 
227
 
    def lock_read(self):
228
 
        self._calls.append('lr')
229
 
 
230
 
    def lock_write(self):
231
 
        self._calls.append('lw')
232
 
 
233
 
    def unlock(self):
234
 
        self._calls.append('ul')
235
 
 
236
 
    @needs_read_lock
237
 
    def do_with_read(self):
238
 
        return 1
239
 
 
240
 
    @needs_read_lock
241
 
    def except_with_read(self):
242
 
        raise RuntimeError
243
 
 
244
 
    @needs_write_lock
245
 
    def do_with_write(self):
246
 
        return 2
247
 
 
248
 
    @needs_write_lock
249
 
    def except_with_write(self):
250
 
        raise RuntimeError
251
 
 
252
 
 
253
 
class TestDecorators(TestCase):
254
 
 
255
 
    def test_needs_read_lock(self):
256
 
        branch = TestDecorator()
257
 
        self.assertEqual(1, branch.do_with_read())
258
 
        self.assertEqual(['lr', 'ul'], branch._calls)
259
 
 
260
 
    def test_excepts_in_read_lock(self):
261
 
        branch = TestDecorator()
262
 
        self.assertRaises(RuntimeError, branch.except_with_read)
263
 
        self.assertEqual(['lr', 'ul'], branch._calls)
264
 
 
265
 
    def test_needs_write_lock(self):
266
 
        branch = TestDecorator()
267
 
        self.assertEqual(2, branch.do_with_write())
268
 
        self.assertEqual(['lw', 'ul'], branch._calls)
269
 
 
270
 
    def test_excepts_in_write_lock(self):
271
 
        branch = TestDecorator()
272
 
        self.assertRaises(RuntimeError, branch.except_with_write)
273
 
        self.assertEqual(['lw', 'ul'], branch._calls)
274
 
 
275
 
 
276
185
class TestBranchTransaction(TestCaseInTempDir):
277
186
 
278
187
    def setUp(self):