~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
                           UnsupportedFormatError,
32
32
                           )
33
33
 
34
 
from bzrlib.tests import TestCase, TestCaseInTempDir
 
34
from bzrlib.tests import TestCase, TestCaseWithTransport
35
35
from bzrlib.transport import get_transport
36
36
 
37
 
#class TestDefaultFormat(TestCase):
38
 
#
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())
44
 
#        try:
45
 
#            """default branch formats make no sense until we have
46
 
#            multiple formats per bzrdir format."""
47
 
#            # directly
48
 
#            #result = branch.Branch.create('memory:/')
49
 
#            #self.assertEqual(result, 'A bzr branch dir')
50
 
#        finally:
51
 
#            branch.BranchFormat.set_default_format(old_format)
52
 
#        self.assertEqual(old_format, branch.BranchFormat.get_default_format())
 
37
class TestDefaultFormat(TestCase):
 
38
 
 
39
    def test_get_set_default_format(self):
 
40
        old_format = branch.BranchFormat.get_default_format()
 
41
        # default is 5
 
42
        self.assertTrue(isinstance(old_format, branch.BzrBranchFormat5))
 
43
        branch.BranchFormat.set_default_format(SampleBranchFormat())
 
44
        try:
 
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')
 
50
        finally:
 
51
            branch.BranchFormat.set_default_format(old_format)
 
52
        self.assertEqual(old_format, branch.BranchFormat.get_default_format())
 
53
 
 
54
 
 
55
class SampleBranchFormat(branch.BranchFormat):
 
56
    """A sample format
 
57
 
 
58
    this format is initializable, unsupported to aid in testing the 
 
59
    open and open_downlevel routines.
 
60
    """
 
61
 
 
62
    def get_format_string(self):
 
63
        """See BzrBranchFormat.get_format_string()."""
 
64
        return "Sample branch format."
 
65
 
 
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()))
 
70
        return 'A branch'
 
71
 
 
72
    def is_supported(self):
 
73
        return False
 
74
 
 
75
    def open(self, transport, _found=False):
 
76
        return "opened branch."
 
77
 
 
78
 
 
79
class TestBzrBranchFormat(TestCaseWithTransport):
 
80
    """Tests for the BzrBranchFormat facility."""
 
81
 
 
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")
 
94
        
 
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,
 
99
                          dir)
 
100
 
 
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,
 
106
                          dir)
 
107
 
 
108
    def test_register_unregister_format(self):
 
109
        format = SampleBranchFormat()
 
110
        # make a control dir
 
111
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
112
        # make a branch
 
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)