/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
19
For interface tests see test_branch_implementations.py.
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.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.
28
from bzrlib.errors import (NotBranchError,
29
                           UnknownFormatError,
30
                           UnsupportedFormatError,
31
                           )
32
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
33
from bzrlib.tests import TestCase, TestCaseInTempDir
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
34
from bzrlib.transport import get_transport
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
35
36
class TestDefaultFormat(TestCase):
37
38
    def test_get_set_default_initializer(self):
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
39
        old_initializer = branch.Branch.get_default_initializer()
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
40
        # default is BzrBranch._initialize
41
        self.assertEqual(branch.BzrBranch._initialize, old_initializer)
42
        def recorder(url):
43
            return "a branch %s" % url
44
        branch.Branch.set_default_initializer(recorder)
45
        try:
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
46
            b = branch.Branch.create("memory:/")
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
47
            self.assertEqual("a branch memory:/", b)
48
        finally:
49
            branch.Branch.set_default_initializer(old_initializer)
50
        self.assertEqual(old_initializer, branch.Branch.get_default_initializer())
51
52
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.
53
class SampleBranchFormat(branch.BzrBranchFormat):
54
    """A sample format
55
56
    this format is initializable, unsupported to aid in testing the 
57
    open and open_downlevel routines.
58
    """
59
60
    def get_format_string(self):
61
        """See BzrBranchFormat.get_format_string()."""
62
        return "Sample branch format."
63
64
    def initialize(self, url):
65
        """Format 4 branches cannot be created."""
66
        t = get_transport(url)
67
        t.mkdir('.bzr')
68
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
69
        return 'A branch'
70
71
    def is_supported(self):
72
        return False
73
74
    def open(self, transport):
75
        return "opened branch."
76
77
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
78
class TestBzrBranchFormat(TestCaseInTempDir):
79
    """Tests for the BzrBranchFormat facility."""
80
81
    def test_find_format(self):
82
        # is the right format object found for a branch?
83
        # create a branch with a few known format objects.
84
        # this is not quite the same as 
85
        self.build_tree(["foo/", "bar/"])
86
        def check_format(format, url):
87
            format.initialize(url)
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
88
            t = get_transport(url)
89
            found_format = branch.BzrBranchFormat.find_format(t)
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
90
            self.failUnless(isinstance(found_format, format.__class__))
91
        check_format(branch.BzrBranchFormat5(), "foo")
92
        check_format(branch.BzrBranchFormat6(), "bar")
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
93
        
94
    def test_find_format_not_branch(self):
95
        self.assertRaises(NotBranchError,
96
                          branch.BzrBranchFormat.find_format,
97
                          get_transport('.'))
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.
98
99
    def test_find_format_unknown_format(self):
100
        t = get_transport('.')
101
        t.mkdir('.bzr')
102
        t.put('.bzr/branch-format', StringIO())
103
        self.assertRaises(UnknownFormatError,
104
                          branch.BzrBranchFormat.find_format,
105
                          get_transport('.'))
106
107
    def test_register_unregister_format(self):
108
        format = SampleBranchFormat()
109
        # make a branch
110
        format.initialize('.')
111
        # register a format for it.
112
        branch.BzrBranchFormat.register_format(format)
113
        # which branch.Open will refuse (not supported)
114
        self.assertRaises(UnsupportedFormatError, branch.Branch.open, '.')
115
        # but open_downlevel will work
116
        t = get_transport('.')
117
        self.assertEqual(format.open(t), branch.Branch.open_downlevel('.'))
118
        # unregister the format
119
        branch.BzrBranchFormat.unregister_format(format)
120
        # now open_downlevel should fail too.
121
        self.assertRaises(UnknownFormatError, branch.Branch.open_downlevel, '.')