/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
2
#
3
# This program is free software; you can redistribute it and/or modify
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
17
"""Tests for the test framework."""
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
18
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
19
import cStringIO
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
20
import os
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
21
from StringIO import StringIO
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
22
import sys
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
23
import time
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
24
import unittest
1185.62.24 by John Arbash Meinel
Changing the exception that sftp.py throws when it can't find paramiko, so that the test suite can handle it.
25
import warnings
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
26
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
27
import bzrlib
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
28
from bzrlib import (
29
    bzrdir,
30
    memorytree,
31
    osutils,
32
    repository,
33
    )
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
34
from bzrlib.progress import _BaseProgressBar
1526.1.3 by Robert Collins
Merge from upstream.
35
from bzrlib.tests import (
1534.4.31 by Robert Collins
cleanedup test_outside_wt
36
                          ChrootedTestCase,
1526.1.3 by Robert Collins
Merge from upstream.
37
                          TestCase,
38
                          TestCaseInTempDir,
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
39
                          TestCaseWithMemoryTransport,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
40
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
41
                          TestSkipped,
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
42
                          TestSuite,
1526.1.3 by Robert Collins
Merge from upstream.
43
                          TextTestRunner,
44
                          )
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
45
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
46
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.
47
import bzrlib.errors as errors
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
48
from bzrlib import symbol_versioning
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
49
from bzrlib.symbol_versioning import zero_ten, zero_eleven
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
50
from bzrlib.trace import note
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
51
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.
52
from bzrlib.version import _get_bzr_source_tree
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
53
54
55
class SelftestTests(TestCase):
56
57
    def test_import_tests(self):
58
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
59
        self.assertEqual(mod.SelftestTests, SelftestTests)
60
61
    def test_import_test_failure(self):
62
        self.assertRaises(ImportError,
63
                          _load_module_by_name,
64
                          'bzrlib.no-name-yet')
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")
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
190
        vfs_factory = "v"
1534.4.39 by Robert Collins
Basic BzrDir support.
191
        server1 = "a"
192
        server2 = "b"
193
        formats = ["c", "d"]
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
194
        adapter = BzrDirTestProviderAdapter(vfs_factory,
195
            server1, server2, formats)
1534.4.39 by Robert Collins
Basic BzrDir support.
196
        suite = adapter.adapt(input_test)
197
        tests = list(iter(suite))
198
        self.assertEqual(2, len(tests))
199
        self.assertEqual(tests[0].bzrdir_format, formats[0])
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
200
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
201
        self.assertEqual(tests[0].transport_server, server1)
202
        self.assertEqual(tests[0].transport_readonly_server, server2)
203
        self.assertEqual(tests[1].bzrdir_format, formats[1])
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
204
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
205
        self.assertEqual(tests[1].transport_server, server1)
206
        self.assertEqual(tests[1].transport_readonly_server, server2)
207
208
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
209
class TestRepositoryProviderAdapter(TestCase):
210
    """A group of tests that test the repository implementation test adapter."""
211
212
    def test_adapted_tests(self):
213
        # check that constructor parameters are passed through to the adapted
214
        # test.
215
        from bzrlib.repository import RepositoryTestProviderAdapter
216
        input_test = TestRepositoryProviderAdapter(
217
            "test_adapted_tests")
218
        server1 = "a"
219
        server2 = "b"
220
        formats = [("c", "C"), ("d", "D")]
221
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
222
        suite = adapter.adapt(input_test)
223
        tests = list(iter(suite))
224
        self.assertEqual(2, len(tests))
225
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
226
        self.assertEqual(tests[0].repository_format, formats[0][0])
227
        self.assertEqual(tests[0].transport_server, server1)
228
        self.assertEqual(tests[0].transport_readonly_server, server2)
229
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
230
        self.assertEqual(tests[1].repository_format, formats[1][0])
231
        self.assertEqual(tests[1].transport_server, server1)
232
        self.assertEqual(tests[1].transport_readonly_server, server2)
233
2018.5.64 by Robert Collins
Allow Repository tests to be backed onto a specific VFS as needed.
234
    def test_setting_vfs_transport(self):
235
        """The vfs_transport_factory can be set optionally."""
236
        from bzrlib.repository import RepositoryTestProviderAdapter
237
        input_test = TestRepositoryProviderAdapter(
238
            "test_adapted_tests")
239
        formats = [("c", "C")]
240
        adapter = RepositoryTestProviderAdapter(None, None, formats,
241
            vfs_transport_factory="vfs")
242
        suite = adapter.adapt(input_test)
243
        tests = list(iter(suite))
244
        self.assertEqual(1, len(tests))
245
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
246
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
247
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
248
class TestInterRepositoryProviderAdapter(TestCase):
249
    """A group of tests that test the InterRepository test adapter."""
250
251
    def test_adapted_tests(self):
252
        # check that constructor parameters are passed through to the adapted
253
        # test.
254
        from bzrlib.repository import InterRepositoryTestProviderAdapter
255
        input_test = TestInterRepositoryProviderAdapter(
256
            "test_adapted_tests")
257
        server1 = "a"
258
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
259
        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.
260
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
261
        suite = adapter.adapt(input_test)
262
        tests = list(iter(suite))
263
        self.assertEqual(2, len(tests))
264
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
265
        self.assertEqual(tests[0].repository_format, formats[0][1])
266
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
267
        self.assertEqual(tests[0].transport_server, server1)
268
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
269
        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.
270
        self.assertEqual(tests[1].repository_format, formats[1][1])
271
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
272
        self.assertEqual(tests[1].transport_server, server1)
273
        self.assertEqual(tests[1].transport_readonly_server, server2)
274
275
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.
276
class TestInterVersionedFileProviderAdapter(TestCase):
277
    """A group of tests that test the InterVersionedFile test adapter."""
278
279
    def test_adapted_tests(self):
280
        # check that constructor parameters are passed through to the adapted
281
        # test.
282
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
283
        input_test = TestInterRepositoryProviderAdapter(
284
            "test_adapted_tests")
285
        server1 = "a"
286
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
287
        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.
288
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
289
        suite = adapter.adapt(input_test)
290
        tests = list(iter(suite))
291
        self.assertEqual(2, len(tests))
292
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
293
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
294
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
295
        self.assertEqual(tests[0].transport_server, server1)
296
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
297
        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.
298
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
299
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
300
        self.assertEqual(tests[1].transport_server, server1)
301
        self.assertEqual(tests[1].transport_readonly_server, server2)
302
303
1563.2.20 by Robert Collins
Add a revision store test adapter.
304
class TestRevisionStoreProviderAdapter(TestCase):
305
    """A group of tests that test the RevisionStore test adapter."""
306
307
    def test_adapted_tests(self):
308
        # check that constructor parameters are passed through to the adapted
309
        # test.
310
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
311
        input_test = TestRevisionStoreProviderAdapter(
312
            "test_adapted_tests")
313
        # revision stores need a store factory - i.e. RevisionKnit
314
        #, a readonly and rw transport 
315
        # transport servers:
316
        server1 = "a"
317
        server2 = "b"
318
        store_factories = ["c", "d"]
319
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
320
        suite = adapter.adapt(input_test)
321
        tests = list(iter(suite))
322
        self.assertEqual(2, len(tests))
323
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
324
        self.assertEqual(tests[0].transport_server, server1)
325
        self.assertEqual(tests[0].transport_readonly_server, server2)
326
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
327
        self.assertEqual(tests[1].transport_server, server1)
328
        self.assertEqual(tests[1].transport_readonly_server, server2)
329
330
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
331
class TestWorkingTreeProviderAdapter(TestCase):
332
    """A group of tests that test the workingtree implementation test adapter."""
333
334
    def test_adapted_tests(self):
335
        # check that constructor parameters are passed through to the adapted
336
        # test.
337
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
338
        input_test = TestWorkingTreeProviderAdapter(
339
            "test_adapted_tests")
340
        server1 = "a"
341
        server2 = "b"
342
        formats = [("c", "C"), ("d", "D")]
343
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
344
        suite = adapter.adapt(input_test)
345
        tests = list(iter(suite))
346
        self.assertEqual(2, len(tests))
347
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
348
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
349
        self.assertEqual(tests[0].transport_server, server1)
350
        self.assertEqual(tests[0].transport_readonly_server, server2)
351
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
352
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
353
        self.assertEqual(tests[1].transport_server, server1)
354
        self.assertEqual(tests[1].transport_readonly_server, server2)
355
356
1852.6.1 by Robert Collins
Start tree implementation tests.
357
class TestTreeProviderAdapter(TestCase):
358
    """Test the setup of tree_implementation tests."""
359
360
    def test_adapted_tests(self):
361
        # the tree implementation adapter is meant to setup one instance for
362
        # each working tree format, and one additional instance that will
363
        # use the default wt format, but create a revision tree for the tests.
364
        # this means that the wt ones should have the workingtree_to_test_tree
365
        # attribute set to 'return_parameter' and the revision one set to
366
        # revision_tree_from_workingtree.
367
368
        from bzrlib.tests.tree_implementations import (
369
            TreeTestProviderAdapter,
370
            return_parameter,
371
            revision_tree_from_workingtree
372
            )
373
        from bzrlib.workingtree import WorkingTreeFormat
374
        input_test = TestTreeProviderAdapter(
375
            "test_adapted_tests")
376
        server1 = "a"
377
        server2 = "b"
378
        formats = [("c", "C"), ("d", "D")]
379
        adapter = TreeTestProviderAdapter(server1, server2, formats)
380
        suite = adapter.adapt(input_test)
381
        tests = list(iter(suite))
382
        self.assertEqual(3, len(tests))
383
        default_format = WorkingTreeFormat.get_default_format()
384
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
385
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
386
        self.assertEqual(tests[0].transport_server, server1)
387
        self.assertEqual(tests[0].transport_readonly_server, server2)
388
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
389
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
390
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
391
        self.assertEqual(tests[1].transport_server, server1)
392
        self.assertEqual(tests[1].transport_readonly_server, server2)
393
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
394
        self.assertEqual(tests[2].workingtree_format, default_format)
395
        self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
396
        self.assertEqual(tests[2].transport_server, server1)
397
        self.assertEqual(tests[2].transport_readonly_server, server2)
398
        self.assertEqual(tests[2].workingtree_to_test_tree,
399
            revision_tree_from_workingtree)
400
401
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
402
class TestInterTreeProviderAdapter(TestCase):
403
    """A group of tests that test the InterTreeTestAdapter."""
404
405
    def test_adapted_tests(self):
406
        # check that constructor parameters are passed through to the adapted
407
        # test.
408
        # for InterTree tests we want the machinery to bring up two trees in
409
        # each instance: the base one, and the one we are interacting with.
410
        # because each optimiser can be direction specific, we need to test
411
        # each optimiser in its chosen direction.
412
        # unlike the TestProviderAdapter we dont want to automatically add a
413
        # parameterised one for WorkingTree - the optimisers will tell us what
414
        # ones to add.
415
        from bzrlib.tests.tree_implementations import (
416
            return_parameter,
417
            revision_tree_from_workingtree
418
            )
419
        from bzrlib.tests.intertree_implementations import (
420
            InterTreeTestProviderAdapter,
421
            )
422
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
423
        input_test = TestInterTreeProviderAdapter(
424
            "test_adapted_tests")
425
        server1 = "a"
426
        server2 = "b"
427
        format1 = WorkingTreeFormat2()
428
        format2 = WorkingTreeFormat3()
429
        formats = [(str, format1, format2, False, True),
430
            (int, format2, format1, False, True)]
431
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
432
        suite = adapter.adapt(input_test)
433
        tests = list(iter(suite))
434
        self.assertEqual(2, len(tests))
435
        self.assertEqual(tests[0].intertree_class, formats[0][0])
436
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
437
        self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
438
        self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
439
        self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
440
        self.assertEqual(tests[0].transport_server, server1)
441
        self.assertEqual(tests[0].transport_readonly_server, server2)
442
        self.assertEqual(tests[1].intertree_class, formats[1][0])
443
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
444
        self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
445
        self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
446
        self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
447
        self.assertEqual(tests[1].transport_server, server1)
448
        self.assertEqual(tests[1].transport_readonly_server, server2)
449
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
450
451
class TestTestCaseInTempDir(TestCaseInTempDir):
452
453
    def test_home_is_not_working(self):
454
        self.assertNotEqual(self.test_dir, self.test_home_dir)
455
        cwd = osutils.getcwd()
1987.1.4 by John Arbash Meinel
fix the home_is_not_working test
456
        self.assertEqual(self.test_dir, cwd)
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
457
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
458
459
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
460
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
461
462
    def test_home_is_non_existant_dir_under_root(self):
463
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
464
465
        This is because TestCaseWithMemoryTransport is for tests that do not
466
        need any disk resources: they should be hooked into bzrlib in such a 
467
        way that no global settings are being changed by the test (only a 
468
        few tests should need to do that), and having a missing dir as home is
469
        an effective way to ensure that this is the case.
470
        """
471
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
472
            self.test_home_dir)
473
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
474
        
475
    def test_cwd_is_TEST_ROOT(self):
476
        self.assertEqual(self.test_dir, self.TEST_ROOT)
477
        cwd = osutils.getcwd()
478
        self.assertEqual(self.test_dir, cwd)
479
480
    def test_make_branch_and_memory_tree(self):
481
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
482
483
        This is hard to comprehensively robustly test, so we settle for making
484
        a branch and checking no directory was created at its relpath.
485
        """
486
        tree = self.make_branch_and_memory_tree('dir')
2227.2.2 by v.ladeuil+lp at free
Cleanup.
487
        # Guard against regression into MemoryTransport leaking
488
        # files to disk instead of keeping them in memory.
489
        self.failIf(osutils.lexists('dir'))
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
490
        self.assertIsInstance(tree, memorytree.MemoryTree)
491
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
492
    def test_make_branch_and_memory_tree_with_format(self):
493
        """make_branch_and_memory_tree should accept a format option."""
494
        format = bzrdir.BzrDirMetaFormat1()
495
        format.repository_format = repository.RepositoryFormat7()
496
        tree = self.make_branch_and_memory_tree('dir', format=format)
2227.2.2 by v.ladeuil+lp at free
Cleanup.
497
        # Guard against regression into MemoryTransport leaking
498
        # files to disk instead of keeping them in memory.
499
        self.failIf(osutils.lexists('dir'))
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
500
        self.assertIsInstance(tree, memorytree.MemoryTree)
501
        self.assertEqual(format.repository_format.__class__,
502
            tree.branch.repository._format.__class__)
503
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
504
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
505
class TestTestCaseWithTransport(TestCaseWithTransport):
506
    """Tests for the convenience functions TestCaseWithTransport introduces."""
507
508
    def test_get_readonly_url_none(self):
509
        from bzrlib.transport import get_transport
510
        from bzrlib.transport.memory import MemoryServer
511
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
512
        self.vfs_transport_factory = MemoryServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
513
        self.transport_readonly_server = None
514
        # calling get_readonly_transport() constructs a decorator on the url
515
        # for the server
516
        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.
517
        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.
518
        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.
519
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
520
        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.
521
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
522
        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.
523
524
    def test_get_readonly_url_http(self):
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
525
        from bzrlib.tests.HttpServer import HttpServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
526
        from bzrlib.transport import get_transport
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
527
        from bzrlib.transport.local import LocalURLServer
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
528
        from bzrlib.transport.http import HttpTransportBase
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
529
        self.transport_server = LocalURLServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
530
        self.transport_readonly_server = HttpServer
531
        # calling get_readonly_transport() gives us a HTTP server instance.
532
        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.
533
        url2 = self.get_readonly_url('foo/bar')
1540.3.6 by Martin Pool
[merge] update from bzr.dev
534
        # 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.
535
        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.
536
        t2 = get_transport(url2)
1540.3.6 by Martin Pool
[merge] update from bzr.dev
537
        self.failUnless(isinstance(t, HttpTransportBase))
538
        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.
539
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
540
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
541
    def test_is_directory(self):
542
        """Test assertIsDirectory assertion"""
543
        t = self.get_transport()
544
        self.build_tree(['a_dir/', 'a_file'], transport=t)
545
        self.assertIsDirectory('a_dir', t)
546
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
547
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
1534.4.31 by Robert Collins
cleanedup test_outside_wt
548
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
549
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
550
class TestTestCaseTransports(TestCaseWithTransport):
551
552
    def setUp(self):
553
        super(TestTestCaseTransports, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
554
        self.vfs_transport_factory = MemoryServer
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
555
556
    def test_make_bzrdir_preserves_transport(self):
557
        t = self.get_transport()
558
        result_bzrdir = self.make_bzrdir('subdir')
559
        self.assertIsInstance(result_bzrdir.transport, 
560
                              MemoryTransport)
561
        # should not be on disk, should only be in memory
562
        self.failIfExists('subdir')
563
564
1534.4.31 by Robert Collins
cleanedup test_outside_wt
565
class TestChrootedTest(ChrootedTestCase):
566
567
    def test_root_is_root(self):
568
        from bzrlib.transport import get_transport
569
        t = get_transport(self.get_readonly_url())
570
        url = t.base
571
        self.assertEqual(url, t.clone('..').base)
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
572
573
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
574
class MockProgress(_BaseProgressBar):
575
    """Progress-bar standin that records calls.
576
577
    Useful for testing pb using code.
578
    """
579
580
    def __init__(self):
581
        _BaseProgressBar.__init__(self)
582
        self.calls = []
583
584
    def tick(self):
585
        self.calls.append(('tick',))
586
587
    def update(self, msg=None, current=None, total=None):
588
        self.calls.append(('update', msg, current, total))
589
590
    def clear(self):
591
        self.calls.append(('clear',))
592
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
593
    def note(self, msg, *args):
594
        self.calls.append(('note', msg, args))
595
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
596
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
597
class TestTestResult(TestCase):
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
598
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
599
    def test_elapsed_time_with_benchmarking(self):
2095.4.1 by Martin Pool
Better progress bars during tests
600
        result = bzrlib.tests.TextTestResult(self._log_file,
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
601
                                        descriptions=0,
602
                                        verbosity=1,
603
                                        )
604
        result._recordTestStartTime()
605
        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)
606
        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).
607
        timed_string = result._testTimeString()
608
        # without explicit benchmarking, we should get a simple time.
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
609
        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).
610
        # 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)
611
        self.time(time.sleep, 0.001)
612
        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).
613
        timed_string = result._testTimeString()
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
614
        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)
615
        # 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).
616
        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)
617
        result.extractBenchmarkTime(
618
            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).
619
        timed_string = result._testTimeString()
2196.1.2 by Martin Pool
Loosen requirements for benchmark formatting in selftest
620
        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)
621
        # cheat. Yes, wash thy mouth out with soap.
622
        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).
623
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
624
    def test_assigned_benchmark_file_stores_date(self):
625
        output = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
626
        result = bzrlib.tests.TextTestResult(self._log_file,
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
627
                                        descriptions=0,
628
                                        verbosity=1,
629
                                        bench_history=output
630
                                        )
631
        output_string = output.getvalue()
2095.4.1 by Martin Pool
Better progress bars during tests
632
        
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
633
        # if you are wondering about the regexp please read the comment in
634
        # 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.
635
        # XXX: what comment?  -- Andrew Bennetts
636
        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
637
638
    def test_benchhistory_records_test_times(self):
639
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
640
        result = bzrlib.tests.TextTestResult(
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
641
            self._log_file,
642
            descriptions=0,
643
            verbosity=1,
644
            bench_history=result_stream
645
            )
646
647
        # we want profile a call and check that its test duration is recorded
648
        # make a new test instance that when run will generate a benchmark
649
        example_test_case = TestTestResult("_time_hello_world_encoding")
650
        # execute the test, which should succeed and record times
651
        example_test_case.run(result)
652
        lines = result_stream.getvalue().splitlines()
653
        self.assertEqual(2, len(lines))
654
        self.assertContainsRe(lines[1],
655
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
656
            "._time_hello_world_encoding")
657
 
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
658
    def _time_hello_world_encoding(self):
659
        """Profile two sleep calls
660
        
661
        This is used to exercise the test framework.
662
        """
663
        self.time(unicode, 'hello', errors='replace')
664
        self.time(unicode, 'world', errors='replace')
665
666
    def test_lsprofiling(self):
667
        """Verbose test result prints lsprof statistics from test cases."""
668
        try:
669
            import bzrlib.lsprof
670
        except ImportError:
671
            raise TestSkipped("lsprof not installed.")
672
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
673
        result = bzrlib.tests.VerboseTestResult(
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
674
            unittest._WritelnDecorator(result_stream),
675
            descriptions=0,
676
            verbosity=2,
677
            )
678
        # we want profile a call of some sort and check it is output by
679
        # addSuccess. We dont care about addError or addFailure as they
680
        # are not that interesting for performance tuning.
681
        # make a new test instance that when run will generate a profile
682
        example_test_case = TestTestResult("_time_hello_world_encoding")
683
        example_test_case._gather_lsprof_in_benchmarks = True
684
        # execute the test, which should succeed and record profiles
685
        example_test_case.run(result)
686
        # lsprofile_something()
687
        # if this worked we want 
688
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
689
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
690
        # (the lsprof header)
691
        # ... an arbitrary number of lines
692
        # and the function call which is time.sleep.
693
        #           1        0            ???         ???       ???(sleep) 
694
        # and then repeated but with 'world', rather than 'hello'.
695
        # this should appear in the output stream of our test result.
1831.2.1 by Martin Pool
[trivial] Simplify & fix up lsprof blackbox test
696
        output = result_stream.getvalue()
697
        self.assertContainsRe(output,
698
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
699
        self.assertContainsRe(output,
700
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
701
        self.assertContainsRe(output,
702
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
703
        self.assertContainsRe(output,
704
            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
705
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
706
707
class TestRunner(TestCase):
708
709
    def dummy_test(self):
710
        pass
711
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
712
    def run_test_runner(self, testrunner, test):
713
        """Run suite in testrunner, saving global state and restoring it.
714
715
        This current saves and restores:
716
        TestCaseInTempDir.TEST_ROOT
717
        
718
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
719
        without using this convenience method, because of our use of global state.
720
        """
721
        old_root = TestCaseInTempDir.TEST_ROOT
722
        try:
723
            TestCaseInTempDir.TEST_ROOT = None
724
            return testrunner.run(test)
725
        finally:
726
            TestCaseInTempDir.TEST_ROOT = old_root
727
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)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
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")
2095.4.1 by Martin Pool
Better progress bars during tests
828
        result = bzrlib.tests.TextTestResult(self._log_file,
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
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")
2095.4.1 by Martin Pool
Better progress bars during tests
848
        result = bzrlib.tests.TextTestResult(self._log_file,
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
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()
2095.4.1 by Martin Pool
Better progress bars during tests
863
        result = bzrlib.tests.VerboseTestResult(
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
864
            unittest._WritelnDecorator(output_stream),
865
            descriptions=0,
2095.4.1 by Martin Pool
Better progress bars during tests
866
            verbosity=2,
867
            num_tests=sample_test.countTestCases())
1707.2.4 by Robert Collins
Teach the bzrlib TestCase to report the time take by calls to self.time as benchmark time, allowing granular reporting of time during benchmarks. See bzrlib.benchmarks.bench_add. (Robert Collins, Martin Pool)
868
        sample_test.run(result)
869
        self.assertContainsRe(
870
            output_stream.getvalue(),
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
871
            r"\d+ms/ +\d+ms\n$")
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
872
873
    def test_hooks_sanitised(self):
874
        """The bzrlib hooks should be sanitised by setUp."""
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
875
        self.assertEqual(bzrlib.branch.BranchHooks(),
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
876
            bzrlib.branch.Branch.hooks)
877
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
878
    def test__gather_lsprof_in_benchmarks(self):
879
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
880
        
881
        Each self.time() call is individually and separately profiled.
882
        """
883
        try:
884
            import bzrlib.lsprof
885
        except ImportError:
886
            raise TestSkipped("lsprof not installed.")
887
        # overrides the class member with an instance member so no cleanup 
888
        # needed.
889
        self._gather_lsprof_in_benchmarks = True
890
        self.time(time.sleep, 0.000)
891
        self.time(time.sleep, 0.003)
892
        self.assertEqual(2, len(self._benchcalls))
893
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
894
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
895
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
896
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
897
1534.11.4 by Robert Collins
Merge from mainline.
898
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
899
@symbol_versioning.deprecated_function(zero_eleven)
900
def sample_deprecated_function():
901
    """A deprecated function to test applyDeprecated with."""
902
    return 2
903
904
905
def sample_undeprecated_function(a_param):
906
    """A undeprecated function to test applyDeprecated with."""
907
908
909
class ApplyDeprecatedHelper(object):
910
    """A helper class for ApplyDeprecated tests."""
911
912
    @symbol_versioning.deprecated_method(zero_eleven)
913
    def sample_deprecated_method(self, param_one):
914
        """A deprecated method for testing with."""
915
        return param_one
916
917
    def sample_normal_method(self):
918
        """A undeprecated method."""
919
920
    @symbol_versioning.deprecated_method(zero_ten)
921
    def sample_nested_deprecation(self):
922
        return sample_deprecated_function()
923
924
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
925
class TestExtraAssertions(TestCase):
926
    """Tests for new test assertions in bzrlib test suite"""
927
928
    def test_assert_isinstance(self):
929
        self.assertIsInstance(2, int)
930
        self.assertIsInstance(u'', basestring)
931
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
932
        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
933
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
934
    def test_assertEndsWith(self):
935
        self.assertEndsWith('foo', 'oo')
936
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
937
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
938
    def test_applyDeprecated_not_deprecated(self):
939
        sample_object = ApplyDeprecatedHelper()
940
        # calling an undeprecated callable raises an assertion
941
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
942
            sample_object.sample_normal_method)
943
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
944
            sample_undeprecated_function, "a param value")
945
        # calling a deprecated callable (function or method) with the wrong
946
        # expected deprecation fails.
947
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
948
            sample_object.sample_deprecated_method, "a param value")
949
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
950
            sample_deprecated_function)
951
        # calling a deprecated callable (function or method) with the right
952
        # expected deprecation returns the functions result.
953
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
954
            sample_object.sample_deprecated_method, "a param value"))
955
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
956
            sample_deprecated_function))
957
        # calling a nested deprecation with the wrong deprecation version
958
        # fails even if a deeper nested function was deprecated with the 
959
        # supplied version.
960
        self.assertRaises(AssertionError, self.applyDeprecated,
961
            zero_eleven, sample_object.sample_nested_deprecation)
962
        # calling a nested deprecation with the right deprecation value
963
        # returns the calls result.
964
        self.assertEqual(2, self.applyDeprecated(zero_ten,
965
            sample_object.sample_nested_deprecation))
966
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
967
    def test_callDeprecated(self):
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
968
        def testfunc(be_deprecated, result=None):
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
969
            if be_deprecated is True:
970
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
971
                                       stacklevel=1)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
972
            return result
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
973
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
974
        self.assertIs(None, result)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
975
        result = self.callDeprecated([], testfunc, False, 'result')
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
976
        self.assertEqual('result', result)
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
977
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
978
        self.callDeprecated([], testfunc, be_deprecated=False)
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
979
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
980
981
class TestConvenienceMakers(TestCaseWithTransport):
982
    """Test for the make_* convenience functions."""
983
984
    def test_make_branch_and_tree_with_format(self):
985
        # we should be able to supply a format to make_branch_and_tree
986
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
987
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
988
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
989
                              bzrlib.bzrdir.BzrDirMetaFormat1)
990
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
991
                              bzrlib.bzrdir.BzrDirFormat6)
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
992
1986.2.1 by Robert Collins
Bugfix - the name of the test for make_branch_and_memory_tree was wrong.
993
    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
994
        # we should be able to get a new branch and a mutable tree from
995
        # TestCaseWithTransport
996
        tree = self.make_branch_and_memory_tree('a')
997
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
998
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
999
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
1000
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1001
1002
    def test_make_tree_for_sftp_branch(self):
1003
        """Transports backed by local directories create local trees."""
1004
1005
        tree = self.make_branch_and_tree('t1')
1006
        base = tree.bzrdir.root_transport.base
1007
        self.failIf(base.startswith('sftp'),
1008
                'base %r is on sftp but should be local' % base)
1009
        self.assertEquals(tree.bzrdir.root_transport,
1010
                tree.branch.bzrdir.root_transport)
1011
        self.assertEquals(tree.bzrdir.root_transport,
1012
                tree.branch.repository.bzrdir.root_transport)
1013
1014
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1015
class TestSelftest(TestCase):
1016
    """Tests of bzrlib.tests.selftest."""
1017
1018
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1019
        factory_called = []
1020
        def factory():
1021
            factory_called.append(True)
1022
            return TestSuite()
1023
        out = StringIO()
1024
        err = StringIO()
1025
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1026
            test_suite_factory=factory)
1027
        self.assertEqual([True], factory_called)
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1028
1029
1030
class TestSelftestCleanOutput(TestCaseInTempDir):
1031
1032
    def test_clean_output(self):
1033
        # test functionality of clean_selftest_output()
1034
        from bzrlib.tests import clean_selftest_output
1035
1036
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
1037
        files = ('bzr', 'setup.py', 'test9999.tmp')
1038
        for i in dirs:
1039
            os.mkdir(i)
1040
        for i in files:
1041
            f = file(i, 'wb')
1042
            f.write('content of ')
1043
            f.write(i)
1044
            f.close()
1045
1046
        root = os.getcwdu()
1047
        before = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1048
        before.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1049
        self.assertEquals(['bzr','bzrlib','setup.py',
1050
                           'test0000.tmp','test0001.tmp',
1051
                           'test9999.tmp','tests'],
1052
                           before)
1053
        clean_selftest_output(root, quiet=True)
1054
        after = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1055
        after.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1056
        self.assertEquals(['bzr','bzrlib','setup.py',
1057
                           'test9999.tmp','tests'],
1058
                           after)