~bzr-pqm/bzr/bzr.dev

1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
1
# (C) 2005 Canonical Ltd
2
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.
7
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.
12
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
16
17
"""Tests for the Branch facility that are not interface  tests.
18
1534.4.39 by Robert Collins
Basic BzrDir support.
19
For interface tests see tests/branch_implementations/*.py.
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
20
21
For concrete class tests see this file, and for meta-branch tests
22
also see this file.
23
"""
24
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
25
from StringIO import StringIO
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
26
27
import bzrlib.branch as branch
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
28
import bzrlib.bzrdir as bzrdir
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
29
from bzrlib.errors import (NotBranchError,
30
                           UnknownFormatError,
31
                           UnsupportedFormatError,
32
                           )
33
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
34
from bzrlib.tests import TestCase, TestCaseWithTransport
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
35
from bzrlib.transport import get_transport
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
36
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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)