25
25
from StringIO import StringIO
27
import bzrlib.branch as branch
28
import bzrlib.bzrdir as bzrdir
28
29
from bzrlib.errors import (NotBranchError,
29
30
UnknownFormatError,
30
31
UnsupportedFormatError,
33
from bzrlib.tests import TestCase, TestCaseInTempDir
34
from bzrlib.tests import TestCase, TestCaseWithTransport
34
35
from bzrlib.transport import get_transport
36
37
class TestDefaultFormat(TestCase):
38
def test_get_set_default_initializer(self):
39
old_initializer = branch.Branch.get_default_initializer()
40
# default is BzrBranch._initialize
41
self.assertEqual(branch.BzrBranch._initialize, old_initializer)
43
return "a branch %s" % url
44
branch.Branch.set_default_initializer(recorder)
39
def test_get_set_default_format(self):
40
old_format = bzrlib.branch.BranchFormat.get_default_format()
42
self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
43
bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
46
b = branch.Branch.create("memory:/")
47
self.assertEqual("a branch memory:/", b)
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')
49
branch.Branch.set_default_initializer(old_initializer)
50
self.assertEqual(old_initializer, branch.Branch.get_default_initializer())
53
class SampleBranchFormat(branch.BzrBranchFormat):
51
bzrlib.branch.BranchFormat.set_default_format(old_format)
52
self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
55
class SampleBranchFormat(bzrlib.branch.BranchFormat):
56
58
this format is initializable, unsupported to aid in testing the
61
63
"""See BzrBranchFormat.get_format_string()."""
62
64
return "Sample branch format."
64
def initialize(self, url):
66
def initialize(self, a_bzrdir):
65
67
"""Format 4 branches cannot be created."""
66
t = get_transport(url)
68
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
68
t = a_bzrdir.get_branch_transport(self)
69
t.put('format', StringIO(self.get_format_string()))
71
72
def is_supported(self):
74
def open(self, transport):
75
def open(self, transport, _found=False):
75
76
return "opened branch."
78
class TestBzrBranchFormat(TestCaseInTempDir):
79
class TestBzrBranchFormat(TestCaseWithTransport):
79
80
"""Tests for the BzrBranchFormat facility."""
81
82
def test_find_format(self):
84
85
# this is not quite the same as
85
86
self.build_tree(["foo/", "bar/"])
86
87
def check_format(format, url):
87
format.initialize(url)
88
t = get_transport(url)
89
found_format = branch.BzrBranchFormat.find_format(t)
88
dir = format._matchingbzrdir.initialize(url)
89
dir.create_repository()
90
format.initialize(dir)
91
found_format = bzrlib.branch.BranchFormat.find_format(dir)
90
92
self.failUnless(isinstance(found_format, format.__class__))
91
check_format(branch.BzrBranchFormat5(), "foo")
92
check_format(branch.BzrBranchFormat6(), "bar")
93
check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
94
95
def test_find_format_not_branch(self):
96
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
95
97
self.assertRaises(NotBranchError,
96
branch.BzrBranchFormat.find_format,
98
bzrlib.branch.BranchFormat.find_format,
99
101
def test_find_format_unknown_format(self):
100
t = get_transport('.')
102
t.put('.bzr/branch-format', StringIO())
102
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
103
SampleBranchFormat().initialize(dir)
103
104
self.assertRaises(UnknownFormatError,
104
branch.BzrBranchFormat.find_format,
105
bzrlib.branch.BranchFormat.find_format,
107
108
def test_register_unregister_format(self):
108
109
format = SampleBranchFormat()
111
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
110
format.initialize('.')
113
format.initialize(dir)
111
114
# register a format for it.
112
branch.BzrBranchFormat.register_format(format)
115
bzrlib.branch.BranchFormat.register_format(format)
113
116
# which branch.Open will refuse (not supported)
114
self.assertRaises(UnsupportedFormatError, branch.Branch.open, '.')
117
self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
115
118
# but open_downlevel will work
116
t = get_transport('.')
117
self.assertEqual(format.open(t), branch.Branch.open_downlevel('.'))
119
self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
118
120
# unregister the format
119
branch.BzrBranchFormat.unregister_format(format)
120
# now open_downlevel should fail too.
121
self.assertRaises(UnknownFormatError, branch.Branch.open_downlevel, '.')
121
bzrlib.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 = bzrlib.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)