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 create_branch(self):
70
"""See BzrDir.create_branch."""
71
return SampleBranch(self)
73
def create_workingtree(self):
74
"""See BzrDir.create_workingtree."""
78
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
81
this format is initializable, unsupported to aid in testing the
82
open and open_downlevel routines.
85
def get_format_string(self):
86
"""See BzrDirFormat.get_format_string()."""
87
return "Sample .bzr dir format."
89
def initialize(self, url):
90
"""Create a bzr dir."""
91
t = get_transport(url)
93
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
94
return SampleBzrDir(t, self)
96
def is_supported(self):
99
def open(self, transport, _found=None):
100
return "opened branch."
103
class TestBzrDirFormat(TestCaseWithTransport):
104
"""Tests for the BzrDirFormat facility."""
106
def test_find_format(self):
107
# is the right format object found for a branch?
108
# create a branch with a few known format objects.
109
# this is not quite the same as
110
t = get_transport(self.get_url())
111
self.build_tree(["foo/", "bar/"], transport=t)
112
def check_format(format, url):
113
format.initialize(url)
114
t = get_transport(url)
115
found_format = bzrdir.BzrDirFormat.find_format(t)
116
self.failUnless(isinstance(found_format, format.__class__))
117
check_format(bzrdir.BzrDirFormat5(), "foo")
118
check_format(bzrdir.BzrDirFormat6(), "bar")
120
def test_find_format_nothing_there(self):
121
self.assertRaises(NotBranchError,
122
bzrdir.BzrDirFormat.find_format,
125
def test_find_format_unknown_format(self):
126
t = get_transport(self.get_url())
128
t.put('.bzr/branch-format', StringIO())
129
self.assertRaises(UnknownFormatError,
130
bzrdir.BzrDirFormat.find_format,
133
def test_register_unregister_format(self):
134
format = SampleBzrDirFormat()
137
format.initialize(url)
138
# register a format for it.
139
bzrdir.BzrDirFormat.register_format(format)
140
# which bzrdir.Open will refuse (not supported)
141
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
142
# but open_downlevel will work
143
t = get_transport(url)
144
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
145
# unregister the format
146
bzrdir.BzrDirFormat.unregister_format(format)
147
# now open_downlevel should fail too.
148
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
150
def test_create_repository(self):
151
format = SampleBzrDirFormat()
152
old_format = bzrdir.BzrDirFormat.get_default_format()
153
bzrdir.BzrDirFormat.set_default_format(format)
155
repo = bzrdir.BzrDir.create_repository(self.get_url())
156
self.assertEqual('A repository', repo)
158
bzrdir.BzrDirFormat.set_default_format(old_format)
160
def test_create_branch_and_repo(self):
161
format = SampleBzrDirFormat()
162
old_format = bzrdir.BzrDirFormat.get_default_format()
163
bzrdir.BzrDirFormat.set_default_format(format)
165
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
166
self.assertTrue(isinstance(branch, SampleBranch))
168
bzrdir.BzrDirFormat.set_default_format(old_format)
170
def test_create_standalone_working_tree(self):
171
format = SampleBzrDirFormat()
172
old_format = bzrdir.BzrDirFormat.get_default_format()
173
bzrdir.BzrDirFormat.set_default_format(format)
175
# note this is deliberately readonly, as this failure should
176
# occur before any writes.
177
self.assertRaises(errors.NotLocalUrl,
178
bzrdir.BzrDir.create_standalone_workingtree,
179
self.get_readonly_url())
180
tree = bzrdir.BzrDir.create_standalone_workingtree('.')
181
self.assertEqual('A tree', tree)
183
bzrdir.BzrDirFormat.set_default_format(old_format)
186
class ChrootedTests(TestCaseWithTransport):
187
"""A support class that provides readonly urls outside the local namespace.
189
This is done by checking if self.transport_server is a MemoryServer. if it
190
is then we are chrooted already, if it is not then an HttpServer is used
195
super(ChrootedTests, self).setUp()
196
if not self.transport_server == MemoryServer:
197
self.transport_readonly_server = HttpServer
199
def test_open_containing(self):
200
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
201
self.get_readonly_url(''))
202
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
203
self.get_readonly_url('g/p/q'))
204
control = bzrdir.BzrDir.create(self.get_url())
205
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
206
self.assertEqual('', relpath)
207
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
208
self.assertEqual('g/p/q', relpath)
211
class TestMeta1DirFormat(TestCaseWithTransport):
212
"""Tests specific to the meta1 dir format."""
214
def test_right_base_dirs(self):
215
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
217
branch_base = t.clone('branch').base
218
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
219
self.assertEqual(branch_base,
220
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
221
repository_base = t.clone('repository').base
222
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
223
self.assertEqual(repository_base,
224
dir.get_repository_transport(repository.RepositoryFormat7()).base)
225
checkout_base = t.clone('checkout').base
226
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
227
self.assertEqual(checkout_base,
228
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)