15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
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
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')
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"""
68
66
br_a = Branch.initialize("a")
69
67
file('a/b', 'wb').write('b')
71
commit(br_a, "silly commit", rev_id='A')
69
commit(br_a, "silly commit")
73
72
br_b = Branch.initialize("b")
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)
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:
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")
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'))
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(), [])
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())
166
def test_store_signature(self):
167
branch = Branch.initialize('.')
168
branch.store_revision_signature(bzrlib.gpg.LoopbackGPGStrategy(None),
170
self.assertEqual('FOO', branch.revision_store.get('A', 'sig').read())
172
def test__relcontrolfilename(self):
173
branch = Branch.initialize('.')
174
self.assertEqual('.bzr/%25', branch._rel_controlfilename('%'))
176
def test__relcontrolfilename_empty(self):
177
branch = Branch.initialize('.')
178
self.assertEqual('.bzr', branch._rel_controlfilename(''))
181
146
class TestRemote(TestCaseWithWebserver):
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'))
194
157
# TODO: rewrite this as a regular unittest, without relying on the displayed output
195
158
# >>> from bzrlib.commit import commit
222
class TestDecorator(object):
228
self._calls.append('lr')
230
def lock_write(self):
231
self._calls.append('lw')
234
self._calls.append('ul')
237
def do_with_read(self):
241
def except_with_read(self):
245
def do_with_write(self):
249
def except_with_write(self):
253
class TestDecorators(TestCase):
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)
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)
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)
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)
276
185
class TestBranchTransaction(TestCaseInTempDir):