78
74
url = self.get_url()
79
75
bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
80
76
bdir.create_repository()
81
branch = BzrBranchFormat5().initialize(bdir)
77
branch = _mod_branch.BzrBranchFormat5().initialize(bdir)
82
78
t = self.get_transport()
83
79
self.log("branch instance is %r" % branch)
84
self.assert_(isinstance(branch, BzrBranch5))
80
self.assert_(isinstance(branch, _mod_branch.BzrBranch5))
85
81
self.assertIsDirectory('.', t)
86
82
self.assertIsDirectory('.bzr/branch', t)
87
83
self.assertIsDirectory('.bzr/branch/lock', t)
105
101
# recursive section - that is, it appends the branch name.
108
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
104
class SampleBranchFormat(_mod_branch.BranchFormat):
109
105
"""A sample format
111
107
this format is initializable, unsupported to aid in testing the
112
108
open and open_downlevel routines.
116
def get_format_string(cls):
111
def get_format_string(self):
117
112
"""See BzrBranchFormat.get_format_string()."""
118
113
return "Sample branch format."
137
131
SampleSupportedBranchFormatString = "Sample supported branch format."
139
133
# And the format class can then reference the constant to avoid skew.
140
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
134
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
141
135
"""A sample supported format."""
144
def get_format_string(cls):
137
def get_format_string(self):
145
138
"""See BzrBranchFormat.get_format_string()."""
146
139
return SampleSupportedBranchFormatString
150
143
t.put_bytes('format', self.get_format_string())
151
144
return 'A branch'
153
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
154
possible_transports=None):
146
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
155
147
return "opened supported branch."
169
161
def initialize(self, a_bzrdir, name=None):
170
162
raise NotImplementedError(self.initialize)
172
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
173
possible_transports=None):
164
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
174
165
raise NotImplementedError(self.open)
186
177
dir = format._matchingbzrdir.initialize(url)
187
178
dir.create_repository()
188
179
format.initialize(dir)
189
found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
180
found_format = _mod_branch.BranchFormat.find_format(dir)
190
181
self.assertIsInstance(found_format, format.__class__)
191
check_format(BzrBranchFormat5(), "bar")
182
check_format(_mod_branch.BzrBranchFormat5(), "bar")
193
184
def test_find_format_factory(self):
194
185
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
201
192
b = _mod_branch.Branch.open(self.get_url())
202
193
self.assertEqual(b, "opened supported branch.")
204
def test_from_string(self):
205
self.assertIsInstance(
206
SampleBranchFormat.from_string("Sample branch format."),
208
self.assertRaises(AssertionError,
209
SampleBranchFormat.from_string, "Different branch format.")
211
195
def test_find_format_not_branch(self):
212
196
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
213
197
self.assertRaises(errors.NotBranchError,
214
_mod_branch.BranchFormatMetadir.find_format,
198
_mod_branch.BranchFormat.find_format,
217
201
def test_find_format_unknown_format(self):
218
202
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
219
203
SampleBranchFormat().initialize(dir)
220
204
self.assertRaises(errors.UnknownFormatError,
221
_mod_branch.BranchFormatMetadir.find_format,
205
_mod_branch.BranchFormat.find_format,
224
def test_find_format_with_features(self):
225
tree = self.make_branch_and_tree('.', format='2a')
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})
231
branch = _mod_branch.Branch.open('.')
232
self.assertEquals(branch._format.features, {})
208
def test_register_unregister_format(self):
209
# Test the deprecated format registration functions
210
format = SampleBranchFormat()
212
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
214
format.initialize(dir)
215
# register a format for it.
216
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
217
_mod_branch.BranchFormat.register_format, format)
218
# which branch.Open will refuse (not supported)
219
self.assertRaises(errors.UnsupportedFormatError,
220
_mod_branch.Branch.open, self.get_url())
221
self.make_branch_and_tree('foo')
222
# but open_downlevel will work
225
bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
226
# unregister the format
227
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
228
_mod_branch.BranchFormat.unregister_format, format)
229
self.make_branch_and_tree('bar')
235
232
class TestBranchFormatRegistry(tests.TestCase):
337
334
self.assertPathDoesNotExist('a/.bzr/branch/parent')
338
335
self.assertEqual('http://example.com', branch.get_parent())
339
336
branch.set_push_location('sftp://example.com')
340
conf = branch.get_config_stack()
341
self.assertEqual('sftp://example.com', conf.get('push_location'))
337
config = branch.get_config()._get_branch_data_config()
338
self.assertEqual('sftp://example.com',
339
config.get_user_option('push_location'))
342
340
branch.set_bound_location('ftp://example.com')
343
341
self.assertPathDoesNotExist('a/.bzr/branch/bound')
344
342
self.assertEqual('ftp://example.com', branch.get_bound_location())
344
def test_set_revision_history(self):
345
builder = self.make_branch_builder('.', format=self.get_format_name())
346
builder.build_snapshot('foo', None,
347
[('add', ('', None, 'directory', None))],
349
builder.build_snapshot('bar', None, [], message='bar')
350
branch = builder.get_branch()
352
self.addCleanup(branch.unlock)
353
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
354
branch.set_revision_history, ['foo', 'bar'])
355
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
356
branch.set_revision_history, ['foo'])
357
self.assertRaises(errors.NotLefthandHistory,
358
self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
359
branch.set_revision_history, ['bar'])
346
361
def do_checkout_test(self, lightweight=False):
347
362
tree = self.make_branch_and_tree('source',
348
363
format=self.get_format_name_subtree())
376
391
def test_light_checkout_with_references(self):
377
392
self.do_checkout_test(lightweight=True)
394
def test_set_push(self):
395
branch = self.make_branch('source', format=self.get_format_name())
396
branch.get_config().set_user_option('push_location', 'old',
397
store=config.STORE_LOCATION)
400
warnings.append(args[0] % args[1:])
401
_warning = trace.warning
402
trace.warning = warning
404
branch.set_push_location('new')
406
trace.warning = _warning
407
self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
380
411
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
539
570
self.assertEqual(opened_branch.base, target_branch.base)
541
572
def test_get_reference(self):
542
"""For a BranchReference, get_reference should return the location."""
573
"""For a BranchReference, get_reference should reutrn the location."""
543
574
branch = self.make_branch('target')
544
575
checkout = branch.create_checkout('checkout', lightweight=True)
545
576
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
555
586
def test_constructor(self):
556
587
"""Check that creating a BranchHooks instance has the right defaults."""
557
588
hooks = _mod_branch.BranchHooks()
589
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
558
590
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
559
591
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
560
592
self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
625
657
super(TestBranchOptions, self).setUp()
626
658
self.branch = self.make_branch('.')
627
self.config_stack = self.branch.get_config_stack()
659
self.config = self.branch.get_config()
629
661
def check_append_revisions_only(self, expected_value, value=None):
630
662
"""Set append_revisions_only in config and check its interpretation."""
631
663
if value is not None:
632
self.config_stack.set('append_revisions_only', value)
664
self.config.set_user_option('append_revisions_only', value)
633
665
self.assertEqual(expected_value,
634
666
self.branch.get_append_revisions_only())
636
668
def test_valid_append_revisions_only(self):
637
669
self.assertEquals(None,
638
self.config_stack.get('append_revisions_only'))
670
self.config.get_user_option('append_revisions_only'))
639
671
self.check_append_revisions_only(None)
640
672
self.check_append_revisions_only(False, 'False')
641
673
self.check_append_revisions_only(True, 'True')
653
685
self.check_append_revisions_only(None, 'not-a-bool')
654
686
self.assertLength(1, self.warnings)
655
687
self.assertEqual(
656
'Value "not-a-bool" is not valid for "append_revisions_only"',
688
'Value "not-a-bool" is not a boolean for "append_revisions_only"',
657
689
self.warnings[0])
659
def test_use_fresh_values(self):
660
copy = _mod_branch.Branch.open(self.branch.base)
663
copy.get_config_stack().set('foo', 'bar')
666
self.assertFalse(self.branch.is_locked())
667
# Since the branch is locked, the option value won't be saved on disk
668
# so trying to access the config of locked branch via another older
669
# non-locked branch object pointing to the same branch is not supported
670
self.assertEqual(None, self.branch.get_config_stack().get('foo'))
671
# Using a newly created branch object works as expected
672
fresh = _mod_branch.Branch.open(self.branch.base)
673
self.assertEqual('bar', fresh.get_config_stack().get('foo'))
675
def test_set_from_config_get_from_config_stack(self):
676
self.branch.lock_write()
677
self.addCleanup(self.branch.unlock)
678
self.branch.get_config().set_user_option('foo', 'bar')
679
result = self.branch.get_config_stack().get('foo')
680
# https://bugs.launchpad.net/bzr/+bug/948344
681
self.expectFailure('BranchStack uses cache after set_user_option',
682
self.assertEqual, 'bar', result)
684
def test_set_from_config_stack_get_from_config(self):
685
self.branch.lock_write()
686
self.addCleanup(self.branch.unlock)
687
self.branch.get_config_stack().set('foo', 'bar')
688
# Since the branch is locked, the option value won't be saved on disk
689
# so mixing get() and get_user_option() is broken by design.
690
self.assertEqual(None,
691
self.branch.get_config().get_user_option('foo'))
693
def test_set_delays_write_when_branch_is_locked(self):
694
self.branch.lock_write()
695
self.addCleanup(self.branch.unlock)
696
self.branch.get_config_stack().set('foo', 'bar')
697
copy = _mod_branch.Branch.open(self.branch.base)
698
result = copy.get_config_stack().get('foo')
699
# Accessing from a different branch object is like accessing from a
700
# different process: the option has not been saved yet and the new
701
# value cannot be seen.
702
self.assertIs(None, result)
705
692
class TestPullResult(tests.TestCase):
694
def test_pull_result_to_int(self):
695
# to support old code, the pull result can be used as an int
696
r = _mod_branch.PullResult()
699
# this usage of results is not recommended for new code (because it
700
# doesn't describe very well what happened), but for api stability
701
# it's still supported
702
self.assertEqual(self.applyDeprecated(
703
symbol_versioning.deprecated_in((2, 3, 0)),
707
707
def test_report_changed(self):
708
708
r = _mod_branch.PullResult()
709
709
r.old_revid = "old-revid"