1
# Copyright (C) 2005 by 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 version 2 as published by
5
# the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Tests for the test framework
24
from bzrlib.tests import (
29
TestCaseWithTransport,
35
class SelftestTests(TestCase):
37
def test_import_tests(self):
38
mod = _load_module_by_name('bzrlib.tests.test_selftest')
39
self.assertEqual(mod.SelftestTests, SelftestTests)
41
def test_import_test_failure(self):
42
self.assertRaises(ImportError,
47
class MetaTestLog(TestCase):
49
def test_logging(self):
50
"""Test logs are captured when a test fails."""
51
self.log('a test message')
52
self._log_file.flush()
53
self.assertContainsRe(self._get_log(), 'a test message\n')
56
class TestTreeShape(TestCaseInTempDir):
58
def test_unicode_paths(self):
59
filename = u'hell\u00d8'
61
self.build_tree_contents([(filename, 'contents of hello')])
62
except UnicodeEncodeError:
63
raise TestSkipped("can't build unicode working tree in "
64
"filesystem encoding %s" % sys.getfilesystemencoding())
65
self.failUnlessExists(filename)
68
class TestSkippedTest(TestCase):
69
"""Try running a test which is skipped, make sure it's reported properly."""
71
def test_skipped_test(self):
72
# must be hidden in here so it's not run as a real test
74
raise TestSkipped('test intentionally skipped')
75
runner = TextTestRunner(stream=self._log_file)
76
test = unittest.FunctionTestCase(skipping_test)
77
result = runner.run(test)
78
self.assertTrue(result.wasSuccessful())
81
class TestTransportProviderAdapter(TestCase):
82
"""A group of tests that test the transport implementation adaption core.
84
This will be generalised in the future which is why it is in this
85
test file even though it is specific to transport tests at the moment.
88
def test_get_transport_permutations(self):
89
# this checks that we the module get_test_permutations call
90
# is made by the adapter get_transport_test_permitations method.
91
class MockModule(object):
92
def get_test_permutations(self):
93
return sample_permutation
94
sample_permutation = [(1,2), (3,4)]
95
from bzrlib.transport import TransportTestProviderAdapter
96
adapter = TransportTestProviderAdapter()
97
self.assertEqual(sample_permutation,
98
adapter.get_transport_test_permutations(MockModule()))
100
def test_adapter_checks_all_modules(self):
101
# this checks that the adapter returns as many permurtations as
102
# there are in all the registered# transport modules for there
103
# - we assume if this matches its probably doing the right thing
104
# especially in combination with the tests for setting the right
106
from bzrlib.transport import (TransportTestProviderAdapter,
107
_get_transport_modules
109
modules = _get_transport_modules()
110
permutation_count = 0
111
for module in modules:
112
permutation_count += len(reduce(getattr,
113
(module + ".get_test_permutations").split('.')[1:],
114
__import__(module))())
115
input_test = TestTransportProviderAdapter(
116
"test_adapter_sets_transport_class")
117
adapter = TransportTestProviderAdapter()
118
self.assertEqual(permutation_count,
119
len(list(iter(adapter.adapt(input_test)))))
121
def test_adapter_sets_transport_class(self):
122
# when the adapter adapts a test it needs to
123
# place one of the permutations from the transport
124
# providers in each test case copy. This checks
125
# that it does not just use the same one all the time.
126
# and that the id is set correctly so that debugging is
128
from bzrlib.transport.local import (LocalTransport,
133
from bzrlib.transport.sftp import (SFTPTransport,
136
SFTPSiblingAbsoluteServer,
138
from bzrlib.transport.http import (HttpTransport,
141
from bzrlib.transport.ftp import FtpTransport
142
from bzrlib.transport.memory import (MemoryTransport,
145
from bzrlib.transport import TransportTestProviderAdapter
146
# FIXME. What we want is a factory for the things
147
# needed to test the implementation. I.e. for transport we want:
148
# the class that connections should get; a local server factory
149
# so we would want the following permutations:
150
# LocalTransport relpath-factory
151
# LocalTransport abspath-factory
152
# LocalTransport file://-factory
153
# SFTPTransport homedir-factory
154
# SFTPTransport abssolute-factory
155
# HTTPTransport http-factory
156
# HTTPTransport https-factory
157
# etc, but we are currently lacking in this, so print out that
158
# this should be fixed.
159
input_test = TestTransportProviderAdapter(
160
"test_adapter_sets_transport_class")
161
suite = TransportTestProviderAdapter().adapt(input_test)
162
test_iter = iter(suite)
163
http_test = test_iter.next()
164
local_relpath_test = test_iter.next()
165
local_abspath_test = test_iter.next()
166
local_urlpath_test = test_iter.next()
167
memory_test = test_iter.next()
168
readonly_test = test_iter.next()
169
sftp_abs_test = test_iter.next()
170
sftp_homedir_test = test_iter.next()
171
sftp_sibling_abs_test = test_iter.next()
172
# ftp_test = test_iter.next()
173
self.assertRaises(StopIteration, test_iter.next)
174
self.assertEqual(LocalTransport, local_relpath_test.transport_class)
175
self.assertEqual(LocalRelpathServer, local_relpath_test.transport_server)
177
self.assertEqual(LocalTransport, local_abspath_test.transport_class)
178
self.assertEqual(LocalAbspathServer, local_abspath_test.transport_server)
180
self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
181
self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
183
self.assertEqual(SFTPTransport, sftp_abs_test.transport_class)
184
self.assertEqual(SFTPAbsoluteServer, sftp_abs_test.transport_server)
185
self.assertEqual(SFTPTransport, sftp_homedir_test.transport_class)
186
self.assertEqual(SFTPHomeDirServer, sftp_homedir_test.transport_server)
187
self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
188
self.assertEqual(SFTPSiblingAbsoluteServer,
189
sftp_sibling_abs_test.transport_server)
191
self.assertEqual(HttpTransport, http_test.transport_class)
192
self.assertEqual(HttpServer, http_test.transport_server)
193
# self.assertEqual(FtpTransport, ftp_test.transport_class)
195
self.assertEqual(MemoryTransport, memory_test.transport_class)
196
self.assertEqual(MemoryServer, memory_test.transport_server)
198
# we could test all of them for .id, but two is probably sufficient.
199
self.assertEqual("bzrlib.tests.test_selftest."
200
"TestTransportProviderAdapter."
201
"test_adapter_sets_transport_class(MemoryServer)",
203
self.assertEqual("bzrlib.tests.test_selftest."
204
"TestTransportProviderAdapter."
205
"test_adapter_sets_transport_class(LocalRelpathServer)",
206
local_relpath_test.id())
209
class TestBranchProviderAdapter(TestCase):
210
"""A group of tests that test the branch implementation test adapter."""
212
def test_adapted_tests(self):
213
# check that constructor parameters are passed through to the adapted
215
from bzrlib.branch import BranchTestProviderAdapter
216
input_test = TestBranchProviderAdapter(
217
"test_adapted_tests")
220
formats = [("c", "C"), ("d", "D")]
221
adapter = BranchTestProviderAdapter(server1, server2, formats)
222
suite = adapter.adapt(input_test)
223
tests = list(iter(suite))
224
self.assertEqual(2, len(tests))
225
self.assertEqual(tests[0].branch_format, formats[0][0])
226
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
227
self.assertEqual(tests[0].transport_server, server1)
228
self.assertEqual(tests[0].transport_readonly_server, server2)
229
self.assertEqual(tests[1].branch_format, formats[1][0])
230
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
231
self.assertEqual(tests[1].transport_server, server1)
232
self.assertEqual(tests[1].transport_readonly_server, server2)
235
class TestBzrDirProviderAdapter(TestCase):
236
"""A group of tests that test the bzr dir implementation test adapter."""
238
def test_adapted_tests(self):
239
# check that constructor parameters are passed through to the adapted
241
from bzrlib.bzrdir import BzrDirTestProviderAdapter
242
input_test = TestBzrDirProviderAdapter(
243
"test_adapted_tests")
247
adapter = BzrDirTestProviderAdapter(server1, server2, formats)
248
suite = adapter.adapt(input_test)
249
tests = list(iter(suite))
250
self.assertEqual(2, len(tests))
251
self.assertEqual(tests[0].bzrdir_format, formats[0])
252
self.assertEqual(tests[0].transport_server, server1)
253
self.assertEqual(tests[0].transport_readonly_server, server2)
254
self.assertEqual(tests[1].bzrdir_format, formats[1])
255
self.assertEqual(tests[1].transport_server, server1)
256
self.assertEqual(tests[1].transport_readonly_server, server2)
259
class TestRepositoryProviderAdapter(TestCase):
260
"""A group of tests that test the repository implementation test adapter."""
262
def test_adapted_tests(self):
263
# check that constructor parameters are passed through to the adapted
265
from bzrlib.repository import RepositoryTestProviderAdapter
266
input_test = TestRepositoryProviderAdapter(
267
"test_adapted_tests")
270
formats = [("c", "C"), ("d", "D")]
271
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
272
suite = adapter.adapt(input_test)
273
tests = list(iter(suite))
274
self.assertEqual(2, len(tests))
275
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
276
self.assertEqual(tests[0].repository_format, formats[0][0])
277
self.assertEqual(tests[0].transport_server, server1)
278
self.assertEqual(tests[0].transport_readonly_server, server2)
279
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
280
self.assertEqual(tests[1].repository_format, formats[1][0])
281
self.assertEqual(tests[1].transport_server, server1)
282
self.assertEqual(tests[1].transport_readonly_server, server2)
285
class TestTestCaseWithTransport(TestCaseWithTransport):
286
"""Tests for the convenience functions TestCaseWithTransport introduces."""
288
def test_get_readonly_url_none(self):
289
from bzrlib.transport import get_transport
290
from bzrlib.transport.memory import MemoryServer
291
from bzrlib.transport.readonly import ReadonlyTransportDecorator
292
self.transport_server = MemoryServer
293
self.transport_readonly_server = None
294
# calling get_readonly_transport() constructs a decorator on the url
296
url = self.get_readonly_url()
297
url2 = self.get_readonly_url('foo/bar')
298
t = get_transport(url)
299
t2 = get_transport(url2)
300
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
301
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
302
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
304
def test_get_readonly_url_http(self):
305
from bzrlib.transport import get_transport
306
from bzrlib.transport.local import LocalRelpathServer
307
from bzrlib.transport.http import HttpServer, HttpTransport
308
self.transport_server = LocalRelpathServer
309
self.transport_readonly_server = HttpServer
310
# calling get_readonly_transport() gives us a HTTP server instance.
311
url = self.get_readonly_url()
312
url2 = self.get_readonly_url('foo/bar')
313
t = get_transport(url)
314
t2 = get_transport(url2)
315
self.failUnless(isinstance(t, HttpTransport))
316
self.failUnless(isinstance(t2, HttpTransport))
317
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
320
class TestChrootedTest(ChrootedTestCase):
322
def test_root_is_root(self):
323
from bzrlib.transport import get_transport
324
t = get_transport(self.get_readonly_url())
326
self.assertEqual(url, t.clone('..').base)