86
87
self.assertIsDirectory('.bzr/branch/lock/held', t)
88
89
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')
90
conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
102
92
branch = self.make_branch('.', format='knit')
103
93
branch.set_push_location('foo')
107
97
"push_location = foo\n"
108
98
"push_location:policy = norecurse\n" % local_path,
99
config.locations_config_filename())
111
101
# TODO RBC 20051029 test getting a push location from a branch in a
112
102
# recursive section - that is, it appends the branch name.
123
113
"""See BzrBranchFormat.get_format_string()."""
124
114
return "Sample branch format."
126
def initialize(self, a_bzrdir, name=None):
116
def initialize(self, a_bzrdir, name=None, repository=None):
127
117
"""Format 4 branches cannot be created."""
128
118
t = a_bzrdir.get_branch_transport(self, name=name)
129
119
t.put_bytes('format', self.get_format_string())
136
126
return "opened branch."
129
# Demonstrating how lazy loading is often implemented:
130
# A constant string is created.
131
SampleSupportedBranchFormatString = "Sample supported branch format."
133
# And the format class can then reference the constant to avoid skew.
134
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
135
"""A sample supported format."""
137
def get_format_string(self):
138
"""See BzrBranchFormat.get_format_string()."""
139
return SampleSupportedBranchFormatString
141
def initialize(self, a_bzrdir, name=None):
142
t = a_bzrdir.get_branch_transport(self, name=name)
143
t.put_bytes('format', self.get_format_string())
146
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
147
return "opened supported branch."
139
150
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
151
"""Tests for the BzrBranchFormat facility."""
152
163
self.failUnless(isinstance(found_format, format.__class__))
153
164
check_format(_mod_branch.BzrBranchFormat5(), "bar")
166
def test_find_format_factory(self):
167
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
168
SampleSupportedBranchFormat().initialize(dir)
169
factory = _mod_branch.MetaDirBranchFormatFactory(
170
SampleSupportedBranchFormatString,
171
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
172
_mod_branch.BranchFormat.register_format(factory)
173
self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
174
b = _mod_branch.Branch.open(self.get_url())
175
self.assertEqual(b, "opened supported branch.")
155
177
def test_find_format_not_branch(self):
156
178
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
179
self.assertRaises(errors.NotBranchError,
186
208
self.make_branch_and_tree('bar')
211
#Used by TestMetaDirBranchFormatFactory
212
FakeLazyFormat = None
215
class TestMetaDirBranchFormatFactory(tests.TestCase):
217
def test_get_format_string_does_not_load(self):
218
"""Formats have a static format string."""
219
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
220
self.assertEqual("yo", factory.get_format_string())
222
def test_call_loads(self):
223
# __call__ is used by the network_format_registry interface to get a
225
global FakeLazyFormat
227
factory = _mod_branch.MetaDirBranchFormatFactory(None,
228
"bzrlib.tests.test_branch", "FakeLazyFormat")
229
self.assertRaises(AttributeError, factory)
231
def test_call_returns_call_of_referenced_object(self):
232
global FakeLazyFormat
233
FakeLazyFormat = lambda:'called'
234
factory = _mod_branch.MetaDirBranchFormatFactory(None,
235
"bzrlib.tests.test_branch", "FakeLazyFormat")
236
self.assertEqual('called', factory())
189
239
class TestBranch67(object):
190
240
"""Common tests for both branch 6 and 7 which are mostly the same."""
217
267
def test_config(self):
218
268
"""Ensure that all configuration data is stored in the branch"""
219
269
branch = self.make_branch('a', format=self.get_format_name())
220
branch.set_parent('http://bazaar-vcs.org')
270
branch.set_parent('http://example.com')
221
271
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')
272
self.assertEqual('http://example.com', branch.get_parent())
273
branch.set_push_location('sftp://example.com')
224
274
config = branch.get_config()._get_branch_data_config()
225
self.assertEqual('sftp://bazaar-vcs.org',
275
self.assertEqual('sftp://example.com',
226
276
config.get_user_option('push_location'))
227
branch.set_bound_location('ftp://bazaar-vcs.org')
277
branch.set_bound_location('ftp://example.com')
228
278
self.failIfExists('a/.bzr/branch/bound')
229
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
279
self.assertEqual('ftp://example.com', branch.get_bound_location())
231
281
def test_set_revision_history(self):
232
282
builder = self.make_branch_builder('.', format=self.get_format_name())
492
542
self.assertTrue(hasattr(params, 'bzrdir'))
493
543
self.assertTrue(hasattr(params, 'branch'))
545
def test_post_branch_init_hook_repr(self):
547
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
548
lambda params: param_reprs.append(repr(params)), None)
549
branch = self.make_branch('a')
550
self.assertLength(1, param_reprs)
551
param_repr = param_reprs[0]
552
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
495
554
def test_post_switch_hook(self):
496
555
from bzrlib import switch
564
623
# this usage of results is not recommended for new code (because it
565
624
# doesn't describe very well what happened), but for api stability
566
625
# it's still supported
567
a = "%d revisions pulled" % r
568
self.assertEqual(a, "10 revisions pulled")
626
self.assertEqual(self.applyDeprecated(
627
symbol_versioning.deprecated_in((2, 3, 0)),
570
631
def test_report_changed(self):
571
632
r = _mod_branch.PullResult()