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