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