/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
2
#
3
# This program is free software; you can redistribute it and/or modify
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
17
"""Tests for the test framework."""
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
18
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
19
import cStringIO
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
20
import os
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
21
from StringIO import StringIO
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
22
import sys
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
23
import time
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
24
import unittest
1185.62.24 by John Arbash Meinel
Changing the exception that sftp.py throws when it can't find paramiko, so that the test suite can handle it.
25
import warnings
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
26
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.
27
import bzrlib
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
28
from bzrlib import (
29
    bzrdir,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
30
    errors,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
31
    memorytree,
32
    osutils,
33
    repository,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
34
    symbol_versioning,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
35
    )
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
36
from bzrlib.progress import _BaseProgressBar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
37
from bzrlib.repofmt import weaverepo
38
from bzrlib.symbol_versioning import zero_ten, zero_eleven
1526.1.3 by Robert Collins
Merge from upstream.
39
from bzrlib.tests import (
1534.4.31 by Robert Collins
cleanedup test_outside_wt
40
                          ChrootedTestCase,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
41
                          ExtendedTestResult,
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
42
                          Feature,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
43
                          KnownFailure,
1526.1.3 by Robert Collins
Merge from upstream.
44
                          TestCase,
45
                          TestCaseInTempDir,
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
46
                          TestCaseWithMemoryTransport,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
47
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
48
                          TestSkipped,
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
49
                          TestSuite,
1526.1.3 by Robert Collins
Merge from upstream.
50
                          TextTestRunner,
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
51
                          UnavailableFeature,
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
52
                          clean_selftest_output,
1526.1.3 by Robert Collins
Merge from upstream.
53
                          )
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
54
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
55
from bzrlib.tests.TestUtil import _load_module_by_name
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
56
from bzrlib.trace import note
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
57
from bzrlib.transport.memory import MemoryServer, MemoryTransport
1951.1.1 by Andrew Bennetts
Make test_bench_history and _get_bzr_source_tree tolerant of UnknownFormatError for the bzr workingtree.
58
from bzrlib.version import _get_bzr_source_tree
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
59
60
61
class SelftestTests(TestCase):
62
63
    def test_import_tests(self):
64
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
65
        self.assertEqual(mod.SelftestTests, SelftestTests)
66
67
    def test_import_test_failure(self):
68
        self.assertRaises(ImportError,
69
                          _load_module_by_name,
70
                          'bzrlib.no-name-yet')
71
72
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.
73
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
74
    def test_logging(self):
75
        """Test logs are captured when a test fails."""
76
        self.log('a test message')
77
        self._log_file.flush()
1927.3.1 by Carl Friedrich Bolz
Throw away on-disk logfile when possible.
78
        self.assertContainsRe(self._get_log(keep_log_file=True),
79
                              'a test message\n')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
80
81
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.
82
class TestTreeShape(TestCaseInTempDir):
83
84
    def test_unicode_paths(self):
85
        filename = u'hell\u00d8'
1526.1.4 by Robert Collins
forgot my self.
86
        try:
87
            self.build_tree_contents([(filename, 'contents of hello')])
88
        except UnicodeEncodeError:
89
            raise TestSkipped("can't build unicode working tree in "
90
                "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.
91
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
92
93
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
94
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
95
    """A group of tests that test the transport implementation adaption core.
96
1551.1.1 by Martin Pool
[merge] branch-formats branch, and reconcile changes
97
    This is a meta test that the tests are applied to all available 
98
    transports.
99
1530.1.21 by Robert Collins
Review feedback fixes.
100
    This will be generalised in the future which is why it is in this 
101
    test file even though it is specific to transport tests at the moment.
102
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
103
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.
104
    def test_get_transport_permutations(self):
1530.1.21 by Robert Collins
Review feedback fixes.
105
        # this checks that we the module get_test_permutations call
106
        # 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.
107
        class MockModule(object):
108
            def get_test_permutations(self):
109
                return sample_permutation
110
        sample_permutation = [(1,2), (3,4)]
111
        from bzrlib.transport import TransportTestProviderAdapter
112
        adapter = TransportTestProviderAdapter()
113
        self.assertEqual(sample_permutation,
114
                         adapter.get_transport_test_permutations(MockModule()))
115
116
    def test_adapter_checks_all_modules(self):
1530.1.21 by Robert Collins
Review feedback fixes.
117
        # this checks that the adapter returns as many permurtations as
118
        # there are in all the registered# transport modules for there
119
        # - we assume if this matches its probably doing the right thing
120
        # especially in combination with the tests for setting the right
121
        # 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.
122
        from bzrlib.transport import (TransportTestProviderAdapter,
123
                                      _get_transport_modules
124
                                      )
125
        modules = _get_transport_modules()
126
        permutation_count = 0
127
        for module in modules:
1185.62.24 by John Arbash Meinel
Changing the exception that sftp.py throws when it can't find paramiko, so that the test suite can handle it.
128
            try:
129
                permutation_count += len(reduce(getattr, 
130
                    (module + ".get_test_permutations").split('.')[1:],
131
                     __import__(module))())
132
            except errors.DependencyNotPresent:
133
                pass
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.
134
        input_test = TestTransportProviderAdapter(
135
            "test_adapter_sets_transport_class")
136
        adapter = TransportTestProviderAdapter()
137
        self.assertEqual(permutation_count,
138
                         len(list(iter(adapter.adapt(input_test)))))
139
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
140
    def test_adapter_sets_transport_class(self):
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
141
        # Check that the test adapter inserts a transport and server into the
142
        # generated test.
143
        #
144
        # This test used to know about all the possible transports and the
145
        # order they were returned but that seems overly brittle (mbp
146
        # 20060307)
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
147
        input_test = TestTransportProviderAdapter(
148
            "test_adapter_sets_transport_class")
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
149
        from bzrlib.transport import TransportTestProviderAdapter
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
150
        suite = TransportTestProviderAdapter().adapt(input_test)
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
151
        tests = list(iter(suite))
152
        self.assertTrue(len(tests) > 6)
153
        # there are at least that many builtin transports
154
        one_test = tests[0]
155
        self.assertTrue(issubclass(one_test.transport_class, 
156
                                   bzrlib.transport.Transport))
157
        self.assertTrue(issubclass(one_test.transport_server, 
158
                                   bzrlib.transport.Server))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
159
160
161
class TestBranchProviderAdapter(TestCase):
162
    """A group of tests that test the branch implementation test adapter."""
163
164
    def test_adapted_tests(self):
165
        # check that constructor parameters are passed through to the adapted
166
        # test.
167
        from bzrlib.branch import BranchTestProviderAdapter
168
        input_test = TestBranchProviderAdapter(
169
            "test_adapted_tests")
170
        server1 = "a"
171
        server2 = "b"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
172
        formats = [("c", "C"), ("d", "D")]
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
173
        adapter = BranchTestProviderAdapter(server1, server2, formats)
174
        suite = adapter.adapt(input_test)
175
        tests = list(iter(suite))
176
        self.assertEqual(2, len(tests))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
177
        self.assertEqual(tests[0].branch_format, formats[0][0])
178
        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.
179
        self.assertEqual(tests[0].transport_server, server1)
180
        self.assertEqual(tests[0].transport_readonly_server, server2)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
181
        self.assertEqual(tests[1].branch_format, formats[1][0])
182
        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.
183
        self.assertEqual(tests[1].transport_server, server1)
184
        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.
185
186
1534.4.39 by Robert Collins
Basic BzrDir support.
187
class TestBzrDirProviderAdapter(TestCase):
188
    """A group of tests that test the bzr dir implementation test adapter."""
189
190
    def test_adapted_tests(self):
191
        # check that constructor parameters are passed through to the adapted
192
        # test.
193
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
194
        input_test = TestBzrDirProviderAdapter(
195
            "test_adapted_tests")
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
196
        vfs_factory = "v"
1534.4.39 by Robert Collins
Basic BzrDir support.
197
        server1 = "a"
198
        server2 = "b"
199
        formats = ["c", "d"]
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
200
        adapter = BzrDirTestProviderAdapter(vfs_factory,
201
            server1, server2, formats)
1534.4.39 by Robert Collins
Basic BzrDir support.
202
        suite = adapter.adapt(input_test)
203
        tests = list(iter(suite))
204
        self.assertEqual(2, len(tests))
205
        self.assertEqual(tests[0].bzrdir_format, formats[0])
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
206
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
207
        self.assertEqual(tests[0].transport_server, server1)
208
        self.assertEqual(tests[0].transport_readonly_server, server2)
209
        self.assertEqual(tests[1].bzrdir_format, formats[1])
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
210
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
211
        self.assertEqual(tests[1].transport_server, server1)
212
        self.assertEqual(tests[1].transport_readonly_server, server2)
213
214
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
215
class TestRepositoryProviderAdapter(TestCase):
216
    """A group of tests that test the repository implementation test adapter."""
217
218
    def test_adapted_tests(self):
219
        # check that constructor parameters are passed through to the adapted
220
        # test.
221
        from bzrlib.repository import RepositoryTestProviderAdapter
222
        input_test = TestRepositoryProviderAdapter(
223
            "test_adapted_tests")
224
        server1 = "a"
225
        server2 = "b"
226
        formats = [("c", "C"), ("d", "D")]
227
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
228
        suite = adapter.adapt(input_test)
229
        tests = list(iter(suite))
230
        self.assertEqual(2, len(tests))
231
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
232
        self.assertEqual(tests[0].repository_format, formats[0][0])
233
        self.assertEqual(tests[0].transport_server, server1)
234
        self.assertEqual(tests[0].transport_readonly_server, server2)
235
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
236
        self.assertEqual(tests[1].repository_format, formats[1][0])
237
        self.assertEqual(tests[1].transport_server, server1)
238
        self.assertEqual(tests[1].transport_readonly_server, server2)
239
2018.5.64 by Robert Collins
Allow Repository tests to be backed onto a specific VFS as needed.
240
    def test_setting_vfs_transport(self):
241
        """The vfs_transport_factory can be set optionally."""
242
        from bzrlib.repository import RepositoryTestProviderAdapter
243
        input_test = TestRepositoryProviderAdapter(
244
            "test_adapted_tests")
245
        formats = [("c", "C")]
246
        adapter = RepositoryTestProviderAdapter(None, None, formats,
247
            vfs_transport_factory="vfs")
248
        suite = adapter.adapt(input_test)
249
        tests = list(iter(suite))
250
        self.assertEqual(1, len(tests))
251
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
252
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
253
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
254
class TestInterRepositoryProviderAdapter(TestCase):
255
    """A group of tests that test the InterRepository test adapter."""
256
257
    def test_adapted_tests(self):
258
        # check that constructor parameters are passed through to the adapted
259
        # test.
260
        from bzrlib.repository import InterRepositoryTestProviderAdapter
261
        input_test = TestInterRepositoryProviderAdapter(
262
            "test_adapted_tests")
263
        server1 = "a"
264
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
265
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
266
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
267
        suite = adapter.adapt(input_test)
268
        tests = list(iter(suite))
269
        self.assertEqual(2, len(tests))
270
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
271
        self.assertEqual(tests[0].repository_format, formats[0][1])
272
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
273
        self.assertEqual(tests[0].transport_server, server1)
274
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
275
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
276
        self.assertEqual(tests[1].repository_format, formats[1][1])
277
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
278
        self.assertEqual(tests[1].transport_server, server1)
279
        self.assertEqual(tests[1].transport_readonly_server, server2)
280
281
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
282
class TestInterVersionedFileProviderAdapter(TestCase):
283
    """A group of tests that test the InterVersionedFile test adapter."""
284
285
    def test_adapted_tests(self):
286
        # check that constructor parameters are passed through to the adapted
287
        # test.
288
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
289
        input_test = TestInterRepositoryProviderAdapter(
290
            "test_adapted_tests")
291
        server1 = "a"
292
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
293
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
294
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
295
        suite = adapter.adapt(input_test)
296
        tests = list(iter(suite))
297
        self.assertEqual(2, len(tests))
298
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
299
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
300
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
301
        self.assertEqual(tests[0].transport_server, server1)
302
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
303
        self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
304
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
305
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
306
        self.assertEqual(tests[1].transport_server, server1)
307
        self.assertEqual(tests[1].transport_readonly_server, server2)
308
309
1563.2.20 by Robert Collins
Add a revision store test adapter.
310
class TestRevisionStoreProviderAdapter(TestCase):
311
    """A group of tests that test the RevisionStore test adapter."""
312
313
    def test_adapted_tests(self):
314
        # check that constructor parameters are passed through to the adapted
315
        # test.
316
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
317
        input_test = TestRevisionStoreProviderAdapter(
318
            "test_adapted_tests")
319
        # revision stores need a store factory - i.e. RevisionKnit
320
        #, a readonly and rw transport 
321
        # transport servers:
322
        server1 = "a"
323
        server2 = "b"
324
        store_factories = ["c", "d"]
325
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
326
        suite = adapter.adapt(input_test)
327
        tests = list(iter(suite))
328
        self.assertEqual(2, len(tests))
329
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
330
        self.assertEqual(tests[0].transport_server, server1)
331
        self.assertEqual(tests[0].transport_readonly_server, server2)
332
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
333
        self.assertEqual(tests[1].transport_server, server1)
334
        self.assertEqual(tests[1].transport_readonly_server, server2)
335
336
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
337
class TestWorkingTreeProviderAdapter(TestCase):
338
    """A group of tests that test the workingtree implementation test adapter."""
339
340
    def test_adapted_tests(self):
341
        # check that constructor parameters are passed through to the adapted
342
        # test.
343
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
344
        input_test = TestWorkingTreeProviderAdapter(
345
            "test_adapted_tests")
346
        server1 = "a"
347
        server2 = "b"
348
        formats = [("c", "C"), ("d", "D")]
349
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
350
        suite = adapter.adapt(input_test)
351
        tests = list(iter(suite))
352
        self.assertEqual(2, len(tests))
353
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
354
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
355
        self.assertEqual(tests[0].transport_server, server1)
356
        self.assertEqual(tests[0].transport_readonly_server, server2)
357
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
358
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
359
        self.assertEqual(tests[1].transport_server, server1)
360
        self.assertEqual(tests[1].transport_readonly_server, server2)
361
362
1852.6.1 by Robert Collins
Start tree implementation tests.
363
class TestTreeProviderAdapter(TestCase):
364
    """Test the setup of tree_implementation tests."""
365
366
    def test_adapted_tests(self):
367
        # the tree implementation adapter is meant to setup one instance for
368
        # each working tree format, and one additional instance that will
369
        # use the default wt format, but create a revision tree for the tests.
370
        # this means that the wt ones should have the workingtree_to_test_tree
371
        # attribute set to 'return_parameter' and the revision one set to
372
        # revision_tree_from_workingtree.
373
374
        from bzrlib.tests.tree_implementations import (
375
            TreeTestProviderAdapter,
376
            return_parameter,
377
            revision_tree_from_workingtree
378
            )
2255.2.164 by Martin Pool
Change the default format for some tests from AB1 back to WorkingTreeFormat3
379
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
1852.6.1 by Robert Collins
Start tree implementation tests.
380
        input_test = TestTreeProviderAdapter(
381
            "test_adapted_tests")
382
        server1 = "a"
383
        server2 = "b"
384
        formats = [("c", "C"), ("d", "D")]
385
        adapter = TreeTestProviderAdapter(server1, server2, formats)
386
        suite = adapter.adapt(input_test)
387
        tests = list(iter(suite))
2255.6.3 by Aaron Bentley
tweak tests
388
        self.assertEqual(4, len(tests))
2255.2.164 by Martin Pool
Change the default format for some tests from AB1 back to WorkingTreeFormat3
389
        # this must match the default format setp up in
390
        # TreeTestProviderAdapter.adapt
391
        default_format = WorkingTreeFormat3
1852.6.1 by Robert Collins
Start tree implementation tests.
392
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
393
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
394
        self.assertEqual(tests[0].transport_server, server1)
395
        self.assertEqual(tests[0].transport_readonly_server, server2)
396
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
397
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
398
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
399
        self.assertEqual(tests[1].transport_server, server1)
400
        self.assertEqual(tests[1].transport_readonly_server, server2)
401
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
402
        self.assertIsInstance(tests[2].workingtree_format, default_format)
403
        #self.assertEqual(tests[2].bzrdir_format,
404
        #                 default_format._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
405
        self.assertEqual(tests[2].transport_server, server1)
406
        self.assertEqual(tests[2].transport_readonly_server, server2)
407
        self.assertEqual(tests[2].workingtree_to_test_tree,
408
            revision_tree_from_workingtree)
409
410
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
411
class TestInterTreeProviderAdapter(TestCase):
412
    """A group of tests that test the InterTreeTestAdapter."""
413
414
    def test_adapted_tests(self):
415
        # check that constructor parameters are passed through to the adapted
416
        # test.
417
        # for InterTree tests we want the machinery to bring up two trees in
418
        # each instance: the base one, and the one we are interacting with.
419
        # because each optimiser can be direction specific, we need to test
420
        # each optimiser in its chosen direction.
421
        # unlike the TestProviderAdapter we dont want to automatically add a
422
        # parameterised one for WorkingTree - the optimisers will tell us what
423
        # ones to add.
424
        from bzrlib.tests.tree_implementations import (
425
            return_parameter,
426
            revision_tree_from_workingtree
427
            )
428
        from bzrlib.tests.intertree_implementations import (
429
            InterTreeTestProviderAdapter,
430
            )
431
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
432
        input_test = TestInterTreeProviderAdapter(
433
            "test_adapted_tests")
434
        server1 = "a"
435
        server2 = "b"
436
        format1 = WorkingTreeFormat2()
437
        format2 = WorkingTreeFormat3()
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
438
        formats = [(str, format1, format2, "converter1"),
439
            (int, format2, format1, "converter2")]
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
440
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
441
        suite = adapter.adapt(input_test)
442
        tests = list(iter(suite))
443
        self.assertEqual(2, len(tests))
444
        self.assertEqual(tests[0].intertree_class, formats[0][0])
445
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
446
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
447
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
448
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
449
        self.assertEqual(tests[0].transport_server, server1)
450
        self.assertEqual(tests[0].transport_readonly_server, server2)
451
        self.assertEqual(tests[1].intertree_class, formats[1][0])
452
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
453
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
454
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
455
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
456
        self.assertEqual(tests[1].transport_server, server1)
457
        self.assertEqual(tests[1].transport_readonly_server, server2)
458
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
459
460
class TestTestCaseInTempDir(TestCaseInTempDir):
461
462
    def test_home_is_not_working(self):
463
        self.assertNotEqual(self.test_dir, self.test_home_dir)
464
        cwd = osutils.getcwd()
1987.1.4 by John Arbash Meinel
fix the home_is_not_working test
465
        self.assertEqual(self.test_dir, cwd)
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
466
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
467
468
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
469
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
470
471
    def test_home_is_non_existant_dir_under_root(self):
472
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
473
474
        This is because TestCaseWithMemoryTransport is for tests that do not
475
        need any disk resources: they should be hooked into bzrlib in such a 
476
        way that no global settings are being changed by the test (only a 
477
        few tests should need to do that), and having a missing dir as home is
478
        an effective way to ensure that this is the case.
479
        """
480
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
481
            self.test_home_dir)
482
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
483
        
484
    def test_cwd_is_TEST_ROOT(self):
485
        self.assertEqual(self.test_dir, self.TEST_ROOT)
486
        cwd = osutils.getcwd()
487
        self.assertEqual(self.test_dir, cwd)
488
489
    def test_make_branch_and_memory_tree(self):
490
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
491
492
        This is hard to comprehensively robustly test, so we settle for making
493
        a branch and checking no directory was created at its relpath.
494
        """
495
        tree = self.make_branch_and_memory_tree('dir')
2227.2.2 by v.ladeuil+lp at free
Cleanup.
496
        # Guard against regression into MemoryTransport leaking
497
        # files to disk instead of keeping them in memory.
498
        self.failIf(osutils.lexists('dir'))
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
499
        self.assertIsInstance(tree, memorytree.MemoryTree)
500
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
501
    def test_make_branch_and_memory_tree_with_format(self):
502
        """make_branch_and_memory_tree should accept a format option."""
503
        format = bzrdir.BzrDirMetaFormat1()
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
504
        format.repository_format = weaverepo.RepositoryFormat7()
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
505
        tree = self.make_branch_and_memory_tree('dir', format=format)
2227.2.2 by v.ladeuil+lp at free
Cleanup.
506
        # Guard against regression into MemoryTransport leaking
507
        # files to disk instead of keeping them in memory.
508
        self.failIf(osutils.lexists('dir'))
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
509
        self.assertIsInstance(tree, memorytree.MemoryTree)
510
        self.assertEqual(format.repository_format.__class__,
511
            tree.branch.repository._format.__class__)
512
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
513
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
514
class TestTestCaseWithTransport(TestCaseWithTransport):
515
    """Tests for the convenience functions TestCaseWithTransport introduces."""
516
517
    def test_get_readonly_url_none(self):
518
        from bzrlib.transport import get_transport
519
        from bzrlib.transport.memory import MemoryServer
520
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
521
        self.vfs_transport_factory = MemoryServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
522
        self.transport_readonly_server = None
523
        # calling get_readonly_transport() constructs a decorator on the url
524
        # for the server
525
        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.
526
        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.
527
        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.
528
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
529
        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.
530
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
531
        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.
532
533
    def test_get_readonly_url_http(self):
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
534
        from bzrlib.tests.HttpServer import HttpServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
535
        from bzrlib.transport import get_transport
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
536
        from bzrlib.transport.local import LocalURLServer
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
537
        from bzrlib.transport.http import HttpTransportBase
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
538
        self.transport_server = LocalURLServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
539
        self.transport_readonly_server = HttpServer
540
        # calling get_readonly_transport() gives us a HTTP server instance.
541
        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.
542
        url2 = self.get_readonly_url('foo/bar')
1540.3.6 by Martin Pool
[merge] update from bzr.dev
543
        # the transport returned may be any HttpTransportBase subclass
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
544
        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.
545
        t2 = get_transport(url2)
1540.3.6 by Martin Pool
[merge] update from bzr.dev
546
        self.failUnless(isinstance(t, HttpTransportBase))
547
        self.failUnless(isinstance(t2, HttpTransportBase))
1534.4.11 by Robert Collins
Convert test_open_containing from being a Remote test to being the more accurate Chrooted test.
548
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
549
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
550
    def test_is_directory(self):
551
        """Test assertIsDirectory assertion"""
552
        t = self.get_transport()
553
        self.build_tree(['a_dir/', 'a_file'], transport=t)
554
        self.assertIsDirectory('a_dir', t)
555
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
556
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
1534.4.31 by Robert Collins
cleanedup test_outside_wt
557
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
558
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
559
class TestTestCaseTransports(TestCaseWithTransport):
560
561
    def setUp(self):
562
        super(TestTestCaseTransports, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
563
        self.vfs_transport_factory = MemoryServer
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
564
565
    def test_make_bzrdir_preserves_transport(self):
566
        t = self.get_transport()
567
        result_bzrdir = self.make_bzrdir('subdir')
568
        self.assertIsInstance(result_bzrdir.transport, 
569
                              MemoryTransport)
570
        # should not be on disk, should only be in memory
571
        self.failIfExists('subdir')
572
573
1534.4.31 by Robert Collins
cleanedup test_outside_wt
574
class TestChrootedTest(ChrootedTestCase):
575
576
    def test_root_is_root(self):
577
        from bzrlib.transport import get_transport
578
        t = get_transport(self.get_readonly_url())
579
        url = t.base
580
        self.assertEqual(url, t.clone('..').base)
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
581
582
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
583
class MockProgress(_BaseProgressBar):
584
    """Progress-bar standin that records calls.
585
586
    Useful for testing pb using code.
587
    """
588
589
    def __init__(self):
590
        _BaseProgressBar.__init__(self)
591
        self.calls = []
592
593
    def tick(self):
594
        self.calls.append(('tick',))
595
596
    def update(self, msg=None, current=None, total=None):
597
        self.calls.append(('update', msg, current, total))
598
599
    def clear(self):
600
        self.calls.append(('clear',))
601
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
602
    def note(self, msg, *args):
603
        self.calls.append(('note', msg, args))
604
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
605
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
606
class TestTestResult(TestCase):
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
607
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
608
    def test_elapsed_time_with_benchmarking(self):
2095.4.1 by Martin Pool
Better progress bars during tests
609
        result = bzrlib.tests.TextTestResult(self._log_file,
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
610
                                        descriptions=0,
611
                                        verbosity=1,
612
                                        )
613
        result._recordTestStartTime()
614
        time.sleep(0.003)
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
615
        result.extractBenchmarkTime(self)
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
616
        timed_string = result._testTimeString()
617
        # without explicit benchmarking, we should get a simple time.
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
618
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
619
        # if a benchmark time is given, we want a x of y style result.
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
620
        self.time(time.sleep, 0.001)
621
        result.extractBenchmarkTime(self)
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
622
        timed_string = result._testTimeString()
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
623
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
624
        # extracting the time from a non-bzrlib testcase sets to None
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
625
        result._recordTestStartTime()
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
626
        result.extractBenchmarkTime(
627
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
628
        timed_string = result._testTimeString()
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
629
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
630
        # cheat. Yes, wash thy mouth out with soap.
631
        self._benchtime = None
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
632
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
633
    def test_assigned_benchmark_file_stores_date(self):
634
        output = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
635
        result = bzrlib.tests.TextTestResult(self._log_file,
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
636
                                        descriptions=0,
637
                                        verbosity=1,
638
                                        bench_history=output
639
                                        )
640
        output_string = output.getvalue()
2095.4.1 by Martin Pool
Better progress bars during tests
641
        
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
642
        # if you are wondering about the regexp please read the comment in
643
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
1951.1.2 by Andrew Bennetts
Relax test_assigned_benchmark_file_stores_date's regexp the same way we relaxed test_bench_history's.
644
        # XXX: what comment?  -- Andrew Bennetts
645
        self.assertContainsRe(output_string, "--date [0-9.]+")
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
646
647
    def test_benchhistory_records_test_times(self):
648
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
649
        result = bzrlib.tests.TextTestResult(
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
650
            self._log_file,
651
            descriptions=0,
652
            verbosity=1,
653
            bench_history=result_stream
654
            )
655
656
        # we want profile a call and check that its test duration is recorded
657
        # make a new test instance that when run will generate a benchmark
658
        example_test_case = TestTestResult("_time_hello_world_encoding")
659
        # execute the test, which should succeed and record times
660
        example_test_case.run(result)
661
        lines = result_stream.getvalue().splitlines()
662
        self.assertEqual(2, len(lines))
663
        self.assertContainsRe(lines[1],
664
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
665
            "._time_hello_world_encoding")
666
 
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
667
    def _time_hello_world_encoding(self):
668
        """Profile two sleep calls
669
        
670
        This is used to exercise the test framework.
671
        """
672
        self.time(unicode, 'hello', errors='replace')
673
        self.time(unicode, 'world', errors='replace')
674
675
    def test_lsprofiling(self):
676
        """Verbose test result prints lsprof statistics from test cases."""
677
        try:
678
            import bzrlib.lsprof
679
        except ImportError:
680
            raise TestSkipped("lsprof not installed.")
681
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
682
        result = bzrlib.tests.VerboseTestResult(
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
683
            unittest._WritelnDecorator(result_stream),
684
            descriptions=0,
685
            verbosity=2,
686
            )
687
        # we want profile a call of some sort and check it is output by
688
        # addSuccess. We dont care about addError or addFailure as they
689
        # are not that interesting for performance tuning.
690
        # make a new test instance that when run will generate a profile
691
        example_test_case = TestTestResult("_time_hello_world_encoding")
692
        example_test_case._gather_lsprof_in_benchmarks = True
693
        # execute the test, which should succeed and record profiles
694
        example_test_case.run(result)
695
        # lsprofile_something()
696
        # if this worked we want 
697
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
698
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
699
        # (the lsprof header)
700
        # ... an arbitrary number of lines
701
        # and the function call which is time.sleep.
702
        #           1        0            ???         ???       ???(sleep) 
703
        # and then repeated but with 'world', rather than 'hello'.
704
        # this should appear in the output stream of our test result.
1831.2.1 by Martin Pool
[trivial] Simplify & fix up lsprof blackbox test
705
        output = result_stream.getvalue()
706
        self.assertContainsRe(output,
707
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
708
        self.assertContainsRe(output,
709
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
710
        self.assertContainsRe(output,
711
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
712
        self.assertContainsRe(output,
713
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
714
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
715
    def test_known_failure(self):
716
        """A KnownFailure being raised should trigger several result actions."""
717
        class InstrumentedTestResult(ExtendedTestResult):
718
719
            def report_test_start(self, test): pass
720
            def report_known_failure(self, test, err):
721
                self._call = test, err
722
        result = InstrumentedTestResult(None, None, None, None)
723
        def test_function():
724
            raise KnownFailure('failed!')
725
        test = unittest.FunctionTestCase(test_function)
726
        test.run(result)
727
        # it should invoke 'report_known_failure'.
728
        self.assertEqual(2, len(result._call))
729
        self.assertEqual(test, result._call[0])
730
        self.assertEqual(KnownFailure, result._call[1][0])
731
        self.assertIsInstance(result._call[1][1], KnownFailure)
732
        # we dont introspec the traceback, if the rest is ok, it would be
733
        # exceptional for it not to be.
734
        # it should update the known_failure_count on the object.
735
        self.assertEqual(1, result.known_failure_count)
736
        # the result should be successful.
737
        self.assertTrue(result.wasSuccessful())
738
739
    def test_verbose_report_known_failure(self):
740
        # verbose test output formatting
741
        result_stream = StringIO()
742
        result = bzrlib.tests.VerboseTestResult(
743
            unittest._WritelnDecorator(result_stream),
744
            descriptions=0,
745
            verbosity=2,
746
            )
747
        test = self.get_passing_test()
748
        result.startTest(test)
749
        result.extractBenchmarkTime(test)
750
        prefix = len(result_stream.getvalue())
751
        # the err parameter has the shape:
752
        # (class, exception object, traceback)
753
        # KnownFailures dont get their tracebacks shown though, so we
754
        # can skip that.
755
        err = (KnownFailure, KnownFailure('foo'), None)
756
        result.report_known_failure(test, err)
757
        output = result_stream.getvalue()[prefix:]
758
        lines = output.splitlines()
759
        self.assertEqual(lines, ['XFAIL                   0ms', '    foo'])
760
    
761
    def test_text_report_known_failure(self):
762
        # text test output formatting
763
        pb = MockProgress()
764
        result = bzrlib.tests.TextTestResult(
765
            None,
766
            descriptions=0,
767
            verbosity=1,
768
            pb=pb,
769
            )
770
        test = self.get_passing_test()
771
        # this seeds the state to handle reporting the test.
772
        result.startTest(test)
773
        result.extractBenchmarkTime(test)
774
        # the err parameter has the shape:
775
        # (class, exception object, traceback)
776
        # KnownFailures dont get their tracebacks shown though, so we
777
        # can skip that.
778
        err = (KnownFailure, KnownFailure('foo'), None)
779
        result.report_known_failure(test, err)
780
        self.assertEqual(
781
            [
782
            ('update', '[1 in 0s] passing_test', None, None),
783
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
784
            ],
785
            pb.calls)
786
        # known_failures should be printed in the summary, so if we run a test
787
        # after there are some known failures, the update prefix should match
788
        # this.
789
        result.known_failure_count = 3
790
        test.run(result)
791
        self.assertEqual(
792
            [
793
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
794
            ],
795
            pb.calls[2:])
796
797
    def get_passing_test(self):
798
        """Return a test object that can't be run usefully."""
799
        def passing_test():
800
            pass
801
        return unittest.FunctionTestCase(passing_test)
802
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
803
    def test_add_not_supported(self):
804
        """Test the behaviour of invoking addNotSupported."""
805
        class InstrumentedTestResult(ExtendedTestResult):
806
            def report_test_start(self, test): pass
807
            def report_unsupported(self, test, feature):
808
                self._call = test, feature
809
        result = InstrumentedTestResult(None, None, None, None)
810
        test = SampleTestCase('_test_pass')
811
        feature = Feature()
812
        result.startTest(test)
813
        result.addNotSupported(test, feature)
814
        # it should invoke 'report_unsupported'.
815
        self.assertEqual(2, len(result._call))
816
        self.assertEqual(test, result._call[0])
817
        self.assertEqual(feature, result._call[1])
818
        # the result should be successful.
819
        self.assertTrue(result.wasSuccessful())
820
        # it should record the test against a count of tests not run due to
821
        # this feature.
822
        self.assertEqual(1, result.unsupported['Feature'])
823
        # and invoking it again should increment that counter
824
        result.addNotSupported(test, feature)
825
        self.assertEqual(2, result.unsupported['Feature'])
826
827
    def test_verbose_report_unsupported(self):
828
        # verbose test output formatting
829
        result_stream = StringIO()
830
        result = bzrlib.tests.VerboseTestResult(
831
            unittest._WritelnDecorator(result_stream),
832
            descriptions=0,
833
            verbosity=2,
834
            )
835
        test = self.get_passing_test()
836
        feature = Feature()
837
        result.startTest(test)
838
        result.extractBenchmarkTime(test)
839
        prefix = len(result_stream.getvalue())
840
        result.report_unsupported(test, feature)
841
        output = result_stream.getvalue()[prefix:]
842
        lines = output.splitlines()
843
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
844
    
845
    def test_text_report_unsupported(self):
846
        # text test output formatting
847
        pb = MockProgress()
848
        result = bzrlib.tests.TextTestResult(
849
            None,
850
            descriptions=0,
851
            verbosity=1,
852
            pb=pb,
853
            )
854
        test = self.get_passing_test()
855
        feature = Feature()
856
        # this seeds the state to handle reporting the test.
857
        result.startTest(test)
858
        result.extractBenchmarkTime(test)
859
        result.report_unsupported(test, feature)
860
        # no output on unsupported features
861
        self.assertEqual(
862
            [('update', '[1 in 0s] passing_test', None, None)
863
            ],
864
            pb.calls)
865
        # the number of missing features should be printed in the progress
866
        # summary, so check for that.
867
        result.unsupported = {'foo':0, 'bar':0}
868
        test.run(result)
869
        self.assertEqual(
870
            [
871
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
872
            ],
873
            pb.calls[1:])
874
    
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
875
    def test_unavailable_exception(self):
876
        """An UnavailableFeature being raised should invoke addNotSupported."""
877
        class InstrumentedTestResult(ExtendedTestResult):
878
879
            def report_test_start(self, test): pass
880
            def addNotSupported(self, test, feature):
881
                self._call = test, feature
882
        result = InstrumentedTestResult(None, None, None, None)
883
        feature = Feature()
884
        def test_function():
885
            raise UnavailableFeature(feature)
886
        test = unittest.FunctionTestCase(test_function)
887
        test.run(result)
888
        # it should invoke 'addNotSupported'.
889
        self.assertEqual(2, len(result._call))
890
        self.assertEqual(test, result._call[0])
891
        self.assertEqual(feature, result._call[1])
892
        # and not count as an error
893
        self.assertEqual(0, result.error_count)
894
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
895
896
class TestRunner(TestCase):
897
898
    def dummy_test(self):
899
        pass
900
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
901
    def run_test_runner(self, testrunner, test):
902
        """Run suite in testrunner, saving global state and restoring it.
903
904
        This current saves and restores:
905
        TestCaseInTempDir.TEST_ROOT
906
        
907
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
908
        without using this convenience method, because of our use of global state.
909
        """
910
        old_root = TestCaseInTempDir.TEST_ROOT
911
        try:
912
            TestCaseInTempDir.TEST_ROOT = None
913
            return testrunner.run(test)
914
        finally:
915
            TestCaseInTempDir.TEST_ROOT = old_root
916
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
917
    def test_known_failure_failed_run(self):
918
        # run a test that generates a known failure which should be printed in
919
        # the final output when real failures occur.
920
        def known_failure_test():
921
            raise KnownFailure('failed')
922
        test = unittest.TestSuite()
923
        test.addTest(unittest.FunctionTestCase(known_failure_test))
924
        def failing_test():
925
            raise AssertionError('foo')
926
        test.addTest(unittest.FunctionTestCase(failing_test))
927
        stream = StringIO()
928
        runner = TextTestRunner(stream=stream)
929
        result = self.run_test_runner(runner, test)
930
        lines = stream.getvalue().splitlines()
931
        self.assertEqual([
932
            '',
933
            '======================================================================',
934
            'FAIL: unittest.FunctionTestCase (failing_test)',
935
            '----------------------------------------------------------------------',
936
            'Traceback (most recent call last):',
937
            '    raise AssertionError(\'foo\')',
938
            'AssertionError: foo',
939
            '',
940
            '----------------------------------------------------------------------',
941
            '',
942
            'FAILED (failures=1, known_failure_count=1)'],
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
943
            lines[0:5] + lines[6:10] + lines[11:])
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
944
945
    def test_known_failure_ok_run(self):
946
        # run a test that generates a known failure which should be printed in the final output.
947
        def known_failure_test():
948
            raise KnownFailure('failed')
949
        test = unittest.FunctionTestCase(known_failure_test)
950
        stream = StringIO()
951
        runner = TextTestRunner(stream=stream)
952
        result = self.run_test_runner(runner, test)
953
        self.assertEqual(
954
            '\n'
955
            '----------------------------------------------------------------------\n'
956
            'Ran 1 test in 0.000s\n'
957
            '\n'
958
            'OK (known_failures=1)\n',
959
            stream.getvalue())
960
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
961
    def test_skipped_test(self):
962
        # run a test that is skipped, and check the suite as a whole still
963
        # succeeds.
964
        # skipping_test must be hidden in here so it's not run as a real test
965
        def skipping_test():
966
            raise TestSkipped('test intentionally skipped')
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
967
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
968
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
969
        test = unittest.FunctionTestCase(skipping_test)
970
        result = self.run_test_runner(runner, test)
971
        self.assertTrue(result.wasSuccessful())
972
973
    def test_skipped_from_setup(self):
974
        class SkippedSetupTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
975
976
            def setUp(self):
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
977
                self.counter = 1
978
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
979
                raise TestSkipped('skipped setup')
980
981
            def test_skip(self):
982
                self.fail('test reached')
983
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
984
            def cleanup(self):
985
                self.counter -= 1
986
987
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
988
        test = SkippedSetupTest('test_skip')
989
        result = self.run_test_runner(runner, test)
990
        self.assertTrue(result.wasSuccessful())
991
        # Check if cleanup was called the right number of times.
992
        self.assertEqual(0, test.counter)
993
994
    def test_skipped_from_test(self):
995
        class SkippedTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
996
997
            def setUp(self):
998
                self.counter = 1
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
999
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1000
1001
            def test_skip(self):
1002
                raise TestSkipped('skipped test')
1003
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1004
            def cleanup(self):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1005
                self.counter -= 1
1006
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1007
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1008
        test = SkippedTest('test_skip')
1009
        result = self.run_test_runner(runner, test)
1010
        self.assertTrue(result.wasSuccessful())
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1011
        # Check if cleanup was called the right number of times.
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1012
        self.assertEqual(0, test.counter)
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1013
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1014
    def test_unsupported_features_listed(self):
1015
        """When unsupported features are encountered they are detailed."""
1016
        class Feature1(Feature):
1017
            def _probe(self): return False
1018
        class Feature2(Feature):
1019
            def _probe(self): return False
1020
        # create sample tests
1021
        test1 = SampleTestCase('_test_pass')
1022
        test1._test_needs_features = [Feature1()]
1023
        test2 = SampleTestCase('_test_pass')
1024
        test2._test_needs_features = [Feature2()]
1025
        test = unittest.TestSuite()
1026
        test.addTest(test1)
1027
        test.addTest(test2)
1028
        stream = StringIO()
1029
        runner = TextTestRunner(stream=stream)
1030
        result = self.run_test_runner(runner, test)
1031
        lines = stream.getvalue().splitlines()
1032
        self.assertEqual([
1033
            'OK',
1034
            "Missing feature 'Feature1' skipped 1 tests.",
1035
            "Missing feature 'Feature2' skipped 1 tests.",
1036
            ],
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1037
            lines[-3:])
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1038
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1039
    def test_bench_history(self):
1951.1.1 by Andrew Bennetts
Make test_bench_history and _get_bzr_source_tree tolerant of UnknownFormatError for the bzr workingtree.
1040
        # tests that the running the benchmark produces a history file
1041
        # containing a timestamp and the revision id of the bzrlib source which
1042
        # was tested.
1043
        workingtree = _get_bzr_source_tree()
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1044
        test = TestRunner('dummy_test')
1045
        output = StringIO()
1046
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
1047
        result = self.run_test_runner(runner, test)
1048
        output_string = output.getvalue()
1951.1.1 by Andrew Bennetts
Make test_bench_history and _get_bzr_source_tree tolerant of UnknownFormatError for the bzr workingtree.
1049
        self.assertContainsRe(output_string, "--date [0-9.]+")
1050
        if workingtree is not None:
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
1051
            revision_id = workingtree.get_parent_ids()[0]
1951.1.1 by Andrew Bennetts
Make test_bench_history and _get_bzr_source_tree tolerant of UnknownFormatError for the bzr workingtree.
1052
            self.assertEndsWith(output_string.rstrip(), revision_id)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1053
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
1054
    def test_success_log_deleted(self):
1055
        """Successful tests have their log deleted"""
1056
1057
        class LogTester(TestCase):
1058
1059
            def test_success(self):
1060
                self.log('this will be removed\n')
1061
1062
        sio = cStringIO.StringIO()
1063
        runner = TextTestRunner(stream=sio)
1064
        test = LogTester('test_success')
1065
        result = self.run_test_runner(runner, test)
1066
1067
        log = test._get_log()
1068
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1069
        self.assertEqual('', test._log_contents)
1070
        self.assertIs(None, test._log_file_name)
1071
1072
    def test_fail_log_kept(self):
1073
        """Failed tests have their log kept"""
1074
1075
        class LogTester(TestCase):
1076
1077
            def test_fail(self):
1078
                self.log('this will be kept\n')
1079
                self.fail('this test fails')
1080
1081
        sio = cStringIO.StringIO()
1082
        runner = TextTestRunner(stream=sio)
1083
        test = LogTester('test_fail')
1084
        result = self.run_test_runner(runner, test)
1085
1086
        text = sio.getvalue()
1087
        self.assertContainsRe(text, 'this will be kept')
1088
        self.assertContainsRe(text, 'this test fails')
1089
1090
        log = test._get_log()
1091
        self.assertContainsRe(log, 'this will be kept')
1092
        self.assertEqual(log, test._log_contents)
1093
1094
    def test_error_log_kept(self):
1095
        """Tests with errors have their log kept"""
1096
1097
        class LogTester(TestCase):
1098
1099
            def test_error(self):
1100
                self.log('this will be kept\n')
1101
                raise ValueError('random exception raised')
1102
1103
        sio = cStringIO.StringIO()
1104
        runner = TextTestRunner(stream=sio)
1105
        test = LogTester('test_error')
1106
        result = self.run_test_runner(runner, test)
1107
1108
        text = sio.getvalue()
1109
        self.assertContainsRe(text, 'this will be kept')
1110
        self.assertContainsRe(text, 'random exception raised')
1111
1112
        log = test._get_log()
1113
        self.assertContainsRe(log, 'this will be kept')
1114
        self.assertEqual(log, test._log_contents)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1115
2036.1.2 by John Arbash Meinel
whitespace fix
1116
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1117
class SampleTestCase(TestCase):
1118
1119
    def _test_pass(self):
1120
        pass
1121
1122
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1123
class TestTestCase(TestCase):
1124
    """Tests that test the core bzrlib TestCase."""
1125
1126
    def inner_test(self):
1127
        # the inner child test
1128
        note("inner_test")
1129
1130
    def outer_child(self):
1131
        # the outer child test
1132
        note("outer_start")
1133
        self.inner_test = TestTestCase("inner_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1134
        result = bzrlib.tests.TextTestResult(self._log_file,
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1135
                                        descriptions=0,
1136
                                        verbosity=1)
1137
        self.inner_test.run(result)
1138
        note("outer finish")
1139
1140
    def test_trace_nesting(self):
1141
        # this tests that each test case nests its trace facility correctly.
1142
        # we do this by running a test case manually. That test case (A)
1143
        # should setup a new log, log content to it, setup a child case (B),
1144
        # which should log independently, then case (A) should log a trailer
1145
        # and return.
1146
        # we do two nested children so that we can verify the state of the 
1147
        # logs after the outer child finishes is correct, which a bad clean
1148
        # up routine in tearDown might trigger a fault in our test with only
1149
        # one child, we should instead see the bad result inside our test with
1150
        # the two children.
1151
        # the outer child test
1152
        original_trace = bzrlib.trace._trace_file
1153
        outer_test = TestTestCase("outer_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1154
        result = bzrlib.tests.TextTestResult(self._log_file,
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1155
                                        descriptions=0,
1156
                                        verbosity=1)
1157
        outer_test.run(result)
1158
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
1159
1160
    def method_that_times_a_bit_twice(self):
1161
        # call self.time twice to ensure it aggregates
1713.1.4 by Robert Collins
Make the test test_time_creates_benchmark_in_result more robust to timing variation.
1162
        self.time(time.sleep, 0.007)
1163
        self.time(time.sleep, 0.007)
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
1164
1165
    def test_time_creates_benchmark_in_result(self):
1166
        """Test that the TestCase.time() method accumulates a benchmark time."""
1167
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1168
        output_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
1169
        result = bzrlib.tests.VerboseTestResult(
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
1170
            unittest._WritelnDecorator(output_stream),
1171
            descriptions=0,
2095.4.1 by Martin Pool
Better progress bars during tests
1172
            verbosity=2,
1173
            num_tests=sample_test.countTestCases())
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
1174
        sample_test.run(result)
1175
        self.assertContainsRe(
1176
            output_stream.getvalue(),
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
1177
            r"\d+ms/ +\d+ms\n$")
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1178
1179
    def test_hooks_sanitised(self):
1180
        """The bzrlib hooks should be sanitised by setUp."""
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
1181
        self.assertEqual(bzrlib.branch.BranchHooks(),
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1182
            bzrlib.branch.Branch.hooks)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
1183
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1184
            bzrlib.smart.server.SmartTCPServer.hooks)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1185
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
1186
    def test__gather_lsprof_in_benchmarks(self):
1187
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1188
        
1189
        Each self.time() call is individually and separately profiled.
1190
        """
1191
        try:
1192
            import bzrlib.lsprof
1193
        except ImportError:
1194
            raise TestSkipped("lsprof not installed.")
1195
        # overrides the class member with an instance member so no cleanup 
1196
        # needed.
1197
        self._gather_lsprof_in_benchmarks = True
1198
        self.time(time.sleep, 0.000)
1199
        self.time(time.sleep, 0.003)
1200
        self.assertEqual(2, len(self._benchcalls))
1201
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1202
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1203
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1204
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1205
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1206
    def test_knownFailure(self):
1207
        """Self.knownFailure() should raise a KnownFailure exception."""
1208
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1209
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1210
    def test_requireFeature_available(self):
1211
        """self.requireFeature(available) is a no-op."""
1212
        class Available(Feature):
1213
            def _probe(self):return True
1214
        feature = Available()
1215
        self.requireFeature(feature)
1216
1217
    def test_requireFeature_unavailable(self):
1218
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1219
        class Unavailable(Feature):
1220
            def _probe(self):return False
1221
        feature = Unavailable()
1222
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1223
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1224
    def test_run_no_parameters(self):
1225
        test = SampleTestCase('_test_pass')
1226
        test.run()
1227
    
1228
    def test_run_enabled_unittest_result(self):
1229
        """Test we revert to regular behaviour when the test is enabled."""
1230
        test = SampleTestCase('_test_pass')
1231
        class EnabledFeature(object):
1232
            def available(self):
1233
                return True
1234
        test._test_needs_features = [EnabledFeature()]
1235
        result = unittest.TestResult()
1236
        test.run(result)
1237
        self.assertEqual(1, result.testsRun)
1238
        self.assertEqual([], result.errors)
1239
        self.assertEqual([], result.failures)
1240
1241
    def test_run_disabled_unittest_result(self):
1242
        """Test our compatability for disabled tests with unittest results."""
1243
        test = SampleTestCase('_test_pass')
1244
        class DisabledFeature(object):
1245
            def available(self):
1246
                return False
1247
        test._test_needs_features = [DisabledFeature()]
1248
        result = unittest.TestResult()
1249
        test.run(result)
1250
        self.assertEqual(1, result.testsRun)
1251
        self.assertEqual([], result.errors)
1252
        self.assertEqual([], result.failures)
1253
1254
    def test_run_disabled_supporting_result(self):
1255
        """Test disabled tests behaviour with support aware results."""
1256
        test = SampleTestCase('_test_pass')
1257
        class DisabledFeature(object):
1258
            def available(self):
1259
                return False
1260
        the_feature = DisabledFeature()
1261
        test._test_needs_features = [the_feature]
1262
        class InstrumentedTestResult(unittest.TestResult):
1263
            def __init__(self):
1264
                unittest.TestResult.__init__(self)
1265
                self.calls = []
1266
            def startTest(self, test):
1267
                self.calls.append(('startTest', test))
1268
            def stopTest(self, test):
1269
                self.calls.append(('stopTest', test))
1270
            def addNotSupported(self, test, feature):
1271
                self.calls.append(('addNotSupported', test, feature))
1272
        result = InstrumentedTestResult()
1273
        test.run(result)
1274
        self.assertEqual([
1275
            ('startTest', test),
1276
            ('addNotSupported', test, the_feature),
1277
            ('stopTest', test),
1278
            ],
1279
            result.calls)
1280
1534.11.4 by Robert Collins
Merge from mainline.
1281
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1282
@symbol_versioning.deprecated_function(zero_eleven)
1283
def sample_deprecated_function():
1284
    """A deprecated function to test applyDeprecated with."""
1285
    return 2
1286
1287
1288
def sample_undeprecated_function(a_param):
1289
    """A undeprecated function to test applyDeprecated with."""
1290
1291
1292
class ApplyDeprecatedHelper(object):
1293
    """A helper class for ApplyDeprecated tests."""
1294
1295
    @symbol_versioning.deprecated_method(zero_eleven)
1296
    def sample_deprecated_method(self, param_one):
1297
        """A deprecated method for testing with."""
1298
        return param_one
1299
1300
    def sample_normal_method(self):
1301
        """A undeprecated method."""
1302
1303
    @symbol_versioning.deprecated_method(zero_ten)
1304
    def sample_nested_deprecation(self):
1305
        return sample_deprecated_function()
1306
1307
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
1308
class TestExtraAssertions(TestCase):
1309
    """Tests for new test assertions in bzrlib test suite"""
1310
1311
    def test_assert_isinstance(self):
1312
        self.assertIsInstance(2, int)
1313
        self.assertIsInstance(u'', basestring)
1314
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1315
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1316
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
1317
    def test_assertEndsWith(self):
1318
        self.assertEndsWith('foo', 'oo')
1319
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1320
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1321
    def test_applyDeprecated_not_deprecated(self):
1322
        sample_object = ApplyDeprecatedHelper()
1323
        # calling an undeprecated callable raises an assertion
1324
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1325
            sample_object.sample_normal_method)
1326
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1327
            sample_undeprecated_function, "a param value")
1328
        # calling a deprecated callable (function or method) with the wrong
1329
        # expected deprecation fails.
1330
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1331
            sample_object.sample_deprecated_method, "a param value")
1332
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1333
            sample_deprecated_function)
1334
        # calling a deprecated callable (function or method) with the right
1335
        # expected deprecation returns the functions result.
1336
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1337
            sample_object.sample_deprecated_method, "a param value"))
1338
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
1339
            sample_deprecated_function))
1340
        # calling a nested deprecation with the wrong deprecation version
1341
        # fails even if a deeper nested function was deprecated with the 
1342
        # supplied version.
1343
        self.assertRaises(AssertionError, self.applyDeprecated,
1344
            zero_eleven, sample_object.sample_nested_deprecation)
1345
        # calling a nested deprecation with the right deprecation value
1346
        # returns the calls result.
1347
        self.assertEqual(2, self.applyDeprecated(zero_ten,
1348
            sample_object.sample_nested_deprecation))
1349
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1350
    def test_callDeprecated(self):
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1351
        def testfunc(be_deprecated, result=None):
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1352
            if be_deprecated is True:
1353
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
1354
                                       stacklevel=1)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1355
            return result
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1356
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1357
        self.assertIs(None, result)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1358
        result = self.callDeprecated([], testfunc, False, 'result')
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1359
        self.assertEqual('result', result)
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1360
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1361
        self.callDeprecated([], testfunc, be_deprecated=False)
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1362
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1363
1364
class TestConvenienceMakers(TestCaseWithTransport):
1365
    """Test for the make_* convenience functions."""
1366
1367
    def test_make_branch_and_tree_with_format(self):
1368
        # we should be able to supply a format to make_branch_and_tree
1369
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1370
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1371
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1372
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1373
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1374
                              bzrlib.bzrdir.BzrDirFormat6)
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1375
1986.2.1 by Robert Collins
Bugfix - the name of the test for make_branch_and_memory_tree was wrong.
1376
    def test_make_branch_and_memory_tree(self):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
1377
        # we should be able to get a new branch and a mutable tree from
1378
        # TestCaseWithTransport
1379
        tree = self.make_branch_and_memory_tree('a')
1380
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1381
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1382
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
1383
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1384
1385
    def test_make_tree_for_sftp_branch(self):
1386
        """Transports backed by local directories create local trees."""
1387
1388
        tree = self.make_branch_and_tree('t1')
1389
        base = tree.bzrdir.root_transport.base
1390
        self.failIf(base.startswith('sftp'),
1391
                'base %r is on sftp but should be local' % base)
1392
        self.assertEquals(tree.bzrdir.root_transport,
1393
                tree.branch.bzrdir.root_transport)
1394
        self.assertEquals(tree.bzrdir.root_transport,
1395
                tree.branch.repository.bzrdir.root_transport)
1396
1397
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1398
class TestSelftest(TestCase):
1399
    """Tests of bzrlib.tests.selftest."""
1400
1401
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1402
        factory_called = []
1403
        def factory():
1404
            factory_called.append(True)
1405
            return TestSuite()
1406
        out = StringIO()
1407
        err = StringIO()
1408
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1409
            test_suite_factory=factory)
1410
        self.assertEqual([True], factory_called)
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1411
1412
1413
class TestSelftestCleanOutput(TestCaseInTempDir):
1414
1415
    def test_clean_output(self):
1416
        # test functionality of clean_selftest_output()
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
1417
        self.build_tree(['test0000.tmp/', 'test0001.tmp/',
1418
                         'bzrlib/', 'tests/',
1419
                         'bzr', 'setup.py', 'test9999.tmp'])
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1420
1421
        root = os.getcwdu()
1422
        before = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1423
        before.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1424
        self.assertEquals(['bzr','bzrlib','setup.py',
1425
                           'test0000.tmp','test0001.tmp',
1426
                           'test9999.tmp','tests'],
1427
                           before)
1428
        clean_selftest_output(root, quiet=True)
1429
        after = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1430
        after.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1431
        self.assertEquals(['bzr','bzrlib','setup.py',
1432
                           'test9999.tmp','tests'],
1433
                           after)
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1434
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
1435
    def test_clean_readonly(self):
1436
        # test for delete read-only files
1437
        self.build_tree(['test0000.tmp/', 'test0000.tmp/foo'])
1438
        osutils.make_readonly('test0000.tmp/foo')
1439
        root = os.getcwdu()
1440
        before = os.listdir(root);  before.sort()
1441
        self.assertEquals(['test0000.tmp'], before)
1442
        clean_selftest_output(root, quiet=True)
1443
        after = os.listdir(root);   after.sort()
1444
        self.assertEquals([], after)
1445
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1446
1447
class TestKnownFailure(TestCase):
1448
1449
    def test_known_failure(self):
1450
        """Check that KnownFailure is defined appropriately."""
1451
        # a KnownFailure is an assertion error for compatability with unaware
1452
        # runners.
1453
        self.assertIsInstance(KnownFailure(""), AssertionError)
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1454
1551.13.9 by Aaron Bentley
Implement TestCase.expectFailure
1455
    def test_expect_failure(self):
1456
        try:
1457
            self.expectFailure("Doomed to failure", self.assertTrue, False)
1458
        except KnownFailure, e:
1459
            self.assertEqual('Doomed to failure', e.args[0])
1460
        try:
1461
            self.expectFailure("Doomed to failure", self.assertTrue, True)
1462
        except AssertionError, e:
1463
            self.assertEqual('Unexpected success.  Should have failed:'
1464
                             ' Doomed to failure', e.args[0])
1465
        else:
1466
            self.fail('Assertion not raised')
1467
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1468
1469
class TestFeature(TestCase):
1470
1471
    def test_caching(self):
1472
        """Feature._probe is called by the feature at most once."""
1473
        class InstrumentedFeature(Feature):
1474
            def __init__(self):
1475
                Feature.__init__(self)
1476
                self.calls = []
1477
            def _probe(self):
1478
                self.calls.append('_probe')
1479
                return False
1480
        feature = InstrumentedFeature()
1481
        feature.available()
1482
        self.assertEqual(['_probe'], feature.calls)
1483
        feature.available()
1484
        self.assertEqual(['_probe'], feature.calls)
1485
1486
    def test_named_str(self):
1487
        """Feature.__str__ should thunk to feature_name()."""
1488
        class NamedFeature(Feature):
1489
            def feature_name(self):
1490
                return 'symlinks'
1491
        feature = NamedFeature()
1492
        self.assertEqual('symlinks', str(feature))
1493
1494
    def test_default_str(self):
1495
        """Feature.__str__ should default to __class__.__name__."""
1496
        class NamedFeature(Feature):
1497
            pass
1498
        feature = NamedFeature()
1499
        self.assertEqual('NamedFeature', str(feature))
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1500
1501
1502
class TestUnavailableFeature(TestCase):
1503
1504
    def test_access_feature(self):
1505
        feature = Feature()
1506
        exception = UnavailableFeature(feature)
1507
        self.assertIs(feature, exception.args[0])