1
# (C) 2005 Canonical Ltd
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.
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.
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
17
"""Tests for the BzrDir facility and any format specific tests.
19
For interface contract tests, see tests/bzr_dir_implementations.
22
from StringIO import StringIO
24
import bzrlib.bzrdir as bzrdir
25
from bzrlib.errors import (NotBranchError,
27
UnsupportedFormatError,
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
36
class TestDefaultFormat(TestCase):
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.
45
result = bzrdir.BzrDir.create('memory:/')
46
self.failUnless(isinstance(result, SampleBzrDir))
48
bzrdir.BzrDirFormat.set_default_format(old_format)
49
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
52
class SampleBzrDir(bzrdir.BzrDir):
53
"""A sample BzrDir implementation to allow testing static methods."""
55
def create_repository(self):
56
"""See BzrDir.create_repository."""
60
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
63
this format is initializable, unsupported to aid in testing the
64
open and open_downlevel routines.
67
def get_format_string(self):
68
"""See BzrDirFormat.get_format_string()."""
69
return "Sample .bzr dir format."
71
def initialize(self, url):
72
"""Create a bzr dir."""
73
t = get_transport(url)
75
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
76
return SampleBzrDir(t, self)
78
def is_supported(self):
81
def open(self, transport):
82
return "opened branch."
85
class TestBzrDirFormat(TestCaseWithTransport):
86
"""Tests for the BzrDirFormat facility."""
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")
102
def test_find_format_nothing_there(self):
103
self.assertRaises(NotBranchError,
104
bzrdir.BzrDirFormat.find_format,
107
def test_find_format_unknown_format(self):
108
t = get_transport(self.get_url())
110
t.put('.bzr/branch-format', StringIO())
111
self.assertRaises(UnknownFormatError,
112
bzrdir.BzrDirFormat.find_format,
115
def test_register_unregister_format(self):
116
format = SampleBzrDirFormat()
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)
132
def test_create_repository(self):
133
format = SampleBzrDirFormat()
134
old_format = bzrdir.BzrDirFormat.get_default_format()
135
bzrdir.BzrDirFormat.set_default_format(format)
137
repo = bzrdir.BzrDir.create_repository(self.get_url())
138
self.assertEqual('A repository', repo)
140
bzrdir.BzrDirFormat.set_default_format(old_format)
143
class ChrootedTests(TestCaseWithTransport):
144
"""A support class that provides readonly urls outside the local namespace.
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
152
super(ChrootedTests, self).setUp()
153
if not self.transport_server == MemoryServer:
154
self.transport_readonly_server = HttpServer
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)