1
# Copyright (C) 2006-2010 Canonical Ltd
1
# Copyright (C) 2006-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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."
150
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
151
"""A sample format that is not usable in a metadir."""
153
def get_format_string(self):
154
# This format is not usable in a metadir.
157
def network_name(self):
158
# Network name always has to be provided.
161
def initialize(self, a_bzrdir, name=None):
162
raise NotImplementedError(self.initialize)
164
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
165
raise NotImplementedError(self.open)
139
168
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
169
"""Tests for the BzrBranchFormat facility."""
152
181
self.failUnless(isinstance(found_format, format.__class__))
153
182
check_format(_mod_branch.BzrBranchFormat5(), "bar")
184
def test_extra_format(self):
185
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
186
SampleSupportedBranchFormat().initialize(dir)
187
format = SampleExtraBranchFormat()
188
_mod_branch.BranchFormat.register_extra_format(format)
189
self.addCleanup(_mod_branch.BranchFormat.unregister_extra_format,
191
self.assertTrue(format in _mod_branch.BranchFormat.get_formats())
192
self.assertEquals(format,
193
_mod_branch.network_format_registry.get("extra"))
195
def test_find_format_factory(self):
196
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
197
SampleSupportedBranchFormat().initialize(dir)
198
factory = _mod_branch.MetaDirBranchFormatFactory(
199
SampleSupportedBranchFormatString,
200
"bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
201
_mod_branch.BranchFormat.register_format(factory)
202
self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
203
b = _mod_branch.Branch.open(self.get_url())
204
self.assertEqual(b, "opened supported branch.")
155
206
def test_find_format_not_branch(self):
156
207
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
208
self.assertRaises(errors.NotBranchError,
186
237
self.make_branch_and_tree('bar')
240
#Used by TestMetaDirBranchFormatFactory
241
FakeLazyFormat = None
244
class TestMetaDirBranchFormatFactory(tests.TestCase):
246
def test_get_format_string_does_not_load(self):
247
"""Formats have a static format string."""
248
factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
249
self.assertEqual("yo", factory.get_format_string())
251
def test_call_loads(self):
252
# __call__ is used by the network_format_registry interface to get a
254
global FakeLazyFormat
256
factory = _mod_branch.MetaDirBranchFormatFactory(None,
257
"bzrlib.tests.test_branch", "FakeLazyFormat")
258
self.assertRaises(AttributeError, factory)
260
def test_call_returns_call_of_referenced_object(self):
261
global FakeLazyFormat
262
FakeLazyFormat = lambda:'called'
263
factory = _mod_branch.MetaDirBranchFormatFactory(None,
264
"bzrlib.tests.test_branch", "FakeLazyFormat")
265
self.assertEqual('called', factory())
189
268
class TestBranch67(object):
190
269
"""Common tests for both branch 6 and 7 which are mostly the same."""
217
296
def test_config(self):
218
297
"""Ensure that all configuration data is stored in the branch"""
219
298
branch = self.make_branch('a', format=self.get_format_name())
220
branch.set_parent('http://bazaar-vcs.org')
299
branch.set_parent('http://example.com')
221
300
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')
301
self.assertEqual('http://example.com', branch.get_parent())
302
branch.set_push_location('sftp://example.com')
224
303
config = branch.get_config()._get_branch_data_config()
225
self.assertEqual('sftp://bazaar-vcs.org',
304
self.assertEqual('sftp://example.com',
226
305
config.get_user_option('push_location'))
227
branch.set_bound_location('ftp://bazaar-vcs.org')
306
branch.set_bound_location('ftp://example.com')
228
307
self.failIfExists('a/.bzr/branch/bound')
229
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
308
self.assertEqual('ftp://example.com', branch.get_bound_location())
231
310
def test_set_revision_history(self):
232
311
builder = self.make_branch_builder('.', format=self.get_format_name())
492
571
self.assertTrue(hasattr(params, 'bzrdir'))
493
572
self.assertTrue(hasattr(params, 'branch'))
574
def test_post_branch_init_hook_repr(self):
576
_mod_branch.Branch.hooks.install_named_hook('post_branch_init',
577
lambda params: param_reprs.append(repr(params)), None)
578
branch = self.make_branch('a')
579
self.assertLength(1, param_reprs)
580
param_repr = param_reprs[0]
581
self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
495
583
def test_post_switch_hook(self):
496
584
from bzrlib import switch
564
652
# this usage of results is not recommended for new code (because it
565
653
# doesn't describe very well what happened), but for api stability
566
654
# it's still supported
567
a = "%d revisions pulled" % r
568
self.assertEqual(a, "10 revisions pulled")
655
self.assertEqual(self.applyDeprecated(
656
symbol_versioning.deprecated_in((2, 3, 0)),
570
660
def test_report_changed(self):
571
661
r = _mod_branch.PullResult()