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
23
from bzrlib.tests import (
32
class SelftestTests(TestCase):
34
def test_import_tests(self):
35
mod = _load_module_by_name('bzrlib.tests.test_selftest')
36
self.assertEqual(mod.SelftestTests, SelftestTests)
38
def test_import_test_failure(self):
39
self.assertRaises(ImportError,
44
class MetaTestLog(TestCase):
46
def test_logging(self):
47
"""Test logs are captured when a test fails."""
48
self.log('a test message')
49
self._log_file.flush()
50
self.assertContainsRe(self._get_log(), 'a test message\n')
53
class TestTreeShape(TestCaseInTempDir):
55
def test_unicode_paths(self):
56
filename = u'hell\u00d8'
58
self.build_tree_contents([(filename, 'contents of hello')])
59
except UnicodeEncodeError:
60
raise TestSkipped("can't build unicode working tree in "
61
"filesystem encoding %s" % sys.getfilesystemencoding())
62
self.failUnlessExists(filename)
65
class TestSkippedTest(TestCase):
66
"""Try running a test which is skipped, make sure it's reported properly."""
68
def test_skipped_test(self):
69
# must be hidden in here so it's not run as a real test
71
raise TestSkipped('test intentionally skipped')
72
runner = TextTestRunner(stream=self._log_file)
73
test = unittest.FunctionTestCase(skipping_test)
74
result = runner.run(test)
75
self.assertTrue(result.wasSuccessful())
78
class TestTransportProviderAdapter(TestCase):
79
"""A group of tests that test the transport implementation adaption core.
81
This will be generalised in the future which is why it is in this
82
test file even though it is specific to transport tests at the moment.
85
def test_get_transport_permutations(self):
86
# this checks that we the module get_test_permutations call
87
# is made by the adapter get_transport_test_permitations method.
88
class MockModule(object):
89
def get_test_permutations(self):
90
return sample_permutation
91
sample_permutation = [(1,2), (3,4)]
92
from bzrlib.transport import TransportTestProviderAdapter
93
adapter = TransportTestProviderAdapter()
94
self.assertEqual(sample_permutation,
95
adapter.get_transport_test_permutations(MockModule()))
97
def test_adapter_checks_all_modules(self):
98
# this checks that the adapter returns as many permurtations as
99
# there are in all the registered# transport modules for there
100
# - we assume if this matches its probably doing the right thing
101
# especially in combination with the tests for setting the right
103
from bzrlib.transport import (TransportTestProviderAdapter,
104
_get_transport_modules
106
modules = _get_transport_modules()
107
permutation_count = 0
108
for module in modules:
109
permutation_count += len(reduce(getattr,
110
(module + ".get_test_permutations").split('.')[1:],
111
__import__(module))())
112
input_test = TestTransportProviderAdapter(
113
"test_adapter_sets_transport_class")
114
adapter = TransportTestProviderAdapter()
115
self.assertEqual(permutation_count,
116
len(list(iter(adapter.adapt(input_test)))))
118
def test_adapter_sets_transport_class(self):
119
# when the adapter adapts a test it needs to
120
# place one of the permutations from the transport
121
# providers in each test case copy. This checks
122
# that it does not just use the same one all the time.
123
# and that the id is set correctly so that debugging is
125
from bzrlib.transport.local import (LocalTransport,
130
from bzrlib.transport.sftp import (SFTPTransport,
133
SFTPSiblingAbsoluteServer,
135
from bzrlib.transport.http import (HttpTransport,
138
from bzrlib.transport.ftp import FtpTransport
139
from bzrlib.transport.memory import (MemoryTransport,
142
from bzrlib.transport import TransportTestProviderAdapter
143
# FIXME. What we want is a factory for the things
144
# needed to test the implementation. I.e. for transport we want:
145
# the class that connections should get; a local server factory
146
# so we would want the following permutations:
147
# LocalTransport relpath-factory
148
# LocalTransport abspath-factory
149
# LocalTransport file://-factory
150
# SFTPTransport homedir-factory
151
# SFTPTransport abssolute-factory
152
# HTTPTransport http-factory
153
# HTTPTransport https-factory
154
# etc, but we are currently lacking in this, so print out that
155
# this should be fixed.
156
input_test = TestTransportProviderAdapter(
157
"test_adapter_sets_transport_class")
158
suite = TransportTestProviderAdapter().adapt(input_test)
159
test_iter = iter(suite)
160
http_test = test_iter.next()
161
local_relpath_test = test_iter.next()
162
local_abspath_test = test_iter.next()
163
local_urlpath_test = test_iter.next()
164
memory_test = test_iter.next()
165
sftp_abs_test = test_iter.next()
166
sftp_homedir_test = test_iter.next()
167
sftp_sibling_abs_test = test_iter.next()
168
# ftp_test = test_iter.next()
169
self.assertRaises(StopIteration, test_iter.next)
170
self.assertEqual(LocalTransport, local_relpath_test.transport_class)
171
self.assertEqual(LocalRelpathServer, local_relpath_test.transport_server)
173
self.assertEqual(LocalTransport, local_abspath_test.transport_class)
174
self.assertEqual(LocalAbspathServer, local_abspath_test.transport_server)
176
self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
177
self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
179
self.assertEqual(SFTPTransport, sftp_abs_test.transport_class)
180
self.assertEqual(SFTPAbsoluteServer, sftp_abs_test.transport_server)
181
self.assertEqual(SFTPTransport, sftp_homedir_test.transport_class)
182
self.assertEqual(SFTPHomeDirServer, sftp_homedir_test.transport_server)
183
self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
184
self.assertEqual(SFTPSiblingAbsoluteServer,
185
sftp_sibling_abs_test.transport_server)
187
self.assertEqual(HttpTransport, http_test.transport_class)
188
self.assertEqual(HttpServer, http_test.transport_server)
189
# self.assertEqual(FtpTransport, ftp_test.transport_class)
191
self.assertEqual(MemoryTransport, memory_test.transport_class)
192
self.assertEqual(MemoryServer, memory_test.transport_server)
194
# we could test all of them for .id, but two is probably sufficient.
195
self.assertEqual("bzrlib.tests.test_selftest."
196
"TestTransportProviderAdapter."
197
"test_adapter_sets_transport_class(MemoryServer)",
199
self.assertEqual("bzrlib.tests.test_selftest."
200
"TestTransportProviderAdapter."
201
"test_adapter_sets_transport_class(LocalRelpathServer)",
202
local_relpath_test.id())