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,
35
UnsupportedFormatError,
38
from bzrlib.tests import TestCase, TestCaseWithTransport
39
from bzrlib.transport import get_transport
41
class TestDefaultFormat(TestCase):
43
def test_get_set_default_format(self):
44
old_format = bzrlib.branch.BranchFormat.get_default_format()
46
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
47
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
49
# the default branch format is used by the meta dir format
50
# which is not the default bzrdir format at this point
51
dir = BzrDirMetaFormat1().initialize('memory:/')
52
result = dir.create_branch()
53
self.assertEqual(result, 'A branch')
55
bzrlib.branch.BranchFormat.set_default_format(old_format)
56
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
59
class TestBranchFormat5(TestCaseWithTransport):
60
"""Tests specific to branch format 5"""
62
def test_branch_format_5_uses_lockdir(self):
64
bzrdir = BzrDirMetaFormat1().initialize(url)
65
bzrdir.create_repository()
66
branch = bzrdir.create_branch()
67
t = self.get_transport()
68
self.log("branch instance is %r" % branch)
69
self.assert_(isinstance(branch, BzrBranch5))
70
self.assertIsDirectory('.', t)
71
self.assertIsDirectory('.bzr/branch', t)
72
self.assertIsDirectory('.bzr/branch/lock', t)
74
self.assertIsDirectory('.bzr/branch/lock/held', t)
77
class SampleBranchFormat(bzrlib.branch.BranchFormat):
80
this format is initializable, unsupported to aid in testing the
81
open and open_downlevel routines.
84
def get_format_string(self):
85
"""See BzrBranchFormat.get_format_string()."""
86
return "Sample branch format."
88
def initialize(self, a_bzrdir):
89
"""Format 4 branches cannot be created."""
90
t = a_bzrdir.get_branch_transport(self)
91
t.put('format', StringIO(self.get_format_string()))
94
def is_supported(self):
97
def open(self, transport, _found=False):
98
return "opened branch."
101
class TestBzrBranchFormat(TestCaseWithTransport):
102
"""Tests for the BzrBranchFormat facility."""
104
def test_find_format(self):
105
# is the right format object found for a branch?
106
# create a branch with a few known format objects.
107
# this is not quite the same as
108
self.build_tree(["foo/", "bar/"])
109
def check_format(format, url):
110
dir = format._matchingbzrdir.initialize(url)
111
dir.create_repository()
112
format.initialize(dir)
113
found_format = bzrlib.branch.BranchFormat.find_format(dir)
114
self.failUnless(isinstance(found_format, format.__class__))
115
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
117
def test_find_format_not_branch(self):
118
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
119
self.assertRaises(NotBranchError,
120
bzrlib.branch.BranchFormat.find_format,
123
def test_find_format_unknown_format(self):
124
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
125
SampleBranchFormat().initialize(dir)
126
self.assertRaises(UnknownFormatError,
127
bzrlib.branch.BranchFormat.find_format,
130
def test_register_unregister_format(self):
131
format = SampleBranchFormat()
133
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
135
format.initialize(dir)
136
# register a format for it.
137
bzrlib.branch.BranchFormat.register_format(format)
138
# which branch.Open will refuse (not supported)
139
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
140
# but open_downlevel will work
141
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
142
# unregister the format
143
bzrlib.branch.BranchFormat.unregister_format(format)
146
class TestBranchReference(TestCaseWithTransport):
147
"""Tests for the branch reference facility."""
149
def test_create_open_reference(self):
150
bzrdirformat = bzrdir.BzrDirMetaFormat1()
151
t = get_transport(self.get_url('.'))
153
dir = bzrdirformat.initialize(self.get_url('repo'))
154
dir.create_repository()
155
target_branch = dir.create_branch()
157
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
158
made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
159
self.assertEqual(made_branch.base, target_branch.base)
160
opened_branch = branch_dir.open_branch()
161
self.assertEqual(opened_branch.base, target_branch.base)