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
from bzrlib.errors import NoSuchRevision, UnlistableBranch, NotBranchError
25
from bzrlib.selftest import TestCase, TestCaseInTempDir
26
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
22
from bzrlib.errors import NoSuchRevision, UnlistableBranch
23
from bzrlib.selftest import TestCaseInTempDir
27
24
from bzrlib.trace import mutter
28
25
import bzrlib.transactions as transactions
30
# TODO: Make a branch using basis branch, and check that it
31
# doesn't request any files that could have been avoided, by
32
# hooking into the Transport.
34
28
class TestBranch(TestCaseInTempDir):
62
56
tree = b2.revision_tree('revision-1')
63
57
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."""
59
def test_push_stores(self):
60
"""Copy the stores from one branch to another"""
68
62
br_a = Branch.initialize("a")
69
63
file('a/b', 'wb').write('b')
71
commit(br_a, "silly commit", rev_id='A')
65
commit(br_a, "silly commit")
73
68
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
69
self.assertRaises(NoSuchRevision, br_b.get_revision,
87
70
br_a.revision_history()[0])
88
71
br_a.push_stores(br_b)
89
# check that b now has all the data from a's first commit.
90
72
rev = br_b.get_revision(br_a.revision_history()[0])
91
73
tree = br_b.revision_tree(br_a.revision_history()[0])
92
74
for file_id in tree:
97
79
def test_copy_branch(self):
98
80
"""Copy the stores from one branch to another"""
99
br_a, br_b = self.get_balanced_branch_pair()
81
br_a, br_b = self.test_push_stores()
100
82
commit(br_b, "silly commit")
102
84
br_c = copy_branch(br_a, 'c', basis_branch=br_b)
103
85
self.assertEqual(br_a.revision_history(), br_c.revision_history())
86
## # basis branches currently disabled for weave format
87
## self.assertFalse(br_b.last_revision() in br_c.revision_history())
88
## br_c.get_revision(br_b.last_revision())
105
90
def test_copy_partial(self):
106
91
"""Copy only part of the history of a branch."""
154
140
'wibble@fofof--20050401--1928390812')
155
141
# list should be cleared when we do a commit
156
142
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
class TestRemote(TestCaseWithWebserver):
183
def test_open_containing(self):
184
self.assertRaises(NotBranchError, Branch.open_containing,
185
self.get_remote_url(''))
186
self.assertRaises(NotBranchError, Branch.open_containing,
187
self.get_remote_url('g/p/q'))
188
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)
194
145
# TODO: rewrite this as a regular unittest, without relying on the displayed output
195
146
# >>> from bzrlib.commit import commit
196
147
# >>> bzrlib.trace.silent = True
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
173
class TestBranchTransaction(TestCaseInTempDir):