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.assertEqual(result, 'A bzr dir')
48
bzrdir.BzrDirFormat.set_default_format(old_format)
49
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
52
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
55
this format is initializable, unsupported to aid in testing the
56
open and open_downlevel routines.
59
def get_format_string(self):
60
"""See BzrDirFormat.get_format_string()."""
61
return "Sample .bzr dir format."
63
def initialize(self, url):
64
"""Create a bzr dir."""
65
t = get_transport(url)
67
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
70
def is_supported(self):
73
def open(self, transport):
74
return "opened branch."
77
class TestBzrDirFormat(TestCaseWithTransport):
78
"""Tests for the BzrDirFormat facility."""
80
def test_find_format(self):
81
# is the right format object found for a branch?
82
# create a branch with a few known format objects.
83
# this is not quite the same as
84
t = get_transport(self.get_url())
85
self.build_tree(["foo/", "bar/"], transport=t)
86
def check_format(format, url):
87
format.initialize(url)
88
t = get_transport(url)
89
found_format = bzrdir.BzrDirFormat.find_format(t)
90
self.failUnless(isinstance(found_format, format.__class__))
91
check_format(bzrdir.BzrDirFormat5(), "foo")
92
check_format(bzrdir.BzrDirFormat6(), "bar")
94
def test_find_format_nothing_there(self):
95
self.assertRaises(NotBranchError,
96
bzrdir.BzrDirFormat.find_format,
99
def test_find_format_unknown_format(self):
100
t = get_transport(self.get_url())
102
t.put('.bzr/branch-format', StringIO())
103
self.assertRaises(UnknownFormatError,
104
bzrdir.BzrDirFormat.find_format,
107
def test_register_unregister_format(self):
108
format = SampleBzrDirFormat()
111
format.initialize(url)
112
# register a format for it.
113
bzrdir.BzrDirFormat.register_format(format)
114
# which bzrdir.Open will refuse (not supported)
115
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
116
# but open_downlevel will work
117
t = get_transport(url)
118
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
119
# unregister the format
120
bzrdir.BzrDirFormat.unregister_format(format)
121
# now open_downlevel should fail too.
122
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
125
class ChrootedTests(TestCaseWithTransport):
126
"""A support class that provides readonly urls outside the local namespace.
128
This is done by checking if self.transport_server is a MemoryServer. if it
129
is then we are chrooted already, if it is not then an HttpServer is used
134
super(ChrootedTests, self).setUp()
135
if not self.transport_server == MemoryServer:
136
self.transport_readonly_server = HttpServer
138
def test_open_containing(self):
139
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
140
self.get_readonly_url(''))
141
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
142
self.get_readonly_url('g/p/q'))
143
control = bzrdir.BzrDir.create(self.get_url())
144
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
145
self.assertEqual('', relpath)
146
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
147
self.assertEqual('g/p/q', relpath)