/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
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
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
21
import unittest
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
22
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
23
import bzrlib
1526.1.3 by Robert Collins
Merge from upstream.
24
from bzrlib.tests import (
25
                          _load_module_by_name,
1534.4.31 by Robert Collins
cleanedup test_outside_wt
26
                          ChrootedTestCase,
1526.1.3 by Robert Collins
Merge from upstream.
27
                          TestCase,
28
                          TestCaseInTempDir,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
29
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
30
                          TestSkipped,
31
                          TextTestRunner,
32
                          )
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
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):
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
48
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
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')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
54
55
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
56
class TestTreeShape(TestCaseInTempDir):
57
58
    def test_unicode_paths(self):
59
        filename = u'hell\u00d8'
1526.1.4 by Robert Collins
forgot my self.
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())
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
65
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
66
67
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
68
class TestSkippedTest(TestCase):
69
    """Try running a test which is skipped, make sure it's reported properly."""
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
70
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
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')
1526.1.3 by Robert Collins
Merge from upstream.
75
        runner = TextTestRunner(stream=self._log_file)
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
76
        test = unittest.FunctionTestCase(skipping_test)
77
        result = runner.run(test)
78
        self.assertTrue(result.wasSuccessful())
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
79
80
81
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
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
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
87
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
88
    def test_get_transport_permutations(self):
1530.1.21 by Robert Collins
Review feedback fixes.
89
        # this checks that we the module get_test_permutations call
90
        # is made by the adapter get_transport_test_permitations method.
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
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):
1530.1.21 by Robert Collins
Review feedback fixes.
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.
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
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
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
121
    def test_adapter_sets_transport_class(self):
1530.1.21 by Robert Collins
Review feedback fixes.
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.
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
128
        from bzrlib.transport.local import (LocalTransport,
129
                                            LocalRelpathServer,
130
                                            LocalAbspathServer,
131
                                            LocalURLServer
132
                                            )
133
        from bzrlib.transport.sftp import (SFTPTransport,
134
                                           SFTPAbsoluteServer,
1530.1.8 by Robert Collins
More NEWS, move sibling sftp tests into new framework, nuke legacy local transport tests.
135
                                           SFTPHomeDirServer,
136
                                           SFTPSiblingAbsoluteServer,
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
137
                                           )
138
        from bzrlib.transport.http import (HttpTransport,
139
                                           HttpServer
140
                                           )
141
        from bzrlib.transport.ftp import FtpTransport
1530.1.3 by Robert Collins
transport implementations now tested consistently.
142
        from bzrlib.transport.memory import (MemoryTransport,
143
                                             MemoryServer
144
                                             )
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
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)
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
163
        http_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
164
        local_relpath_test = test_iter.next()
165
        local_abspath_test = test_iter.next()
166
        local_urlpath_test = test_iter.next()
1530.1.19 by Robert Collins
Make transport test adapter tests reliable.
167
        memory_test = test_iter.next()
1534.4.9 by Robert Collins
Add a readonly decorator for transports.
168
        readonly_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
169
        sftp_abs_test = test_iter.next()
170
        sftp_homedir_test = test_iter.next()
1530.1.8 by Robert Collins
More NEWS, move sibling sftp tests into new framework, nuke legacy local transport tests.
171
        sftp_sibling_abs_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
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)
1530.1.8 by Robert Collins
More NEWS, move sibling sftp tests into new framework, nuke legacy local transport tests.
187
        self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
188
        self.assertEqual(SFTPSiblingAbsoluteServer,
189
                         sftp_sibling_abs_test.transport_server)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
190
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
191
        self.assertEqual(HttpTransport, http_test.transport_class)
192
        self.assertEqual(HttpServer, http_test.transport_server)
193
        # self.assertEqual(FtpTransport, ftp_test.transport_class)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
194
195
        self.assertEqual(MemoryTransport, memory_test.transport_class)
196
        self.assertEqual(MemoryServer, memory_test.transport_server)
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
197
        
1530.1.3 by Robert Collins
transport implementations now tested consistently.
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())
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
220
        formats = [("c", "C"), ("d", "D")]
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
221
        adapter = BranchTestProviderAdapter(server1, server2, formats)
222
        suite = adapter.adapt(input_test)
223
        tests = list(iter(suite))
224
        self.assertEqual(2, len(tests))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
225
        self.assertEqual(tests[0].branch_format, formats[0][0])
226
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
227
        self.assertEqual(tests[0].transport_server, server1)
228
        self.assertEqual(tests[0].transport_readonly_server, server2)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
229
        self.assertEqual(tests[1].branch_format, formats[1][0])
230
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
231
        self.assertEqual(tests[1].transport_server, server1)
232
        self.assertEqual(tests[1].transport_readonly_server, server2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
233
234
1534.4.39 by Robert Collins
Basic BzrDir support.
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
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
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()
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
297
        url2 = self.get_readonly_url('foo/bar')
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
298
        t = get_transport(url)
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
299
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
300
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
301
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
302
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
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()
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
312
        url2 = self.get_readonly_url('foo/bar')
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
313
        t = get_transport(url)
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
314
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
315
        self.failUnless(isinstance(t, HttpTransport))
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
316
        self.failUnless(isinstance(t2, HttpTransport))
317
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
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)