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_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.
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."""
59
def create_branch(self):
60
"""See BzrDir.create_branch."""
64
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
67
this format is initializable, unsupported to aid in testing the
68
open and open_downlevel routines.
71
def get_format_string(self):
72
"""See BzrDirFormat.get_format_string()."""
73
return "Sample .bzr dir format."
75
def initialize(self, url):
76
"""Create a bzr dir."""
77
t = get_transport(url)
79
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
80
return SampleBzrDir(t, self)
82
def is_supported(self):
85
def open(self, transport, _found=None):
86
return "opened branch."
89
class TestBzrDirFormat(TestCaseWithTransport):
90
"""Tests for the BzrDirFormat facility."""
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")
106
def test_find_format_nothing_there(self):
107
self.assertRaises(NotBranchError,
108
bzrdir.BzrDirFormat.find_format,
111
def test_find_format_unknown_format(self):
112
t = get_transport(self.get_url())
114
t.put('.bzr/branch-format', StringIO())
115
self.assertRaises(UnknownFormatError,
116
bzrdir.BzrDirFormat.find_format,
119
def test_register_unregister_format(self):
120
format = SampleBzrDirFormat()
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)
136
def test_create_repository(self):
137
format = SampleBzrDirFormat()
138
old_format = bzrdir.BzrDirFormat.get_default_format()
139
bzrdir.BzrDirFormat.set_default_format(format)
141
repo = bzrdir.BzrDir.create_repository(self.get_url())
142
self.assertEqual('A repository', repo)
144
bzrdir.BzrDirFormat.set_default_format(old_format)
146
def test_create_branch(self):
147
format = SampleBzrDirFormat()
148
old_format = bzrdir.BzrDirFormat.get_default_format()
149
bzrdir.BzrDirFormat.set_default_format(format)
151
repo = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
152
self.assertEqual('A branch', repo)
154
bzrdir.BzrDirFormat.set_default_format(old_format)
157
class ChrootedTests(TestCaseWithTransport):
158
"""A support class that provides readonly urls outside the local namespace.
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
166
super(ChrootedTests, self).setUp()
167
if not self.transport_server == MemoryServer:
168
self.transport_readonly_server = HttpServer
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)