/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

Branch now uses BzrDir reasonably sanely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 BzrDir facility and any format specific tests.
 
18
 
 
19
For interface contract tests, see tests/bzr_dir_implementations.
 
20
"""
 
21
 
 
22
from StringIO import StringIO
 
23
 
 
24
import bzrlib.bzrdir as bzrdir
 
25
from bzrlib.errors import (NotBranchError,
 
26
                           UnknownFormatError,
 
27
                           UnsupportedFormatError,
 
28
                           )
 
29
 
 
30
from bzrlib.tests import TestCase, TestCaseWithTransport
 
31
from bzrlib.transport import get_transport
 
32
from bzrlib.transport.http import HttpServer
 
33
from bzrlib.transport.memory import MemoryServer
 
34
 
 
35
 
 
36
class TestDefaultFormat(TestCase):
 
37
 
 
38
    def test_get_set_default_format(self):
 
39
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
40
        # default is BzrDirFormat6
 
41
        self.failUnless(isinstance(old_format, bzrdir.BzrDirFormat6))
 
42
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
 
43
        # creating a bzr dir should now create an instrumented dir.
 
44
        try:
 
45
            result = bzrdir.BzrDir.create('memory:/')
 
46
            self.failUnless(isinstance(result, SampleBzrDir))
 
47
        finally:
 
48
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
49
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
 
50
 
 
51
 
 
52
class SampleBzrDir(bzrdir.BzrDir):
 
53
    """A sample BzrDir implementation to allow testing static methods."""
 
54
 
 
55
    def create_repository(self):
 
56
        """See BzrDir.create_repository."""
 
57
        return "A repository"
 
58
 
 
59
    def create_branch(self):
 
60
        """See BzrDir.create_branch."""
 
61
        return "A branch"
 
62
 
 
63
 
 
64
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
 
65
    """A sample format
 
66
 
 
67
    this format is initializable, unsupported to aid in testing the 
 
68
    open and open_downlevel routines.
 
69
    """
 
70
 
 
71
    def get_format_string(self):
 
72
        """See BzrDirFormat.get_format_string()."""
 
73
        return "Sample .bzr dir format."
 
74
 
 
75
    def initialize(self, url):
 
76
        """Create a bzr dir."""
 
77
        t = get_transport(url)
 
78
        t.mkdir('.bzr')
 
79
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
80
        return SampleBzrDir(t, self)
 
81
 
 
82
    def is_supported(self):
 
83
        return False
 
84
 
 
85
    def open(self, transport, _found=None):
 
86
        return "opened branch."
 
87
 
 
88
 
 
89
class TestBzrDirFormat(TestCaseWithTransport):
 
90
    """Tests for the BzrDirFormat facility."""
 
91
 
 
92
    def test_find_format(self):
 
93
        # is the right format object found for a branch?
 
94
        # create a branch with a few known format objects.
 
95
        # this is not quite the same as 
 
96
        t = get_transport(self.get_url())
 
97
        self.build_tree(["foo/", "bar/"], transport=t)
 
98
        def check_format(format, url):
 
99
            format.initialize(url)
 
100
            t = get_transport(url)
 
101
            found_format = bzrdir.BzrDirFormat.find_format(t)
 
102
            self.failUnless(isinstance(found_format, format.__class__))
 
103
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
104
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
105
        
 
106
    def test_find_format_nothing_there(self):
 
107
        self.assertRaises(NotBranchError,
 
108
                          bzrdir.BzrDirFormat.find_format,
 
109
                          get_transport('.'))
 
110
 
 
111
    def test_find_format_unknown_format(self):
 
112
        t = get_transport(self.get_url())
 
113
        t.mkdir('.bzr')
 
114
        t.put('.bzr/branch-format', StringIO())
 
115
        self.assertRaises(UnknownFormatError,
 
116
                          bzrdir.BzrDirFormat.find_format,
 
117
                          get_transport('.'))
 
118
 
 
119
    def test_register_unregister_format(self):
 
120
        format = SampleBzrDirFormat()
 
121
        url = self.get_url()
 
122
        # make a bzrdir
 
123
        format.initialize(url)
 
124
        # register a format for it.
 
125
        bzrdir.BzrDirFormat.register_format(format)
 
126
        # which bzrdir.Open will refuse (not supported)
 
127
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
 
128
        # but open_downlevel will work
 
129
        t = get_transport(url)
 
130
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
 
131
        # unregister the format
 
132
        bzrdir.BzrDirFormat.unregister_format(format)
 
133
        # now open_downlevel should fail too.
 
134
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
135
 
 
136
    def test_create_repository(self):
 
137
        format = SampleBzrDirFormat()
 
138
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
139
        bzrdir.BzrDirFormat.set_default_format(format)
 
140
        try:
 
141
            repo = bzrdir.BzrDir.create_repository(self.get_url())
 
142
            self.assertEqual('A repository', repo)
 
143
        finally:
 
144
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
145
 
 
146
    def test_create_branch(self):
 
147
        format = SampleBzrDirFormat()
 
148
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
149
        bzrdir.BzrDirFormat.set_default_format(format)
 
150
        try:
 
151
            repo = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
 
152
            self.assertEqual('A branch', repo)
 
153
        finally:
 
154
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
155
 
 
156
 
 
157
class ChrootedTests(TestCaseWithTransport):
 
158
    """A support class that provides readonly urls outside the local namespace.
 
159
 
 
160
    This is done by checking if self.transport_server is a MemoryServer. if it
 
161
    is then we are chrooted already, if it is not then an HttpServer is used
 
162
    for readonly urls.
 
163
    """
 
164
 
 
165
    def setUp(self):
 
166
        super(ChrootedTests, self).setUp()
 
167
        if not self.transport_server == MemoryServer:
 
168
            self.transport_readonly_server = HttpServer
 
169
 
 
170
    def test_open_containing(self):
 
171
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
172
                          self.get_readonly_url(''))
 
173
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
174
                          self.get_readonly_url('g/p/q'))
 
175
        control = bzrdir.BzrDir.create(self.get_url())
 
176
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
177
        self.assertEqual('', relpath)
 
178
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
179
        self.assertEqual('g/p/q', relpath)