35
branch as _mod_bzrbranch,
38
from ..bzr.fullhistory import (
37
from bzrlib.branchfmt.fullhistory import (
42
from ..sixish import (
47
class TestErrors(tests.TestCase):
49
def test_unstackable_branch_format(self):
52
error = _mod_branch.UnstackableBranchFormat(format, url)
54
"The branch '/foo'(foo) is not a stackable format. "
55
"You will need to upgrade the branch to permit branch stacking.",
59
43
class TestDefaultFormat(tests.TestCase):
133
116
def get_format_string(cls):
134
117
"""See BzrBranchFormat.get_format_string()."""
135
return b"Sample branch format."
118
return "Sample branch format."
137
def initialize(self, a_controldir, name=None, repository=None,
120
def initialize(self, a_bzrdir, name=None, repository=None,
138
121
append_revisions_only=None):
139
122
"""Format 4 branches cannot be created."""
140
t = a_controldir.get_branch_transport(self, name=name)
123
t = a_bzrdir.get_branch_transport(self, name=name)
141
124
t.put_bytes('format', self.get_format_string())
142
125
return 'A branch'
162
145
"""See BzrBranchFormat.get_format_string()."""
163
146
return SampleSupportedBranchFormatString
165
def initialize(self, a_controldir, name=None, append_revisions_only=None):
166
t = a_controldir.get_branch_transport(self, name=name)
148
def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
149
t = a_bzrdir.get_branch_transport(self, name=name)
167
150
t.put_bytes('format', self.get_format_string())
168
151
return 'A branch'
200
183
# this is not quite the same as
201
184
self.build_tree(["foo/", "bar/"])
202
185
def check_format(format, url):
203
dir = format._matchingcontroldir.initialize(url)
186
dir = format._matchingbzrdir.initialize(url)
204
187
dir.create_repository()
205
188
format.initialize(dir)
206
found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(dir)
189
found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
207
190
self.assertIsInstance(found_format, format.__class__)
208
191
check_format(BzrBranchFormat5(), "bar")
193
def test_find_format_factory(self):
194
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
195
SampleSupportedBranchFormat().initialize(dir)
196
factory = _mod_branch.MetaDirBranchFormatFactory(
197
SampleSupportedBranchFormatString,
198
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
199
_mod_branch.format_registry.register(factory)
200
self.addCleanup(_mod_branch.format_registry.remove, factory)
201
b = _mod_branch.Branch.open(self.get_url())
202
self.assertEqual(b, "opened supported branch.")
210
204
def test_from_string(self):
211
205
self.assertIsInstance(
212
SampleBranchFormat.from_string(b"Sample branch format."),
206
SampleBranchFormat.from_string("Sample branch format."),
213
207
SampleBranchFormat)
214
208
self.assertRaises(AssertionError,
215
SampleBranchFormat.from_string, b"Different branch format.")
209
SampleBranchFormat.from_string, "Different branch format.")
217
211
def test_find_format_not_branch(self):
218
212
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
219
213
self.assertRaises(errors.NotBranchError,
220
_mod_bzrbranch.BranchFormatMetadir.find_format,
214
_mod_branch.BranchFormatMetadir.find_format,
223
217
def test_find_format_unknown_format(self):
224
218
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
225
219
SampleBranchFormat().initialize(dir)
226
220
self.assertRaises(errors.UnknownFormatError,
227
_mod_bzrbranch.BranchFormatMetadir.find_format,
221
_mod_branch.BranchFormatMetadir.find_format,
230
224
def test_find_format_with_features(self):
231
225
tree = self.make_branch_and_tree('.', format='2a')
232
tree.branch.update_feature_flags({b"name": b"optional"})
233
found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(tree.controldir)
234
self.assertIsInstance(found_format, _mod_bzrbranch.BranchFormatMetadir)
235
self.assertEqual(found_format.features.get(b"name"), b"optional")
236
tree.branch.update_feature_flags({b"name": None})
226
tree.branch.update_feature_flags({"name": "optional"})
227
found_format = _mod_branch.BranchFormatMetadir.find_format(tree.bzrdir)
228
self.assertIsInstance(found_format, _mod_branch.BranchFormatMetadir)
229
self.assertEquals(found_format.features.get("name"), "optional")
230
tree.branch.update_feature_flags({"name": None})
237
231
branch = _mod_branch.Branch.open('.')
238
self.assertEqual(branch._format.features, {})
232
self.assertEquals(branch._format.features, {})
241
235
class TestBranchFormatRegistry(tests.TestCase):
248
242
self.assertIs(None, self.registry.get_default())
249
243
format = SampleBranchFormat()
250
244
self.registry.set_default(format)
251
self.assertEqual(format, self.registry.get_default())
245
self.assertEquals(format, self.registry.get_default())
253
247
def test_register_unregister_format(self):
254
248
format = SampleBranchFormat()
255
249
self.registry.register(format)
256
self.assertEqual(format,
257
self.registry.get(b"Sample branch format."))
250
self.assertEquals(format,
251
self.registry.get("Sample branch format."))
258
252
self.registry.remove(format)
259
253
self.assertRaises(KeyError, self.registry.get,
260
b"Sample branch format.")
254
"Sample branch format.")
262
256
def test_get_all(self):
263
257
format = SampleBranchFormat()
264
self.assertEqual([], self.registry._get_all())
258
self.assertEquals([], self.registry._get_all())
265
259
self.registry.register(format)
266
self.assertEqual([format], self.registry._get_all())
260
self.assertEquals([format], self.registry._get_all())
268
262
def test_register_extra(self):
269
263
format = SampleExtraBranchFormat()
270
self.assertEqual([], self.registry._get_all())
264
self.assertEquals([], self.registry._get_all())
271
265
self.registry.register_extra(format)
272
self.assertEqual([format], self.registry._get_all())
266
self.assertEquals([format], self.registry._get_all())
274
268
def test_register_extra_lazy(self):
275
self.assertEqual([], self.registry._get_all())
276
self.registry.register_extra_lazy("breezy.tests.test_branch",
269
self.assertEquals([], self.registry._get_all())
270
self.registry.register_extra_lazy("bzrlib.tests.test_branch",
277
271
"SampleExtraBranchFormat")
278
272
formats = self.registry._get_all()
279
self.assertEqual(1, len(formats))
273
self.assertEquals(1, len(formats))
280
274
self.assertIsInstance(formats[0], SampleExtraBranchFormat)
277
#Used by TestMetaDirBranchFormatFactory
278
FakeLazyFormat = None
281
class TestMetaDirBranchFormatFactory(tests.TestCase):
283
def test_get_format_string_does_not_load(self):
284
"""Formats have a static format string."""
285
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
286
self.assertEqual("yo", factory.get_format_string())
288
def test_call_loads(self):
289
# __call__ is used by the network_format_registry interface to get a
291
global FakeLazyFormat
293
factory = _mod_branch.MetaDirBranchFormatFactory(None,
294
"bzrlib.tests.test_branch", "FakeLazyFormat")
295
self.assertRaises(AttributeError, factory)
297
def test_call_returns_call_of_referenced_object(self):
298
global FakeLazyFormat
299
FakeLazyFormat = lambda:'called'
300
factory = _mod_branch.MetaDirBranchFormatFactory(None,
301
"bzrlib.tests.test_branch", "FakeLazyFormat")
302
self.assertEqual('called', factory())
283
305
class TestBranch67(object):
284
306
"""Common tests for both branch 6 and 7 which are mostly the same."""
369
391
def test_set_stacked_on_url_errors(self):
370
392
branch = self.make_branch('a', format=self.get_format_name())
371
self.assertRaises(_mod_branch.UnstackableBranchFormat,
393
self.assertRaises(errors.UnstackableBranchFormat,
372
394
branch.set_stacked_on_url, None)
374
396
def test_default_stacked_location(self):
375
397
branch = self.make_branch('a', format=self.get_format_name())
376
self.assertRaises(_mod_branch.UnstackableBranchFormat, branch.get_stacked_on_url)
398
self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
379
401
class TestBranch7(TestBranch67, tests.TestCaseWithTransport):
381
403
def get_class(self):
382
return _mod_bzrbranch.BzrBranch7
404
return _mod_branch.BzrBranch7
384
406
def get_format_name(self):
390
412
def test_set_stacked_on_url_unstackable_repo(self):
391
413
repo = self.make_repository('a', format='dirstate-tags')
392
control = repo.controldir
393
branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
414
control = repo.bzrdir
415
branch = _mod_branch.BzrBranchFormat7().initialize(control)
394
416
target = self.make_branch('b')
395
417
self.assertRaises(errors.UnstackableRepositoryFormat,
396
418
branch.set_stacked_on_url, target.base)
398
420
def test_clone_stacked_on_unstackable_repo(self):
399
421
repo = self.make_repository('a', format='dirstate-tags')
400
control = repo.controldir
401
branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
422
control = repo.bzrdir
423
branch = _mod_branch.BzrBranchFormat7().initialize(control)
402
424
# Calling clone should not raise UnstackableRepositoryFormat.
403
425
cloned_bzrdir = control.clone('cloned')
431
453
def make_branch(self, location, format=None):
432
454
if format is None:
433
format = controldir.format_registry.make_controldir('1.9')
434
format.set_branch_format(_mod_bzrbranch.BzrBranchFormat8())
455
format = controldir.format_registry.make_bzrdir('1.9')
456
format.set_branch_format(_mod_branch.BzrBranchFormat8())
435
457
return tests.TestCaseWithTransport.make_branch(
436
458
self, location, format=format)
438
460
def create_branch_with_reference(self):
439
461
branch = self.make_branch('branch')
440
branch._set_all_reference_info({'path': ('location', 'file-id')})
462
branch._set_all_reference_info({'file-id': ('path', 'location')})
454
476
branch.lock_read()
455
477
self.addCleanup(branch.unlock)
456
478
self.instrument_branch(branch, gets)
457
branch.get_reference_info('path')
458
branch.get_reference_info('path')
479
branch.get_reference_info('file-id')
480
branch.get_reference_info('file-id')
459
481
self.assertEqual(1, len(gets))
461
483
def test_reference_info_caching_read_unlocked(self):
463
485
branch = self.create_branch_with_reference()
464
486
self.instrument_branch(branch, gets)
465
branch.get_reference_info('path')
466
branch.get_reference_info('path')
487
branch.get_reference_info('file-id')
488
branch.get_reference_info('file-id')
467
489
self.assertEqual(2, len(gets))
469
491
def test_reference_info_caching_write_locked(self):
472
494
branch.lock_write()
473
495
self.instrument_branch(branch, gets)
474
496
self.addCleanup(branch.unlock)
475
branch._set_all_reference_info({'path2': ('location2', 'file-id')})
476
location, file_id = branch.get_reference_info('path2')
497
branch._set_all_reference_info({'file-id': ('path2', 'location2')})
498
path, location = branch.get_reference_info('file-id')
477
499
self.assertEqual(0, len(gets))
478
self.assertEqual('file-id', file_id)
500
self.assertEqual('path2', path)
479
501
self.assertEqual('location2', location)
481
503
def test_reference_info_caches_cleared(self):
482
504
branch = self.make_branch('branch')
483
with branch.lock_write():
484
branch.set_reference_info('path2', 'location2', b'file-id')
506
branch.set_reference_info('file-id', 'path2', 'location2')
485
508
doppelganger = _mod_branch.Branch.open('branch')
486
doppelganger.set_reference_info('path3', 'location3', b'file-id')
487
self.assertEqual(('location3', b'file-id'),
488
branch.get_reference_info('path3'))
509
doppelganger.set_reference_info('file-id', 'path3', 'location3')
510
self.assertEqual(('path3', 'location3'),
511
branch.get_reference_info('file-id'))
490
513
def _recordParentMapCalls(self, repo):
491
514
self._parent_map_calls = []
519
542
"""For a BranchReference, get_reference should return the location."""
520
543
branch = self.make_branch('target')
521
544
checkout = branch.create_checkout('checkout', lightweight=True)
522
reference_url = branch.controldir.root_transport.abspath('') + '/'
545
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
523
546
# if the api for create_checkout changes to return different checkout types
524
547
# then this file read will fail.
525
548
self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
526
549
self.assertEqual(reference_url,
527
_mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
550
_mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
530
553
class TestHooks(tests.TestCaseWithTransport):
581
604
self.build_tree(['branch-1/file-1'])
582
605
tree.add('file-1')
583
606
tree.commit('rev1')
584
to_branch = tree.controldir.sprout('branch-2').open_branch()
607
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
585
608
self.build_tree(['branch-1/file-2'])
586
609
tree.add('file-2')
587
610
tree.remove('file-1')
588
611
tree.commit('rev2')
589
612
checkout = tree.branch.create_checkout('checkout')
590
613
self.assertLength(0, calls)
591
switch.switch(checkout.controldir, to_branch)
614
switch.switch(checkout.bzrdir, to_branch)
592
615
self.assertLength(1, calls)
593
616
params = calls[0]
594
617
self.assertIsInstance(params, _mod_branch.SwitchHookParams)