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