107
101
"push_location = foo\n"
108
102
"push_location:policy = norecurse\n" % local_path,
103
config.locations_config_filename())
111
105
# TODO RBC 20051029 test getting a push location from a branch in a
112
106
# recursive section - that is, it appends the branch name.
115
class SampleBranchFormat(_mod_branch.BranchFormat):
109
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
116
110
"""A sample format
118
112
this format is initializable, unsupported to aid in testing the
119
113
open and open_downlevel routines.
122
def get_format_string(self):
117
def get_format_string(cls):
123
118
"""See BzrBranchFormat.get_format_string()."""
124
119
return "Sample branch format."
126
def initialize(self, a_bzrdir, name=None):
121
def initialize(self, a_bzrdir, name=None, repository=None,
122
append_revisions_only=None):
127
123
"""Format 4 branches cannot be created."""
128
124
t = a_bzrdir.get_branch_transport(self, name=name)
129
125
t.put_bytes('format', self.get_format_string())
132
128
def is_supported(self):
135
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
131
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
132
possible_transports=None):
136
133
return "opened branch."
136
# Demonstrating how lazy loading is often implemented:
137
# A constant string is created.
138
SampleSupportedBranchFormatString = "Sample supported branch format."
140
# And the format class can then reference the constant to avoid skew.
141
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
142
"""A sample supported format."""
145
def get_format_string(cls):
146
"""See BzrBranchFormat.get_format_string()."""
147
return SampleSupportedBranchFormatString
149
def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
150
t = a_bzrdir.get_branch_transport(self, name=name)
151
t.put_bytes('format', self.get_format_string())
154
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
155
possible_transports=None):
156
return "opened supported branch."
159
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
160
"""A sample format that is not usable in a metadir."""
162
def get_format_string(self):
163
# This format is not usable in a metadir.
166
def network_name(self):
167
# Network name always has to be provided.
170
def initialize(self, a_bzrdir, name=None):
171
raise NotImplementedError(self.initialize)
173
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
174
possible_transports=None):
175
raise NotImplementedError(self.open)
139
178
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
179
"""Tests for the BzrBranchFormat facility."""
148
187
dir = format._matchingbzrdir.initialize(url)
149
188
dir.create_repository()
150
189
format.initialize(dir)
151
found_format = _mod_branch.BranchFormat.find_format(dir)
152
self.failUnless(isinstance(found_format, format.__class__))
153
check_format(_mod_branch.BzrBranchFormat5(), "bar")
190
found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
191
self.assertIsInstance(found_format, format.__class__)
192
check_format(BzrBranchFormat5(), "bar")
194
def test_find_format_factory(self):
195
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
196
SampleSupportedBranchFormat().initialize(dir)
197
factory = _mod_branch.MetaDirBranchFormatFactory(
198
SampleSupportedBranchFormatString,
199
"breezy.tests.test_branch", "SampleSupportedBranchFormat")
200
_mod_branch.format_registry.register(factory)
201
self.addCleanup(_mod_branch.format_registry.remove, factory)
202
b = _mod_branch.Branch.open(self.get_url())
203
self.assertEqual(b, "opened supported branch.")
205
def test_from_string(self):
206
self.assertIsInstance(
207
SampleBranchFormat.from_string("Sample branch format."),
209
self.assertRaises(AssertionError,
210
SampleBranchFormat.from_string, "Different branch format.")
155
212
def test_find_format_not_branch(self):
156
213
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
214
self.assertRaises(errors.NotBranchError,
158
_mod_branch.BranchFormat.find_format,
215
_mod_branch.BranchFormatMetadir.find_format,
161
218
def test_find_format_unknown_format(self):
162
219
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
163
220
SampleBranchFormat().initialize(dir)
164
221
self.assertRaises(errors.UnknownFormatError,
165
_mod_branch.BranchFormat.find_format,
222
_mod_branch.BranchFormatMetadir.find_format,
225
def test_find_format_with_features(self):
226
tree = self.make_branch_and_tree('.', format='2a')
227
tree.branch.update_feature_flags({"name": "optional"})
228
found_format = _mod_branch.BranchFormatMetadir.find_format(tree.bzrdir)
229
self.assertIsInstance(found_format, _mod_branch.BranchFormatMetadir)
230
self.assertEqual(found_format.features.get("name"), "optional")
231
tree.branch.update_feature_flags({"name": None})
232
branch = _mod_branch.Branch.open('.')
233
self.assertEqual(branch._format.features, {})
236
class TestBranchFormatRegistry(tests.TestCase):
239
super(TestBranchFormatRegistry, self).setUp()
240
self.registry = _mod_branch.BranchFormatRegistry()
242
def test_default(self):
243
self.assertIs(None, self.registry.get_default())
244
format = SampleBranchFormat()
245
self.registry.set_default(format)
246
self.assertEqual(format, self.registry.get_default())
168
248
def test_register_unregister_format(self):
169
249
format = SampleBranchFormat()
171
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
173
format.initialize(dir)
174
# register a format for it.
175
_mod_branch.BranchFormat.register_format(format)
176
# which branch.Open will refuse (not supported)
177
self.assertRaises(errors.UnsupportedFormatError,
178
_mod_branch.Branch.open, self.get_url())
179
self.make_branch_and_tree('foo')
180
# but open_downlevel will work
183
bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
184
# unregister the format
185
_mod_branch.BranchFormat.unregister_format(format)
186
self.make_branch_and_tree('bar')
250
self.registry.register(format)
251
self.assertEqual(format,
252
self.registry.get("Sample branch format."))
253
self.registry.remove(format)
254
self.assertRaises(KeyError, self.registry.get,
255
"Sample branch format.")
257
def test_get_all(self):
258
format = SampleBranchFormat()
259
self.assertEqual([], self.registry._get_all())
260
self.registry.register(format)
261
self.assertEqual([format], self.registry._get_all())
263
def test_register_extra(self):
264
format = SampleExtraBranchFormat()
265
self.assertEqual([], self.registry._get_all())
266
self.registry.register_extra(format)
267
self.assertEqual([format], self.registry._get_all())
269
def test_register_extra_lazy(self):
270
self.assertEqual([], self.registry._get_all())
271
self.registry.register_extra_lazy("breezy.tests.test_branch",
272
"SampleExtraBranchFormat")
273
formats = self.registry._get_all()
274
self.assertEqual(1, len(formats))
275
self.assertIsInstance(formats[0], SampleExtraBranchFormat)
278
#Used by TestMetaDirBranchFormatFactory
279
FakeLazyFormat = None
282
class TestMetaDirBranchFormatFactory(tests.TestCase):
284
def test_get_format_string_does_not_load(self):
285
"""Formats have a static format string."""
286
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
287
self.assertEqual("yo", factory.get_format_string())
289
def test_call_loads(self):
290
# __call__ is used by the network_format_registry interface to get a
292
global FakeLazyFormat
294
factory = _mod_branch.MetaDirBranchFormatFactory(None,
295
"breezy.tests.test_branch", "FakeLazyFormat")
296
self.assertRaises(AttributeError, factory)
298
def test_call_returns_call_of_referenced_object(self):
299
global FakeLazyFormat
300
FakeLazyFormat = lambda:'called'
301
factory = _mod_branch.MetaDirBranchFormatFactory(None,
302
"breezy.tests.test_branch", "FakeLazyFormat")
303
self.assertEqual('called', factory())
189
306
class TestBranch67(object):
211
328
def test_layout(self):
212
329
branch = self.make_branch('a', format=self.get_format_name())
213
self.failUnlessExists('a/.bzr/branch/last-revision')
214
self.failIfExists('a/.bzr/branch/revision-history')
215
self.failIfExists('a/.bzr/branch/references')
330
self.assertPathExists('a/.bzr/branch/last-revision')
331
self.assertPathDoesNotExist('a/.bzr/branch/revision-history')
332
self.assertPathDoesNotExist('a/.bzr/branch/references')
217
334
def test_config(self):
218
335
"""Ensure that all configuration data is stored in the branch"""
219
336
branch = self.make_branch('a', format=self.get_format_name())
220
branch.set_parent('http://bazaar-vcs.org')
221
self.failIfExists('a/.bzr/branch/parent')
222
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
223
branch.set_push_location('sftp://bazaar-vcs.org')
224
config = branch.get_config()._get_branch_data_config()
225
self.assertEqual('sftp://bazaar-vcs.org',
226
config.get_user_option('push_location'))
227
branch.set_bound_location('ftp://bazaar-vcs.org')
228
self.failIfExists('a/.bzr/branch/bound')
229
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
231
def test_set_revision_history(self):
232
builder = self.make_branch_builder('.', format=self.get_format_name())
233
builder.build_snapshot('foo', None,
234
[('add', ('', None, 'directory', None))],
236
builder.build_snapshot('bar', None, [], message='bar')
237
branch = builder.get_branch()
239
self.addCleanup(branch.unlock)
240
branch.set_revision_history(['foo', 'bar'])
241
branch.set_revision_history(['foo'])
242
self.assertRaises(errors.NotLefthandHistory,
243
branch.set_revision_history, ['bar'])
337
branch.set_parent('http://example.com')
338
self.assertPathDoesNotExist('a/.bzr/branch/parent')
339
self.assertEqual('http://example.com', branch.get_parent())
340
branch.set_push_location('sftp://example.com')
341
conf = branch.get_config_stack()
342
self.assertEqual('sftp://example.com', conf.get('push_location'))
343
branch.set_bound_location('ftp://example.com')
344
self.assertPathDoesNotExist('a/.bzr/branch/bound')
345
self.assertEqual('ftp://example.com', branch.get_bound_location())
245
347
def do_checkout_test(self, lightweight=False):
246
348
tree = self.make_branch_and_tree('source',
550
654
self.check_append_revisions_only(None, 'not-a-bool')
551
655
self.assertLength(1, self.warnings)
552
656
self.assertEqual(
553
'Value "not-a-bool" is not a boolean for "append_revisions_only"',
657
'Value "not-a-bool" is not valid for "append_revisions_only"',
554
658
self.warnings[0])
660
def test_use_fresh_values(self):
661
copy = _mod_branch.Branch.open(self.branch.base)
664
copy.get_config_stack().set('foo', 'bar')
667
self.assertFalse(self.branch.is_locked())
668
# Since the branch is locked, the option value won't be saved on disk
669
# so trying to access the config of locked branch via another older
670
# non-locked branch object pointing to the same branch is not supported
671
self.assertEqual(None, self.branch.get_config_stack().get('foo'))
672
# Using a newly created branch object works as expected
673
fresh = _mod_branch.Branch.open(self.branch.base)
674
self.assertEqual('bar', fresh.get_config_stack().get('foo'))
676
def test_set_from_config_get_from_config_stack(self):
677
self.branch.lock_write()
678
self.addCleanup(self.branch.unlock)
679
self.branch.get_config().set_user_option('foo', 'bar')
680
result = self.branch.get_config_stack().get('foo')
681
# https://bugs.launchpad.net/bzr/+bug/948344
682
self.expectFailure('BranchStack uses cache after set_user_option',
683
self.assertEqual, 'bar', result)
685
def test_set_from_config_stack_get_from_config(self):
686
self.branch.lock_write()
687
self.addCleanup(self.branch.unlock)
688
self.branch.get_config_stack().set('foo', 'bar')
689
# Since the branch is locked, the option value won't be saved on disk
690
# so mixing get() and get_user_option() is broken by design.
691
self.assertEqual(None,
692
self.branch.get_config().get_user_option('foo'))
694
def test_set_delays_write_when_branch_is_locked(self):
695
self.branch.lock_write()
696
self.addCleanup(self.branch.unlock)
697
self.branch.get_config_stack().set('foo', 'bar')
698
copy = _mod_branch.Branch.open(self.branch.base)
699
result = copy.get_config_stack().get('foo')
700
# Accessing from a different branch object is like accessing from a
701
# different process: the option has not been saved yet and the new
702
# value cannot be seen.
703
self.assertIs(None, result)
557
706
class TestPullResult(tests.TestCase):
559
def test_pull_result_to_int(self):
560
# to support old code, the pull result can be used as an int
561
r = _mod_branch.PullResult()
564
# this usage of results is not recommended for new code (because it
565
# doesn't describe very well what happened), but for api stability
566
# it's still supported
567
a = "%d revisions pulled" % r
568
self.assertEqual(a, "10 revisions pulled")
570
708
def test_report_changed(self):
571
709
r = _mod_branch.PullResult()
572
710
r.old_revid = "old-revid"
574
712
r.new_revid = "new-revid"
578
716
self.assertEqual("Now on revision 20.\n", f.getvalue())
717
self.assertEqual("Now on revision 20.\n", f.getvalue())
580
719
def test_report_unchanged(self):
581
720
r = _mod_branch.PullResult()
582
721
r.old_revid = "same-revid"
583
722
r.new_revid = "same-revid"
586
self.assertEqual("No revisions to pull.\n", f.getvalue())
589
class _StubLockable(object):
590
"""Helper for TestRunWithWriteLockedTarget."""
592
def __init__(self, calls, unlock_exc=None):
594
self.unlock_exc = unlock_exc
596
def lock_write(self):
597
self.calls.append('lock_write')
600
self.calls.append('unlock')
601
if self.unlock_exc is not None:
602
raise self.unlock_exc
605
class _ErrorFromCallable(Exception):
606
"""Helper for TestRunWithWriteLockedTarget."""
609
class _ErrorFromUnlock(Exception):
610
"""Helper for TestRunWithWriteLockedTarget."""
613
class TestRunWithWriteLockedTarget(tests.TestCase):
614
"""Tests for _run_with_write_locked_target."""
617
tests.TestCase.setUp(self)
620
def func_that_returns_ok(self):
621
self._calls.append('func called')
624
def func_that_raises(self):
625
self._calls.append('func called')
626
raise _ErrorFromCallable()
628
def test_success_unlocks(self):
629
lockable = _StubLockable(self._calls)
630
result = _mod_branch._run_with_write_locked_target(
631
lockable, self.func_that_returns_ok)
632
self.assertEqual('ok', result)
633
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
635
def test_exception_unlocks_and_propagates(self):
636
lockable = _StubLockable(self._calls)
637
self.assertRaises(_ErrorFromCallable,
638
_mod_branch._run_with_write_locked_target,
639
lockable, self.func_that_raises)
640
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
642
def test_callable_succeeds_but_error_during_unlock(self):
643
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
644
self.assertRaises(_ErrorFromUnlock,
645
_mod_branch._run_with_write_locked_target,
646
lockable, self.func_that_returns_ok)
647
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
649
def test_error_during_unlock_does_not_mask_original_error(self):
650
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
651
self.assertRaises(_ErrorFromCallable,
652
_mod_branch._run_with_write_locked_target,
653
lockable, self.func_that_raises)
654
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
725
self.assertEqual("No revisions or tags to pull.\n", f.getvalue())