41
41
def test_default_format(self):
42
42
# update this if you change the default branch format
43
self.assertIsInstance(_mod_branch.BranchFormat.get_default_format(),
43
self.assertIsInstance(_mod_branch.format_registry.get_default(),
44
44
_mod_branch.BzrBranchFormat7)
46
46
def test_default_format_is_same_as_bzrdir_default(self):
48
48
# set, but at the moment that's not true -- mbp 20070814 --
49
49
# https://bugs.launchpad.net/bzr/+bug/132376
51
_mod_branch.BranchFormat.get_default_format(),
51
_mod_branch.format_registry.get_default(),
52
52
bzrdir.BzrDirFormat.get_default_format().get_branch_format())
54
54
def test_get_set_default_format(self):
55
55
# set the format and then set it back again
56
old_format = _mod_branch.BranchFormat.get_default_format()
57
_mod_branch.BranchFormat.set_default_format(SampleBranchFormat())
56
old_format = _mod_branch.format_registry.get_default()
57
_mod_branch.format_registry.set_default(SampleBranchFormat())
59
59
# the default branch format is used by the meta dir format
60
60
# which is not the default bzrdir format at this point
62
62
result = dir.create_branch()
63
63
self.assertEqual(result, 'A branch')
65
_mod_branch.BranchFormat.set_default_format(old_format)
65
_mod_branch.format_registry.set_default(old_format)
66
66
self.assertEqual(old_format,
67
_mod_branch.BranchFormat.get_default_format())
67
_mod_branch.format_registry.get_default())
70
70
class TestBranchFormat5(tests.TestCaseWithTransport):
74
74
url = self.get_url()
75
75
bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
76
76
bdir.create_repository()
77
branch = bdir.create_branch()
77
branch = _mod_branch.BzrBranchFormat5().initialize(bdir)
78
78
t = self.get_transport()
79
79
self.log("branch instance is %r" % branch)
80
80
self.assert_(isinstance(branch, _mod_branch.BzrBranch5))
86
86
self.assertIsDirectory('.bzr/branch/lock/held', t)
88
88
def test_set_push_location(self):
89
from bzrlib.config import (locations_config_filename,
90
ensure_config_dir_exists)
91
ensure_config_dir_exists()
92
fn = locations_config_filename()
93
# write correct newlines to locations.conf
94
# by default ConfigObj uses native line-endings for new files
95
# but uses already existing line-endings if file is not empty
98
f.write('# comment\n')
89
conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
102
91
branch = self.make_branch('.', format='knit')
103
92
branch.set_push_location('foo')
123
112
"""See BzrBranchFormat.get_format_string()."""
124
113
return "Sample branch format."
126
def initialize(self, a_bzrdir, name=None):
115
def initialize(self, a_bzrdir, name=None, repository=None):
127
116
"""Format 4 branches cannot be created."""
128
117
t = a_bzrdir.get_branch_transport(self, name=name)
129
118
t.put_bytes('format', self.get_format_string())
157
146
return "opened supported branch."
149
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
150
"""A sample format that is not usable in a metadir."""
152
def get_format_string(self):
153
# This format is not usable in a metadir.
156
def network_name(self):
157
# Network name always has to be provided.
160
def initialize(self, a_bzrdir, name=None):
161
raise NotImplementedError(self.initialize)
163
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
164
raise NotImplementedError(self.open)
160
167
class TestBzrBranchFormat(tests.TestCaseWithTransport):
161
168
"""Tests for the BzrBranchFormat facility."""
170
177
dir.create_repository()
171
178
format.initialize(dir)
172
179
found_format = _mod_branch.BranchFormat.find_format(dir)
173
self.failUnless(isinstance(found_format, format.__class__))
180
self.assertIsInstance(found_format, format.__class__)
174
181
check_format(_mod_branch.BzrBranchFormat5(), "bar")
176
183
def test_find_format_factory(self):
179
186
factory = _mod_branch.MetaDirBranchFormatFactory(
180
187
SampleSupportedBranchFormatString,
181
188
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
182
_mod_branch.BranchFormat.register_format(factory)
183
self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
189
_mod_branch.format_registry.register(factory)
190
self.addCleanup(_mod_branch.format_registry.remove, factory)
184
191
b = _mod_branch.Branch.open(self.get_url())
185
192
self.assertEqual(b, "opened supported branch.")
200
207
def test_register_unregister_format(self):
208
# Test the deprecated format registration functions
201
209
format = SampleBranchFormat()
202
210
# make a control dir
203
211
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
205
213
format.initialize(dir)
206
214
# register a format for it.
207
_mod_branch.BranchFormat.register_format(format)
215
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
216
_mod_branch.BranchFormat.register_format, format)
208
217
# which branch.Open will refuse (not supported)
209
218
self.assertRaises(errors.UnsupportedFormatError,
210
219
_mod_branch.Branch.open, self.get_url())
214
223
format.open(dir),
215
224
bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
216
225
# unregister the format
217
_mod_branch.BranchFormat.unregister_format(format)
226
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
227
_mod_branch.BranchFormat.unregister_format, format)
218
228
self.make_branch_and_tree('bar')
231
class TestBranchFormatRegistry(tests.TestCase):
234
super(TestBranchFormatRegistry, self).setUp()
235
self.registry = _mod_branch.BranchFormatRegistry()
237
def test_default(self):
238
self.assertIs(None, self.registry.get_default())
239
format = SampleBranchFormat()
240
self.registry.set_default(format)
241
self.assertEquals(format, self.registry.get_default())
243
def test_register_unregister_format(self):
244
format = SampleBranchFormat()
245
self.registry.register(format)
246
self.assertEquals(format,
247
self.registry.get("Sample branch format."))
248
self.registry.remove(format)
249
self.assertRaises(KeyError, self.registry.get,
250
"Sample branch format.")
252
def test_get_all(self):
253
format = SampleBranchFormat()
254
self.assertEquals([], self.registry._get_all())
255
self.registry.register(format)
256
self.assertEquals([format], self.registry._get_all())
258
def test_register_extra(self):
259
format = SampleExtraBranchFormat()
260
self.assertEquals([], self.registry._get_all())
261
self.registry.register_extra(format)
262
self.assertEquals([format], self.registry._get_all())
264
def test_register_extra_lazy(self):
265
self.assertEquals([], self.registry._get_all())
266
self.registry.register_extra_lazy("bzrlib.tests.test_branch",
267
"SampleExtraBranchFormat")
268
formats = self.registry._get_all()
269
self.assertEquals(1, len(formats))
270
self.assertIsInstance(formats[0], SampleExtraBranchFormat)
221
273
#Used by TestMetaDirBranchFormatFactory
222
274
FakeLazyFormat = None
271
323
def test_layout(self):
272
324
branch = self.make_branch('a', format=self.get_format_name())
273
self.failUnlessExists('a/.bzr/branch/last-revision')
274
self.failIfExists('a/.bzr/branch/revision-history')
275
self.failIfExists('a/.bzr/branch/references')
325
self.assertPathExists('a/.bzr/branch/last-revision')
326
self.assertPathDoesNotExist('a/.bzr/branch/revision-history')
327
self.assertPathDoesNotExist('a/.bzr/branch/references')
277
329
def test_config(self):
278
330
"""Ensure that all configuration data is stored in the branch"""
279
331
branch = self.make_branch('a', format=self.get_format_name())
280
branch.set_parent('http://bazaar-vcs.org')
281
self.failIfExists('a/.bzr/branch/parent')
282
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
283
branch.set_push_location('sftp://bazaar-vcs.org')
332
branch.set_parent('http://example.com')
333
self.assertPathDoesNotExist('a/.bzr/branch/parent')
334
self.assertEqual('http://example.com', branch.get_parent())
335
branch.set_push_location('sftp://example.com')
284
336
config = branch.get_config()._get_branch_data_config()
285
self.assertEqual('sftp://bazaar-vcs.org',
337
self.assertEqual('sftp://example.com',
286
338
config.get_user_option('push_location'))
287
branch.set_bound_location('ftp://bazaar-vcs.org')
288
self.failIfExists('a/.bzr/branch/bound')
289
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
339
branch.set_bound_location('ftp://example.com')
340
self.assertPathDoesNotExist('a/.bzr/branch/bound')
341
self.assertEqual('ftp://example.com', branch.get_bound_location())
291
343
def test_set_revision_history(self):
292
344
builder = self.make_branch_builder('.', format=self.get_format_name())
297
349
branch = builder.get_branch()
298
350
branch.lock_write()
299
351
self.addCleanup(branch.unlock)
300
branch.set_revision_history(['foo', 'bar'])
301
branch.set_revision_history(['foo'])
352
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
353
branch.set_revision_history, ['foo', 'bar'])
354
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
355
branch.set_revision_history, ['foo'])
302
356
self.assertRaises(errors.NotLefthandHistory,
303
branch.set_revision_history, ['bar'])
357
self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
358
branch.set_revision_history, ['bar'])
305
360
def do_checkout_test(self, lightweight=False):
306
361
tree = self.make_branch_and_tree('source',
319
374
subtree.commit('a subtree file')
320
375
subsubtree.commit('a subsubtree file')
321
376
tree.branch.create_checkout('target', lightweight=lightweight)
322
self.failUnlessExists('target')
323
self.failUnlessExists('target/subtree')
324
self.failUnlessExists('target/subtree/file')
325
self.failUnlessExists('target/subtree/subsubtree/file')
377
self.assertPathExists('target')
378
self.assertPathExists('target/subtree')
379
self.assertPathExists('target/subtree/file')
380
self.assertPathExists('target/subtree/subsubtree/file')
326
381
subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
328
383
self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
552
607
self.assertTrue(hasattr(params, 'bzrdir'))
553
608
self.assertTrue(hasattr(params, 'branch'))
610
def test_post_branch_init_hook_repr(self):
612
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
613
lambda params: param_reprs.append(repr(params)), None)
614
branch = self.make_branch('a')
615
self.assertLength(1, param_reprs)
616
param_repr = param_reprs[0]
617
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
555
619
def test_post_switch_hook(self):
556
620
from bzrlib import switch
624
688
# this usage of results is not recommended for new code (because it
625
689
# doesn't describe very well what happened), but for api stability
626
690
# it's still supported
627
a = "%d revisions pulled" % r
628
self.assertEqual(a, "10 revisions pulled")
691
self.assertEqual(self.applyDeprecated(
692
symbol_versioning.deprecated_in((2, 3, 0)),
630
696
def test_report_changed(self):
631
697
r = _mod_branch.PullResult()