1
# (C) 2005 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
27
import bzrlib.branch as branch
28
import bzrlib.bzrdir as bzrdir
29
from bzrlib.errors import (NotBranchError,
31
UnsupportedFormatError,
34
from bzrlib.tests import TestCase, TestCaseWithTransport
35
from bzrlib.transport import get_transport
37
class TestDefaultFormat(TestCase):
39
def test_get_set_default_format(self):
40
old_format = branch.BranchFormat.get_default_format()
42
self.assertTrue(isinstance(old_format, branch.BzrBranchFormat5))
43
branch.BranchFormat.set_default_format(SampleBranchFormat())
45
# the default branch format is used by the meta dir format
46
# which is not the default bzrdir format at this point
47
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
48
result = dir.create_branch()
49
self.assertEqual(result, 'A branch')
51
branch.BranchFormat.set_default_format(old_format)
52
self.assertEqual(old_format, branch.BranchFormat.get_default_format())
55
class SampleBranchFormat(branch.BranchFormat):
58
this format is initializable, unsupported to aid in testing the
59
open and open_downlevel routines.
62
def get_format_string(self):
63
"""See BzrBranchFormat.get_format_string()."""
64
return "Sample branch format."
66
def initialize(self, a_bzrdir):
67
"""Format 4 branches cannot be created."""
68
t = a_bzrdir.get_branch_transport(self)
69
t.put('format', StringIO(self.get_format_string()))
72
def is_supported(self):
75
def open(self, transport, _found=False):
76
return "opened branch."
79
class TestBzrBranchFormat(TestCaseWithTransport):
80
"""Tests for the BzrBranchFormat facility."""
82
def test_find_format(self):
83
# is the right format object found for a branch?
84
# create a branch with a few known format objects.
85
# this is not quite the same as
86
self.build_tree(["foo/", "bar/"])
87
def check_format(format, url):
88
dir = format._matchingbzrdir.initialize(url)
89
dir.create_repository()
90
format.initialize(dir)
91
found_format = branch.BranchFormat.find_format(dir)
92
self.failUnless(isinstance(found_format, format.__class__))
93
check_format(branch.BzrBranchFormat5(), "bar")
95
def test_find_format_not_branch(self):
96
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
97
self.assertRaises(NotBranchError,
98
branch.BranchFormat.find_format,
101
def test_find_format_unknown_format(self):
102
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
103
SampleBranchFormat().initialize(dir)
104
self.assertRaises(UnknownFormatError,
105
branch.BranchFormat.find_format,
108
def test_register_unregister_format(self):
109
format = SampleBranchFormat()
111
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
113
format.initialize(dir)
114
# register a format for it.
115
branch.BranchFormat.register_format(format)
116
# which branch.Open will refuse (not supported)
117
self.assertRaises(UnsupportedFormatError, branch.Branch.open, self.get_url())
118
# but open_downlevel will work
119
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
120
# unregister the format
121
branch.BranchFormat.unregister_format(format)
124
class TestBranchReference(TestCaseWithTransport):
125
"""Tests for the branch reference facility."""
127
def test_create_open_reference(self):
128
bzrdirformat = bzrdir.BzrDirMetaFormat1()
129
t = get_transport(self.get_url('.'))
131
dir = bzrdirformat.initialize(self.get_url('repo'))
132
dir.create_repository()
133
target_branch = dir.create_branch()
135
branch_dir = bzrdirformat.initialize(self.get_url('branch'))
136
made_branch = branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
137
self.assertEqual(made_branch.base, target_branch.base)
138
opened_branch = branch_dir.open_branch()
139
self.assertEqual(opened_branch.base, target_branch.base)