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