~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

[merge] 0.7-bugfix: Fix fileid_involved to unescape xml characters, fix StubServer to handle paramiko > 1.5.2

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
from bzrlib.tests import (
 
24
                          _load_module_by_name,
 
25
                          TestCase,
 
26
                          TestCaseInTempDir,
 
27
                          TestSkipped,
 
28
                          TextTestRunner,
 
29
                          )
 
30
 
 
31
 
 
32
class SelftestTests(TestCase):
 
33
 
 
34
    def test_import_tests(self):
 
35
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
 
36
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
37
 
 
38
    def test_import_test_failure(self):
 
39
        self.assertRaises(ImportError,
 
40
                          _load_module_by_name,
 
41
                          'bzrlib.no-name-yet')
 
42
 
 
43
 
 
44
class MetaTestLog(TestCase):
 
45
 
 
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')
 
51
 
 
52
 
 
53
class TestTreeShape(TestCaseInTempDir):
 
54
 
 
55
    def test_unicode_paths(self):
 
56
        filename = u'hell\u00d8'
 
57
        try:
 
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)
 
63
 
 
64
 
 
65
class TestSkippedTest(TestCase):
 
66
    """Try running a test which is skipped, make sure it's reported properly."""
 
67
 
 
68
    def test_skipped_test(self):
 
69
        # must be hidden in here so it's not run as a real test
 
70
        def skipping_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())
 
76
 
 
77
 
 
78
class TestTransportProviderAdapter(TestCase):
 
79
    """A group of tests that test the transport implementation adaption core.
 
80
 
 
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.
 
83
    """
 
84
 
 
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()))
 
96
 
 
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
 
102
        # classes below.
 
103
        from bzrlib.transport import (TransportTestProviderAdapter,
 
104
                                      _get_transport_modules
 
105
                                      )
 
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)))))
 
117
 
 
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
 
124
        # easy.
 
125
        from bzrlib.transport.local import (LocalTransport,
 
126
                                            LocalRelpathServer,
 
127
                                            LocalAbspathServer,
 
128
                                            LocalURLServer
 
129
                                            )
 
130
        from bzrlib.transport.sftp import (SFTPTransport,
 
131
                                           SFTPAbsoluteServer,
 
132
                                           SFTPHomeDirServer,
 
133
                                           SFTPSiblingAbsoluteServer,
 
134
                                           )
 
135
        from bzrlib.transport.http import (HttpTransport,
 
136
                                           HttpServer
 
137
                                           )
 
138
        from bzrlib.transport.ftp import FtpTransport
 
139
        from bzrlib.transport.memory import (MemoryTransport,
 
140
                                             MemoryServer
 
141
                                             )
 
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)
 
172
        
 
173
        self.assertEqual(LocalTransport, local_abspath_test.transport_class)
 
174
        self.assertEqual(LocalAbspathServer, local_abspath_test.transport_server)
 
175
 
 
176
        self.assertEqual(LocalTransport, local_urlpath_test.transport_class)
 
177
        self.assertEqual(LocalURLServer, local_urlpath_test.transport_server)
 
178
 
 
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)
 
186
 
 
187
        self.assertEqual(HttpTransport, http_test.transport_class)
 
188
        self.assertEqual(HttpServer, http_test.transport_server)
 
189
        # self.assertEqual(FtpTransport, ftp_test.transport_class)
 
190
 
 
191
        self.assertEqual(MemoryTransport, memory_test.transport_class)
 
192
        self.assertEqual(MemoryServer, memory_test.transport_server)
 
193
        
 
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)",
 
198
                         memory_test.id())
 
199
        self.assertEqual("bzrlib.tests.test_selftest."
 
200
                         "TestTransportProviderAdapter."
 
201
                         "test_adapter_sets_transport_class(LocalRelpathServer)",
 
202
                         local_relpath_test.id())