1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the Branch facility that are not interface tests.
19
For interface tests see tests/branch_implementations/*.py.
21
For concrete class tests see this file, and for meta-branch tests
25
from StringIO import StringIO
28
branch as _mod_branch,
35
from bzrlib.branch import (
39
BranchReferenceFormat,
45
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
47
from bzrlib.errors import (NotBranchError,
50
UnsupportedFormatError,
53
from bzrlib.tests import TestCase, TestCaseWithTransport
54
from bzrlib.transport import get_transport
57
class TestDefaultFormat(TestCase):
59
def test_default_format(self):
60
# update this if you change the default branch format
61
self.assertIsInstance(BranchFormat.get_default_format(),
64
def test_default_format_is_same_as_bzrdir_default(self):
65
# XXX: it might be nice if there was only one place the default was
66
# set, but at the moment that's not true -- mbp 20070814 --
67
# https://bugs.launchpad.net/bzr/+bug/132376
68
self.assertEqual(BranchFormat.get_default_format(),
69
BzrDirFormat.get_default_format().get_branch_format())
71
def test_get_set_default_format(self):
72
# set the format and then set it back again
73
old_format = BranchFormat.get_default_format()
74
BranchFormat.set_default_format(SampleBranchFormat())
76
# the default branch format is used by the meta dir format
77
# which is not the default bzrdir format at this point
78
dir = BzrDirMetaFormat1().initialize('memory:///')
79
result = dir.create_branch()
80
self.assertEqual(result, 'A branch')
82
BranchFormat.set_default_format(old_format)
83
self.assertEqual(old_format, BranchFormat.get_default_format())
86
class TestBranchFormat5(TestCaseWithTransport):
87
"""Tests specific to branch format 5"""
89
def test_branch_format_5_uses_lockdir(self):
91
bzrdir = BzrDirMetaFormat1().initialize(url)
92
bzrdir.create_repository()
93
branch = bzrdir.create_branch()
94
t = self.get_transport()
95
self.log("branch instance is %r" % branch)
96
self.assert_(isinstance(branch, BzrBranch5))
97
self.assertIsDirectory('.', t)
98
self.assertIsDirectory('.bzr/branch', t)
99
self.assertIsDirectory('.bzr/branch/lock', t)
102
self.assertIsDirectory('.bzr/branch/lock/held', t)
106
def test_set_push_location(self):
107
from bzrlib.config import (locations_config_filename,
108
ensure_config_dir_exists)
109
ensure_config_dir_exists()
110
fn = locations_config_filename()
111
# write correct newlines to locations.conf
112
# by default ConfigObj uses native line-endings for new files
113
# but uses already existing line-endings if file is not empty
116
f.write('# comment\n')
120
branch = self.make_branch('.', format='knit')
121
branch.set_push_location('foo')
122
local_path = urlutils.local_path_from_url(branch.base[:-1])
123
self.assertFileEqual("# comment\n"
125
"push_location = foo\n"
126
"push_location:policy = norecurse\n" % local_path,
129
# TODO RBC 20051029 test getting a push location from a branch in a
130
# recursive section - that is, it appends the branch name.
133
class SampleBranchFormat(BranchFormat):
136
this format is initializable, unsupported to aid in testing the
137
open and open_downlevel routines.
140
def get_format_string(self):
141
"""See BzrBranchFormat.get_format_string()."""
142
return "Sample branch format."
144
def initialize(self, a_bzrdir):
145
"""Format 4 branches cannot be created."""
146
t = a_bzrdir.get_branch_transport(self)
147
t.put_bytes('format', self.get_format_string())
150
def is_supported(self):
153
def open(self, transport, _found=False):
154
return "opened branch."
157
class TestBzrBranchFormat(TestCaseWithTransport):
158
"""Tests for the BzrBranchFormat facility."""
160
def test_find_format(self):
161
# is the right format object found for a branch?
162
# create a branch with a few known format objects.
163
# this is not quite the same as
164
self.build_tree(["foo/", "bar/"])
165
def check_format(format, url):
166
dir = format._matchingbzrdir.initialize(url)
167
dir.create_repository()
168
format.initialize(dir)
169
found_format = BranchFormat.find_format(dir)
170
self.failUnless(isinstance(found_format, format.__class__))
171
check_format(BzrBranchFormat5(), "bar")
173
def test_find_format_not_branch(self):
174
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
175
self.assertRaises(NotBranchError,
176
BranchFormat.find_format,
179
def test_find_format_unknown_format(self):
180
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
181
SampleBranchFormat().initialize(dir)
182
self.assertRaises(UnknownFormatError,
183
BranchFormat.find_format,
186
def test_register_unregister_format(self):
187
format = SampleBranchFormat()
189
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
191
format.initialize(dir)
192
# register a format for it.
193
BranchFormat.register_format(format)
194
# which branch.Open will refuse (not supported)
195
self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
196
self.make_branch_and_tree('foo')
197
# but open_downlevel will work
198
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
199
# unregister the format
200
BranchFormat.unregister_format(format)
201
self.make_branch_and_tree('bar')
204
class TestBranch6(TestCaseWithTransport):
206
def test_creation(self):
207
format = BzrDirMetaFormat1()
208
format.set_branch_format(_mod_branch.BzrBranchFormat6())
209
branch = self.make_branch('a', format=format)
210
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
211
branch = self.make_branch('b', format='dirstate-tags')
212
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
213
branch = _mod_branch.Branch.open('a')
214
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
216
def test_layout(self):
217
branch = self.make_branch('a', format='dirstate-tags')
218
self.failUnlessExists('a/.bzr/branch/last-revision')
219
self.failIfExists('a/.bzr/branch/revision-history')
221
def test_config(self):
222
"""Ensure that all configuration data is stored in the branch"""
223
branch = self.make_branch('a', format='dirstate-tags')
224
branch.set_parent('http://bazaar-vcs.org')
225
self.failIfExists('a/.bzr/branch/parent')
226
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
227
branch.set_push_location('sftp://bazaar-vcs.org')
228
config = branch.get_config()._get_branch_data_config()
229
self.assertEqual('sftp://bazaar-vcs.org',
230
config.get_user_option('push_location'))
231
branch.set_bound_location('ftp://bazaar-vcs.org')
232
self.failIfExists('a/.bzr/branch/bound')
233
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
235
def test_set_revision_history(self):
236
tree = self.make_branch_and_memory_tree('.',
237
format='dirstate-tags')
241
tree.commit('foo', rev_id='foo')
242
tree.commit('bar', rev_id='bar')
243
tree.branch.set_revision_history(['foo', 'bar'])
244
tree.branch.set_revision_history(['foo'])
245
self.assertRaises(errors.NotLefthandHistory,
246
tree.branch.set_revision_history, ['bar'])
250
def do_checkout_test(self, lightweight=False):
251
tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
252
subtree = self.make_branch_and_tree('source/subtree',
253
format='dirstate-with-subtree')
254
subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
255
format='dirstate-with-subtree')
256
self.build_tree(['source/subtree/file',
257
'source/subtree/subsubtree/file'])
258
subsubtree.add('file')
260
subtree.add_reference(subsubtree)
261
tree.add_reference(subtree)
262
tree.commit('a revision')
263
subtree.commit('a subtree file')
264
subsubtree.commit('a subsubtree file')
265
tree.branch.create_checkout('target', lightweight=lightweight)
266
self.failUnlessExists('target')
267
self.failUnlessExists('target/subtree')
268
self.failUnlessExists('target/subtree/file')
269
self.failUnlessExists('target/subtree/subsubtree/file')
270
subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
272
self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
274
self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
276
def test_checkout_with_references(self):
277
self.do_checkout_test()
279
def test_light_checkout_with_references(self):
280
self.do_checkout_test(lightweight=True)
282
def test_set_push(self):
283
branch = self.make_branch('source', format='dirstate-tags')
284
branch.get_config().set_user_option('push_location', 'old',
285
store=config.STORE_LOCATION)
288
warnings.append(args[0] % args[1:])
289
_warning = trace.warning
290
trace.warning = warning
292
branch.set_push_location('new')
294
trace.warning = _warning
295
self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
299
class TestBranchReference(TestCaseWithTransport):
300
"""Tests for the branch reference facility."""
302
def test_create_open_reference(self):
303
bzrdirformat = bzrdir.BzrDirMetaFormat1()
304
t = get_transport(self.get_url('.'))
306
dir = bzrdirformat.initialize(self.get_url('repo'))
307
dir.create_repository()
308
target_branch = dir.create_branch()
310
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
311
made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
312
self.assertEqual(made_branch.base, target_branch.base)
313
opened_branch = branch_dir.open_branch()
314
self.assertEqual(opened_branch.base, target_branch.base)
316
def test_get_reference(self):
317
"""For a BranchReference, get_reference should reutrn the location."""
318
branch = self.make_branch('target')
319
checkout = branch.create_checkout('checkout', lightweight=True)
320
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
321
# if the api for create_checkout changes to return different checkout types
322
# then this file read will fail.
323
self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
324
self.assertEqual(reference_url,
325
_mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
328
class TestHooks(TestCase):
330
def test_constructor(self):
331
"""Check that creating a BranchHooks instance has the right defaults."""
332
hooks = BranchHooks()
333
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
334
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
335
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
336
self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
337
self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
338
self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
339
self.assertTrue("post_change_branch_tip" in hooks,
340
"post_change_branch_tip not in %s" % hooks)
342
def test_installed_hooks_are_BranchHooks(self):
343
"""The installed hooks object should be a BranchHooks."""
344
# the installed hooks are saved in self._preserved_hooks.
345
self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
348
class TestPullResult(TestCase):
350
def test_pull_result_to_int(self):
351
# to support old code, the pull result can be used as an int
355
# this usage of results is not recommended for new code (because it
356
# doesn't describe very well what happened), but for api stability
357
# it's still supported
358
a = "%d revisions pulled" % r
359
self.assertEqual(a, "10 revisions pulled")