48
52
# set, but at the moment that's not true -- mbp 20070814 --
49
53
# https://bugs.launchpad.net/bzr/+bug/132376
51
_mod_branch.BranchFormat.get_default_format(),
55
_mod_branch.format_registry.get_default(),
52
56
bzrdir.BzrDirFormat.get_default_format().get_branch_format())
54
58
def test_get_set_default_format(self):
55
59
# 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())
60
old_format = _mod_branch.format_registry.get_default()
61
_mod_branch.format_registry.set_default(SampleBranchFormat())
59
63
# the default branch format is used by the meta dir format
60
64
# which is not the default bzrdir format at this point
62
66
result = dir.create_branch()
63
67
self.assertEqual(result, 'A branch')
65
_mod_branch.BranchFormat.set_default_format(old_format)
69
_mod_branch.format_registry.set_default(old_format)
66
70
self.assertEqual(old_format,
67
_mod_branch.BranchFormat.get_default_format())
71
_mod_branch.format_registry.get_default())
74
class TestBranchFormat4(tests.TestCaseWithTransport):
75
"""Tests specific to branch format 4"""
77
def test_no_metadir_support(self):
79
bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
80
bdir.create_repository()
81
self.assertRaises(errors.IncompatibleFormat,
82
BzrBranchFormat4().initialize, bdir)
84
def test_supports_bzrdir_6(self):
86
bdir = bzrdir.BzrDirFormat6().initialize(url)
87
bdir.create_repository()
88
BzrBranchFormat4().initialize(bdir)
70
91
class TestBranchFormat5(tests.TestCaseWithTransport):
86
107
self.assertIsDirectory('.bzr/branch/lock/held', t)
88
109
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')
110
conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
102
112
branch = self.make_branch('.', format='knit')
103
113
branch.set_push_location('foo')
123
133
"""See BzrBranchFormat.get_format_string()."""
124
134
return "Sample branch format."
126
def initialize(self, a_bzrdir, name=None):
136
def initialize(self, a_bzrdir, name=None, repository=None):
127
137
"""Format 4 branches cannot be created."""
128
138
t = a_bzrdir.get_branch_transport(self, name=name)
129
139
t.put_bytes('format', self.get_format_string())
136
146
return "opened branch."
149
# Demonstrating how lazy loading is often implemented:
150
# A constant string is created.
151
SampleSupportedBranchFormatString = "Sample supported branch format."
153
# And the format class can then reference the constant to avoid skew.
154
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
155
"""A sample supported format."""
157
def get_format_string(self):
158
"""See BzrBranchFormat.get_format_string()."""
159
return SampleSupportedBranchFormatString
161
def initialize(self, a_bzrdir, name=None):
162
t = a_bzrdir.get_branch_transport(self, name=name)
163
t.put_bytes('format', self.get_format_string())
166
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
167
return "opened supported branch."
170
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
171
"""A sample format that is not usable in a metadir."""
173
def get_format_string(self):
174
# This format is not usable in a metadir.
177
def network_name(self):
178
# Network name always has to be provided.
181
def initialize(self, a_bzrdir, name=None):
182
raise NotImplementedError(self.initialize)
184
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
185
raise NotImplementedError(self.open)
139
188
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
189
"""Tests for the BzrBranchFormat facility."""
152
201
self.failUnless(isinstance(found_format, format.__class__))
153
202
check_format(_mod_branch.BzrBranchFormat5(), "bar")
204
def test_find_format_factory(self):
205
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
206
SampleSupportedBranchFormat().initialize(dir)
207
factory = _mod_branch.MetaDirBranchFormatFactory(
208
SampleSupportedBranchFormatString,
209
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
210
_mod_branch.format_registry.register(factory)
211
self.addCleanup(_mod_branch.format_registry.remove, factory)
212
b = _mod_branch.Branch.open(self.get_url())
213
self.assertEqual(b, "opened supported branch.")
155
215
def test_find_format_not_branch(self):
156
216
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
217
self.assertRaises(errors.NotBranchError,
168
228
def test_register_unregister_format(self):
229
# Test the deprecated format registration functions
169
230
format = SampleBranchFormat()
170
231
# make a control dir
171
232
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
173
234
format.initialize(dir)
174
235
# register a format for it.
175
_mod_branch.BranchFormat.register_format(format)
236
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
237
_mod_branch.BranchFormat.register_format, format)
176
238
# which branch.Open will refuse (not supported)
177
239
self.assertRaises(errors.UnsupportedFormatError,
178
240
_mod_branch.Branch.open, self.get_url())
182
244
format.open(dir),
183
245
bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
184
246
# unregister the format
185
_mod_branch.BranchFormat.unregister_format(format)
247
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
248
_mod_branch.BranchFormat.unregister_format, format)
186
249
self.make_branch_and_tree('bar')
252
class TestBranchFormatRegistry(tests.TestCase):
255
super(TestBranchFormatRegistry, self).setUp()
256
self.registry = _mod_branch.BranchFormatRegistry()
258
def test_default(self):
259
self.assertIs(None, self.registry.get_default())
260
format = SampleBranchFormat()
261
self.registry.set_default(format)
262
self.assertEquals(format, self.registry.get_default())
264
def test_register_unregister_format(self):
265
format = SampleBranchFormat()
266
self.registry.register(format)
267
self.assertEquals(format,
268
self.registry.get("Sample branch format."))
269
self.registry.remove(format)
270
self.assertRaises(KeyError, self.registry.get,
271
"Sample branch format.")
273
def test_get_all(self):
274
format = SampleBranchFormat()
275
self.assertEquals([], self.registry._get_all())
276
self.registry.register(format)
277
self.assertEquals([format], self.registry._get_all())
279
def test_register_extra(self):
280
format = SampleExtraBranchFormat()
281
self.assertEquals([], self.registry._get_all())
282
self.registry.register_extra(format)
283
self.assertEquals([format], self.registry._get_all())
285
def test_register_extra_lazy(self):
286
self.assertEquals([], self.registry._get_all())
287
self.registry.register_extra_lazy("bzrlib.tests.test_branch",
288
"SampleExtraBranchFormat")
289
formats = self.registry._get_all()
290
self.assertEquals(1, len(formats))
291
self.assertIsInstance(formats[0], SampleExtraBranchFormat)
294
#Used by TestMetaDirBranchFormatFactory
295
FakeLazyFormat = None
298
class TestMetaDirBranchFormatFactory(tests.TestCase):
300
def test_get_format_string_does_not_load(self):
301
"""Formats have a static format string."""
302
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
303
self.assertEqual("yo", factory.get_format_string())
305
def test_call_loads(self):
306
# __call__ is used by the network_format_registry interface to get a
308
global FakeLazyFormat
310
factory = _mod_branch.MetaDirBranchFormatFactory(None,
311
"bzrlib.tests.test_branch", "FakeLazyFormat")
312
self.assertRaises(AttributeError, factory)
314
def test_call_returns_call_of_referenced_object(self):
315
global FakeLazyFormat
316
FakeLazyFormat = lambda:'called'
317
factory = _mod_branch.MetaDirBranchFormatFactory(None,
318
"bzrlib.tests.test_branch", "FakeLazyFormat")
319
self.assertEqual('called', factory())
189
322
class TestBranch67(object):
190
323
"""Common tests for both branch 6 and 7 which are mostly the same."""
217
350
def test_config(self):
218
351
"""Ensure that all configuration data is stored in the branch"""
219
352
branch = self.make_branch('a', format=self.get_format_name())
220
branch.set_parent('http://bazaar-vcs.org')
353
branch.set_parent('http://example.com')
221
354
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')
355
self.assertEqual('http://example.com', branch.get_parent())
356
branch.set_push_location('sftp://example.com')
224
357
config = branch.get_config()._get_branch_data_config()
225
self.assertEqual('sftp://bazaar-vcs.org',
358
self.assertEqual('sftp://example.com',
226
359
config.get_user_option('push_location'))
227
branch.set_bound_location('ftp://bazaar-vcs.org')
360
branch.set_bound_location('ftp://example.com')
228
361
self.failIfExists('a/.bzr/branch/bound')
229
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
362
self.assertEqual('ftp://example.com', branch.get_bound_location())
231
364
def test_set_revision_history(self):
232
365
builder = self.make_branch_builder('.', format=self.get_format_name())
492
625
self.assertTrue(hasattr(params, 'bzrdir'))
493
626
self.assertTrue(hasattr(params, 'branch'))
628
def test_post_branch_init_hook_repr(self):
630
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
631
lambda params: param_reprs.append(repr(params)), None)
632
branch = self.make_branch('a')
633
self.assertLength(1, param_reprs)
634
param_repr = param_reprs[0]
635
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
495
637
def test_post_switch_hook(self):
496
638
from bzrlib import switch
564
706
# this usage of results is not recommended for new code (because it
565
707
# doesn't describe very well what happened), but for api stability
566
708
# it's still supported
567
a = "%d revisions pulled" % r
568
self.assertEqual(a, "10 revisions pulled")
709
self.assertEqual(self.applyDeprecated(
710
symbol_versioning.deprecated_in((2, 3, 0)),
570
714
def test_report_changed(self):
571
715
r = _mod_branch.PullResult()