31
31
UnsupportedFormatError,
34
from bzrlib.tests import TestCase, TestCaseInTempDir
34
from bzrlib.tests import TestCase, TestCaseWithTransport
35
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()
41
# # default is None - we cannot create a Branch independently yet
42
# self.assertEqual(old_format, None)
43
# branch.BranchFormat.set_default_format(SampleBranchFormat())
45
# """default branch formats make no sense until we have
46
# multiple formats per bzrdir format."""
48
# #result = branch.Branch.create('memory:/')
49
# #self.assertEqual(result, 'A bzr branch dir')
51
# branch.BranchFormat.set_default_format(old_format)
52
# self.assertEqual(old_format, branch.BranchFormat.get_default_format())
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
format.initialize(dir)
90
t = get_transport(url)
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), branch.Branch.open_downlevel(self.get_url()))
120
# unregister the format
121
branch.BranchFormat.unregister_format(format)