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