~bzr-pqm/bzr/bzr.dev

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,
26
                          TestCase,
27
                          TestCaseInTempDir,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
28
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
29
                          TestSkipped,
30
                          TextTestRunner,
31
                          )
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
32
33
34
class SelftestTests(TestCase):
35
36
    def test_import_tests(self):
37
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
38
        self.assertEqual(mod.SelftestTests, SelftestTests)
39
40
    def test_import_test_failure(self):
41
        self.assertRaises(ImportError,
42
                          _load_module_by_name,
43
                          'bzrlib.no-name-yet')
44
45
46
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.
47
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
48
    def test_logging(self):
49
        """Test logs are captured when a test fails."""
50
        self.log('a test message')
51
        self._log_file.flush()
52
        self.assertContainsRe(self._get_log(), 'a test message\n')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
53
54
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.
55
class TestTreeShape(TestCaseInTempDir):
56
57
    def test_unicode_paths(self):
58
        filename = u'hell\u00d8'
1526.1.4 by Robert Collins
forgot my self.
59
        try:
60
            self.build_tree_contents([(filename, 'contents of hello')])
61
        except UnicodeEncodeError:
62
            raise TestSkipped("can't build unicode working tree in "
63
                "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.
64
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
65
66
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
67
class TestSkippedTest(TestCase):
68
    """Try running a test which is skipped, make sure it's reported properly."""
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
69
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
70
    def test_skipped_test(self):
71
        # must be hidden in here so it's not run as a real test
72
        def skipping_test():
73
            raise TestSkipped('test intentionally skipped')
1526.1.3 by Robert Collins
Merge from upstream.
74
        runner = TextTestRunner(stream=self._log_file)
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
75
        test = unittest.FunctionTestCase(skipping_test)
76
        result = runner.run(test)
77
        self.assertTrue(result.wasSuccessful())
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
78
79
80
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
81
    """A group of tests that test the transport implementation adaption core.
82
83
    This will be generalised in the future which is why it is in this 
84
    test file even though it is specific to transport tests at the moment.
85
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
86
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.
87
    def test_get_transport_permutations(self):
1530.1.21 by Robert Collins
Review feedback fixes.
88
        # this checks that we the module get_test_permutations call
89
        # 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.
90
        class MockModule(object):
91
            def get_test_permutations(self):
92
                return sample_permutation
93
        sample_permutation = [(1,2), (3,4)]
94
        from bzrlib.transport import TransportTestProviderAdapter
95
        adapter = TransportTestProviderAdapter()
96
        self.assertEqual(sample_permutation,
97
                         adapter.get_transport_test_permutations(MockModule()))
98
99
    def test_adapter_checks_all_modules(self):
1530.1.21 by Robert Collins
Review feedback fixes.
100
        # this checks that the adapter returns as many permurtations as
101
        # there are in all the registered# transport modules for there
102
        # - we assume if this matches its probably doing the right thing
103
        # especially in combination with the tests for setting the right
104
        # 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.
105
        from bzrlib.transport import (TransportTestProviderAdapter,
106
                                      _get_transport_modules
107
                                      )
108
        modules = _get_transport_modules()
109
        permutation_count = 0
110
        for module in modules:
111
            permutation_count += len(reduce(getattr, 
112
                (module + ".get_test_permutations").split('.')[1:],
113
                 __import__(module))())
114
        input_test = TestTransportProviderAdapter(
115
            "test_adapter_sets_transport_class")
116
        adapter = TransportTestProviderAdapter()
117
        self.assertEqual(permutation_count,
118
                         len(list(iter(adapter.adapt(input_test)))))
119
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
120
    def test_adapter_sets_transport_class(self):
1530.1.21 by Robert Collins
Review feedback fixes.
121
        # when the adapter adapts a test it needs to 
122
        # place one of the permutations from the transport
123
        # providers in each test case copy. This checks
124
        # that it does not just use the same one all the time.
125
        # and that the id is set correctly so that debugging is
126
        # easy.
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
127
        from bzrlib.transport.local import (LocalTransport,
128
                                            LocalRelpathServer,
129
                                            LocalAbspathServer,
130
                                            LocalURLServer
131
                                            )
132
        from bzrlib.transport.sftp import (SFTPTransport,
133
                                           SFTPAbsoluteServer,
1530.1.8 by Robert Collins
More NEWS, move sibling sftp tests into new framework, nuke legacy local transport tests.
134
                                           SFTPHomeDirServer,
135
                                           SFTPSiblingAbsoluteServer,
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
136
                                           )
137
        from bzrlib.transport.http import (HttpTransport,
138
                                           HttpServer
139
                                           )
140
        from bzrlib.transport.ftp import FtpTransport
1530.1.3 by Robert Collins
transport implementations now tested consistently.
141
        from bzrlib.transport.memory import (MemoryTransport,
142
                                             MemoryServer
143
                                             )
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
144
        from bzrlib.transport import TransportTestProviderAdapter
145
        # FIXME. What we want is a factory for the things
146
        # needed to test the implementation. I.e. for transport we want:
147
        # the class that connections should get; a local server factory
148
        # so we would want the following permutations:
149
        # LocalTransport relpath-factory
150
        # LocalTransport abspath-factory
151
        # LocalTransport file://-factory
152
        # SFTPTransport homedir-factory
153
        # SFTPTransport abssolute-factory
154
        # HTTPTransport http-factory
155
        # HTTPTransport https-factory
156
        # etc, but we are currently lacking in this, so print out that
157
        # this should be fixed.
158
        input_test = TestTransportProviderAdapter(
159
            "test_adapter_sets_transport_class")
160
        suite = TransportTestProviderAdapter().adapt(input_test)
161
        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.
162
        http_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
163
        local_relpath_test = test_iter.next()
164
        local_abspath_test = test_iter.next()
165
        local_urlpath_test = test_iter.next()
1530.1.19 by Robert Collins
Make transport test adapter tests reliable.
166
        memory_test = test_iter.next()
1534.4.9 by Robert Collins
Add a readonly decorator for transports.
167
        readonly_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
168
        sftp_abs_test = test_iter.next()
169
        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.
170
        sftp_sibling_abs_test = test_iter.next()
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
171
        # ftp_test = test_iter.next()
172
        self.assertRaises(StopIteration, test_iter.next)
173
        self.assertEqual(LocalTransport, local_relpath_test.transport_class)
174
        self.assertEqual(LocalRelpathServer, local_relpath_test.transport_server)
175
        
176
        self.assertEqual(LocalTransport, local_abspath_test.transport_class)
177
        self.assertEqual(LocalAbspathServer, local_abspath_test.transport_server)
178
179
        self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
180
        self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
181
182
        self.assertEqual(SFTPTransport, sftp_abs_test.transport_class)
183
        self.assertEqual(SFTPAbsoluteServer, sftp_abs_test.transport_server)
184
        self.assertEqual(SFTPTransport, sftp_homedir_test.transport_class)
185
        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.
186
        self.assertEqual(SFTPTransport, sftp_sibling_abs_test.transport_class)
187
        self.assertEqual(SFTPSiblingAbsoluteServer,
188
                         sftp_sibling_abs_test.transport_server)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
189
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
190
        self.assertEqual(HttpTransport, http_test.transport_class)
191
        self.assertEqual(HttpServer, http_test.transport_server)
192
        # self.assertEqual(FtpTransport, ftp_test.transport_class)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
193
194
        self.assertEqual(MemoryTransport, memory_test.transport_class)
195
        self.assertEqual(MemoryServer, memory_test.transport_server)
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
196
        
1530.1.3 by Robert Collins
transport implementations now tested consistently.
197
        # we could test all of them for .id, but two is probably sufficient.
198
        self.assertEqual("bzrlib.tests.test_selftest."
199
                         "TestTransportProviderAdapter."
200
                         "test_adapter_sets_transport_class(MemoryServer)",
201
                         memory_test.id())
202
        self.assertEqual("bzrlib.tests.test_selftest."
203
                         "TestTransportProviderAdapter."
204
                         "test_adapter_sets_transport_class(LocalRelpathServer)",
205
                         local_relpath_test.id())
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
206
207
208
class TestBranchProviderAdapter(TestCase):
209
    """A group of tests that test the branch implementation test adapter."""
210
211
    def test_adapted_tests(self):
212
        # check that constructor parameters are passed through to the adapted
213
        # test.
214
        from bzrlib.branch import BranchTestProviderAdapter
215
        input_test = TestBranchProviderAdapter(
216
            "test_adapted_tests")
217
        server1 = "a"
218
        server2 = "b"
219
        formats = ["c", "d"]
220
        adapter = BranchTestProviderAdapter(server1, server2, formats)
221
        suite = adapter.adapt(input_test)
222
        tests = list(iter(suite))
223
        self.assertEqual(2, len(tests))
224
        self.assertEqual(tests[0].branch_format, formats[0])
225
        self.assertEqual(tests[0].transport_server, server1)
226
        self.assertEqual(tests[0].transport_readonly_server, server2)
227
        self.assertEqual(tests[1].branch_format, formats[1])
228
        self.assertEqual(tests[1].transport_server, server1)
229
        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.
230
231
232
class TestTestCaseWithTransport(TestCaseWithTransport):
233
    """Tests for the convenience functions TestCaseWithTransport introduces."""
234
235
    def test_get_readonly_url_none(self):
236
        from bzrlib.transport import get_transport
237
        from bzrlib.transport.memory import MemoryServer
238
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
239
        self.transport_server = MemoryServer
240
        self.transport_readonly_server = None
241
        # calling get_readonly_transport() constructs a decorator on the url
242
        # for the server
243
        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.
244
        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.
245
        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.
246
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
247
        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.
248
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
249
        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.
250
251
    def test_get_readonly_url_http(self):
252
        from bzrlib.transport import get_transport
253
        from bzrlib.transport.local import LocalRelpathServer
254
        from bzrlib.transport.http import HttpServer, HttpTransport
255
        self.transport_server = LocalRelpathServer
256
        self.transport_readonly_server = HttpServer
257
        # calling get_readonly_transport() gives us a HTTP server instance.
258
        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.
259
        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.
260
        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.
261
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
262
        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.
263
        self.failUnless(isinstance(t2, HttpTransport))
264
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))