38
branch as _mod_bzrbranch,
41
from ..bzr.fullhistory import (
37
from brzlib.branchfmt.fullhistory 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):
61
45
def test_default_format(self):
62
46
# update this if you change the default branch format
63
47
self.assertIsInstance(_mod_branch.format_registry.get_default(),
64
_mod_bzrbranch.BzrBranchFormat7)
48
_mod_branch.BzrBranchFormat7)
66
50
def test_default_format_is_same_as_bzrdir_default(self):
67
51
# XXX: it might be nice if there was only one place the default was
112
95
branch = self.make_branch('.', format='knit')
113
96
branch.set_push_location('foo')
114
97
local_path = urlutils.local_path_from_url(branch.base[:-1])
115
self.assertFileEqual(b"# comment\n"
117
b"push_location = foo\n"
118
b"push_location:policy = norecurse\n" % local_path.encode(
120
bedding.locations_config_path())
98
self.assertFileEqual("# comment\n"
100
"push_location = foo\n"
101
"push_location:policy = norecurse\n" % local_path,
102
config.locations_config_filename())
122
104
# TODO RBC 20051029 test getting a push location from a branch in a
123
105
# recursive section - that is, it appends the branch name.
126
class SampleBranchFormat(_mod_bzrbranch.BranchFormatMetadir):
108
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
127
109
"""A sample format
129
111
this format is initializable, unsupported to aid in testing the
134
116
def get_format_string(cls):
135
117
"""See BzrBranchFormat.get_format_string()."""
136
return b"Sample branch format."
118
return "Sample branch format."
138
def initialize(self, a_controldir, name=None, repository=None,
120
def initialize(self, a_bzrdir, name=None, repository=None,
139
121
append_revisions_only=None):
140
122
"""Format 4 branches cannot be created."""
141
t = a_controldir.get_branch_transport(self, name=name)
123
t = a_bzrdir.get_branch_transport(self, name=name)
142
124
t.put_bytes('format', self.get_format_string())
143
125
return 'A branch'
165
145
"""See BzrBranchFormat.get_format_string()."""
166
146
return SampleSupportedBranchFormatString
168
def initialize(self, a_controldir, name=None, append_revisions_only=None):
169
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)
170
150
t.put_bytes('format', self.get_format_string())
171
151
return 'A branch'
202
182
# create a branch with a few known format objects.
203
183
# this is not quite the same as
204
184
self.build_tree(["foo/", "bar/"])
206
185
def check_format(format, url):
207
dir = format._matchingcontroldir.initialize(url)
186
dir = format._matchingbzrdir.initialize(url)
208
187
dir.create_repository()
209
188
format.initialize(dir)
210
found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(dir)
189
found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
211
190
self.assertIsInstance(found_format, format.__class__)
212
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
"brzlib.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.")
214
204
def test_from_string(self):
215
205
self.assertIsInstance(
216
SampleBranchFormat.from_string(b"Sample branch format."),
206
SampleBranchFormat.from_string("Sample branch format."),
217
207
SampleBranchFormat)
218
208
self.assertRaises(AssertionError,
219
SampleBranchFormat.from_string, b"Different branch format.")
209
SampleBranchFormat.from_string, "Different branch format.")
221
211
def test_find_format_not_branch(self):
222
212
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
223
213
self.assertRaises(errors.NotBranchError,
224
_mod_bzrbranch.BranchFormatMetadir.find_format,
214
_mod_branch.BranchFormatMetadir.find_format,
227
217
def test_find_format_unknown_format(self):
228
218
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
229
219
SampleBranchFormat().initialize(dir)
230
220
self.assertRaises(errors.UnknownFormatError,
231
_mod_bzrbranch.BranchFormatMetadir.find_format,
221
_mod_branch.BranchFormatMetadir.find_format,
234
224
def test_find_format_with_features(self):
235
225
tree = self.make_branch_and_tree('.', format='2a')
236
tree.branch.update_feature_flags({b"name": b"optional"})
237
found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(
239
self.assertIsInstance(found_format, _mod_bzrbranch.BranchFormatMetadir)
240
self.assertEqual(found_format.features.get(b"name"), b"optional")
241
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.assertEqual(found_format.features.get("name"), "optional")
230
tree.branch.update_feature_flags({"name": None})
242
231
branch = _mod_branch.Branch.open('.')
243
232
self.assertEqual(branch._format.features, {})
279
268
def test_register_extra_lazy(self):
280
269
self.assertEqual([], self.registry._get_all())
281
self.registry.register_extra_lazy("breezy.tests.test_branch",
282
"SampleExtraBranchFormat")
270
self.registry.register_extra_lazy("brzlib.tests.test_branch",
271
"SampleExtraBranchFormat")
283
272
formats = self.registry._get_all()
284
273
self.assertEqual(1, len(formats))
285
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
"brzlib.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
"brzlib.tests.test_branch", "FakeLazyFormat")
302
self.assertEqual('called', factory())
288
305
class TestBranch67(object):
289
306
"""Common tests for both branch 6 and 7 which are mostly the same."""
326
343
self.assertPathDoesNotExist('a/.bzr/branch/bound')
327
344
self.assertEqual('ftp://example.com', branch.get_bound_location())
329
def do_checkout_test(self, lightweight):
346
def do_checkout_test(self, lightweight=False):
330
347
tree = self.make_branch_and_tree('source',
331
format=self.get_format_name_subtree())
348
format=self.get_format_name_subtree())
332
349
subtree = self.make_branch_and_tree('source/subtree',
333
format=self.get_format_name_subtree())
350
format=self.get_format_name_subtree())
334
351
subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
335
format=self.get_format_name_subtree())
352
format=self.get_format_name_subtree())
336
353
self.build_tree(['source/subtree/file',
337
354
'source/subtree/subsubtree/file'])
338
355
subsubtree.add('file')
339
356
subtree.add('file')
340
357
subtree.add_reference(subsubtree)
341
subtree.set_reference_info('subsubtree', subsubtree.branch.user_url)
342
358
tree.add_reference(subtree)
343
tree.set_reference_info('subtree', subtree.branch.user_url)
344
359
tree.commit('a revision')
345
360
subtree.commit('a subtree file')
346
361
subsubtree.commit('a subsubtree file')
376
391
def test_set_stacked_on_url_errors(self):
377
392
branch = self.make_branch('a', format=self.get_format_name())
378
self.assertRaises(_mod_branch.UnstackableBranchFormat,
379
branch.set_stacked_on_url, None)
393
self.assertRaises(errors.UnstackableBranchFormat,
394
branch.set_stacked_on_url, None)
381
396
def test_default_stacked_location(self):
382
397
branch = self.make_branch('a', format=self.get_format_name())
383
self.assertRaises(_mod_branch.UnstackableBranchFormat,
384
branch.get_stacked_on_url)
398
self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
387
401
class TestBranch7(TestBranch67, tests.TestCaseWithTransport):
389
403
def get_class(self):
390
return _mod_bzrbranch.BzrBranch7
404
return _mod_branch.BzrBranch7
392
406
def get_format_name(self):
398
412
def test_set_stacked_on_url_unstackable_repo(self):
399
413
repo = self.make_repository('a', format='dirstate-tags')
400
control = repo.controldir
401
branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
414
control = repo.bzrdir
415
branch = _mod_branch.BzrBranchFormat7().initialize(control)
402
416
target = self.make_branch('b')
403
417
self.assertRaises(errors.UnstackableRepositoryFormat,
404
branch.set_stacked_on_url, target.base)
418
branch.set_stacked_on_url, target.base)
406
420
def test_clone_stacked_on_unstackable_repo(self):
407
421
repo = self.make_repository('a', format='dirstate-tags')
408
control = repo.controldir
409
branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
422
control = repo.bzrdir
423
branch = _mod_branch.BzrBranchFormat7().initialize(control)
410
424
# Calling clone should not raise UnstackableRepositoryFormat.
411
425
cloned_bzrdir = control.clone('cloned')
439
453
def make_branch(self, location, format=None):
440
454
if format is None:
441
format = controldir.format_registry.make_controldir('1.9')
442
format.set_branch_format(_mod_bzrbranch.BzrBranchFormat8())
455
format = controldir.format_registry.make_bzrdir('1.9')
456
format.set_branch_format(_mod_branch.BzrBranchFormat8())
443
457
return tests.TestCaseWithTransport.make_branch(
444
458
self, location, format=format)
446
460
def create_branch_with_reference(self):
447
461
branch = self.make_branch('branch')
448
branch._set_all_reference_info({'path': ('location', b'file-id')})
462
branch._set_all_reference_info({'file-id': ('path', 'location')})
452
466
def instrument_branch(branch, gets):
453
467
old_get = branch._transport.get
455
468
def get(*args, **kwargs):
456
469
gets.append((args, kwargs))
457
470
return old_get(*args, **kwargs)
463
476
branch.lock_read()
464
477
self.addCleanup(branch.unlock)
465
478
self.instrument_branch(branch, gets)
466
branch.get_reference_info('path')
467
branch.get_reference_info('path')
479
branch.get_reference_info('file-id')
480
branch.get_reference_info('file-id')
468
481
self.assertEqual(1, len(gets))
470
483
def test_reference_info_caching_read_unlocked(self):
472
485
branch = self.create_branch_with_reference()
473
486
self.instrument_branch(branch, gets)
474
branch.get_reference_info('path')
475
branch.get_reference_info('path')
487
branch.get_reference_info('file-id')
488
branch.get_reference_info('file-id')
476
489
self.assertEqual(2, len(gets))
478
491
def test_reference_info_caching_write_locked(self):
481
494
branch.lock_write()
482
495
self.instrument_branch(branch, gets)
483
496
self.addCleanup(branch.unlock)
484
branch._set_all_reference_info({'path2': ('location2', b'file-id')})
485
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')
486
499
self.assertEqual(0, len(gets))
487
self.assertEqual(b'file-id', file_id)
500
self.assertEqual('path2', path)
488
501
self.assertEqual('location2', location)
490
503
def test_reference_info_caches_cleared(self):
491
504
branch = self.make_branch('branch')
492
with branch.lock_write():
493
branch.set_reference_info(b'file-id', 'location2', 'path2')
506
branch.set_reference_info('file-id', 'path2', 'location2')
494
508
doppelganger = _mod_branch.Branch.open('branch')
495
doppelganger.set_reference_info(b'file-id', 'location3', 'path3')
496
self.assertEqual(('location3', 'path3'),
497
branch.get_reference_info(b'file-id'))
509
doppelganger.set_reference_info('file-id', 'path3', 'location3')
510
self.assertEqual(('path3', 'location3'),
511
branch.get_reference_info('file-id'))
499
513
def _recordParentMapCalls(self, repo):
500
514
self._parent_map_calls = []
501
515
orig_get_parent_map = repo.revisions.get_parent_map
503
516
def get_parent_map(q):
505
518
self._parent_map_calls.extend([e[0] for e in q])
529
542
"""For a BranchReference, get_reference should return the location."""
530
543
branch = self.make_branch('target')
531
544
checkout = branch.create_checkout('checkout', lightweight=True)
532
reference_url = branch.controldir.root_transport.abspath('') + '/'
545
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
533
546
# if the api for create_checkout changes to return different checkout types
534
547
# then this file read will fail.
535
self.assertFileEqual(reference_url.encode('utf-8'),
536
'checkout/.bzr/branch/location')
548
self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
537
549
self.assertEqual(reference_url,
538
_mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
550
_mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
541
553
class TestHooks(tests.TestCaseWithTransport):
544
556
"""Check that creating a BranchHooks instance has the right defaults."""
545
557
hooks = _mod_branch.BranchHooks()
546
558
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
547
self.assertTrue("post_commit" in hooks,
548
"post_commit not in %s" % hooks)
559
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
549
560
self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
550
561
self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
551
562
self.assertTrue("post_uncommit" in hooks,
566
577
def test_post_branch_init_hook(self):
568
579
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
570
581
self.assertLength(0, calls)
571
582
branch = self.make_branch('a')
572
583
self.assertLength(1, calls)
573
584
params = calls[0]
574
585
self.assertIsInstance(params, _mod_branch.BranchInitHookParams)
575
self.assertTrue(hasattr(params, 'controldir'))
586
self.assertTrue(hasattr(params, 'bzrdir'))
576
587
self.assertTrue(hasattr(params, 'branch'))
578
589
def test_post_branch_init_hook_repr(self):
580
591
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
581
lambda params: param_reprs.append(repr(params)), None)
592
lambda params: param_reprs.append(repr(params)), None)
582
593
branch = self.make_branch('a')
583
594
self.assertLength(1, param_reprs)
584
595
param_repr = param_reprs[0]
585
596
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
587
598
def test_post_switch_hook(self):
588
from .. import switch
599
from brzlib import switch
590
601
_mod_branch.Branch.hooks.install_named_hook('post_switch',
592
603
tree = self.make_branch_and_tree('branch-1')
593
604
self.build_tree(['branch-1/file-1'])
594
605
tree.add('file-1')
595
606
tree.commit('rev1')
596
to_branch = tree.controldir.sprout('branch-2').open_branch()
607
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
597
608
self.build_tree(['branch-1/file-2'])
598
609
tree.add('file-2')
599
610
tree.remove('file-1')
600
611
tree.commit('rev2')
601
612
checkout = tree.branch.create_checkout('checkout')
602
613
self.assertLength(0, calls)
603
switch.switch(checkout.controldir, to_branch)
614
switch.switch(checkout.bzrdir, to_branch)
604
615
self.assertLength(1, calls)
605
616
params = calls[0]
606
617
self.assertIsInstance(params, _mod_branch.SwitchHookParams)
697
707
def test_report_changed(self):
698
708
r = _mod_branch.PullResult()
699
r.old_revid = b"old-revid"
709
r.old_revid = "old-revid"
701
r.new_revid = b"new-revid"
711
r.new_revid = "new-revid"
705
715
self.assertEqual("Now on revision 20.\n", f.getvalue())
716
self.assertEqual("Now on revision 20.\n", f.getvalue())
707
718
def test_report_unchanged(self):
708
719
r = _mod_branch.PullResult()
709
r.old_revid = b"same-revid"
710
r.new_revid = b"same-revid"
720
r.old_revid = "same-revid"
721
r.new_revid = "same-revid"
713
724
self.assertEqual("No revisions or tags to pull.\n", f.getvalue())