1
# Copyright (C) 2005, 2006 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,
34
from bzrlib.branch import (BzrBranch5,
36
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
38
from bzrlib.errors import (NotBranchError,
41
UnsupportedFormatError,
44
from bzrlib.tests import TestCase, TestCaseWithTransport
45
from bzrlib.transport import get_transport
47
class TestDefaultFormat(TestCase):
49
def test_get_set_default_format(self):
50
old_format = bzrlib.branch.BranchFormat.get_default_format()
52
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
53
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
55
# the default branch format is used by the meta dir format
56
# which is not the default bzrdir format at this point
57
dir = BzrDirMetaFormat1().initialize('memory:///')
58
result = dir.create_branch()
59
self.assertEqual(result, 'A branch')
61
bzrlib.branch.BranchFormat.set_default_format(old_format)
62
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
65
class TestBranchFormat5(TestCaseWithTransport):
66
"""Tests specific to branch format 5"""
68
def test_branch_format_5_uses_lockdir(self):
70
bzrdir = BzrDirMetaFormat1().initialize(url)
71
bzrdir.create_repository()
72
branch = bzrdir.create_branch()
73
t = self.get_transport()
74
self.log("branch instance is %r" % branch)
75
self.assert_(isinstance(branch, BzrBranch5))
76
self.assertIsDirectory('.', t)
77
self.assertIsDirectory('.bzr/branch', t)
78
self.assertIsDirectory('.bzr/branch/lock', t)
81
self.assertIsDirectory('.bzr/branch/lock/held', t)
85
def test_set_push_location(self):
86
from bzrlib.config import (locations_config_filename,
87
ensure_config_dir_exists)
88
ensure_config_dir_exists()
89
fn = locations_config_filename()
90
branch = self.make_branch('.', format='knit')
91
branch.set_push_location('foo')
92
local_path = urlutils.local_path_from_url(branch.base[:-1])
93
self.assertFileEqual("[%s]\n"
94
"push_location = foo\n"
95
"push_location:policy = norecurse" % local_path,
98
# TODO RBC 20051029 test getting a push location from a branch in a
99
# recursive section - that is, it appends the branch name.
102
class SampleBranchFormat(bzrlib.branch.BranchFormat):
105
this format is initializable, unsupported to aid in testing the
106
open and open_downlevel routines.
109
def get_format_string(self):
110
"""See BzrBranchFormat.get_format_string()."""
111
return "Sample branch format."
113
def initialize(self, a_bzrdir):
114
"""Format 4 branches cannot be created."""
115
t = a_bzrdir.get_branch_transport(self)
116
t.put_bytes('format', self.get_format_string())
119
def is_supported(self):
122
def open(self, transport, _found=False):
123
return "opened branch."
126
class TestBzrBranchFormat(TestCaseWithTransport):
127
"""Tests for the BzrBranchFormat facility."""
129
def test_find_format(self):
130
# is the right format object found for a branch?
131
# create a branch with a few known format objects.
132
# this is not quite the same as
133
self.build_tree(["foo/", "bar/"])
134
def check_format(format, url):
135
dir = format._matchingbzrdir.initialize(url)
136
dir.create_repository()
137
format.initialize(dir)
138
found_format = bzrlib.branch.BranchFormat.find_format(dir)
139
self.failUnless(isinstance(found_format, format.__class__))
140
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
142
def test_find_format_not_branch(self):
143
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
self.assertRaises(NotBranchError,
145
bzrlib.branch.BranchFormat.find_format,
148
def test_find_format_unknown_format(self):
149
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
150
SampleBranchFormat().initialize(dir)
151
self.assertRaises(UnknownFormatError,
152
bzrlib.branch.BranchFormat.find_format,
155
def test_register_unregister_format(self):
156
format = SampleBranchFormat()
158
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
160
format.initialize(dir)
161
# register a format for it.
162
bzrlib.branch.BranchFormat.register_format(format)
163
# which branch.Open will refuse (not supported)
164
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
165
self.make_branch_and_tree('foo')
166
# but open_downlevel will work
167
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
168
# unregister the format
169
bzrlib.branch.BranchFormat.unregister_format(format)
170
self.make_branch_and_tree('bar')
172
def test_checkout_format(self):
173
branch = self.make_repository('repository', shared=True)
174
branch = self.make_branch('repository/branch',
176
tree = branch.create_checkout('checkout')
177
self.assertIs(tree.branch.__class__, _mod_branch.BzrBranch5)
180
class TestBranch6(TestCaseWithTransport):
182
def test_creation(self):
183
format = BzrDirMetaFormat1()
184
format.set_branch_format(_mod_branch.BzrBranchFormat6())
185
branch = self.make_branch('a', format=format)
186
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
187
branch = self.make_branch('b', format='experimental-branch6')
188
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
189
branch = _mod_branch.Branch.open('a')
190
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
192
def test_layout(self):
193
branch = self.make_branch('a', format='experimental-branch6')
194
self.failUnlessExists('a/.bzr/branch/last-revision')
195
self.failIfExists('a/.bzr/branch/revision-history')
197
def test_config(self):
198
"""Ensure that all configuration data is stored in the branch"""
199
branch = self.make_branch('a', format='experimental-branch6')
200
branch.set_parent('http://bazaar-vcs.org')
201
self.failIfExists('a/.bzr/branch/parent')
202
self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
203
branch.set_push_location('sftp://bazaar-vcs.org')
204
config = branch.get_config()._get_branch_data_config()
205
self.assertEqual('sftp://bazaar-vcs.org',
206
config.get_user_option('push_location'))
207
branch.set_bound_location('ftp://bazaar-vcs.org')
208
self.failIfExists('a/.bzr/branch/bound')
209
self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
211
def test_set_revision_history(self):
212
tree = self.make_branch_and_memory_tree('.',
213
format='experimental-branch6')
217
tree.commit('foo', rev_id='foo')
218
tree.commit('bar', rev_id='bar')
219
tree.branch.set_revision_history(['foo', 'bar'])
220
tree.branch.set_revision_history(['foo'])
221
self.assertRaises(errors.NotLefthandHistory,
222
tree.branch.set_revision_history, ['bar'])
226
def test_append_revision(self):
227
tree = self.make_branch_and_tree('branch1',
228
format='experimental-branch6')
232
tree.commit('foo', rev_id='foo')
233
tree.commit('bar', rev_id='bar')
234
tree.commit('baz', rev_id='baz')
235
tree.set_last_revision('bar')
236
tree.branch.set_last_revision_info(2, 'bar')
237
tree.commit('qux', rev_id='qux')
238
tree.add_parent_tree_id('baz')
239
tree.commit('qux', rev_id='quxx')
240
tree.branch.set_last_revision_info(0, 'null:')
241
self.assertRaises(errors.NotLeftParentDescendant,
242
tree.branch.append_revision, 'bar')
243
tree.branch.append_revision('foo')
244
self.assertRaises(errors.NotLeftParentDescendant,
245
tree.branch.append_revision, 'baz')
246
tree.branch.append_revision('bar')
247
tree.branch.append_revision('baz')
248
self.assertRaises(errors.NotLeftParentDescendant,
249
tree.branch.append_revision, 'quxx')
254
class TestBranchReference(TestCaseWithTransport):
255
"""Tests for the branch reference facility."""
257
def test_create_open_reference(self):
258
bzrdirformat = bzrdir.BzrDirMetaFormat1()
259
t = get_transport(self.get_url('.'))
261
dir = bzrdirformat.initialize(self.get_url('repo'))
262
dir.create_repository()
263
target_branch = dir.create_branch()
265
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
266
made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
267
self.assertEqual(made_branch.base, target_branch.base)
268
opened_branch = branch_dir.open_branch()
269
self.assertEqual(opened_branch.base, target_branch.base)
272
class TestHooks(TestCase):
274
def test_constructor(self):
275
"""Check that creating a BranchHooks instance has the right defaults."""
276
hooks = bzrlib.branch.BranchHooks()
277
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
278
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
279
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
280
self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
281
self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
283
def test_installed_hooks_are_BranchHooks(self):
284
"""The installed hooks object should be a BranchHooks."""
285
# the installed hooks are saved in self._preserved_hooks.
286
self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
288
def test_install_hook_raises_unknown_hook(self):
289
"""install_hook should raise UnknownHook if a hook is unknown."""
290
hooks = bzrlib.branch.BranchHooks()
291
self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
293
def test_install_hook_appends_known_hook(self):
294
"""install_hook should append the callable for known hooks."""
295
hooks = bzrlib.branch.BranchHooks()
296
hooks.install_hook('set_rh', None)
297
self.assertEqual(hooks['set_rh'], [None])