/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

Add RepositoryFormats and allow bzrdir.open or create _repository to be used.

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_initializer(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
 
 
60
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
 
61
    """A sample format
 
62
 
 
63
    this format is initializable, unsupported to aid in testing the 
 
64
    open and open_downlevel routines.
 
65
    """
 
66
 
 
67
    def get_format_string(self):
 
68
        """See BzrDirFormat.get_format_string()."""
 
69
        return "Sample .bzr dir format."
 
70
 
 
71
    def initialize(self, url):
 
72
        """Create a bzr dir."""
 
73
        t = get_transport(url)
 
74
        t.mkdir('.bzr')
 
75
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
76
        return SampleBzrDir(t, self)
 
77
 
 
78
    def is_supported(self):
 
79
        return False
 
80
 
 
81
    def open(self, transport):
 
82
        return "opened branch."
 
83
 
 
84
 
 
85
class TestBzrDirFormat(TestCaseWithTransport):
 
86
    """Tests for the BzrDirFormat facility."""
 
87
 
 
88
    def test_find_format(self):
 
89
        # is the right format object found for a branch?
 
90
        # create a branch with a few known format objects.
 
91
        # this is not quite the same as 
 
92
        t = get_transport(self.get_url())
 
93
        self.build_tree(["foo/", "bar/"], transport=t)
 
94
        def check_format(format, url):
 
95
            format.initialize(url)
 
96
            t = get_transport(url)
 
97
            found_format = bzrdir.BzrDirFormat.find_format(t)
 
98
            self.failUnless(isinstance(found_format, format.__class__))
 
99
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
100
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
101
        
 
102
    def test_find_format_nothing_there(self):
 
103
        self.assertRaises(NotBranchError,
 
104
                          bzrdir.BzrDirFormat.find_format,
 
105
                          get_transport('.'))
 
106
 
 
107
    def test_find_format_unknown_format(self):
 
108
        t = get_transport(self.get_url())
 
109
        t.mkdir('.bzr')
 
110
        t.put('.bzr/branch-format', StringIO())
 
111
        self.assertRaises(UnknownFormatError,
 
112
                          bzrdir.BzrDirFormat.find_format,
 
113
                          get_transport('.'))
 
114
 
 
115
    def test_register_unregister_format(self):
 
116
        format = SampleBzrDirFormat()
 
117
        url = self.get_url()
 
118
        # make a bzrdir
 
119
        format.initialize(url)
 
120
        # register a format for it.
 
121
        bzrdir.BzrDirFormat.register_format(format)
 
122
        # which bzrdir.Open will refuse (not supported)
 
123
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
 
124
        # but open_downlevel will work
 
125
        t = get_transport(url)
 
126
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
 
127
        # unregister the format
 
128
        bzrdir.BzrDirFormat.unregister_format(format)
 
129
        # now open_downlevel should fail too.
 
130
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
131
 
 
132
    def test_create_repository(self):
 
133
        format = SampleBzrDirFormat()
 
134
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
135
        bzrdir.BzrDirFormat.set_default_format(format)
 
136
        try:
 
137
            repo = bzrdir.BzrDir.create_repository(self.get_url())
 
138
            self.assertEqual('A repository', repo)
 
139
        finally:
 
140
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
141
        
 
142
 
 
143
class ChrootedTests(TestCaseWithTransport):
 
144
    """A support class that provides readonly urls outside the local namespace.
 
145
 
 
146
    This is done by checking if self.transport_server is a MemoryServer. if it
 
147
    is then we are chrooted already, if it is not then an HttpServer is used
 
148
    for readonly urls.
 
149
    """
 
150
 
 
151
    def setUp(self):
 
152
        super(ChrootedTests, self).setUp()
 
153
        if not self.transport_server == MemoryServer:
 
154
            self.transport_readonly_server = HttpServer
 
155
 
 
156
    def test_open_containing(self):
 
157
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
158
                          self.get_readonly_url(''))
 
159
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
160
                          self.get_readonly_url('g/p/q'))
 
161
        control = bzrdir.BzrDir.create(self.get_url())
 
162
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
163
        self.assertEqual('', relpath)
 
164
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
165
        self.assertEqual('g/p/q', relpath)