/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1
# Copyright (C) 2005, 2006, 2007 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,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
30
    errors,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
31
    memorytree,
32
    osutils,
33
    repository,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
34
    symbol_versioning,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
35
    )
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
36
from bzrlib.progress import _BaseProgressBar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
37
from bzrlib.repofmt import weaverepo
38
from bzrlib.symbol_versioning import zero_ten, zero_eleven
1526.1.3 by Robert Collins
Merge from upstream.
39
from bzrlib.tests import (
1534.4.31 by Robert Collins
cleanedup test_outside_wt
40
                          ChrootedTestCase,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
41
                          ExtendedTestResult,
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
42
                          Feature,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
43
                          KnownFailure,
1526.1.3 by Robert Collins
Merge from upstream.
44
                          TestCase,
45
                          TestCaseInTempDir,
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
46
                          TestCaseWithMemoryTransport,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
47
                          TestCaseWithTransport,
1526.1.3 by Robert Collins
Merge from upstream.
48
                          TestSkipped,
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
49
                          TestSuite,
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
50
                          TestUtil,
1526.1.3 by Robert Collins
Merge from upstream.
51
                          TextTestRunner,
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
52
                          UnavailableFeature,
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
53
                          clean_selftest_output,
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
54
                          iter_suite_tests,
55
                          filter_suite_by_re,
56
                          sort_suite_by_re,
2493.2.8 by Aaron Bentley
Convert old lsprof tests to require new LSProf Feature instead of skipping
57
                          test_lsprof,
58
                          test_suite,
1526.1.3 by Robert Collins
Merge from upstream.
59
                          )
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
60
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
61
from bzrlib.tests.TestUtil import _load_module_by_name
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
62
from bzrlib.trace import note
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
63
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.
64
from bzrlib.version import _get_bzr_source_tree
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
65
66
67
class SelftestTests(TestCase):
68
69
    def test_import_tests(self):
70
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
71
        self.assertEqual(mod.SelftestTests, SelftestTests)
72
73
    def test_import_test_failure(self):
74
        self.assertRaises(ImportError,
75
                          _load_module_by_name,
76
                          'bzrlib.no-name-yet')
77
78
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.
79
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
80
    def test_logging(self):
81
        """Test logs are captured when a test fails."""
82
        self.log('a test message')
83
        self._log_file.flush()
1927.3.1 by Carl Friedrich Bolz
Throw away on-disk logfile when possible.
84
        self.assertContainsRe(self._get_log(keep_log_file=True),
85
                              'a test message\n')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
86
87
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.
88
class TestTreeShape(TestCaseInTempDir):
89
90
    def test_unicode_paths(self):
91
        filename = u'hell\u00d8'
1526.1.4 by Robert Collins
forgot my self.
92
        try:
93
            self.build_tree_contents([(filename, 'contents of hello')])
94
        except UnicodeEncodeError:
95
            raise TestSkipped("can't build unicode working tree in "
96
                "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.
97
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
98
99
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
100
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
101
    """A group of tests that test the transport implementation adaption core.
102
1551.1.1 by Martin Pool
[merge] branch-formats branch, and reconcile changes
103
    This is a meta test that the tests are applied to all available 
104
    transports.
105
1530.1.21 by Robert Collins
Review feedback fixes.
106
    This will be generalised in the future which is why it is in this 
107
    test file even though it is specific to transport tests at the moment.
108
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
109
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
110
    def test_get_transport_permutations(self):
1530.1.21 by Robert Collins
Review feedback fixes.
111
        # this checks that we the module get_test_permutations call
112
        # 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.
113
        class MockModule(object):
114
            def get_test_permutations(self):
115
                return sample_permutation
116
        sample_permutation = [(1,2), (3,4)]
117
        from bzrlib.transport import TransportTestProviderAdapter
118
        adapter = TransportTestProviderAdapter()
119
        self.assertEqual(sample_permutation,
120
                         adapter.get_transport_test_permutations(MockModule()))
121
122
    def test_adapter_checks_all_modules(self):
1530.1.21 by Robert Collins
Review feedback fixes.
123
        # this checks that the adapter returns as many permurtations as
124
        # there are in all the registered# transport modules for there
125
        # - we assume if this matches its probably doing the right thing
126
        # especially in combination with the tests for setting the right
127
        # 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.
128
        from bzrlib.transport import (TransportTestProviderAdapter,
129
                                      _get_transport_modules
130
                                      )
131
        modules = _get_transport_modules()
132
        permutation_count = 0
133
        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.
134
            try:
135
                permutation_count += len(reduce(getattr, 
136
                    (module + ".get_test_permutations").split('.')[1:],
137
                     __import__(module))())
138
            except errors.DependencyNotPresent:
139
                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.
140
        input_test = TestTransportProviderAdapter(
141
            "test_adapter_sets_transport_class")
142
        adapter = TransportTestProviderAdapter()
143
        self.assertEqual(permutation_count,
144
                         len(list(iter(adapter.adapt(input_test)))))
145
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
146
    def test_adapter_sets_transport_class(self):
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
147
        # Check that the test adapter inserts a transport and server into the
148
        # generated test.
149
        #
150
        # This test used to know about all the possible transports and the
151
        # order they were returned but that seems overly brittle (mbp
152
        # 20060307)
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
153
        input_test = TestTransportProviderAdapter(
154
            "test_adapter_sets_transport_class")
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
155
        from bzrlib.transport import TransportTestProviderAdapter
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
156
        suite = TransportTestProviderAdapter().adapt(input_test)
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
157
        tests = list(iter(suite))
158
        self.assertTrue(len(tests) > 6)
159
        # there are at least that many builtin transports
160
        one_test = tests[0]
161
        self.assertTrue(issubclass(one_test.transport_class, 
162
                                   bzrlib.transport.Transport))
163
        self.assertTrue(issubclass(one_test.transport_server, 
164
                                   bzrlib.transport.Server))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
165
166
167
class TestBranchProviderAdapter(TestCase):
168
    """A group of tests that test the branch implementation test adapter."""
169
170
    def test_adapted_tests(self):
171
        # check that constructor parameters are passed through to the adapted
172
        # test.
173
        from bzrlib.branch import BranchTestProviderAdapter
174
        input_test = TestBranchProviderAdapter(
175
            "test_adapted_tests")
176
        server1 = "a"
177
        server2 = "b"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
178
        formats = [("c", "C"), ("d", "D")]
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
179
        adapter = BranchTestProviderAdapter(server1, server2, formats)
180
        suite = adapter.adapt(input_test)
181
        tests = list(iter(suite))
182
        self.assertEqual(2, len(tests))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
183
        self.assertEqual(tests[0].branch_format, formats[0][0])
184
        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.
185
        self.assertEqual(tests[0].transport_server, server1)
186
        self.assertEqual(tests[0].transport_readonly_server, server2)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
187
        self.assertEqual(tests[1].branch_format, formats[1][0])
188
        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.
189
        self.assertEqual(tests[1].transport_server, server1)
190
        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.
191
192
1534.4.39 by Robert Collins
Basic BzrDir support.
193
class TestBzrDirProviderAdapter(TestCase):
194
    """A group of tests that test the bzr dir implementation test adapter."""
195
196
    def test_adapted_tests(self):
197
        # check that constructor parameters are passed through to the adapted
198
        # test.
199
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
200
        input_test = TestBzrDirProviderAdapter(
201
            "test_adapted_tests")
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
202
        vfs_factory = "v"
1534.4.39 by Robert Collins
Basic BzrDir support.
203
        server1 = "a"
204
        server2 = "b"
205
        formats = ["c", "d"]
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
206
        adapter = BzrDirTestProviderAdapter(vfs_factory,
207
            server1, server2, formats)
1534.4.39 by Robert Collins
Basic BzrDir support.
208
        suite = adapter.adapt(input_test)
209
        tests = list(iter(suite))
210
        self.assertEqual(2, len(tests))
211
        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 :).
212
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
213
        self.assertEqual(tests[0].transport_server, server1)
214
        self.assertEqual(tests[0].transport_readonly_server, server2)
215
        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 :).
216
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
1534.4.39 by Robert Collins
Basic BzrDir support.
217
        self.assertEqual(tests[1].transport_server, server1)
218
        self.assertEqual(tests[1].transport_readonly_server, server2)
219
220
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
221
class TestRepositoryProviderAdapter(TestCase):
222
    """A group of tests that test the repository implementation test adapter."""
223
224
    def test_adapted_tests(self):
225
        # check that constructor parameters are passed through to the adapted
226
        # test.
227
        from bzrlib.repository import RepositoryTestProviderAdapter
228
        input_test = TestRepositoryProviderAdapter(
229
            "test_adapted_tests")
230
        server1 = "a"
231
        server2 = "b"
232
        formats = [("c", "C"), ("d", "D")]
233
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
234
        suite = adapter.adapt(input_test)
235
        tests = list(iter(suite))
236
        self.assertEqual(2, len(tests))
237
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
238
        self.assertEqual(tests[0].repository_format, formats[0][0])
239
        self.assertEqual(tests[0].transport_server, server1)
240
        self.assertEqual(tests[0].transport_readonly_server, server2)
241
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
242
        self.assertEqual(tests[1].repository_format, formats[1][0])
243
        self.assertEqual(tests[1].transport_server, server1)
244
        self.assertEqual(tests[1].transport_readonly_server, server2)
245
2018.5.64 by Robert Collins
Allow Repository tests to be backed onto a specific VFS as needed.
246
    def test_setting_vfs_transport(self):
247
        """The vfs_transport_factory can be set optionally."""
248
        from bzrlib.repository import RepositoryTestProviderAdapter
249
        input_test = TestRepositoryProviderAdapter(
250
            "test_adapted_tests")
251
        formats = [("c", "C")]
252
        adapter = RepositoryTestProviderAdapter(None, None, formats,
253
            vfs_transport_factory="vfs")
254
        suite = adapter.adapt(input_test)
255
        tests = list(iter(suite))
256
        self.assertEqual(1, len(tests))
257
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
258
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
259
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
260
class TestInterRepositoryProviderAdapter(TestCase):
261
    """A group of tests that test the InterRepository test adapter."""
262
263
    def test_adapted_tests(self):
264
        # check that constructor parameters are passed through to the adapted
265
        # test.
266
        from bzrlib.repository import InterRepositoryTestProviderAdapter
267
        input_test = TestInterRepositoryProviderAdapter(
268
            "test_adapted_tests")
269
        server1 = "a"
270
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
271
        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.
272
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
273
        suite = adapter.adapt(input_test)
274
        tests = list(iter(suite))
275
        self.assertEqual(2, len(tests))
276
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
277
        self.assertEqual(tests[0].repository_format, formats[0][1])
278
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
279
        self.assertEqual(tests[0].transport_server, server1)
280
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
281
        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.
282
        self.assertEqual(tests[1].repository_format, formats[1][1])
283
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
284
        self.assertEqual(tests[1].transport_server, server1)
285
        self.assertEqual(tests[1].transport_readonly_server, server2)
286
287
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
class TestInterVersionedFileProviderAdapter(TestCase):
289
    """A group of tests that test the InterVersionedFile test adapter."""
290
291
    def test_adapted_tests(self):
292
        # check that constructor parameters are passed through to the adapted
293
        # test.
294
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
295
        input_test = TestInterRepositoryProviderAdapter(
296
            "test_adapted_tests")
297
        server1 = "a"
298
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
299
        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.
300
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
301
        suite = adapter.adapt(input_test)
302
        tests = list(iter(suite))
303
        self.assertEqual(2, len(tests))
304
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
305
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
306
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
307
        self.assertEqual(tests[0].transport_server, server1)
308
        self.assertEqual(tests[0].transport_readonly_server, server2)
1563.2.20 by Robert Collins
Add a revision store test adapter.
309
        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.
310
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
311
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
312
        self.assertEqual(tests[1].transport_server, server1)
313
        self.assertEqual(tests[1].transport_readonly_server, server2)
314
315
1563.2.20 by Robert Collins
Add a revision store test adapter.
316
class TestRevisionStoreProviderAdapter(TestCase):
317
    """A group of tests that test the RevisionStore test adapter."""
318
319
    def test_adapted_tests(self):
320
        # check that constructor parameters are passed through to the adapted
321
        # test.
322
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
323
        input_test = TestRevisionStoreProviderAdapter(
324
            "test_adapted_tests")
325
        # revision stores need a store factory - i.e. RevisionKnit
326
        #, a readonly and rw transport 
327
        # transport servers:
328
        server1 = "a"
329
        server2 = "b"
330
        store_factories = ["c", "d"]
331
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
332
        suite = adapter.adapt(input_test)
333
        tests = list(iter(suite))
334
        self.assertEqual(2, len(tests))
335
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
336
        self.assertEqual(tests[0].transport_server, server1)
337
        self.assertEqual(tests[0].transport_readonly_server, server2)
338
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
339
        self.assertEqual(tests[1].transport_server, server1)
340
        self.assertEqual(tests[1].transport_readonly_server, server2)
341
342
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
343
class TestWorkingTreeProviderAdapter(TestCase):
344
    """A group of tests that test the workingtree implementation test adapter."""
345
346
    def test_adapted_tests(self):
347
        # check that constructor parameters are passed through to the adapted
348
        # test.
349
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
350
        input_test = TestWorkingTreeProviderAdapter(
351
            "test_adapted_tests")
352
        server1 = "a"
353
        server2 = "b"
354
        formats = [("c", "C"), ("d", "D")]
355
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
356
        suite = adapter.adapt(input_test)
357
        tests = list(iter(suite))
358
        self.assertEqual(2, len(tests))
359
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
360
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
361
        self.assertEqual(tests[0].transport_server, server1)
362
        self.assertEqual(tests[0].transport_readonly_server, server2)
363
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
364
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
365
        self.assertEqual(tests[1].transport_server, server1)
366
        self.assertEqual(tests[1].transport_readonly_server, server2)
367
368
1852.6.1 by Robert Collins
Start tree implementation tests.
369
class TestTreeProviderAdapter(TestCase):
370
    """Test the setup of tree_implementation tests."""
371
372
    def test_adapted_tests(self):
373
        # the tree implementation adapter is meant to setup one instance for
374
        # each working tree format, and one additional instance that will
375
        # use the default wt format, but create a revision tree for the tests.
376
        # this means that the wt ones should have the workingtree_to_test_tree
377
        # attribute set to 'return_parameter' and the revision one set to
378
        # revision_tree_from_workingtree.
379
380
        from bzrlib.tests.tree_implementations import (
381
            TreeTestProviderAdapter,
382
            return_parameter,
383
            revision_tree_from_workingtree
384
            )
2255.2.164 by Martin Pool
Change the default format for some tests from AB1 back to WorkingTreeFormat3
385
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
1852.6.1 by Robert Collins
Start tree implementation tests.
386
        input_test = TestTreeProviderAdapter(
387
            "test_adapted_tests")
388
        server1 = "a"
389
        server2 = "b"
390
        formats = [("c", "C"), ("d", "D")]
391
        adapter = TreeTestProviderAdapter(server1, server2, formats)
392
        suite = adapter.adapt(input_test)
393
        tests = list(iter(suite))
2255.6.3 by Aaron Bentley
tweak tests
394
        self.assertEqual(4, len(tests))
2255.2.164 by Martin Pool
Change the default format for some tests from AB1 back to WorkingTreeFormat3
395
        # this must match the default format setp up in
396
        # TreeTestProviderAdapter.adapt
397
        default_format = WorkingTreeFormat3
1852.6.1 by Robert Collins
Start tree implementation tests.
398
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
399
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
400
        self.assertEqual(tests[0].transport_server, server1)
401
        self.assertEqual(tests[0].transport_readonly_server, server2)
402
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
403
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
404
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
405
        self.assertEqual(tests[1].transport_server, server1)
406
        self.assertEqual(tests[1].transport_readonly_server, server2)
407
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
408
        self.assertIsInstance(tests[2].workingtree_format, default_format)
409
        #self.assertEqual(tests[2].bzrdir_format,
410
        #                 default_format._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
411
        self.assertEqual(tests[2].transport_server, server1)
412
        self.assertEqual(tests[2].transport_readonly_server, server2)
413
        self.assertEqual(tests[2].workingtree_to_test_tree,
414
            revision_tree_from_workingtree)
415
416
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
417
class TestInterTreeProviderAdapter(TestCase):
418
    """A group of tests that test the InterTreeTestAdapter."""
419
420
    def test_adapted_tests(self):
421
        # check that constructor parameters are passed through to the adapted
422
        # test.
423
        # for InterTree tests we want the machinery to bring up two trees in
424
        # each instance: the base one, and the one we are interacting with.
425
        # because each optimiser can be direction specific, we need to test
426
        # each optimiser in its chosen direction.
427
        # unlike the TestProviderAdapter we dont want to automatically add a
428
        # parameterised one for WorkingTree - the optimisers will tell us what
429
        # ones to add.
430
        from bzrlib.tests.tree_implementations import (
431
            return_parameter,
432
            revision_tree_from_workingtree
433
            )
434
        from bzrlib.tests.intertree_implementations import (
435
            InterTreeTestProviderAdapter,
436
            )
437
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
438
        input_test = TestInterTreeProviderAdapter(
439
            "test_adapted_tests")
440
        server1 = "a"
441
        server2 = "b"
442
        format1 = WorkingTreeFormat2()
443
        format2 = WorkingTreeFormat3()
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
444
        formats = [(str, format1, format2, "converter1"),
445
            (int, format2, format1, "converter2")]
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
446
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
447
        suite = adapter.adapt(input_test)
448
        tests = list(iter(suite))
449
        self.assertEqual(2, len(tests))
450
        self.assertEqual(tests[0].intertree_class, formats[0][0])
451
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
452
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
453
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
454
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
455
        self.assertEqual(tests[0].transport_server, server1)
456
        self.assertEqual(tests[0].transport_readonly_server, server2)
457
        self.assertEqual(tests[1].intertree_class, formats[1][0])
458
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
459
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
460
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
461
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
462
        self.assertEqual(tests[1].transport_server, server1)
463
        self.assertEqual(tests[1].transport_readonly_server, server2)
464
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
465
466
class TestTestCaseInTempDir(TestCaseInTempDir):
467
468
    def test_home_is_not_working(self):
469
        self.assertNotEqual(self.test_dir, self.test_home_dir)
470
        cwd = osutils.getcwd()
1987.1.4 by John Arbash Meinel
fix the home_is_not_working test
471
        self.assertEqual(self.test_dir, cwd)
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
472
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
473
474
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
475
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
476
477
    def test_home_is_non_existant_dir_under_root(self):
478
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
479
480
        This is because TestCaseWithMemoryTransport is for tests that do not
481
        need any disk resources: they should be hooked into bzrlib in such a 
482
        way that no global settings are being changed by the test (only a 
483
        few tests should need to do that), and having a missing dir as home is
484
        an effective way to ensure that this is the case.
485
        """
486
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
487
            self.test_home_dir)
488
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
489
        
490
    def test_cwd_is_TEST_ROOT(self):
491
        self.assertEqual(self.test_dir, self.TEST_ROOT)
492
        cwd = osutils.getcwd()
493
        self.assertEqual(self.test_dir, cwd)
494
495
    def test_make_branch_and_memory_tree(self):
496
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
497
498
        This is hard to comprehensively robustly test, so we settle for making
499
        a branch and checking no directory was created at its relpath.
500
        """
501
        tree = self.make_branch_and_memory_tree('dir')
2227.2.2 by v.ladeuil+lp at free
Cleanup.
502
        # Guard against regression into MemoryTransport leaking
503
        # files to disk instead of keeping them in memory.
504
        self.failIf(osutils.lexists('dir'))
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
505
        self.assertIsInstance(tree, memorytree.MemoryTree)
506
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
507
    def test_make_branch_and_memory_tree_with_format(self):
508
        """make_branch_and_memory_tree should accept a format option."""
509
        format = bzrdir.BzrDirMetaFormat1()
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
510
        format.repository_format = weaverepo.RepositoryFormat7()
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
511
        tree = self.make_branch_and_memory_tree('dir', format=format)
2227.2.2 by v.ladeuil+lp at free
Cleanup.
512
        # Guard against regression into MemoryTransport leaking
513
        # files to disk instead of keeping them in memory.
514
        self.failIf(osutils.lexists('dir'))
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
515
        self.assertIsInstance(tree, memorytree.MemoryTree)
516
        self.assertEqual(format.repository_format.__class__,
517
            tree.branch.repository._format.__class__)
518
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
519
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
520
class TestTestCaseWithTransport(TestCaseWithTransport):
521
    """Tests for the convenience functions TestCaseWithTransport introduces."""
522
523
    def test_get_readonly_url_none(self):
524
        from bzrlib.transport import get_transport
525
        from bzrlib.transport.memory import MemoryServer
526
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
527
        self.vfs_transport_factory = MemoryServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
528
        self.transport_readonly_server = None
529
        # calling get_readonly_transport() constructs a decorator on the url
530
        # for the server
531
        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.
532
        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.
533
        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.
534
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
535
        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.
536
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
537
        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.
538
539
    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 :)
540
        from bzrlib.tests.HttpServer import HttpServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
541
        from bzrlib.transport import get_transport
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
542
        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 :)
543
        from bzrlib.transport.http import HttpTransportBase
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
544
        self.transport_server = LocalURLServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
545
        self.transport_readonly_server = HttpServer
546
        # calling get_readonly_transport() gives us a HTTP server instance.
547
        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.
548
        url2 = self.get_readonly_url('foo/bar')
1540.3.6 by Martin Pool
[merge] update from bzr.dev
549
        # 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.
550
        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.
551
        t2 = get_transport(url2)
1540.3.6 by Martin Pool
[merge] update from bzr.dev
552
        self.failUnless(isinstance(t, HttpTransportBase))
553
        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.
554
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
555
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
556
    def test_is_directory(self):
557
        """Test assertIsDirectory assertion"""
558
        t = self.get_transport()
559
        self.build_tree(['a_dir/', 'a_file'], transport=t)
560
        self.assertIsDirectory('a_dir', t)
561
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
562
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
1534.4.31 by Robert Collins
cleanedup test_outside_wt
563
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
564
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
565
class TestTestCaseTransports(TestCaseWithTransport):
566
567
    def setUp(self):
568
        super(TestTestCaseTransports, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
569
        self.vfs_transport_factory = MemoryServer
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
570
571
    def test_make_bzrdir_preserves_transport(self):
572
        t = self.get_transport()
573
        result_bzrdir = self.make_bzrdir('subdir')
574
        self.assertIsInstance(result_bzrdir.transport, 
575
                              MemoryTransport)
576
        # should not be on disk, should only be in memory
577
        self.failIfExists('subdir')
578
579
1534.4.31 by Robert Collins
cleanedup test_outside_wt
580
class TestChrootedTest(ChrootedTestCase):
581
582
    def test_root_is_root(self):
583
        from bzrlib.transport import get_transport
584
        t = get_transport(self.get_readonly_url())
585
        url = t.base
586
        self.assertEqual(url, t.clone('..').base)
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
587
588
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
589
class MockProgress(_BaseProgressBar):
590
    """Progress-bar standin that records calls.
591
592
    Useful for testing pb using code.
593
    """
594
595
    def __init__(self):
596
        _BaseProgressBar.__init__(self)
597
        self.calls = []
598
599
    def tick(self):
600
        self.calls.append(('tick',))
601
602
    def update(self, msg=None, current=None, total=None):
603
        self.calls.append(('update', msg, current, total))
604
605
    def clear(self):
606
        self.calls.append(('clear',))
607
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
608
    def note(self, msg, *args):
609
        self.calls.append(('note', msg, args))
610
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
611
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
612
class TestTestResult(TestCase):
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
613
1707.2.3 by Robert Collins
Add a setBenchmarkTime method to the bzrlib test result allowing introduction of granular benchmarking. (Robert Collins, Martin Pool).
614
    def test_elapsed_time_with_benchmarking(self):
2095.4.1 by Martin Pool
Better progress bars during tests
615
        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).
616
                                        descriptions=0,
617
                                        verbosity=1,
618
                                        )
619
        result._recordTestStartTime()
620
        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)
621
        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).
622
        timed_string = result._testTimeString()
623
        # without explicit benchmarking, we should get a simple time.
2492.1.2 by John Arbash Meinel
Tweak the regex a bit more
624
        self.assertContainsRe(timed_string, "^ +[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).
625
        # 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)
626
        self.time(time.sleep, 0.001)
627
        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).
628
        timed_string = result._testTimeString()
2485.2.1 by Marien Zwart
Make test_elapsed_time_with_benchmarking pass on a slow cpu.
629
        self.assertContainsRe(
2492.1.2 by John Arbash Meinel
Tweak the regex a bit more
630
            timed_string, "^ +[0-9]+ms/ +[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)
631
        # 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).
632
        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)
633
        result.extractBenchmarkTime(
634
            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).
635
        timed_string = result._testTimeString()
2492.1.2 by John Arbash Meinel
Tweak the regex a bit more
636
        self.assertContainsRe(timed_string, "^ +[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)
637
        # cheat. Yes, wash thy mouth out with soap.
638
        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).
639
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
640
    def test_assigned_benchmark_file_stores_date(self):
641
        output = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
642
        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
643
                                        descriptions=0,
644
                                        verbosity=1,
645
                                        bench_history=output
646
                                        )
647
        output_string = output.getvalue()
2095.4.1 by Martin Pool
Better progress bars during tests
648
        
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
649
        # if you are wondering about the regexp please read the comment in
650
        # 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.
651
        # XXX: what comment?  -- Andrew Bennetts
652
        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
653
654
    def test_benchhistory_records_test_times(self):
655
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
656
        result = bzrlib.tests.TextTestResult(
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
657
            self._log_file,
658
            descriptions=0,
659
            verbosity=1,
660
            bench_history=result_stream
661
            )
662
663
        # we want profile a call and check that its test duration is recorded
664
        # make a new test instance that when run will generate a benchmark
665
        example_test_case = TestTestResult("_time_hello_world_encoding")
666
        # execute the test, which should succeed and record times
667
        example_test_case.run(result)
668
        lines = result_stream.getvalue().splitlines()
669
        self.assertEqual(2, len(lines))
670
        self.assertContainsRe(lines[1],
671
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
672
            "._time_hello_world_encoding")
673
 
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
674
    def _time_hello_world_encoding(self):
675
        """Profile two sleep calls
676
        
677
        This is used to exercise the test framework.
678
        """
679
        self.time(unicode, 'hello', errors='replace')
680
        self.time(unicode, 'world', errors='replace')
681
682
    def test_lsprofiling(self):
683
        """Verbose test result prints lsprof statistics from test cases."""
1551.15.28 by Aaron Bentley
Improve Feature usage style w/ lsprof
684
        self.requireFeature(test_lsprof.LSProfFeature)
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
685
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
686
        result = bzrlib.tests.VerboseTestResult(
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
687
            unittest._WritelnDecorator(result_stream),
688
            descriptions=0,
689
            verbosity=2,
690
            )
691
        # we want profile a call of some sort and check it is output by
692
        # addSuccess. We dont care about addError or addFailure as they
693
        # are not that interesting for performance tuning.
694
        # make a new test instance that when run will generate a profile
695
        example_test_case = TestTestResult("_time_hello_world_encoding")
696
        example_test_case._gather_lsprof_in_benchmarks = True
697
        # execute the test, which should succeed and record profiles
698
        example_test_case.run(result)
699
        # lsprofile_something()
700
        # if this worked we want 
701
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
702
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
703
        # (the lsprof header)
704
        # ... an arbitrary number of lines
705
        # and the function call which is time.sleep.
706
        #           1        0            ???         ???       ???(sleep) 
707
        # and then repeated but with 'world', rather than 'hello'.
708
        # this should appear in the output stream of our test result.
1831.2.1 by Martin Pool
[trivial] Simplify & fix up lsprof blackbox test
709
        output = result_stream.getvalue()
710
        self.assertContainsRe(output,
711
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
712
        self.assertContainsRe(output,
713
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
714
        self.assertContainsRe(output,
715
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
716
        self.assertContainsRe(output,
717
            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
718
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
719
    def test_known_failure(self):
720
        """A KnownFailure being raised should trigger several result actions."""
721
        class InstrumentedTestResult(ExtendedTestResult):
722
723
            def report_test_start(self, test): pass
724
            def report_known_failure(self, test, err):
725
                self._call = test, err
726
        result = InstrumentedTestResult(None, None, None, None)
727
        def test_function():
728
            raise KnownFailure('failed!')
729
        test = unittest.FunctionTestCase(test_function)
730
        test.run(result)
731
        # it should invoke 'report_known_failure'.
732
        self.assertEqual(2, len(result._call))
733
        self.assertEqual(test, result._call[0])
734
        self.assertEqual(KnownFailure, result._call[1][0])
735
        self.assertIsInstance(result._call[1][1], KnownFailure)
736
        # we dont introspec the traceback, if the rest is ok, it would be
737
        # exceptional for it not to be.
738
        # it should update the known_failure_count on the object.
739
        self.assertEqual(1, result.known_failure_count)
740
        # the result should be successful.
741
        self.assertTrue(result.wasSuccessful())
742
743
    def test_verbose_report_known_failure(self):
744
        # verbose test output formatting
745
        result_stream = StringIO()
746
        result = bzrlib.tests.VerboseTestResult(
747
            unittest._WritelnDecorator(result_stream),
748
            descriptions=0,
749
            verbosity=2,
750
            )
751
        test = self.get_passing_test()
752
        result.startTest(test)
753
        result.extractBenchmarkTime(test)
754
        prefix = len(result_stream.getvalue())
755
        # the err parameter has the shape:
756
        # (class, exception object, traceback)
757
        # KnownFailures dont get their tracebacks shown though, so we
758
        # can skip that.
759
        err = (KnownFailure, KnownFailure('foo'), None)
760
        result.report_known_failure(test, err)
761
        output = result_stream.getvalue()[prefix:]
762
        lines = output.splitlines()
2418.3.1 by John Arbash Meinel
Remove timing dependencies from the selftest tests.
763
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
764
        self.assertEqual(lines[1], '    foo')
765
        self.assertEqual(2, len(lines))
766
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
767
    def test_text_report_known_failure(self):
768
        # text test output formatting
769
        pb = MockProgress()
770
        result = bzrlib.tests.TextTestResult(
771
            None,
772
            descriptions=0,
773
            verbosity=1,
774
            pb=pb,
775
            )
776
        test = self.get_passing_test()
777
        # this seeds the state to handle reporting the test.
778
        result.startTest(test)
779
        result.extractBenchmarkTime(test)
780
        # the err parameter has the shape:
781
        # (class, exception object, traceback)
782
        # KnownFailures dont get their tracebacks shown though, so we
783
        # can skip that.
784
        err = (KnownFailure, KnownFailure('foo'), None)
785
        result.report_known_failure(test, err)
786
        self.assertEqual(
787
            [
788
            ('update', '[1 in 0s] passing_test', None, None),
789
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
790
            ],
791
            pb.calls)
792
        # known_failures should be printed in the summary, so if we run a test
793
        # after there are some known failures, the update prefix should match
794
        # this.
795
        result.known_failure_count = 3
796
        test.run(result)
797
        self.assertEqual(
798
            [
799
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
800
            ],
801
            pb.calls[2:])
802
803
    def get_passing_test(self):
804
        """Return a test object that can't be run usefully."""
805
        def passing_test():
806
            pass
807
        return unittest.FunctionTestCase(passing_test)
808
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
809
    def test_add_not_supported(self):
810
        """Test the behaviour of invoking addNotSupported."""
811
        class InstrumentedTestResult(ExtendedTestResult):
812
            def report_test_start(self, test): pass
813
            def report_unsupported(self, test, feature):
814
                self._call = test, feature
815
        result = InstrumentedTestResult(None, None, None, None)
816
        test = SampleTestCase('_test_pass')
817
        feature = Feature()
818
        result.startTest(test)
819
        result.addNotSupported(test, feature)
820
        # it should invoke 'report_unsupported'.
821
        self.assertEqual(2, len(result._call))
822
        self.assertEqual(test, result._call[0])
823
        self.assertEqual(feature, result._call[1])
824
        # the result should be successful.
825
        self.assertTrue(result.wasSuccessful())
826
        # it should record the test against a count of tests not run due to
827
        # this feature.
828
        self.assertEqual(1, result.unsupported['Feature'])
829
        # and invoking it again should increment that counter
830
        result.addNotSupported(test, feature)
831
        self.assertEqual(2, result.unsupported['Feature'])
832
833
    def test_verbose_report_unsupported(self):
834
        # verbose test output formatting
835
        result_stream = StringIO()
836
        result = bzrlib.tests.VerboseTestResult(
837
            unittest._WritelnDecorator(result_stream),
838
            descriptions=0,
839
            verbosity=2,
840
            )
841
        test = self.get_passing_test()
842
        feature = Feature()
843
        result.startTest(test)
844
        result.extractBenchmarkTime(test)
845
        prefix = len(result_stream.getvalue())
846
        result.report_unsupported(test, feature)
847
        output = result_stream.getvalue()[prefix:]
848
        lines = output.splitlines()
849
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
850
    
851
    def test_text_report_unsupported(self):
852
        # text test output formatting
853
        pb = MockProgress()
854
        result = bzrlib.tests.TextTestResult(
855
            None,
856
            descriptions=0,
857
            verbosity=1,
858
            pb=pb,
859
            )
860
        test = self.get_passing_test()
861
        feature = Feature()
862
        # this seeds the state to handle reporting the test.
863
        result.startTest(test)
864
        result.extractBenchmarkTime(test)
865
        result.report_unsupported(test, feature)
866
        # no output on unsupported features
867
        self.assertEqual(
868
            [('update', '[1 in 0s] passing_test', None, None)
869
            ],
870
            pb.calls)
871
        # the number of missing features should be printed in the progress
872
        # summary, so check for that.
873
        result.unsupported = {'foo':0, 'bar':0}
874
        test.run(result)
875
        self.assertEqual(
876
            [
877
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
878
            ],
879
            pb.calls[1:])
880
    
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
881
    def test_unavailable_exception(self):
882
        """An UnavailableFeature being raised should invoke addNotSupported."""
883
        class InstrumentedTestResult(ExtendedTestResult):
884
885
            def report_test_start(self, test): pass
886
            def addNotSupported(self, test, feature):
887
                self._call = test, feature
888
        result = InstrumentedTestResult(None, None, None, None)
889
        feature = Feature()
890
        def test_function():
891
            raise UnavailableFeature(feature)
892
        test = unittest.FunctionTestCase(test_function)
893
        test.run(result)
894
        # it should invoke 'addNotSupported'.
895
        self.assertEqual(2, len(result._call))
896
        self.assertEqual(test, result._call[0])
897
        self.assertEqual(feature, result._call[1])
898
        # and not count as an error
899
        self.assertEqual(0, result.error_count)
900
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
901
902
class TestRunner(TestCase):
903
904
    def dummy_test(self):
905
        pass
906
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
907
    def run_test_runner(self, testrunner, test):
908
        """Run suite in testrunner, saving global state and restoring it.
909
910
        This current saves and restores:
911
        TestCaseInTempDir.TEST_ROOT
912
        
913
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
914
        without using this convenience method, because of our use of global state.
915
        """
916
        old_root = TestCaseInTempDir.TEST_ROOT
917
        try:
918
            TestCaseInTempDir.TEST_ROOT = None
919
            return testrunner.run(test)
920
        finally:
921
            TestCaseInTempDir.TEST_ROOT = old_root
922
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
923
    def test_known_failure_failed_run(self):
924
        # run a test that generates a known failure which should be printed in
925
        # the final output when real failures occur.
926
        def known_failure_test():
927
            raise KnownFailure('failed')
928
        test = unittest.TestSuite()
929
        test.addTest(unittest.FunctionTestCase(known_failure_test))
930
        def failing_test():
931
            raise AssertionError('foo')
932
        test.addTest(unittest.FunctionTestCase(failing_test))
933
        stream = StringIO()
934
        runner = TextTestRunner(stream=stream)
935
        result = self.run_test_runner(runner, test)
936
        lines = stream.getvalue().splitlines()
937
        self.assertEqual([
938
            '',
939
            '======================================================================',
940
            'FAIL: unittest.FunctionTestCase (failing_test)',
941
            '----------------------------------------------------------------------',
942
            'Traceback (most recent call last):',
943
            '    raise AssertionError(\'foo\')',
944
            'AssertionError: foo',
945
            '',
946
            '----------------------------------------------------------------------',
947
            '',
948
            'FAILED (failures=1, known_failure_count=1)'],
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
949
            lines[0:5] + lines[6:10] + lines[11:])
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
950
951
    def test_known_failure_ok_run(self):
952
        # run a test that generates a known failure which should be printed in the final output.
953
        def known_failure_test():
954
            raise KnownFailure('failed')
955
        test = unittest.FunctionTestCase(known_failure_test)
956
        stream = StringIO()
957
        runner = TextTestRunner(stream=stream)
958
        result = self.run_test_runner(runner, test)
2418.3.1 by John Arbash Meinel
Remove timing dependencies from the selftest tests.
959
        self.assertContainsRe(stream.getvalue(),
960
            '\n'
961
            '-*\n'
962
            'Ran 1 test in .*\n'
963
            '\n'
964
            'OK \\(known_failures=1\\)\n')
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
965
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
966
    def test_skipped_test(self):
967
        # run a test that is skipped, and check the suite as a whole still
968
        # succeeds.
969
        # skipping_test must be hidden in here so it's not run as a real test
970
        def skipping_test():
971
            raise TestSkipped('test intentionally skipped')
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
972
2485.6.5 by Martin Pool
Remove keep_output option
973
        runner = TextTestRunner(stream=self._log_file)
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
974
        test = unittest.FunctionTestCase(skipping_test)
975
        result = self.run_test_runner(runner, test)
976
        self.assertTrue(result.wasSuccessful())
977
978
    def test_skipped_from_setup(self):
979
        class SkippedSetupTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
980
981
            def setUp(self):
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
982
                self.counter = 1
983
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
984
                raise TestSkipped('skipped setup')
985
986
            def test_skip(self):
987
                self.fail('test reached')
988
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
989
            def cleanup(self):
990
                self.counter -= 1
991
2485.6.5 by Martin Pool
Remove keep_output option
992
        runner = TextTestRunner(stream=self._log_file)
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
993
        test = SkippedSetupTest('test_skip')
994
        result = self.run_test_runner(runner, test)
995
        self.assertTrue(result.wasSuccessful())
996
        # Check if cleanup was called the right number of times.
997
        self.assertEqual(0, test.counter)
998
999
    def test_skipped_from_test(self):
1000
        class SkippedTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1001
1002
            def setUp(self):
1003
                self.counter = 1
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1004
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1005
1006
            def test_skip(self):
1007
                raise TestSkipped('skipped test')
1008
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1009
            def cleanup(self):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1010
                self.counter -= 1
1011
2485.6.5 by Martin Pool
Remove keep_output option
1012
        runner = TextTestRunner(stream=self._log_file)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1013
        test = SkippedTest('test_skip')
1014
        result = self.run_test_runner(runner, test)
1015
        self.assertTrue(result.wasSuccessful())
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1016
        # Check if cleanup was called the right number of times.
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1017
        self.assertEqual(0, test.counter)
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1018
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1019
    def test_unsupported_features_listed(self):
1020
        """When unsupported features are encountered they are detailed."""
1021
        class Feature1(Feature):
1022
            def _probe(self): return False
1023
        class Feature2(Feature):
1024
            def _probe(self): return False
1025
        # create sample tests
1026
        test1 = SampleTestCase('_test_pass')
1027
        test1._test_needs_features = [Feature1()]
1028
        test2 = SampleTestCase('_test_pass')
1029
        test2._test_needs_features = [Feature2()]
1030
        test = unittest.TestSuite()
1031
        test.addTest(test1)
1032
        test.addTest(test2)
1033
        stream = StringIO()
1034
        runner = TextTestRunner(stream=stream)
1035
        result = self.run_test_runner(runner, test)
1036
        lines = stream.getvalue().splitlines()
1037
        self.assertEqual([
1038
            'OK',
1039
            "Missing feature 'Feature1' skipped 1 tests.",
1040
            "Missing feature 'Feature2' skipped 1 tests.",
1041
            ],
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1042
            lines[-3:])
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1043
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1044
    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.
1045
        # tests that the running the benchmark produces a history file
1046
        # containing a timestamp and the revision id of the bzrlib source which
1047
        # was tested.
1048
        workingtree = _get_bzr_source_tree()
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1049
        test = TestRunner('dummy_test')
1050
        output = StringIO()
1051
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
1052
        result = self.run_test_runner(runner, test)
1053
        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.
1054
        self.assertContainsRe(output_string, "--date [0-9.]+")
1055
        if workingtree is not None:
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
1056
            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.
1057
            self.assertEndsWith(output_string.rstrip(), revision_id)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1058
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
1059
    def test_success_log_deleted(self):
1060
        """Successful tests have their log deleted"""
1061
1062
        class LogTester(TestCase):
1063
1064
            def test_success(self):
1065
                self.log('this will be removed\n')
1066
1067
        sio = cStringIO.StringIO()
1068
        runner = TextTestRunner(stream=sio)
1069
        test = LogTester('test_success')
1070
        result = self.run_test_runner(runner, test)
1071
1072
        log = test._get_log()
1073
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1074
        self.assertEqual('', test._log_contents)
1075
        self.assertIs(None, test._log_file_name)
1076
1077
    def test_fail_log_kept(self):
1078
        """Failed tests have their log kept"""
1079
1080
        class LogTester(TestCase):
1081
1082
            def test_fail(self):
1083
                self.log('this will be kept\n')
1084
                self.fail('this test fails')
1085
1086
        sio = cStringIO.StringIO()
1087
        runner = TextTestRunner(stream=sio)
1088
        test = LogTester('test_fail')
1089
        result = self.run_test_runner(runner, test)
1090
1091
        text = sio.getvalue()
1092
        self.assertContainsRe(text, 'this will be kept')
1093
        self.assertContainsRe(text, 'this test fails')
1094
1095
        log = test._get_log()
1096
        self.assertContainsRe(log, 'this will be kept')
1097
        self.assertEqual(log, test._log_contents)
1098
1099
    def test_error_log_kept(self):
1100
        """Tests with errors have their log kept"""
1101
1102
        class LogTester(TestCase):
1103
1104
            def test_error(self):
1105
                self.log('this will be kept\n')
1106
                raise ValueError('random exception raised')
1107
1108
        sio = cStringIO.StringIO()
1109
        runner = TextTestRunner(stream=sio)
1110
        test = LogTester('test_error')
1111
        result = self.run_test_runner(runner, test)
1112
1113
        text = sio.getvalue()
1114
        self.assertContainsRe(text, 'this will be kept')
1115
        self.assertContainsRe(text, 'random exception raised')
1116
1117
        log = test._get_log()
1118
        self.assertContainsRe(log, 'this will be kept')
1119
        self.assertEqual(log, test._log_contents)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1120
2036.1.2 by John Arbash Meinel
whitespace fix
1121
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1122
class SampleTestCase(TestCase):
1123
1124
    def _test_pass(self):
1125
        pass
1126
1127
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1128
class TestTestCase(TestCase):
1129
    """Tests that test the core bzrlib TestCase."""
1130
2560.1.1 by Robert Collins
Make debug.debug_flags be isolated for all tests.
1131
    def test_debug_flags_sanitised(self):
1132
        """The bzrlib debug flags should be sanitised by setUp."""
1133
        # we could set something and run a test that will check
1134
        # it gets santised, but this is probably sufficient for now:
1135
        # if someone runs the test with -Dsomething it will error.
1136
        self.assertEqual(set(), bzrlib.debug.debug_flags)
1137
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1138
    def inner_test(self):
1139
        # the inner child test
1140
        note("inner_test")
1141
1142
    def outer_child(self):
1143
        # the outer child test
1144
        note("outer_start")
1145
        self.inner_test = TestTestCase("inner_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1146
        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.
1147
                                        descriptions=0,
1148
                                        verbosity=1)
1149
        self.inner_test.run(result)
1150
        note("outer finish")
1151
1152
    def test_trace_nesting(self):
1153
        # this tests that each test case nests its trace facility correctly.
1154
        # we do this by running a test case manually. That test case (A)
1155
        # should setup a new log, log content to it, setup a child case (B),
1156
        # which should log independently, then case (A) should log a trailer
1157
        # and return.
1158
        # we do two nested children so that we can verify the state of the 
1159
        # logs after the outer child finishes is correct, which a bad clean
1160
        # up routine in tearDown might trigger a fault in our test with only
1161
        # one child, we should instead see the bad result inside our test with
1162
        # the two children.
1163
        # the outer child test
1164
        original_trace = bzrlib.trace._trace_file
1165
        outer_test = TestTestCase("outer_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1166
        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.
1167
                                        descriptions=0,
1168
                                        verbosity=1)
1169
        outer_test.run(result)
1170
        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)
1171
1172
    def method_that_times_a_bit_twice(self):
1173
        # 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.
1174
        self.time(time.sleep, 0.007)
1175
        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)
1176
1177
    def test_time_creates_benchmark_in_result(self):
1178
        """Test that the TestCase.time() method accumulates a benchmark time."""
1179
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1180
        output_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
1181
        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)
1182
            unittest._WritelnDecorator(output_stream),
1183
            descriptions=0,
2095.4.1 by Martin Pool
Better progress bars during tests
1184
            verbosity=2,
1185
            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)
1186
        sample_test.run(result)
1187
        self.assertContainsRe(
1188
            output_stream.getvalue(),
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
1189
            r"\d+ms/ +\d+ms\n$")
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1190
1191
    def test_hooks_sanitised(self):
1192
        """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.
1193
        self.assertEqual(bzrlib.branch.BranchHooks(),
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1194
            bzrlib.branch.Branch.hooks)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
1195
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1196
            bzrlib.smart.server.SmartTCPServer.hooks)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1197
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
1198
    def test__gather_lsprof_in_benchmarks(self):
1199
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1200
        
1201
        Each self.time() call is individually and separately profiled.
1202
        """
1551.15.28 by Aaron Bentley
Improve Feature usage style w/ lsprof
1203
        self.requireFeature(test_lsprof.LSProfFeature)
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
1204
        # overrides the class member with an instance member so no cleanup 
1205
        # needed.
1206
        self._gather_lsprof_in_benchmarks = True
1207
        self.time(time.sleep, 0.000)
1208
        self.time(time.sleep, 0.003)
1209
        self.assertEqual(2, len(self._benchcalls))
1210
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1211
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1212
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1213
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1214
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1215
    def test_knownFailure(self):
1216
        """Self.knownFailure() should raise a KnownFailure exception."""
1217
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1218
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1219
    def test_requireFeature_available(self):
1220
        """self.requireFeature(available) is a no-op."""
1221
        class Available(Feature):
1222
            def _probe(self):return True
1223
        feature = Available()
1224
        self.requireFeature(feature)
1225
1226
    def test_requireFeature_unavailable(self):
1227
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1228
        class Unavailable(Feature):
1229
            def _probe(self):return False
1230
        feature = Unavailable()
1231
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1232
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1233
    def test_run_no_parameters(self):
1234
        test = SampleTestCase('_test_pass')
1235
        test.run()
1236
    
1237
    def test_run_enabled_unittest_result(self):
1238
        """Test we revert to regular behaviour when the test is enabled."""
1239
        test = SampleTestCase('_test_pass')
1240
        class EnabledFeature(object):
1241
            def available(self):
1242
                return True
1243
        test._test_needs_features = [EnabledFeature()]
1244
        result = unittest.TestResult()
1245
        test.run(result)
1246
        self.assertEqual(1, result.testsRun)
1247
        self.assertEqual([], result.errors)
1248
        self.assertEqual([], result.failures)
1249
1250
    def test_run_disabled_unittest_result(self):
1251
        """Test our compatability for disabled tests with unittest results."""
1252
        test = SampleTestCase('_test_pass')
1253
        class DisabledFeature(object):
1254
            def available(self):
1255
                return False
1256
        test._test_needs_features = [DisabledFeature()]
1257
        result = unittest.TestResult()
1258
        test.run(result)
1259
        self.assertEqual(1, result.testsRun)
1260
        self.assertEqual([], result.errors)
1261
        self.assertEqual([], result.failures)
1262
1263
    def test_run_disabled_supporting_result(self):
1264
        """Test disabled tests behaviour with support aware results."""
1265
        test = SampleTestCase('_test_pass')
1266
        class DisabledFeature(object):
1267
            def available(self):
1268
                return False
1269
        the_feature = DisabledFeature()
1270
        test._test_needs_features = [the_feature]
1271
        class InstrumentedTestResult(unittest.TestResult):
1272
            def __init__(self):
1273
                unittest.TestResult.__init__(self)
1274
                self.calls = []
1275
            def startTest(self, test):
1276
                self.calls.append(('startTest', test))
1277
            def stopTest(self, test):
1278
                self.calls.append(('stopTest', test))
1279
            def addNotSupported(self, test, feature):
1280
                self.calls.append(('addNotSupported', test, feature))
1281
        result = InstrumentedTestResult()
1282
        test.run(result)
1283
        self.assertEqual([
1284
            ('startTest', test),
1285
            ('addNotSupported', test, the_feature),
1286
            ('stopTest', test),
1287
            ],
1288
            result.calls)
1289
1534.11.4 by Robert Collins
Merge from mainline.
1290
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1291
@symbol_versioning.deprecated_function(zero_eleven)
1292
def sample_deprecated_function():
1293
    """A deprecated function to test applyDeprecated with."""
1294
    return 2
1295
1296
1297
def sample_undeprecated_function(a_param):
1298
    """A undeprecated function to test applyDeprecated with."""
1299
1300
1301
class ApplyDeprecatedHelper(object):
1302
    """A helper class for ApplyDeprecated tests."""
1303
1304
    @symbol_versioning.deprecated_method(zero_eleven)
1305
    def sample_deprecated_method(self, param_one):
1306
        """A deprecated method for testing with."""
1307
        return param_one
1308
1309
    def sample_normal_method(self):
1310
        """A undeprecated method."""
1311
1312
    @symbol_versioning.deprecated_method(zero_ten)
1313
    def sample_nested_deprecation(self):
1314
        return sample_deprecated_function()
1315
1316
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
1317
class TestExtraAssertions(TestCase):
1318
    """Tests for new test assertions in bzrlib test suite"""
1319
1320
    def test_assert_isinstance(self):
1321
        self.assertIsInstance(2, int)
1322
        self.assertIsInstance(u'', basestring)
1323
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1324
        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
1325
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
1326
    def test_assertEndsWith(self):
1327
        self.assertEndsWith('foo', 'oo')
1328
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1329
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1330
    def test_applyDeprecated_not_deprecated(self):
1331
        sample_object = ApplyDeprecatedHelper()
1332
        # calling an undeprecated callable raises an assertion
1333
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1334
            sample_object.sample_normal_method)
1335
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1336
            sample_undeprecated_function, "a param value")
1337
        # calling a deprecated callable (function or method) with the wrong
1338
        # expected deprecation fails.
1339
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1340
            sample_object.sample_deprecated_method, "a param value")
1341
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1342
            sample_deprecated_function)
1343
        # calling a deprecated callable (function or method) with the right
1344
        # expected deprecation returns the functions result.
1345
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1346
            sample_object.sample_deprecated_method, "a param value"))
1347
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
1348
            sample_deprecated_function))
1349
        # calling a nested deprecation with the wrong deprecation version
1350
        # fails even if a deeper nested function was deprecated with the 
1351
        # supplied version.
1352
        self.assertRaises(AssertionError, self.applyDeprecated,
1353
            zero_eleven, sample_object.sample_nested_deprecation)
1354
        # calling a nested deprecation with the right deprecation value
1355
        # returns the calls result.
1356
        self.assertEqual(2, self.applyDeprecated(zero_ten,
1357
            sample_object.sample_nested_deprecation))
1358
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1359
    def test_callDeprecated(self):
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1360
        def testfunc(be_deprecated, result=None):
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1361
            if be_deprecated is True:
1362
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
1363
                                       stacklevel=1)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1364
            return result
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1365
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1366
        self.assertIs(None, result)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1367
        result = self.callDeprecated([], testfunc, False, 'result')
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1368
        self.assertEqual('result', result)
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1369
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1370
        self.callDeprecated([], testfunc, be_deprecated=False)
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1371
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1372
1373
class TestConvenienceMakers(TestCaseWithTransport):
1374
    """Test for the make_* convenience functions."""
1375
1376
    def test_make_branch_and_tree_with_format(self):
1377
        # we should be able to supply a format to make_branch_and_tree
1378
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1379
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1380
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1381
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1382
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1383
                              bzrlib.bzrdir.BzrDirFormat6)
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1384
1986.2.1 by Robert Collins
Bugfix - the name of the test for make_branch_and_memory_tree was wrong.
1385
    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
1386
        # we should be able to get a new branch and a mutable tree from
1387
        # TestCaseWithTransport
1388
        tree = self.make_branch_and_memory_tree('a')
1389
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1390
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1391
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
1392
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1393
1394
    def test_make_tree_for_sftp_branch(self):
1395
        """Transports backed by local directories create local trees."""
1396
1397
        tree = self.make_branch_and_tree('t1')
1398
        base = tree.bzrdir.root_transport.base
1399
        self.failIf(base.startswith('sftp'),
1400
                'base %r is on sftp but should be local' % base)
1401
        self.assertEquals(tree.bzrdir.root_transport,
1402
                tree.branch.bzrdir.root_transport)
1403
        self.assertEquals(tree.bzrdir.root_transport,
1404
                tree.branch.repository.bzrdir.root_transport)
1405
1406
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1407
class TestSelftest(TestCase):
1408
    """Tests of bzrlib.tests.selftest."""
1409
1410
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1411
        factory_called = []
1412
        def factory():
1413
            factory_called.append(True)
1414
            return TestSuite()
1415
        out = StringIO()
1416
        err = StringIO()
1417
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1418
            test_suite_factory=factory)
1419
        self.assertEqual([True], factory_called)
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1420
1421
1422
class TestSelftestCleanOutput(TestCaseInTempDir):
1423
1424
    def test_clean_output(self):
1425
        # test functionality of clean_selftest_output()
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
1426
        self.build_tree(['test0000.tmp/', 'test0001.tmp/',
1427
                         'bzrlib/', 'tests/',
1428
                         'bzr', 'setup.py', 'test9999.tmp'])
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1429
1430
        root = os.getcwdu()
1431
        before = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1432
        before.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1433
        self.assertEquals(['bzr','bzrlib','setup.py',
1434
                           'test0000.tmp','test0001.tmp',
1435
                           'test9999.tmp','tests'],
1436
                           before)
1437
        clean_selftest_output(root, quiet=True)
1438
        after = os.listdir(root)
2172.4.5 by Alexander Belchenko
Small fix: output of os.listdir() should be sorted manually
1439
        after.sort()
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1440
        self.assertEquals(['bzr','bzrlib','setup.py',
1441
                           'test9999.tmp','tests'],
1442
                           after)
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1443
2379.6.4 by Alexander Belchenko
Teach `bzr selftest --clean-output` to remove read-only files (win32-specific)
1444
    def test_clean_readonly(self):
1445
        # test for delete read-only files
1446
        self.build_tree(['test0000.tmp/', 'test0000.tmp/foo'])
1447
        osutils.make_readonly('test0000.tmp/foo')
1448
        root = os.getcwdu()
1449
        before = os.listdir(root);  before.sort()
1450
        self.assertEquals(['test0000.tmp'], before)
1451
        clean_selftest_output(root, quiet=True)
1452
        after = os.listdir(root);   after.sort()
1453
        self.assertEquals([], after)
1454
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1455
1456
class TestKnownFailure(TestCase):
1457
1458
    def test_known_failure(self):
1459
        """Check that KnownFailure is defined appropriately."""
1460
        # a KnownFailure is an assertion error for compatability with unaware
1461
        # runners.
1462
        self.assertIsInstance(KnownFailure(""), AssertionError)
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1463
1551.13.9 by Aaron Bentley
Implement TestCase.expectFailure
1464
    def test_expect_failure(self):
1465
        try:
1466
            self.expectFailure("Doomed to failure", self.assertTrue, False)
1467
        except KnownFailure, e:
1468
            self.assertEqual('Doomed to failure', e.args[0])
1469
        try:
1470
            self.expectFailure("Doomed to failure", self.assertTrue, True)
1471
        except AssertionError, e:
1472
            self.assertEqual('Unexpected success.  Should have failed:'
1473
                             ' Doomed to failure', e.args[0])
1474
        else:
1475
            self.fail('Assertion not raised')
1476
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1477
1478
class TestFeature(TestCase):
1479
1480
    def test_caching(self):
1481
        """Feature._probe is called by the feature at most once."""
1482
        class InstrumentedFeature(Feature):
1483
            def __init__(self):
1484
                Feature.__init__(self)
1485
                self.calls = []
1486
            def _probe(self):
1487
                self.calls.append('_probe')
1488
                return False
1489
        feature = InstrumentedFeature()
1490
        feature.available()
1491
        self.assertEqual(['_probe'], feature.calls)
1492
        feature.available()
1493
        self.assertEqual(['_probe'], feature.calls)
1494
1495
    def test_named_str(self):
1496
        """Feature.__str__ should thunk to feature_name()."""
1497
        class NamedFeature(Feature):
1498
            def feature_name(self):
1499
                return 'symlinks'
1500
        feature = NamedFeature()
1501
        self.assertEqual('symlinks', str(feature))
1502
1503
    def test_default_str(self):
1504
        """Feature.__str__ should default to __class__.__name__."""
1505
        class NamedFeature(Feature):
1506
            pass
1507
        feature = NamedFeature()
1508
        self.assertEqual('NamedFeature', str(feature))
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1509
1510
1511
class TestUnavailableFeature(TestCase):
1512
1513
    def test_access_feature(self):
1514
        feature = Feature()
1515
        exception = UnavailableFeature(feature)
1516
        self.assertIs(feature, exception.args[0])
2394.2.5 by Ian Clatworthy
list-only working, include test not
1517
1518
1519
class TestSelftestFiltering(TestCase):
1520
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
1521
    def setUp(self):
1522
        self.suite = TestUtil.TestSuite()
1523
        self.loader = TestUtil.TestLoader()
1524
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1525
            'bzrlib.tests.test_selftest']))
1526
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
1527
2394.2.5 by Ian Clatworthy
list-only working, include test not
1528
    def test_filter_suite_by_re(self):
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
1529
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1530
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
1531
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1532
            'TestSelftestFiltering.test_filter_suite_by_re'])
1533
            
1534
    def test_sort_suite_by_re(self):
1535
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1536
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
1537
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1538
            'TestSelftestFiltering.test_filter_suite_by_re')
1539
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1540