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
from bzrlib.branch import (BzrBranch5,
30
import bzrlib.bzrdir as bzrdir
31
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1,
33
from bzrlib.errors import (NotBranchError,
36
UnsupportedFormatError,
39
from bzrlib.tests import TestCase, TestCaseWithTransport
40
from bzrlib.transport import get_transport
42
class TestDefaultFormat(TestCase):
44
def test_get_set_default_format(self):
45
old_format = bzrlib.branch.BranchFormat.get_default_format()
47
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
48
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
50
# the default branch format is used by the meta dir format
51
# which is not the default bzrdir format at this point
52
dir = BzrDirMetaFormat1().initialize('memory:///')
53
result = dir.create_branch()
54
self.assertEqual(result, 'A branch')
56
bzrlib.branch.BranchFormat.set_default_format(old_format)
57
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
60
class TestBranchFormat5(TestCaseWithTransport):
61
"""Tests specific to branch format 5"""
63
def test_branch_format_5_uses_lockdir(self):
65
bzrdir = BzrDirMetaFormat1().initialize(url)
66
bzrdir.create_repository()
67
branch = bzrdir.create_branch()
68
t = self.get_transport()
69
self.log("branch instance is %r" % branch)
70
self.assert_(isinstance(branch, BzrBranch5))
71
self.assertIsDirectory('.', t)
72
self.assertIsDirectory('.bzr/branch', t)
73
self.assertIsDirectory('.bzr/branch/lock', t)
76
self.assertIsDirectory('.bzr/branch/lock/held', t)
81
class SampleBranchFormat(bzrlib.branch.BranchFormat):
84
this format is initializable, unsupported to aid in testing the
85
open and open_downlevel routines.
88
def get_format_string(self):
89
"""See BzrBranchFormat.get_format_string()."""
90
return "Sample branch format."
92
def initialize(self, a_bzrdir):
93
"""Format 4 branches cannot be created."""
94
t = a_bzrdir.get_branch_transport(self)
95
t.put_bytes('format', self.get_format_string())
98
def is_supported(self):
101
def open(self, transport, _found=False):
102
return "opened branch."
105
class TestBzrBranchFormat(TestCaseWithTransport):
106
"""Tests for the BzrBranchFormat facility."""
108
def test_find_format(self):
109
# is the right format object found for a branch?
110
# create a branch with a few known format objects.
111
# this is not quite the same as
112
self.build_tree(["foo/", "bar/"])
113
def check_format(format, url):
114
dir = format._matchingbzrdir.initialize(url)
115
dir.create_repository()
116
format.initialize(dir)
117
found_format = bzrlib.branch.BranchFormat.find_format(dir)
118
self.failUnless(isinstance(found_format, format.__class__))
119
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
121
def test_find_format_not_branch(self):
122
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
123
self.assertRaises(NotBranchError,
124
bzrlib.branch.BranchFormat.find_format,
127
def test_find_format_unknown_format(self):
128
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
129
SampleBranchFormat().initialize(dir)
130
self.assertRaises(UnknownFormatError,
131
bzrlib.branch.BranchFormat.find_format,
134
def test_register_unregister_format(self):
135
format = SampleBranchFormat()
137
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
139
format.initialize(dir)
140
# register a format for it.
141
bzrlib.branch.BranchFormat.register_format(format)
142
# which branch.Open will refuse (not supported)
143
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
144
# but open_downlevel will work
145
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
146
# unregister the format
147
bzrlib.branch.BranchFormat.unregister_format(format)
150
class TestBranchReference(TestCaseWithTransport):
151
"""Tests for the branch reference facility."""
153
def test_create_open_reference(self):
154
bzrdirformat = bzrdir.BzrDirMetaFormat1()
155
t = get_transport(self.get_url('.'))
157
dir = bzrdirformat.initialize(self.get_url('repo'))
158
dir.create_repository()
159
target_branch = dir.create_branch()
161
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
162
made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
163
self.assertEqual(made_branch.base, target_branch.base)
164
opened_branch = branch_dir.open_branch()
165
self.assertEqual(opened_branch.base, target_branch.base)
168
class TestHooks(TestCase):
170
def test_constructor(self):
171
"""Check that creating a BranchHooks instance has the right defaults."""
172
hooks = bzrlib.branch.BranchHooks()
173
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
175
def test_installed_hooks_are_BranchHooks(self):
176
"""The installed hooks object should be a BranchHooks."""
177
# the installed hooks are saved in self._preserved_hooks.
178
self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
180
def test_install_hook_raises_unknown_hook(self):
181
"""install_hook should raise UnknownHook if a hook is unknown."""
182
hooks = bzrlib.branch.BranchHooks()
183
self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
185
def test_install_hook_appends_known_hook(self):
186
"""install_hook should append the callable for known hooks."""
187
hooks = bzrlib.branch.BranchHooks()
188
hooks.install_hook('set_rh', None)
189
self.assertEqual(hooks['set_rh'], [None])