~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

Merge in bzrdir work to enable checkout improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import os
20
20
import sys
21
21
import unittest
 
22
import warnings
22
23
 
 
24
import bzrlib
23
25
from bzrlib.tests import (
24
26
                          _load_module_by_name,
 
27
                          ChrootedTestCase,
25
28
                          TestCase,
26
29
                          TestCaseInTempDir,
 
30
                          TestCaseWithTransport,
27
31
                          TestSkipped,
28
32
                          TextTestRunner,
29
33
                          )
 
34
import bzrlib.errors as errors
30
35
 
31
36
 
32
37
class SelftestTests(TestCase):
76
81
 
77
82
 
78
83
class TestTransportProviderAdapter(TestCase):
 
84
    """A group of tests that test the transport implementation adaption core.
 
85
 
 
86
    This is a meta test that the tests are applied to all available 
 
87
    transports.
 
88
 
 
89
    This will be generalised in the future which is why it is in this 
 
90
    test file even though it is specific to transport tests at the moment.
 
91
    """
79
92
 
80
93
    def test_get_transport_permutations(self):
 
94
        # this checks that we the module get_test_permutations call
 
95
        # is made by the adapter get_transport_test_permitations method.
81
96
        class MockModule(object):
82
97
            def get_test_permutations(self):
83
98
                return sample_permutation
88
103
                         adapter.get_transport_test_permutations(MockModule()))
89
104
 
90
105
    def test_adapter_checks_all_modules(self):
 
106
        # this checks that the adapter returns as many permurtations as
 
107
        # there are in all the registered# transport modules for there
 
108
        # - we assume if this matches its probably doing the right thing
 
109
        # especially in combination with the tests for setting the right
 
110
        # classes below.
91
111
        from bzrlib.transport import (TransportTestProviderAdapter,
92
112
                                      _get_transport_modules
93
113
                                      )
94
114
        modules = _get_transport_modules()
95
115
        permutation_count = 0
96
116
        for module in modules:
97
 
            permutation_count += len(reduce(getattr, 
98
 
                (module + ".get_test_permutations").split('.')[1:],
99
 
                 __import__(module))())
 
117
            try:
 
118
                permutation_count += len(reduce(getattr, 
 
119
                    (module + ".get_test_permutations").split('.')[1:],
 
120
                     __import__(module))())
 
121
            except errors.DependencyNotPresent:
 
122
                pass
100
123
        input_test = TestTransportProviderAdapter(
101
124
            "test_adapter_sets_transport_class")
102
125
        adapter = TransportTestProviderAdapter()
104
127
                         len(list(iter(adapter.adapt(input_test)))))
105
128
 
106
129
    def test_adapter_sets_transport_class(self):
 
130
        # when the adapter adapts a test it needs to 
 
131
        # place one of the permutations from the transport
 
132
        # providers in each test case copy. This checks
 
133
        # that it does not just use the same one all the time.
 
134
        # and that the id is set correctly so that debugging is
 
135
        # easy.
 
136
        # 
 
137
        # An instance of this test is actually used as the input
 
138
        # for adapting it to all the available transports
 
139
        # (or i think so - ??? mbp)
107
140
        from bzrlib.transport.local import (LocalTransport,
108
141
                                            LocalRelpathServer,
109
142
                                            LocalAbspathServer,
110
143
                                            LocalURLServer
111
144
                                            )
112
 
        from bzrlib.transport.sftp import (SFTPTransport,
113
 
                                           SFTPAbsoluteServer,
114
 
                                           SFTPHomeDirServer,
115
 
                                           SFTPSiblingAbsoluteServer,
116
 
                                           )
 
145
        try:
 
146
            from bzrlib.transport.sftp import (SFTPTransport,
 
147
                                               SFTPAbsoluteServer,
 
148
                                               SFTPHomeDirServer,
 
149
                                               SFTPSiblingAbsoluteServer,
 
150
                                               )
 
151
        except errors.ParamikoNotPresent, e:
 
152
            warnings.warn(str(e))
 
153
            has_paramiko = False
 
154
        else:
 
155
            has_paramiko = True
117
156
        from bzrlib.transport.http import (HttpTransport,
118
157
                                           HttpServer
119
158
                                           )
138
177
        input_test = TestTransportProviderAdapter(
139
178
            "test_adapter_sets_transport_class")
140
179
        suite = TransportTestProviderAdapter().adapt(input_test)
 
180
        # tests are generated in collation order. 
 
181
        # XXX: but i'm not sure the order should really be part of the 
 
182
        # contract of the adapter, should it -- mbp 20060201
141
183
        test_iter = iter(suite)
142
184
        http_test = test_iter.next()
143
185
        local_relpath_test = test_iter.next()
144
186
        local_abspath_test = test_iter.next()
145
187
        local_urlpath_test = test_iter.next()
146
188
        memory_test = test_iter.next()
147
 
        sftp_abs_test = test_iter.next()
148
 
        sftp_homedir_test = test_iter.next()
149
 
        sftp_sibling_abs_test = test_iter.next()
 
189
        readonly_test = test_iter.next()
 
190
        if has_paramiko:
 
191
            sftp_abs_test = test_iter.next()
 
192
            sftp_homedir_test = test_iter.next()
 
193
            sftp_sibling_abs_test = test_iter.next()
150
194
        # ftp_test = test_iter.next()
 
195
        # should now be at the end of the test
151
196
        self.assertRaises(StopIteration, test_iter.next)
152
197
        self.assertEqual(LocalTransport, local_relpath_test.transport_class)
153
198
        self.assertEqual(LocalRelpathServer, local_relpath_test.transport_server)
158
203
        self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
159
204
        self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
160
205
 
161
 
        self.assertEqual(SFTPTransport, sftp_abs_test.transport_class)
162
 
        self.assertEqual(SFTPAbsoluteServer, sftp_abs_test.transport_server)
163
 
        self.assertEqual(SFTPTransport, sftp_homedir_test.transport_class)
164
 
        self.assertEqual(SFTPHomeDirServer, sftp_homedir_test.transport_server)
165
 
        self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
166
 
        self.assertEqual(SFTPSiblingAbsoluteServer,
167
 
                         sftp_sibling_abs_test.transport_server)
 
206
        if has_paramiko:
 
207
            self.assertEqual(SFTPTransport, sftp_abs_test.transport_class)
 
208
            self.assertEqual(SFTPAbsoluteServer, sftp_abs_test.transport_server)
 
209
            self.assertEqual(SFTPTransport, sftp_homedir_test.transport_class)
 
210
            self.assertEqual(SFTPHomeDirServer, sftp_homedir_test.transport_server)
 
211
            self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
 
212
            self.assertEqual(SFTPSiblingAbsoluteServer,
 
213
                             sftp_sibling_abs_test.transport_server)
168
214
 
169
215
        self.assertEqual(HttpTransport, http_test.transport_class)
170
216
        self.assertEqual(HttpServer, http_test.transport_server)
182
228
                         "TestTransportProviderAdapter."
183
229
                         "test_adapter_sets_transport_class(LocalRelpathServer)",
184
230
                         local_relpath_test.id())
 
231
 
 
232
 
 
233
class TestBranchProviderAdapter(TestCase):
 
234
    """A group of tests that test the branch implementation test adapter."""
 
235
 
 
236
    def test_adapted_tests(self):
 
237
        # check that constructor parameters are passed through to the adapted
 
238
        # test.
 
239
        from bzrlib.branch import BranchTestProviderAdapter
 
240
        input_test = TestBranchProviderAdapter(
 
241
            "test_adapted_tests")
 
242
        server1 = "a"
 
243
        server2 = "b"
 
244
        formats = [("c", "C"), ("d", "D")]
 
245
        adapter = BranchTestProviderAdapter(server1, server2, formats)
 
246
        suite = adapter.adapt(input_test)
 
247
        tests = list(iter(suite))
 
248
        self.assertEqual(2, len(tests))
 
249
        self.assertEqual(tests[0].branch_format, formats[0][0])
 
250
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
251
        self.assertEqual(tests[0].transport_server, server1)
 
252
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
253
        self.assertEqual(tests[1].branch_format, formats[1][0])
 
254
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
255
        self.assertEqual(tests[1].transport_server, server1)
 
256
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
257
 
 
258
 
 
259
class TestBzrDirProviderAdapter(TestCase):
 
260
    """A group of tests that test the bzr dir 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.bzrdir import BzrDirTestProviderAdapter
 
266
        input_test = TestBzrDirProviderAdapter(
 
267
            "test_adapted_tests")
 
268
        server1 = "a"
 
269
        server2 = "b"
 
270
        formats = ["c", "d"]
 
271
        adapter = BzrDirTestProviderAdapter(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])
 
276
        self.assertEqual(tests[0].transport_server, server1)
 
277
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
278
        self.assertEqual(tests[1].bzrdir_format, formats[1])
 
279
        self.assertEqual(tests[1].transport_server, server1)
 
280
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
281
 
 
282
 
 
283
class TestRepositoryProviderAdapter(TestCase):
 
284
    """A group of tests that test the repository implementation test adapter."""
 
285
 
 
286
    def test_adapted_tests(self):
 
287
        # check that constructor parameters are passed through to the adapted
 
288
        # test.
 
289
        from bzrlib.repository import RepositoryTestProviderAdapter
 
290
        input_test = TestRepositoryProviderAdapter(
 
291
            "test_adapted_tests")
 
292
        server1 = "a"
 
293
        server2 = "b"
 
294
        formats = [("c", "C"), ("d", "D")]
 
295
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
 
296
        suite = adapter.adapt(input_test)
 
297
        tests = list(iter(suite))
 
298
        self.assertEqual(2, len(tests))
 
299
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
300
        self.assertEqual(tests[0].repository_format, formats[0][0])
 
301
        self.assertEqual(tests[0].transport_server, server1)
 
302
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
303
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
304
        self.assertEqual(tests[1].repository_format, formats[1][0])
 
305
        self.assertEqual(tests[1].transport_server, server1)
 
306
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
307
 
 
308
 
 
309
class TestWorkingTreeProviderAdapter(TestCase):
 
310
    """A group of tests that test the workingtree implementation test adapter."""
 
311
 
 
312
    def test_adapted_tests(self):
 
313
        # check that constructor parameters are passed through to the adapted
 
314
        # test.
 
315
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
 
316
        input_test = TestWorkingTreeProviderAdapter(
 
317
            "test_adapted_tests")
 
318
        server1 = "a"
 
319
        server2 = "b"
 
320
        formats = [("c", "C"), ("d", "D")]
 
321
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
 
322
        suite = adapter.adapt(input_test)
 
323
        tests = list(iter(suite))
 
324
        self.assertEqual(2, len(tests))
 
325
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
326
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
327
        self.assertEqual(tests[0].transport_server, server1)
 
328
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
329
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
330
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
331
        self.assertEqual(tests[1].transport_server, server1)
 
332
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
333
 
 
334
 
 
335
class TestTestCaseWithTransport(TestCaseWithTransport):
 
336
    """Tests for the convenience functions TestCaseWithTransport introduces."""
 
337
 
 
338
    def test_get_readonly_url_none(self):
 
339
        from bzrlib.transport import get_transport
 
340
        from bzrlib.transport.memory import MemoryServer
 
341
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
 
342
        self.transport_server = MemoryServer
 
343
        self.transport_readonly_server = None
 
344
        # calling get_readonly_transport() constructs a decorator on the url
 
345
        # for the server
 
346
        url = self.get_readonly_url()
 
347
        url2 = self.get_readonly_url('foo/bar')
 
348
        t = get_transport(url)
 
349
        t2 = get_transport(url2)
 
350
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
351
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
 
352
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
353
 
 
354
    def test_get_readonly_url_http(self):
 
355
        from bzrlib.transport import get_transport
 
356
        from bzrlib.transport.local import LocalRelpathServer
 
357
        from bzrlib.transport.http import HttpServer, HttpTransport
 
358
        self.transport_server = LocalRelpathServer
 
359
        self.transport_readonly_server = HttpServer
 
360
        # calling get_readonly_transport() gives us a HTTP server instance.
 
361
        url = self.get_readonly_url()
 
362
        url2 = self.get_readonly_url('foo/bar')
 
363
        t = get_transport(url)
 
364
        t2 = get_transport(url2)
 
365
        self.failUnless(isinstance(t, HttpTransport))
 
366
        self.failUnless(isinstance(t2, HttpTransport))
 
367
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
368
 
 
369
 
 
370
class TestChrootedTest(ChrootedTestCase):
 
371
 
 
372
    def test_root_is_root(self):
 
373
        from bzrlib.transport import get_transport
 
374
        t = get_transport(self.get_readonly_url())
 
375
        url = t.base
 
376
        self.assertEqual(url, t.clone('..').base)