/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

Branch now uses BzrDir reasonably sanely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
#
 
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.
 
6
#
 
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.
 
11
#
 
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
 
15
 
 
16
"""Tests for the test framework
 
17
"""
 
18
 
 
19
import os
 
20
import sys
 
21
import unittest
 
22
 
 
23
import bzrlib
 
24
from bzrlib.tests import (
 
25
                          _load_module_by_name,
 
26
                          ChrootedTestCase,
 
27
                          TestCase,
 
28
                          TestCaseInTempDir,
 
29
                          TestCaseWithTransport,
 
30
                          TestSkipped,
 
31
                          TextTestRunner,
 
32
                          )
 
33
 
 
34
 
 
35
class SelftestTests(TestCase):
 
36
 
 
37
    def test_import_tests(self):
 
38
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
 
39
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
40
 
 
41
    def test_import_test_failure(self):
 
42
        self.assertRaises(ImportError,
 
43
                          _load_module_by_name,
 
44
                          'bzrlib.no-name-yet')
 
45
 
 
46
 
 
47
class MetaTestLog(TestCase):
 
48
 
 
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')
 
54
 
 
55
 
 
56
class TestTreeShape(TestCaseInTempDir):
 
57
 
 
58
    def test_unicode_paths(self):
 
59
        filename = u'hell\u00d8'
 
60
        try:
 
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)
 
66
 
 
67
 
 
68
class TestSkippedTest(TestCase):
 
69
    """Try running a test which is skipped, make sure it's reported properly."""
 
70
 
 
71
    def test_skipped_test(self):
 
72
        # must be hidden in here so it's not run as a real test
 
73
        def skipping_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())
 
79
 
 
80
 
 
81
class TestTransportProviderAdapter(TestCase):
 
82
    """A group of tests that test the transport implementation adaption core.
 
83
 
 
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.
 
86
    """
 
87
 
 
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()))
 
99
 
 
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
 
105
        # classes below.
 
106
        from bzrlib.transport import (TransportTestProviderAdapter,
 
107
                                      _get_transport_modules
 
108
                                      )
 
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)))))
 
120
 
 
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
 
127
        # easy.
 
128
        from bzrlib.transport.local import (LocalTransport,
 
129
                                            LocalRelpathServer,
 
130
                                            LocalAbspathServer,
 
131
                                            LocalURLServer
 
132
                                            )
 
133
        from bzrlib.transport.sftp import (SFTPTransport,
 
134
                                           SFTPAbsoluteServer,
 
135
                                           SFTPHomeDirServer,
 
136
                                           SFTPSiblingAbsoluteServer,
 
137
                                           )
 
138
        from bzrlib.transport.http import (HttpTransport,
 
139
                                           HttpServer
 
140
                                           )
 
141
        from bzrlib.transport.ftp import FtpTransport
 
142
        from bzrlib.transport.memory import (MemoryTransport,
 
143
                                             MemoryServer
 
144
                                             )
 
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)
 
176
        
 
177
        self.assertEqual(LocalTransport, local_abspath_test.transport_class)
 
178
        self.assertEqual(LocalAbspathServer, local_abspath_test.transport_server)
 
179
 
 
180
        self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
 
181
        self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
 
182
 
 
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)
 
190
 
 
191
        self.assertEqual(HttpTransport, http_test.transport_class)
 
192
        self.assertEqual(HttpServer, http_test.transport_server)
 
193
        # self.assertEqual(FtpTransport, ftp_test.transport_class)
 
194
 
 
195
        self.assertEqual(MemoryTransport, memory_test.transport_class)
 
196
        self.assertEqual(MemoryServer, memory_test.transport_server)
 
197
        
 
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)",
 
202
                         memory_test.id())
 
203
        self.assertEqual("bzrlib.tests.test_selftest."
 
204
                         "TestTransportProviderAdapter."
 
205
                         "test_adapter_sets_transport_class(LocalRelpathServer)",
 
206
                         local_relpath_test.id())
 
207
 
 
208
 
 
209
class TestBranchProviderAdapter(TestCase):
 
210
    """A group of tests that test the branch implementation test adapter."""
 
211
 
 
212
    def test_adapted_tests(self):
 
213
        # check that constructor parameters are passed through to the adapted
 
214
        # test.
 
215
        from bzrlib.branch import BranchTestProviderAdapter
 
216
        input_test = TestBranchProviderAdapter(
 
217
            "test_adapted_tests")
 
218
        server1 = "a"
 
219
        server2 = "b"
 
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)
 
233
 
 
234
 
 
235
class TestBzrDirProviderAdapter(TestCase):
 
236
    """A group of tests that test the bzr dir implementation test adapter."""
 
237
 
 
238
    def test_adapted_tests(self):
 
239
        # check that constructor parameters are passed through to the adapted
 
240
        # test.
 
241
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
 
242
        input_test = TestBzrDirProviderAdapter(
 
243
            "test_adapted_tests")
 
244
        server1 = "a"
 
245
        server2 = "b"
 
246
        formats = ["c", "d"]
 
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)
 
257
 
 
258
 
 
259
class TestRepositoryProviderAdapter(TestCase):
 
260
    """A group of tests that test the repository implementation test adapter."""
 
261
 
 
262
    def test_adapted_tests(self):
 
263
        # check that constructor parameters are passed through to the adapted
 
264
        # test.
 
265
        from bzrlib.repository import RepositoryTestProviderAdapter
 
266
        input_test = TestRepositoryProviderAdapter(
 
267
            "test_adapted_tests")
 
268
        server1 = "a"
 
269
        server2 = "b"
 
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)
 
283
 
 
284
 
 
285
class TestTestCaseWithTransport(TestCaseWithTransport):
 
286
    """Tests for the convenience functions TestCaseWithTransport introduces."""
 
287
 
 
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
 
295
        # for the server
 
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'))
 
303
 
 
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'))
 
318
 
 
319
 
 
320
class TestChrootedTest(ChrootedTestCase):
 
321
 
 
322
    def test_root_is_root(self):
 
323
        from bzrlib.transport import get_transport
 
324
        t = get_transport(self.get_readonly_url())
 
325
        url = t.base
 
326
        self.assertEqual(url, t.clone('..').base)