22
24
from bzrlib.clone import copy_branch
23
25
from bzrlib.commit import commit
24
26
import bzrlib.errors as errors
25
from bzrlib.errors import NoSuchRevision, UnlistableBranch, NotBranchError
27
from bzrlib.errors import (NoSuchRevision,
28
UninitializableFormat,
27
33
from bzrlib.osutils import getcwd
28
from bzrlib.tests import TestCase, TestCaseInTempDir
34
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped
29
35
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
30
36
from bzrlib.trace import mutter
31
37
import bzrlib.transactions as transactions
35
41
# doesn't request any files that could have been avoided, by
36
42
# hooking into the Transport.
38
class TestBranch(TestCaseInTempDir):
45
class TestCaseWithBranch(TestCaseInTempDir):
48
super(TestCaseWithBranch, self).setUp()
52
if self.branch is None:
53
self.branch = self.make_branch('.')
56
def make_branch(self, relpath):
58
return self.branch_format.initialize(relpath)
59
except UninitializableFormat:
60
raise TestSkipped("Format %s is not initializable.")
63
class TestBranch(TestCaseWithBranch):
40
65
def test_append_revisions(self):
41
66
"""Test appending more than one revision"""
42
br = Branch.initialize(u".")
67
br = self.get_branch()
43
68
br.append_revision("rev1")
44
69
self.assertEquals(br.revision_history(), ["rev1",])
45
70
br.append_revision("rev2", "rev3")
78
103
def get_unbalanced_branch_pair(self):
79
104
"""Return two branches, a and b, with one file in a."""
81
br_a = Branch.initialize("a")
106
br_a = self.make_branch('a')
82
107
file('a/b', 'wb').write('b')
83
108
br_a.working_tree().add('b')
84
109
commit(br_a, "silly commit", rev_id='A')
86
br_b = Branch.initialize("b")
111
br_b = self.make_branch('b')
89
114
def get_balanced_branch_pair(self):
181
205
branch.revision_store.get('A', 'sig').read())
183
207
def test_store_signature(self):
184
branch = Branch.initialize(u'.')
208
branch = self.get_branch()
185
209
branch.store_revision_signature(bzrlib.gpg.LoopbackGPGStrategy(None),
187
211
self.assertEqual('FOO', branch.revision_store.get('A', 'sig').read())
189
213
def test__relcontrolfilename(self):
190
branch = Branch.initialize(u'.')
191
self.assertEqual('.bzr/%25', branch._rel_controlfilename('%'))
214
self.assertEqual('.bzr/%25', self.get_branch()._rel_controlfilename('%'))
193
216
def test__relcontrolfilename_empty(self):
194
branch = Branch.initialize(u'.')
195
self.assertEqual('.bzr', branch._rel_controlfilename(''))
217
self.assertEqual('.bzr', self.get_branch()._rel_controlfilename(''))
197
219
def test_nicks(self):
198
220
"""Branch nicknames"""
199
221
os.mkdir('bzr.dev')
200
branch = Branch.initialize('bzr.dev')
222
branch = self.make_branch('bzr.dev')
201
223
self.assertEqual(branch.nick, 'bzr.dev')
202
224
os.rename('bzr.dev', 'bzr.ab')
203
225
branch = Branch.open('bzr.ab')
230
252
self.get_remote_url(''))
231
253
self.assertRaises(NotBranchError, Branch.open_containing,
232
254
self.get_remote_url('g/p/q'))
233
b = Branch.initialize(u'.')
256
branch = self.branch_format.initialize('.')
257
except UninitializableFormat:
258
raise TestSkipped("Format %s is not initializable.")
234
259
branch, relpath = Branch.open_containing(self.get_remote_url(''))
235
260
self.assertEqual('', relpath)
236
261
branch, relpath = Branch.open_containing(self.get_remote_url('g/p/q'))
318
343
self.assertEqual(['lw', 'ul'], branch._calls)
321
class TestBranchTransaction(TestCaseInTempDir):
346
class TestBranchTransaction(TestCaseWithBranch):
324
349
super(TestBranchTransaction, self).setUp()
325
self.branch = Branch.initialize(u'.')
327
352
def test_default_get_transaction(self):
328
353
"""branch.get_transaction on a new branch should give a PassThrough."""
329
self.failUnless(isinstance(self.branch.get_transaction(),
354
self.failUnless(isinstance(self.get_branch().get_transaction(),
330
355
transactions.PassThroughTransaction))
332
357
def test__set_new_transaction(self):
333
self.branch._set_transaction(transactions.ReadOnlyTransaction())
358
self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
335
360
def test__set_over_existing_transaction_raises(self):
336
self.branch._set_transaction(transactions.ReadOnlyTransaction())
361
self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
337
362
self.assertRaises(errors.LockError,
338
self.branch._set_transaction,
363
self.get_branch()._set_transaction,
339
364
transactions.ReadOnlyTransaction())
341
366
def test_finish_no_transaction_raises(self):
342
self.assertRaises(errors.LockError, self.branch._finish_transaction)
367
self.assertRaises(errors.LockError, self.get_branch()._finish_transaction)
344
369
def test_finish_readonly_transaction_works(self):
345
self.branch._set_transaction(transactions.ReadOnlyTransaction())
346
self.branch._finish_transaction()
347
self.assertEqual(None, self.branch._transaction)
370
self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
371
self.get_branch()._finish_transaction()
372
self.assertEqual(None, self.get_branch()._transaction)
349
374
def test_unlock_calls_finish(self):
350
self.branch.lock_read()
375
self.get_branch().lock_read()
351
376
transaction = InstrumentedTransaction()
352
self.branch._transaction = transaction
377
self.get_branch()._transaction = transaction
378
self.get_branch().unlock()
354
379
self.assertEqual(['finish'], transaction.calls)
356
381
def test_lock_read_acquires_ro_transaction(self):
357
self.branch.lock_read()
358
self.failUnless(isinstance(self.branch.get_transaction(),
382
self.get_branch().lock_read()
383
self.failUnless(isinstance(self.get_branch().get_transaction(),
359
384
transactions.ReadOnlyTransaction))
385
self.get_branch().unlock()
362
387
def test_lock_write_acquires_passthrough_transaction(self):
363
self.branch.lock_write()
388
self.get_branch().lock_write()
364
389
# cannot use get_transaction as its magic
365
self.failUnless(isinstance(self.branch._transaction,
390
self.failUnless(isinstance(self.get_branch()._transaction,
366
391
transactions.PassThroughTransaction))
370
class TestBranchPushLocations(TestCaseInTempDir):
373
super(TestBranchPushLocations, self).setUp()
374
self.branch = Branch.initialize(u'.')
392
self.get_branch().unlock()
395
class TestBranchPushLocations(TestCaseWithBranch):
376
397
def test_get_push_location_unset(self):
377
self.assertEqual(None, self.branch.get_push_location())
398
self.assertEqual(None, self.get_branch().get_push_location())
379
400
def test_get_push_location_exact(self):
380
401
from bzrlib.config import (branches_config_filename,
384
405
print >> open(fn, 'wt'), ("[%s]\n"
385
406
"push_location=foo" %
387
self.assertEqual("foo", self.branch.get_push_location())
408
self.assertEqual("foo", self.get_branch().get_push_location())
389
410
def test_set_push_location(self):
390
411
from bzrlib.config import (branches_config_filename,
391
412
ensure_config_dir_exists)
392
413
ensure_config_dir_exists()
393
414
fn = branches_config_filename()
394
self.branch.set_push_location('foo')
415
self.get_branch().set_push_location('foo')
395
416
self.assertFileEqual("[%s]\n"
396
417
"push_location = foo" % getcwd(),
400
421
# recursive section - that is, it appends the branch name.
403
class TestDefaultFormat(TestCase):
405
def test_get_set_default_initializer(self):
406
old_initializer = Branch.get_default_initializer()
407
# default is BzrBranch._initialize
408
self.assertEqual(branch.BzrBranch._initialize, old_initializer)
410
return "a branch %s" % url
411
Branch.set_default_initializer(recorder)
413
b = Branch.initialize("memory:/")
414
self.assertEqual("a branch memory:/", b)
416
Branch.set_default_initializer(old_initializer)
417
self.assertEqual(old_initializer, Branch.get_default_initializer())
420
class TestBzrBranchFormat(TestCaseInTempDir):
421
"""Tests for the BzrBranchFormat facility."""
423
def test_find_format(self):
424
# is the right format object found for a branch?
425
# create a branch with a known format object
426
self.build_tree(["foo/", "bar/"])
427
def check_format(format, url):
428
format.initialize(url)
429
found_format = branch.BzrBranchFormat.find_format(url)
430
self.failUnless(isinstance(found_format, format.__class__))
431
check_format(branch.BzrBranchFormat5(), "foo")
432
check_format(branch.BzrBranchFormat6(), "bar")
434
def test_initialize_returns_branch(self):
435
self.failUnless(isinstance(branch.BzrBranchFormat5().initialize("."), branch.Branch))
424
class TestFormat(TestCaseWithBranch):
425
"""Tests for the format itself."""
427
def test_format_initialize_find_open(self):
428
# loopback test to check the current format initializes to itself.
429
made_branch = self.make_branch('.')
430
self.assertEqual(self.branch_format,
431
branch.BzrBranchFormat.find_format('.'))
432
opened_branch = branch.Branch.open('.')
433
self.assertEqual(made_branch._branch_format, opened_branch._branch_format)