/brz/remove-bazaar

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