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
25
import bzrlib.bzrdir as bzrdir
26
import bzrlib.errors as errors
27
from bzrlib.errors import (NotBranchError,
29
UnsupportedFormatError,
31
import bzrlib.repository as repository
32
from bzrlib.tests import TestCase, TestCaseWithTransport
33
from bzrlib.transport import get_transport
34
from bzrlib.transport.http import HttpServer
35
from bzrlib.transport.memory import MemoryServer
36
import bzrlib.workingtree as workingtree
39
class TestDefaultFormat(TestCase):
41
def test_get_set_default_format(self):
42
old_format = bzrdir.BzrDirFormat.get_default_format()
43
# default is BzrDirFormat6
44
self.failUnless(isinstance(old_format, bzrdir.BzrDirFormat6))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
46
# creating a bzr dir should now create an instrumented dir.
48
result = bzrdir.BzrDir.create('memory:/')
49
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
52
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
55
class SampleBranch(bzrlib.branch.Branch):
56
"""A dummy branch for guess what, dummy use."""
58
def __init__(self, dir):
62
class SampleBzrDir(bzrdir.BzrDir):
63
"""A sample BzrDir implementation to allow testing static methods."""
65
def create_repository(self):
66
"""See BzrDir.create_repository."""
69
def open_repository(self):
70
"""See BzrDir.open_repository."""
73
def create_branch(self):
74
"""See BzrDir.create_branch."""
75
return SampleBranch(self)
77
def create_workingtree(self):
78
"""See BzrDir.create_workingtree."""
82
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
85
this format is initializable, unsupported to aid in testing the
86
open and open_downlevel routines.
89
def get_format_string(self):
90
"""See BzrDirFormat.get_format_string()."""
91
return "Sample .bzr dir format."
93
def initialize(self, url):
94
"""Create a bzr dir."""
95
t = get_transport(url)
97
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
98
return SampleBzrDir(t, self)
100
def is_supported(self):
103
def open(self, transport, _found=None):
104
return "opened branch."
107
class TestBzrDirFormat(TestCaseWithTransport):
108
"""Tests for the BzrDirFormat facility."""
110
def test_find_format(self):
111
# is the right format object found for a branch?
112
# create a branch with a few known format objects.
113
# this is not quite the same as
114
t = get_transport(self.get_url())
115
self.build_tree(["foo/", "bar/"], transport=t)
116
def check_format(format, url):
117
format.initialize(url)
118
t = get_transport(url)
119
found_format = bzrdir.BzrDirFormat.find_format(t)
120
self.failUnless(isinstance(found_format, format.__class__))
121
check_format(bzrdir.BzrDirFormat5(), "foo")
122
check_format(bzrdir.BzrDirFormat6(), "bar")
124
def test_find_format_nothing_there(self):
125
self.assertRaises(NotBranchError,
126
bzrdir.BzrDirFormat.find_format,
129
def test_find_format_unknown_format(self):
130
t = get_transport(self.get_url())
132
t.put('.bzr/branch-format', StringIO())
133
self.assertRaises(UnknownFormatError,
134
bzrdir.BzrDirFormat.find_format,
137
def test_register_unregister_format(self):
138
format = SampleBzrDirFormat()
141
format.initialize(url)
142
# register a format for it.
143
bzrdir.BzrDirFormat.register_format(format)
144
# which bzrdir.Open will refuse (not supported)
145
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
146
# but open_downlevel will work
147
t = get_transport(url)
148
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
149
# unregister the format
150
bzrdir.BzrDirFormat.unregister_format(format)
151
# now open_downlevel should fail too.
152
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
154
def test_create_repository(self):
155
format = SampleBzrDirFormat()
156
old_format = bzrdir.BzrDirFormat.get_default_format()
157
bzrdir.BzrDirFormat.set_default_format(format)
159
repo = bzrdir.BzrDir.create_repository(self.get_url())
160
self.assertEqual('A repository', repo)
162
bzrdir.BzrDirFormat.set_default_format(old_format)
164
def test_create_branch_and_repo_uses_default(self):
165
format = SampleBzrDirFormat()
166
old_format = bzrdir.BzrDirFormat.get_default_format()
167
bzrdir.BzrDirFormat.set_default_format(format)
169
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
170
self.assertTrue(isinstance(branch, SampleBranch))
172
bzrdir.BzrDirFormat.set_default_format(old_format)
174
def test_create_branch_and_repo_under_shared(self):
175
# creating a branch and repo in a shared repo uses the
177
old_format = bzrdir.BzrDirFormat.get_default_format()
178
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
180
self.make_repository('.', shared=True)
181
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
182
self.assertRaises(errors.NoRepositoryPresent,
183
branch.bzrdir.open_repository)
185
bzrdir.BzrDirFormat.set_default_format(old_format)
187
def test_create_branch_and_repo_under_shared_force_new(self):
188
# creating a branch and repo in a shared repo can be forced to
190
old_format = bzrdir.BzrDirFormat.get_default_format()
191
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
193
self.make_repository('.', shared=True)
194
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
196
branch.bzrdir.open_repository()
198
bzrdir.BzrDirFormat.set_default_format(old_format)
200
def test_create_standalone_working_tree(self):
201
format = SampleBzrDirFormat()
202
old_format = bzrdir.BzrDirFormat.get_default_format()
203
bzrdir.BzrDirFormat.set_default_format(format)
205
# note this is deliberately readonly, as this failure should
206
# occur before any writes.
207
self.assertRaises(errors.NotLocalUrl,
208
bzrdir.BzrDir.create_standalone_workingtree,
209
self.get_readonly_url())
210
tree = bzrdir.BzrDir.create_standalone_workingtree('.')
211
self.assertEqual('A tree', tree)
213
bzrdir.BzrDirFormat.set_default_format(old_format)
216
class ChrootedTests(TestCaseWithTransport):
217
"""A support class that provides readonly urls outside the local namespace.
219
This is done by checking if self.transport_server is a MemoryServer. if it
220
is then we are chrooted already, if it is not then an HttpServer is used
225
super(ChrootedTests, self).setUp()
226
if not self.transport_server == MemoryServer:
227
self.transport_readonly_server = HttpServer
229
def test_open_containing(self):
230
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
231
self.get_readonly_url(''))
232
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
233
self.get_readonly_url('g/p/q'))
234
control = bzrdir.BzrDir.create(self.get_url())
235
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
236
self.assertEqual('', relpath)
237
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
238
self.assertEqual('g/p/q', relpath)
240
def test_open_containing_transport(self):
241
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_transport,
242
get_transport(self.get_readonly_url('')))
243
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_transport,
244
get_transport(self.get_readonly_url('g/p/q')))
245
control = bzrdir.BzrDir.create(self.get_url())
246
branch, relpath = bzrdir.BzrDir.open_containing_transport(
247
get_transport(self.get_readonly_url('')))
248
self.assertEqual('', relpath)
249
branch, relpath = bzrdir.BzrDir.open_containing_transport(
250
get_transport(self.get_readonly_url('g/p/q')))
251
self.assertEqual('g/p/q', relpath)
254
class TestMeta1DirFormat(TestCaseWithTransport):
255
"""Tests specific to the meta1 dir format."""
257
def test_right_base_dirs(self):
258
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
260
branch_base = t.clone('branch').base
261
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
262
self.assertEqual(branch_base,
263
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
264
repository_base = t.clone('repository').base
265
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
266
self.assertEqual(repository_base,
267
dir.get_repository_transport(repository.RepositoryFormat7()).base)
268
checkout_base = t.clone('checkout').base
269
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
270
self.assertEqual(checkout_base,
271
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)