~bzr-pqm/bzr/bzr.dev

1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
1
# Copyright (C) 2005, 2006 by 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
4
# it under the terms of the GNU General Public License version 2 as published by
5
# the Free Software Foundation.
6
#
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
# GNU General Public License for more details.
11
#
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
16
"""Tests for the test framework."""
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
17
18
import os
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
19
from StringIO import StringIO
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
20
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).
21
import time
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
22
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.
23
import warnings
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
24
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
25
from bzrlib import osutils
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.
26
import bzrlib
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
27
from bzrlib.progress import _BaseProgressBar
1526.1.3 by Robert Collins
Merge from upstream.
28
from bzrlib.tests import (
1534.4.31 by Robert Collins
cleanedup test_outside_wt
29
                          ChrootedTestCase,
1526.1.3 by Robert Collins
Merge from upstream.
30
                          TestCase,
31
                          TestCaseInTempDir,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
32
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
33
                          TestSkipped,
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
34
                          TestSuite,
1526.1.3 by Robert Collins
Merge from upstream.
35
                          TextTestRunner,
36
                          )
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
37
from bzrlib.tests.TestUtil import _load_module_by_name
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.
38
import bzrlib.errors as errors
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
39
from bzrlib import symbol_versioning
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
40
from bzrlib.trace import note
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
41
42
43
class SelftestTests(TestCase):
44
45
    def test_import_tests(self):
46
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
47
        self.assertEqual(mod.SelftestTests, SelftestTests)
48
49
    def test_import_test_failure(self):
50
        self.assertRaises(ImportError,
51
                          _load_module_by_name,
52
                          'bzrlib.no-name-yet')
53
54
55
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.
56
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
57
    def test_logging(self):
58
        """Test logs are captured when a test fails."""
59
        self.log('a test message')
60
        self._log_file.flush()
61
        self.assertContainsRe(self._get_log(), 'a test message\n')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
62
63
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
64
class TestTreeShape(TestCaseInTempDir):
65
66
    def test_unicode_paths(self):
67
        filename = u'hell\u00d8'
1526.1.4 by Robert Collins
forgot my self.
68
        try:
69
            self.build_tree_contents([(filename, 'contents of hello')])
70
        except UnicodeEncodeError:
71
            raise TestSkipped("can't build unicode working tree in "
72
                "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.
73
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
74
75
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
76
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
77
    """A group of tests that test the transport implementation adaption core.
78
1551.1.1 by Martin Pool
[merge] branch-formats branch, and reconcile changes
79
    This is a meta test that the tests are applied to all available 
80
    transports.
81
1530.1.21 by Robert Collins
Review feedback fixes.
82
    This will be generalised in the future which is why it is in this 
83
    test file even though it is specific to transport tests at the moment.
84
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
85
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.
86
    def test_get_transport_permutations(self):
1530.1.21 by Robert Collins
Review feedback fixes.
87
        # this checks that we the module get_test_permutations call
88
        # 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.
89
        class MockModule(object):
90
            def get_test_permutations(self):
91
                return sample_permutation
92
        sample_permutation = [(1,2), (3,4)]
93
        from bzrlib.transport import TransportTestProviderAdapter
94
        adapter = TransportTestProviderAdapter()
95
        self.assertEqual(sample_permutation,
96
                         adapter.get_transport_test_permutations(MockModule()))
97
98
    def test_adapter_checks_all_modules(self):
1530.1.21 by Robert Collins
Review feedback fixes.
99
        # this checks that the adapter returns as many permurtations as
100
        # there are in all the registered# transport modules for there
101
        # - we assume if this matches its probably doing the right thing
102
        # especially in combination with the tests for setting the right
103
        # 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.
104
        from bzrlib.transport import (TransportTestProviderAdapter,
105
                                      _get_transport_modules
106
                                      )
107
        modules = _get_transport_modules()
108
        permutation_count = 0
109
        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.
110
            try:
111
                permutation_count += len(reduce(getattr, 
112
                    (module + ".get_test_permutations").split('.')[1:],
113
                     __import__(module))())
114
            except errors.DependencyNotPresent:
115
                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.
116
        input_test = TestTransportProviderAdapter(
117
            "test_adapter_sets_transport_class")
118
        adapter = TransportTestProviderAdapter()
119
        self.assertEqual(permutation_count,
120
                         len(list(iter(adapter.adapt(input_test)))))
121
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
122
    def test_adapter_sets_transport_class(self):
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
123
        # Check that the test adapter inserts a transport and server into the
124
        # generated test.
125
        #
126
        # This test used to know about all the possible transports and the
127
        # order they were returned but that seems overly brittle (mbp
128
        # 20060307)
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
129
        input_test = TestTransportProviderAdapter(
130
            "test_adapter_sets_transport_class")
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
131
        from bzrlib.transport import TransportTestProviderAdapter
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
132
        suite = TransportTestProviderAdapter().adapt(input_test)
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
133
        tests = list(iter(suite))
134
        self.assertTrue(len(tests) > 6)
135
        # there are at least that many builtin transports
136
        one_test = tests[0]
137
        self.assertTrue(issubclass(one_test.transport_class, 
138
                                   bzrlib.transport.Transport))
139
        self.assertTrue(issubclass(one_test.transport_server, 
140
                                   bzrlib.transport.Server))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
141
142
143
class TestBranchProviderAdapter(TestCase):
144
    """A group of tests that test the branch implementation test adapter."""
145
146
    def test_adapted_tests(self):
147
        # check that constructor parameters are passed through to the adapted
148
        # test.
149
        from bzrlib.branch import BranchTestProviderAdapter
150
        input_test = TestBranchProviderAdapter(
151
            "test_adapted_tests")
152
        server1 = "a"
153
        server2 = "b"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
154
        formats = [("c", "C"), ("d", "D")]
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
155
        adapter = BranchTestProviderAdapter(server1, server2, formats)
156
        suite = adapter.adapt(input_test)
157
        tests = list(iter(suite))
158
        self.assertEqual(2, len(tests))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
159
        self.assertEqual(tests[0].branch_format, formats[0][0])
160
        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.
161
        self.assertEqual(tests[0].transport_server, server1)
162
        self.assertEqual(tests[0].transport_readonly_server, server2)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
163
        self.assertEqual(tests[1].branch_format, formats[1][0])
164
        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.
165
        self.assertEqual(tests[1].transport_server, server1)
166
        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.
167
168
1534.4.39 by Robert Collins
Basic BzrDir support.
169
class TestBzrDirProviderAdapter(TestCase):
170
    """A group of tests that test the bzr dir implementation test adapter."""
171
172
    def test_adapted_tests(self):
173
        # check that constructor parameters are passed through to the adapted
174
        # test.
175
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
176
        input_test = TestBzrDirProviderAdapter(
177
            "test_adapted_tests")
178
        server1 = "a"
179
        server2 = "b"
180
        formats = ["c", "d"]
181
        adapter = BzrDirTestProviderAdapter(server1, server2, formats)
182
        suite = adapter.adapt(input_test)
183
        tests = list(iter(suite))
184
        self.assertEqual(2, len(tests))
185
        self.assertEqual(tests[0].bzrdir_format, formats[0])
186
        self.assertEqual(tests[0].transport_server, server1)
187
        self.assertEqual(tests[0].transport_readonly_server, server2)
188
        self.assertEqual(tests[1].bzrdir_format, formats[1])
189
        self.assertEqual(tests[1].transport_server, server1)
190
        self.assertEqual(tests[1].transport_readonly_server, server2)
191
192
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
193
class TestRepositoryProviderAdapter(TestCase):
194
    """A group of tests that test the repository implementation test adapter."""
195
196
    def test_adapted_tests(self):
197
        # check that constructor parameters are passed through to the adapted
198
        # test.
199
        from bzrlib.repository import RepositoryTestProviderAdapter
200
        input_test = TestRepositoryProviderAdapter(
201
            "test_adapted_tests")
202
        server1 = "a"
203
        server2 = "b"
204
        formats = [("c", "C"), ("d", "D")]
205
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
206
        suite = adapter.adapt(input_test)
207
        tests = list(iter(suite))
208
        self.assertEqual(2, len(tests))
209
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
210
        self.assertEqual(tests[0].repository_format, formats[0][0])
211
        self.assertEqual(tests[0].transport_server, server1)
212
        self.assertEqual(tests[0].transport_readonly_server, server2)
213
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
214
        self.assertEqual(tests[1].repository_format, formats[1][0])
215
        self.assertEqual(tests[1].transport_server, server1)
216
        self.assertEqual(tests[1].transport_readonly_server, server2)
217
218
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
219
class TestInterRepositoryProviderAdapter(TestCase):
220
    """A group of tests that test the InterRepository test adapter."""
221
222
    def test_adapted_tests(self):
223
        # check that constructor parameters are passed through to the adapted
224
        # test.
225
        from bzrlib.repository import InterRepositoryTestProviderAdapter
226
        input_test = TestInterRepositoryProviderAdapter(
227
            "test_adapted_tests")
228
        server1 = "a"
229
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
230
        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.
231
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
232
        suite = adapter.adapt(input_test)
233
        tests = list(iter(suite))
234
        self.assertEqual(2, len(tests))
235
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
236
        self.assertEqual(tests[0].repository_format, formats[0][1])
237
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
238
        self.assertEqual(tests[0].transport_server, server1)
239
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
240
        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.
241
        self.assertEqual(tests[1].repository_format, formats[1][1])
242
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
243
        self.assertEqual(tests[1].transport_server, server1)
244
        self.assertEqual(tests[1].transport_readonly_server, server2)
245
246
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.
247
class TestInterVersionedFileProviderAdapter(TestCase):
248
    """A group of tests that test the InterVersionedFile test adapter."""
249
250
    def test_adapted_tests(self):
251
        # check that constructor parameters are passed through to the adapted
252
        # test.
253
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
254
        input_test = TestInterRepositoryProviderAdapter(
255
            "test_adapted_tests")
256
        server1 = "a"
257
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
258
        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.
259
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
260
        suite = adapter.adapt(input_test)
261
        tests = list(iter(suite))
262
        self.assertEqual(2, len(tests))
263
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
264
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
265
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
266
        self.assertEqual(tests[0].transport_server, server1)
267
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
268
        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.
269
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
270
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
271
        self.assertEqual(tests[1].transport_server, server1)
272
        self.assertEqual(tests[1].transport_readonly_server, server2)
273
274
1563.2.20 by Robert Collins
Add a revision store test adapter.
275
class TestRevisionStoreProviderAdapter(TestCase):
276
    """A group of tests that test the RevisionStore test adapter."""
277
278
    def test_adapted_tests(self):
279
        # check that constructor parameters are passed through to the adapted
280
        # test.
281
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
282
        input_test = TestRevisionStoreProviderAdapter(
283
            "test_adapted_tests")
284
        # revision stores need a store factory - i.e. RevisionKnit
285
        #, a readonly and rw transport 
286
        # transport servers:
287
        server1 = "a"
288
        server2 = "b"
289
        store_factories = ["c", "d"]
290
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
291
        suite = adapter.adapt(input_test)
292
        tests = list(iter(suite))
293
        self.assertEqual(2, len(tests))
294
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
295
        self.assertEqual(tests[0].transport_server, server1)
296
        self.assertEqual(tests[0].transport_readonly_server, server2)
297
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
298
        self.assertEqual(tests[1].transport_server, server1)
299
        self.assertEqual(tests[1].transport_readonly_server, server2)
300
301
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
302
class TestWorkingTreeProviderAdapter(TestCase):
303
    """A group of tests that test the workingtree implementation test adapter."""
304
305
    def test_adapted_tests(self):
306
        # check that constructor parameters are passed through to the adapted
307
        # test.
308
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
309
        input_test = TestWorkingTreeProviderAdapter(
310
            "test_adapted_tests")
311
        server1 = "a"
312
        server2 = "b"
313
        formats = [("c", "C"), ("d", "D")]
314
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
315
        suite = adapter.adapt(input_test)
316
        tests = list(iter(suite))
317
        self.assertEqual(2, len(tests))
318
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
319
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
320
        self.assertEqual(tests[0].transport_server, server1)
321
        self.assertEqual(tests[0].transport_readonly_server, server2)
322
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
323
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
324
        self.assertEqual(tests[1].transport_server, server1)
325
        self.assertEqual(tests[1].transport_readonly_server, server2)
326
327
1852.6.1 by Robert Collins
Start tree implementation tests.
328
class TestTreeProviderAdapter(TestCase):
329
    """Test the setup of tree_implementation tests."""
330
331
    def test_adapted_tests(self):
332
        # the tree implementation adapter is meant to setup one instance for
333
        # each working tree format, and one additional instance that will
334
        # use the default wt format, but create a revision tree for the tests.
335
        # this means that the wt ones should have the workingtree_to_test_tree
336
        # attribute set to 'return_parameter' and the revision one set to
337
        # revision_tree_from_workingtree.
338
339
        from bzrlib.tests.tree_implementations import (
340
            TreeTestProviderAdapter,
341
            return_parameter,
342
            revision_tree_from_workingtree
343
            )
344
        from bzrlib.workingtree import WorkingTreeFormat
345
        input_test = TestTreeProviderAdapter(
346
            "test_adapted_tests")
347
        server1 = "a"
348
        server2 = "b"
349
        formats = [("c", "C"), ("d", "D")]
350
        adapter = TreeTestProviderAdapter(server1, server2, formats)
351
        suite = adapter.adapt(input_test)
352
        tests = list(iter(suite))
353
        self.assertEqual(3, len(tests))
354
        default_format = WorkingTreeFormat.get_default_format()
355
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
356
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
357
        self.assertEqual(tests[0].transport_server, server1)
358
        self.assertEqual(tests[0].transport_readonly_server, server2)
359
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
360
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
361
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
362
        self.assertEqual(tests[1].transport_server, server1)
363
        self.assertEqual(tests[1].transport_readonly_server, server2)
364
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
365
        self.assertEqual(tests[2].workingtree_format, default_format)
366
        self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
367
        self.assertEqual(tests[2].transport_server, server1)
368
        self.assertEqual(tests[2].transport_readonly_server, server2)
369
        self.assertEqual(tests[2].workingtree_to_test_tree,
370
            revision_tree_from_workingtree)
371
372
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
373
class TestInterTreeProviderAdapter(TestCase):
374
    """A group of tests that test the InterTreeTestAdapter."""
375
376
    def test_adapted_tests(self):
377
        # check that constructor parameters are passed through to the adapted
378
        # test.
379
        # for InterTree tests we want the machinery to bring up two trees in
380
        # each instance: the base one, and the one we are interacting with.
381
        # because each optimiser can be direction specific, we need to test
382
        # each optimiser in its chosen direction.
383
        # unlike the TestProviderAdapter we dont want to automatically add a
384
        # parameterised one for WorkingTree - the optimisers will tell us what
385
        # ones to add.
386
        from bzrlib.tests.tree_implementations import (
387
            return_parameter,
388
            revision_tree_from_workingtree
389
            )
390
        from bzrlib.tests.intertree_implementations import (
391
            InterTreeTestProviderAdapter,
392
            )
393
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
394
        input_test = TestInterTreeProviderAdapter(
395
            "test_adapted_tests")
396
        server1 = "a"
397
        server2 = "b"
398
        format1 = WorkingTreeFormat2()
399
        format2 = WorkingTreeFormat3()
400
        formats = [(str, format1, format2, False, True),
401
            (int, format2, format1, False, True)]
402
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
403
        suite = adapter.adapt(input_test)
404
        tests = list(iter(suite))
405
        self.assertEqual(2, len(tests))
406
        self.assertEqual(tests[0].intertree_class, formats[0][0])
407
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
408
        self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
409
        self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
410
        self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
411
        self.assertEqual(tests[0].transport_server, server1)
412
        self.assertEqual(tests[0].transport_readonly_server, server2)
413
        self.assertEqual(tests[1].intertree_class, formats[1][0])
414
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
415
        self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
416
        self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
417
        self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
418
        self.assertEqual(tests[1].transport_server, server1)
419
        self.assertEqual(tests[1].transport_readonly_server, server2)
420
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
421
class TestTestCaseWithTransport(TestCaseWithTransport):
422
    """Tests for the convenience functions TestCaseWithTransport introduces."""
423
424
    def test_get_readonly_url_none(self):
425
        from bzrlib.transport import get_transport
426
        from bzrlib.transport.memory import MemoryServer
427
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
428
        self.transport_server = MemoryServer
429
        self.transport_readonly_server = None
430
        # calling get_readonly_transport() constructs a decorator on the url
431
        # for the server
432
        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.
433
        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.
434
        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.
435
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
436
        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.
437
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
438
        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.
439
440
    def test_get_readonly_url_http(self):
441
        from bzrlib.transport import get_transport
442
        from bzrlib.transport.local import LocalRelpathServer
1540.3.6 by Martin Pool
[merge] update from bzr.dev
443
        from bzrlib.transport.http import HttpServer, HttpTransportBase
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
444
        self.transport_server = LocalRelpathServer
445
        self.transport_readonly_server = HttpServer
446
        # calling get_readonly_transport() gives us a HTTP server instance.
447
        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.
448
        url2 = self.get_readonly_url('foo/bar')
1540.3.6 by Martin Pool
[merge] update from bzr.dev
449
        # 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.
450
        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.
451
        t2 = get_transport(url2)
1540.3.6 by Martin Pool
[merge] update from bzr.dev
452
        self.failUnless(isinstance(t, HttpTransportBase))
453
        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.
454
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
455
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
456
    def test_is_directory(self):
457
        """Test assertIsDirectory assertion"""
458
        t = self.get_transport()
459
        self.build_tree(['a_dir/', 'a_file'], transport=t)
460
        self.assertIsDirectory('a_dir', t)
461
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
462
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
1534.4.31 by Robert Collins
cleanedup test_outside_wt
463
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
464
1534.4.31 by Robert Collins
cleanedup test_outside_wt
465
class TestChrootedTest(ChrootedTestCase):
466
467
    def test_root_is_root(self):
468
        from bzrlib.transport import get_transport
469
        t = get_transport(self.get_readonly_url())
470
        url = t.base
471
        self.assertEqual(url, t.clone('..').base)
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
472
473
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
474
class MockProgress(_BaseProgressBar):
475
    """Progress-bar standin that records calls.
476
477
    Useful for testing pb using code.
478
    """
479
480
    def __init__(self):
481
        _BaseProgressBar.__init__(self)
482
        self.calls = []
483
484
    def tick(self):
485
        self.calls.append(('tick',))
486
487
    def update(self, msg=None, current=None, total=None):
488
        self.calls.append(('update', msg, current, total))
489
490
    def clear(self):
491
        self.calls.append(('clear',))
492
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
493
    def note(self, msg, *args):
494
        self.calls.append(('note', msg, args))
495
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
496
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
497
class TestTestResult(TestCase):
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
498
499
    def test_progress_bar_style_quiet(self):
500
        # test using a progress bar.
1728.1.2 by Robert Collins
Bugfix missing search-and-replace of TestResult.
501
        dummy_test = TestTestResult('test_progress_bar_style_quiet')
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
502
        dummy_error = (Exception, None, [])
503
        mypb = MockProgress()
504
        mypb.update('Running tests', 0, 4)
505
        last_calls = mypb.calls[:]
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
506
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
507
        result = bzrlib.tests._MyResult(self._log_file,
508
                                        descriptions=0,
509
                                        verbosity=1,
510
                                        pb=mypb)
511
        self.assertEqual(last_calls, mypb.calls)
512
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
513
        def shorten(s):
514
            """Shorten a string based on the terminal width"""
515
            return result._ellipsise_unimportant_words(s,
516
                                 osutils.terminal_width())
517
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
518
        # an error 
519
        result.startTest(dummy_test)
1534.11.3 by Robert Collins
Show test names and status in the progress bar.
520
        # starting a test prints the test name
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
521
        last_calls += [('update', '...tyle_quiet', 0, None)]
522
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
523
        result.addError(dummy_test, dummy_error)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
524
        last_calls += [('update', 'ERROR        ', 1, None),
525
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
526
                      ]
527
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
528
529
        # a failure
530
        result.startTest(dummy_test)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
531
        last_calls += [('update', '...tyle_quiet', 1, None)]
532
        self.assertEqual(last_calls, mypb.calls)
533
        last_calls += [('update', 'FAIL         ', 2, None),
534
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
535
                      ]
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
536
        result.addFailure(dummy_test, dummy_error)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
537
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
538
539
        # a success
540
        result.startTest(dummy_test)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
541
        last_calls += [('update', '...tyle_quiet', 2, None)]
542
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
543
        result.addSuccess(dummy_test)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
544
        last_calls += [('update', 'OK           ', 3, None)]
545
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
546
547
        # a skip
548
        result.startTest(dummy_test)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
549
        last_calls += [('update', '...tyle_quiet', 3, None)]
550
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
551
        result.addSkipped(dummy_test, dummy_error)
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
552
        last_calls += [('update', 'SKIP         ', 4, None)]
553
        self.assertEqual(last_calls, mypb.calls)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
554
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
555
    def test_elapsed_time_with_benchmarking(self):
556
        result = bzrlib.tests._MyResult(self._log_file,
557
                                        descriptions=0,
558
                                        verbosity=1,
559
                                        )
560
        result._recordTestStartTime()
561
        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)
562
        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).
563
        timed_string = result._testTimeString()
564
        # without explicit benchmarking, we should get a simple time.
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)
565
        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).
566
        # 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)
567
        self.time(time.sleep, 0.001)
568
        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).
569
        timed_string = result._testTimeString()
1740.2.5 by Aaron Bentley
Merge from bzr.dev
570
        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)
571
        # 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).
572
        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)
573
        result.extractBenchmarkTime(
574
            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).
575
        timed_string = result._testTimeString()
1740.2.5 by Aaron Bentley
Merge from bzr.dev
576
        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)
577
        # cheat. Yes, wash thy mouth out with soap.
578
        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).
579
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
580
    def test_assigned_benchmark_file_stores_date(self):
581
        output = StringIO()
582
        result = bzrlib.tests._MyResult(self._log_file,
583
                                        descriptions=0,
584
                                        verbosity=1,
585
                                        bench_history=output
586
                                        )
587
        output_string = output.getvalue()
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
588
        # if you are wondering about the regexp please read the comment in
589
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
590
        self.assertContainsRe(output_string, "--date [0-9.]+ \S")
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
591
592
    def test_benchhistory_records_test_times(self):
593
        result_stream = StringIO()
594
        result = bzrlib.tests._MyResult(
595
            self._log_file,
596
            descriptions=0,
597
            verbosity=1,
598
            bench_history=result_stream
599
            )
600
601
        # we want profile a call and check that its test duration is recorded
602
        # make a new test instance that when run will generate a benchmark
603
        example_test_case = TestTestResult("_time_hello_world_encoding")
604
        # execute the test, which should succeed and record times
605
        example_test_case.run(result)
606
        lines = result_stream.getvalue().splitlines()
607
        self.assertEqual(2, len(lines))
608
        self.assertContainsRe(lines[1],
609
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
610
            "._time_hello_world_encoding")
611
 
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
612
    def _time_hello_world_encoding(self):
613
        """Profile two sleep calls
614
        
615
        This is used to exercise the test framework.
616
        """
617
        self.time(unicode, 'hello', errors='replace')
618
        self.time(unicode, 'world', errors='replace')
619
620
    def test_lsprofiling(self):
621
        """Verbose test result prints lsprof statistics from test cases."""
622
        try:
623
            import bzrlib.lsprof
624
        except ImportError:
625
            raise TestSkipped("lsprof not installed.")
626
        result_stream = StringIO()
627
        result = bzrlib.tests._MyResult(
628
            unittest._WritelnDecorator(result_stream),
629
            descriptions=0,
630
            verbosity=2,
631
            )
632
        # we want profile a call of some sort and check it is output by
633
        # addSuccess. We dont care about addError or addFailure as they
634
        # are not that interesting for performance tuning.
635
        # make a new test instance that when run will generate a profile
636
        example_test_case = TestTestResult("_time_hello_world_encoding")
637
        example_test_case._gather_lsprof_in_benchmarks = True
638
        # execute the test, which should succeed and record profiles
639
        example_test_case.run(result)
640
        # lsprofile_something()
641
        # if this worked we want 
642
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
643
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
644
        # (the lsprof header)
645
        # ... an arbitrary number of lines
646
        # and the function call which is time.sleep.
647
        #           1        0            ???         ???       ???(sleep) 
648
        # and then repeated but with 'world', rather than 'hello'.
649
        # this should appear in the output stream of our test result.
1831.2.1 by Martin Pool
[trivial] Simplify & fix up lsprof blackbox test
650
        output = result_stream.getvalue()
651
        self.assertContainsRe(output,
652
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
653
        self.assertContainsRe(output,
654
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
655
        self.assertContainsRe(output,
656
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
657
        self.assertContainsRe(output,
658
            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
659
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
660
661
class TestRunner(TestCase):
662
663
    def dummy_test(self):
664
        pass
665
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
666
    def run_test_runner(self, testrunner, test):
667
        """Run suite in testrunner, saving global state and restoring it.
668
669
        This current saves and restores:
670
        TestCaseInTempDir.TEST_ROOT
671
        
672
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
673
        without using this convenience method, because of our use of global state.
674
        """
675
        old_root = TestCaseInTempDir.TEST_ROOT
676
        try:
677
            TestCaseInTempDir.TEST_ROOT = None
678
            return testrunner.run(test)
679
        finally:
680
            TestCaseInTempDir.TEST_ROOT = old_root
681
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
682
    def test_accepts_and_uses_pb_parameter(self):
683
        test = TestRunner('dummy_test')
684
        mypb = MockProgress()
685
        self.assertEqual([], mypb.calls)
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
686
        runner = TextTestRunner(stream=self._log_file, pb=mypb)
687
        result = self.run_test_runner(runner, test)
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
688
        self.assertEqual(1, result.testsRun)
689
        self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
1534.11.3 by Robert Collins
Show test names and status in the progress bar.
690
        self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
691
        self.assertEqual(('update', 'OK           ', 1, None), mypb.calls[2])
692
        self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
693
        self.assertEqual(('clear',), mypb.calls[4])
694
        self.assertEqual(5, len(mypb.calls))
1534.11.4 by Robert Collins
Merge from mainline.
695
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
696
    def test_skipped_test(self):
697
        # run a test that is skipped, and check the suite as a whole still
698
        # succeeds.
699
        # skipping_test must be hidden in here so it's not run as a real test
700
        def skipping_test():
701
            raise TestSkipped('test intentionally skipped')
702
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
703
        test = unittest.FunctionTestCase(skipping_test)
704
        result = self.run_test_runner(runner, test)
705
        self.assertTrue(result.wasSuccessful())
706
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
707
    def test_bench_history(self):
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
708
        import bzrlib.branch
709
        import bzrlib.revisionspec
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
710
        test = TestRunner('dummy_test')
711
        output = StringIO()
712
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
713
        result = self.run_test_runner(runner, test)
714
        output_string = output.getvalue()
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
715
        # does anyone know a good regexp for revision ids?
716
        # here we are using \S instead and checking the revision id afterwards
717
        self.assertContainsRe(output_string, "--date [0-9.]+ \S")
718
        branch = bzrlib.branch.Branch.open_containing('.')[0]
719
        revision_id = bzrlib.revisionspec.RevisionSpec(branch.revno()).in_history(branch).rev_id
720
        self.assert_(output_string.rstrip().endswith(revision_id))
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
721
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
722
723
class TestTestCase(TestCase):
724
    """Tests that test the core bzrlib TestCase."""
725
726
    def inner_test(self):
727
        # the inner child test
728
        note("inner_test")
729
730
    def outer_child(self):
731
        # the outer child test
732
        note("outer_start")
733
        self.inner_test = TestTestCase("inner_child")
734
        result = bzrlib.tests._MyResult(self._log_file,
735
                                        descriptions=0,
736
                                        verbosity=1)
737
        self.inner_test.run(result)
738
        note("outer finish")
739
740
    def test_trace_nesting(self):
741
        # this tests that each test case nests its trace facility correctly.
742
        # we do this by running a test case manually. That test case (A)
743
        # should setup a new log, log content to it, setup a child case (B),
744
        # which should log independently, then case (A) should log a trailer
745
        # and return.
746
        # we do two nested children so that we can verify the state of the 
747
        # logs after the outer child finishes is correct, which a bad clean
748
        # up routine in tearDown might trigger a fault in our test with only
749
        # one child, we should instead see the bad result inside our test with
750
        # the two children.
751
        # the outer child test
752
        original_trace = bzrlib.trace._trace_file
753
        outer_test = TestTestCase("outer_child")
754
        result = bzrlib.tests._MyResult(self._log_file,
755
                                        descriptions=0,
756
                                        verbosity=1)
757
        outer_test.run(result)
758
        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)
759
760
    def method_that_times_a_bit_twice(self):
761
        # 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.
762
        self.time(time.sleep, 0.007)
763
        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)
764
765
    def test_time_creates_benchmark_in_result(self):
766
        """Test that the TestCase.time() method accumulates a benchmark time."""
767
        sample_test = TestTestCase("method_that_times_a_bit_twice")
768
        output_stream = StringIO()
769
        result = bzrlib.tests._MyResult(
770
            unittest._WritelnDecorator(output_stream),
771
            descriptions=0,
772
            verbosity=2)
773
        sample_test.run(result)
774
        self.assertContainsRe(
775
            output_stream.getvalue(),
776
            "[1-9][0-9]ms/   [1-9][0-9]ms\n$")
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
777
        
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
778
    def test__gather_lsprof_in_benchmarks(self):
779
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
780
        
781
        Each self.time() call is individually and separately profiled.
782
        """
783
        try:
784
            import bzrlib.lsprof
785
        except ImportError:
786
            raise TestSkipped("lsprof not installed.")
787
        # overrides the class member with an instance member so no cleanup 
788
        # needed.
789
        self._gather_lsprof_in_benchmarks = True
790
        self.time(time.sleep, 0.000)
791
        self.time(time.sleep, 0.003)
792
        self.assertEqual(2, len(self._benchcalls))
793
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
794
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
795
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
796
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
797
1534.11.4 by Robert Collins
Merge from mainline.
798
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
799
class TestExtraAssertions(TestCase):
800
    """Tests for new test assertions in bzrlib test suite"""
801
802
    def test_assert_isinstance(self):
803
        self.assertIsInstance(2, int)
804
        self.assertIsInstance(u'', basestring)
805
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
806
        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
807
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
808
    def test_assertEndsWith(self):
809
        self.assertEndsWith('foo', 'oo')
810
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
811
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
812
    def test_assertDeprecated(self):
813
        def testfunc(be_deprecated):
814
            if be_deprecated is True:
815
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
816
                                       stacklevel=1)
817
        self.assertDeprecated(['i am deprecated'], testfunc, True)
818
        self.assertDeprecated([], testfunc, False)
819
        self.assertDeprecated(['i am deprecated'], testfunc, 
820
                              be_deprecated=True)
821
        self.assertDeprecated([], testfunc, be_deprecated=False)
822
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
823
824
class TestConvenienceMakers(TestCaseWithTransport):
825
    """Test for the make_* convenience functions."""
826
827
    def test_make_branch_and_tree_with_format(self):
828
        # we should be able to supply a format to make_branch_and_tree
829
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
830
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
831
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
832
                              bzrlib.bzrdir.BzrDirMetaFormat1)
833
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
834
                              bzrlib.bzrdir.BzrDirFormat6)
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
835
836
837
class TestSelftest(TestCase):
838
    """Tests of bzrlib.tests.selftest."""
839
840
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
841
        factory_called = []
842
        def factory():
843
            factory_called.append(True)
844
            return TestSuite()
845
        out = StringIO()
846
        err = StringIO()
847
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
848
            test_suite_factory=factory)
849
        self.assertEqual([True], factory_called)