/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
1
# Copyright (C) 2005, 2006, 2007, 2008 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,
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
33
    remote,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
34
    repository,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
35
    symbol_versioning,
3193.1.1 by Vincent Ladeuil
Helper to filter test suite building by module when loading a list.
36
    tests,
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
37
    workingtree,
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
38
    )
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
39
from bzrlib.progress import _BaseProgressBar
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
40
from bzrlib.repofmt import (
41
    pack_repo,
42
    weaverepo,
43
    )
2696.1.1 by Martin Pool
Remove things deprecated in 0.11 and earlier
44
from bzrlib.symbol_versioning import (
3128.1.2 by Vincent Ladeuil
Tweak as per review feedback: s/randomise.*/randomize&/, 0.92 -> 1.0.
45
    one_zero,
46
    zero_eleven,
47
    zero_ten,
48
    )
1526.1.3 by Robert Collins
Merge from upstream.
49
from bzrlib.tests import (
1534.4.31 by Robert Collins
cleanedup test_outside_wt
50
                          ChrootedTestCase,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
51
                          ExtendedTestResult,
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
52
                          Feature,
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
53
                          KnownFailure,
1526.1.3 by Robert Collins
Merge from upstream.
54
                          TestCase,
55
                          TestCaseInTempDir,
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
56
                          TestCaseWithMemoryTransport,
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
57
                          TestCaseWithTransport,
2729.1.1 by Martin Pool
Add TestNotApplicable exception and handling of it; document test parameterization
58
                          TestNotApplicable,
1526.1.3 by Robert Collins
Merge from upstream.
59
                          TestSkipped,
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
60
                          TestSuite,
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
61
                          TestUtil,
1526.1.3 by Robert Collins
Merge from upstream.
62
                          TextTestRunner,
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
63
                          UnavailableFeature,
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
64
                          condition_id_re,
2921.6.8 by Robert Collins
* New helper function ``bzrlib.tests.condition_isinstance`` which helps
65
                          condition_isinstance,
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
66
                          exclude_tests_by_condition,
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
67
                          exclude_tests_by_re,
2921.6.7 by Robert Collins
* New helper function ``bzrlib.tests.filter_suite_by_condition`` which
68
                          filter_suite_by_condition,
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
69
                          filter_suite_by_re,
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
70
                          iter_suite_tests,
2921.6.6 by Robert Collins
* The ``exclude_pattern`` and ``random_order`` parameters to the function
71
                          preserve_input,
3128.1.2 by Vincent Ladeuil
Tweak as per review feedback: s/randomise.*/randomize&/, 0.92 -> 1.0.
72
                          randomize_suite,
3350.5.1 by Robert Collins
* New helper function for splitting test suites ``split_suite_by_condition``.
73
                          split_suite_by_condition,
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
74
                          split_suite_by_re,
2493.2.8 by Aaron Bentley
Convert old lsprof tests to require new LSProf Feature instead of skipping
75
                          test_lsprof,
76
                          test_suite,
1526.1.3 by Robert Collins
Merge from upstream.
77
                          )
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
78
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
79
from bzrlib.tests.TestUtil import _load_module_by_name
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
80
from bzrlib.trace import note
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
81
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.
82
from bzrlib.version import _get_bzr_source_tree
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
83
84
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
85
def _test_ids(test_suite):
86
    """Get the ids for the tests in a test suite."""
87
    return [t.id() for t in iter_suite_tests(test_suite)]
88
89
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
90
class SelftestTests(TestCase):
91
92
    def test_import_tests(self):
93
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
94
        self.assertEqual(mod.SelftestTests, SelftestTests)
95
96
    def test_import_test_failure(self):
97
        self.assertRaises(ImportError,
98
                          _load_module_by_name,
99
                          'bzrlib.no-name-yet')
100
101
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.
102
1185.51.1 by Martin Pool
Better message when failing to import a test suite.
103
    def test_logging(self):
104
        """Test logs are captured when a test fails."""
105
        self.log('a test message')
106
        self._log_file.flush()
1927.3.1 by Carl Friedrich Bolz
Throw away on-disk logfile when possible.
107
        self.assertContainsRe(self._get_log(keep_log_file=True),
108
                              'a test message\n')
1185.33.95 by Martin Pool
New TestSkipped facility, and tests for it.
109
110
3287.20.2 by John Arbash Meinel
Raise a clear error about the offending filename when there is a filename with bad characters.
111
class TestUnicodeFilename(TestCase):
112
113
    def test_probe_passes(self):
114
        """UnicodeFilename._probe passes."""
115
        # We can't test much more than that because the behaviour depends
116
        # on the platform.
117
        tests.UnicodeFilename._probe()
118
119
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.
120
class TestTreeShape(TestCaseInTempDir):
121
122
    def test_unicode_paths(self):
3287.20.2 by John Arbash Meinel
Raise a clear error about the offending filename when there is a filename with bad characters.
123
        self.requireFeature(tests.UnicodeFilename)
124
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.
125
        filename = u'hell\u00d8'
3287.20.2 by John Arbash Meinel
Raise a clear error about the offending filename when there is a filename with bad characters.
126
        self.build_tree_contents([(filename, 'contents of hello')])
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.
127
        self.failUnlessExists(filename)
1526.1.3 by Robert Collins
Merge from upstream.
128
129
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
130
class TestTransportProviderAdapter(TestCase):
1530.1.21 by Robert Collins
Review feedback fixes.
131
    """A group of tests that test the transport implementation adaption core.
132
1551.1.1 by Martin Pool
[merge] branch-formats branch, and reconcile changes
133
    This is a meta test that the tests are applied to all available 
134
    transports.
135
1530.1.21 by Robert Collins
Review feedback fixes.
136
    This will be generalised in the future which is why it is in this 
137
    test file even though it is specific to transport tests at the moment.
138
    """
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
139
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
140
    def test_get_transport_permutations(self):
3455.1.1 by Vincent Ladeuil
Fix typos in comments.
141
        # this checks that get_test_permutations defined by the module is
142
        # called by the adapter get_transport_test_permutations 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.
143
        class MockModule(object):
144
            def get_test_permutations(self):
145
                return sample_permutation
146
        sample_permutation = [(1,2), (3,4)]
2553.2.5 by Robert Collins
And overhaul TransportTestProviderAdapter too.
147
        from bzrlib.tests.test_transport_implementations \
148
            import TransportTestProviderAdapter
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.
149
        adapter = TransportTestProviderAdapter()
150
        self.assertEqual(sample_permutation,
151
                         adapter.get_transport_test_permutations(MockModule()))
152
153
    def test_adapter_checks_all_modules(self):
3455.1.1 by Vincent Ladeuil
Fix typos in comments.
154
        # this checks that the adapter returns as many permutations as there
155
        # are in all the registered transport modules - we assume if this
156
        # matches its probably doing the right thing especially in combination
157
        # with the tests for setting the right classes below.
2553.2.5 by Robert Collins
And overhaul TransportTestProviderAdapter too.
158
        from bzrlib.tests.test_transport_implementations \
159
            import TransportTestProviderAdapter
160
        from bzrlib.transport import _get_transport_modules
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.
161
        modules = _get_transport_modules()
162
        permutation_count = 0
163
        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.
164
            try:
165
                permutation_count += len(reduce(getattr, 
166
                    (module + ".get_test_permutations").split('.')[1:],
167
                     __import__(module))())
168
            except errors.DependencyNotPresent:
169
                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.
170
        input_test = TestTransportProviderAdapter(
171
            "test_adapter_sets_transport_class")
172
        adapter = TransportTestProviderAdapter()
173
        self.assertEqual(permutation_count,
174
                         len(list(iter(adapter.adapt(input_test)))))
175
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
176
    def test_adapter_sets_transport_class(self):
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
177
        # Check that the test adapter inserts a transport and server into the
178
        # generated test.
179
        #
180
        # This test used to know about all the possible transports and the
181
        # order they were returned but that seems overly brittle (mbp
182
        # 20060307)
2553.2.5 by Robert Collins
And overhaul TransportTestProviderAdapter too.
183
        from bzrlib.tests.test_transport_implementations \
184
            import TransportTestProviderAdapter
185
        scenarios = TransportTestProviderAdapter().scenarios
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
186
        # there are at least that many builtin transports
2553.2.5 by Robert Collins
And overhaul TransportTestProviderAdapter too.
187
        self.assertTrue(len(scenarios) > 6)
188
        one_scenario = scenarios[0]
189
        self.assertIsInstance(one_scenario[0], str)
190
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
191
                                   bzrlib.transport.Transport))
2553.2.5 by Robert Collins
And overhaul TransportTestProviderAdapter too.
192
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
1540.3.21 by Martin Pool
Trim test for TestTransportProviderAdapter to be less dependent on
193
                                   bzrlib.transport.Server))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
194
195
196
class TestBranchProviderAdapter(TestCase):
197
    """A group of tests that test the branch implementation test adapter."""
198
2553.2.6 by Robert Collins
And overhaul BranchTestProviderAdapter too.
199
    def test_constructor(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
200
        # check that constructor parameters are passed through to the adapted
201
        # test.
2553.2.6 by Robert Collins
And overhaul BranchTestProviderAdapter too.
202
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
203
        server1 = "a"
204
        server2 = "b"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
205
        formats = [("c", "C"), ("d", "D")]
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
206
        adapter = BranchTestProviderAdapter(server1, server2, formats)
2553.2.6 by Robert Collins
And overhaul BranchTestProviderAdapter too.
207
        self.assertEqual(2, len(adapter.scenarios))
208
        self.assertEqual([
209
            ('str',
210
             {'branch_format': 'c',
211
              'bzrdir_format': 'C',
212
              'transport_readonly_server': 'b',
213
              'transport_server': 'a'}),
214
            ('str',
215
             {'branch_format': 'd',
216
              'bzrdir_format': 'D',
217
              'transport_readonly_server': 'b',
218
              'transport_server': 'a'})],
219
            adapter.scenarios)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
220
221
1534.4.39 by Robert Collins
Basic BzrDir support.
222
class TestBzrDirProviderAdapter(TestCase):
223
    """A group of tests that test the bzr dir implementation test adapter."""
224
225
    def test_adapted_tests(self):
226
        # check that constructor parameters are passed through to the adapted
227
        # test.
2553.2.7 by Robert Collins
And overhaul BzrDirTestProviderAdapter too.
228
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
229
        vfs_factory = "v"
1534.4.39 by Robert Collins
Basic BzrDir support.
230
        server1 = "a"
231
        server2 = "b"
232
        formats = ["c", "d"]
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
233
        adapter = BzrDirTestProviderAdapter(vfs_factory,
234
            server1, server2, formats)
2553.2.7 by Robert Collins
And overhaul BzrDirTestProviderAdapter too.
235
        self.assertEqual([
236
            ('str',
237
             {'bzrdir_format': 'c',
238
              'transport_readonly_server': 'b',
239
              'transport_server': 'a',
240
              'vfs_transport_factory': 'v'}),
241
            ('str',
242
             {'bzrdir_format': 'd',
243
              'transport_readonly_server': 'b',
244
              'transport_server': 'a',
245
              'vfs_transport_factory': 'v'})],
246
            adapter.scenarios)
1534.4.39 by Robert Collins
Basic BzrDir support.
247
248
3221.10.5 by Robert Collins
Update repository parameterisation tests to match refactoring.
249
class TestRepositoryParameterisation(TestCase):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
250
    """A group of tests that test the repository implementation test adapter."""
251
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
252
    def test_formats_to_scenarios(self):
253
        """The adapter can generate all the scenarios needed."""
3221.10.5 by Robert Collins
Update repository parameterisation tests to match refactoring.
254
        from bzrlib.tests.repository_implementations import formats_to_scenarios
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
255
        formats = [("(c)", remote.RemoteRepositoryFormat()),
256
                   ("(d)", repository.format_registry.get(
257
                        'Bazaar pack repository format 1 (needs bzr 0.92)\n'))]
3221.10.5 by Robert Collins
Update repository parameterisation tests to match refactoring.
258
        no_vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
259
            None)
260
        vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
261
            vfs_transport_factory="vfs")
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
262
        # no_vfs generate scenarios without vfs_transport_factory
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
263
        self.assertEqual([
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
264
            ('RemoteRepositoryFormat(c)',
265
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
266
              'repository_format': remote.RemoteRepositoryFormat(),
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
267
              'transport_readonly_server': 'readonly',
268
              'transport_server': 'server'}),
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
269
            ('RepositoryFormatKnitPack1(d)',
270
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
271
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
272
              'transport_readonly_server': 'readonly',
273
              'transport_server': 'server'})],
3221.10.5 by Robert Collins
Update repository parameterisation tests to match refactoring.
274
            no_vfs_scenarios)
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
275
        self.assertEqual([
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
276
            ('RemoteRepositoryFormat(c)',
277
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
278
              'repository_format': remote.RemoteRepositoryFormat(),
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
279
              'transport_readonly_server': 'readonly',
280
              'transport_server': 'server',
281
              'vfs_transport_factory': 'vfs'}),
3543.1.4 by Martin Pool
test_formats_to_scenarios uses real format objects
282
            ('RepositoryFormatKnitPack1(d)',
283
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
284
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
285
              'transport_readonly_server': 'readonly',
286
              'transport_server': 'server',
287
              'vfs_transport_factory': 'vfs'})],
3221.10.5 by Robert Collins
Update repository parameterisation tests to match refactoring.
288
            vfs_scenarios)
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
289
2553.2.3 by Robert Collins
Split out the common test scenario support from the repository implementation specific code.
290
291
class TestTestScenarioApplier(TestCase):
292
    """Tests for the test adaption facilities."""
293
294
    def test_adapt_applies_scenarios(self):
295
        from bzrlib.tests.repository_implementations import TestScenarioApplier
296
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
297
        adapter = TestScenarioApplier()
298
        adapter.scenarios = [("1", "dict"), ("2", "settings")]
299
        calls = []
300
        def capture_call(test, scenario):
301
            calls.append((test, scenario))
302
            return test
303
        adapter.adapt_test_to_scenario = capture_call
304
        adapter.adapt(input_test)
305
        self.assertEqual([(input_test, ("1", "dict")),
306
            (input_test, ("2", "settings"))], calls)
307
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
308
    def test_adapt_test_to_scenario(self):
2553.2.3 by Robert Collins
Split out the common test scenario support from the repository implementation specific code.
309
        from bzrlib.tests.repository_implementations import TestScenarioApplier
310
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
311
        adapter = TestScenarioApplier()
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
312
        # setup two adapted tests
313
        adapted_test1 = adapter.adapt_test_to_scenario(input_test,
314
            ("new id",
315
            {"bzrdir_format":"bzr_format",
316
             "repository_format":"repo_fmt",
317
             "transport_server":"transport_server",
318
             "transport_readonly_server":"readonly-server"}))
319
        adapted_test2 = adapter.adapt_test_to_scenario(input_test,
320
            ("new id 2", {"bzrdir_format":None}))
321
        # input_test should have been altered.
322
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
323
        # the new tests are mutually incompatible, ensuring it has 
324
        # made new ones, and unspecified elements in the scenario
325
        # should not have been altered.
326
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
327
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
328
        self.assertEqual("transport_server", adapted_test1.transport_server)
329
        self.assertEqual("readonly-server",
330
            adapted_test1.transport_readonly_server)
331
        self.assertEqual(
2553.2.3 by Robert Collins
Split out the common test scenario support from the repository implementation specific code.
332
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
333
            "test_adapt_test_to_scenario(new id)",
334
            adapted_test1.id())
335
        self.assertEqual(None, adapted_test2.bzrdir_format)
336
        self.assertEqual(
2553.2.3 by Robert Collins
Split out the common test scenario support from the repository implementation specific code.
337
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
2553.2.1 by Robert Collins
Overhaul RepositoryTestAdapter to be cleaner and more modular.
338
            "test_adapt_test_to_scenario(new id 2)",
339
            adapted_test2.id())
2018.5.64 by Robert Collins
Allow Repository tests to be backed onto a specific VFS as needed.
340
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
341
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
342
class TestInterRepositoryProviderAdapter(TestCase):
343
    """A group of tests that test the InterRepository test adapter."""
344
345
    def test_adapted_tests(self):
346
        # check that constructor parameters are passed through to the adapted
347
        # test.
2553.2.4 by Robert Collins
Treat InterRepositoryTestProviderAdapter like RepositoryTestProviderAdapter
348
        from bzrlib.tests.interrepository_implementations import \
349
            InterRepositoryTestProviderAdapter
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
350
        server1 = "a"
351
        server2 = "b"
1563.2.20 by Robert Collins
Add a revision store test adapter.
352
        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.
353
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
2553.2.4 by Robert Collins
Treat InterRepositoryTestProviderAdapter like RepositoryTestProviderAdapter
354
        self.assertEqual([
3302.5.4 by Vincent Ladeuil
Make interreop parametrized tests IDs unique.
355
            ('str,str,str',
2553.2.4 by Robert Collins
Treat InterRepositoryTestProviderAdapter like RepositoryTestProviderAdapter
356
             {'interrepo_class': str,
357
              'repository_format': 'C1',
358
              'repository_format_to': 'C2',
359
              'transport_readonly_server': 'b',
360
              'transport_server': 'a'}),
3302.5.4 by Vincent Ladeuil
Make interreop parametrized tests IDs unique.
361
            ('int,str,str',
2553.2.4 by Robert Collins
Treat InterRepositoryTestProviderAdapter like RepositoryTestProviderAdapter
362
             {'interrepo_class': int,
363
              'repository_format': 'D1',
364
              'repository_format_to': 'D2',
365
              'transport_readonly_server': 'b',
366
              'transport_server': 'a'})],
367
            adapter.formats_to_scenarios(formats))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
368
369
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
370
class TestWorkingTreeProviderAdapter(TestCase):
371
    """A group of tests that test the workingtree implementation test adapter."""
372
2553.2.10 by Robert Collins
And overhaul WorkingTreeTestProviderAdapter too.
373
    def test_scenarios(self):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
374
        # check that constructor parameters are passed through to the adapted
375
        # test.
2553.2.10 by Robert Collins
And overhaul WorkingTreeTestProviderAdapter too.
376
        from bzrlib.tests.workingtree_implementations \
377
            import WorkingTreeTestProviderAdapter
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
378
        server1 = "a"
379
        server2 = "b"
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
380
        formats = [workingtree.WorkingTreeFormat2(),
381
                   workingtree.WorkingTreeFormat3(),]
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
382
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
2553.2.10 by Robert Collins
And overhaul WorkingTreeTestProviderAdapter too.
383
        self.assertEqual([
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
384
            ('WorkingTreeFormat2',
385
             {'bzrdir_format': formats[0]._matchingbzrdir,
386
              'transport_readonly_server': 'b',
387
              'transport_server': 'a',
388
              'workingtree_format': formats[0]}),
389
            ('WorkingTreeFormat3',
390
             {'bzrdir_format': formats[1]._matchingbzrdir,
391
              'transport_readonly_server': 'b',
392
              'transport_server': 'a',
393
              'workingtree_format': formats[1]})],
2553.2.10 by Robert Collins
And overhaul WorkingTreeTestProviderAdapter too.
394
            adapter.scenarios)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
395
396
1852.6.1 by Robert Collins
Start tree implementation tests.
397
class TestTreeProviderAdapter(TestCase):
398
    """Test the setup of tree_implementation tests."""
399
400
    def test_adapted_tests(self):
401
        # the tree implementation adapter is meant to setup one instance for
402
        # each working tree format, and one additional instance that will
403
        # use the default wt format, but create a revision tree for the tests.
404
        # this means that the wt ones should have the workingtree_to_test_tree
405
        # attribute set to 'return_parameter' and the revision one set to
406
        # revision_tree_from_workingtree.
407
408
        from bzrlib.tests.tree_implementations import (
409
            TreeTestProviderAdapter,
410
            return_parameter,
411
            revision_tree_from_workingtree
412
            )
413
        input_test = TestTreeProviderAdapter(
414
            "test_adapted_tests")
415
        server1 = "a"
416
        server2 = "b"
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
417
        formats = [workingtree.WorkingTreeFormat2(),
418
                   workingtree.WorkingTreeFormat3(),]
1852.6.1 by Robert Collins
Start tree implementation tests.
419
        adapter = TreeTestProviderAdapter(server1, server2, formats)
420
        suite = adapter.adapt(input_test)
421
        tests = list(iter(suite))
3363.2.6 by Aaron Bentley
Fix adatation test
422
        # XXX We should not have tests fail as we add more scenarios
423
        # abentley 20080412
424
        self.assertEqual(5, len(tests))
2255.2.164 by Martin Pool
Change the default format for some tests from AB1 back to WorkingTreeFormat3
425
        # this must match the default format setp up in
426
        # TreeTestProviderAdapter.adapt
3543.1.8 by Martin Pool
Update more scenario tests to use real format objects.
427
        default_format = workingtree.WorkingTreeFormat3
3543.1.6 by Martin Pool
update TestTreeProviderAdapter to pass real formats
428
        self.assertEqual(tests[0].workingtree_format, formats[0])
429
        self.assertEqual(tests[0].bzrdir_format, formats[0]._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
430
        self.assertEqual(tests[0].transport_server, server1)
431
        self.assertEqual(tests[0].transport_readonly_server, server2)
3363.1.5 by Aaron Bentley
Update tests
432
        self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
3543.1.6 by Martin Pool
update TestTreeProviderAdapter to pass real formats
433
        self.assertEqual(tests[1].workingtree_format, formats[1])
434
        self.assertEqual(tests[1].bzrdir_format, formats[1]._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
435
        self.assertEqual(tests[1].transport_server, server1)
436
        self.assertEqual(tests[1].transport_readonly_server, server2)
3363.1.5 by Aaron Bentley
Update tests
437
        self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
438
        self.assertIsInstance(tests[2].workingtree_format, default_format)
439
        #self.assertEqual(tests[2].bzrdir_format,
440
        #                 default_format._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
441
        self.assertEqual(tests[2].transport_server, server1)
442
        self.assertEqual(tests[2].transport_readonly_server, server2)
3363.1.5 by Aaron Bentley
Update tests
443
        self.assertEqual(tests[2]._workingtree_to_test_tree,
1852.6.1 by Robert Collins
Start tree implementation tests.
444
            revision_tree_from_workingtree)
445
446
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
447
class TestInterTreeProviderAdapter(TestCase):
448
    """A group of tests that test the InterTreeTestAdapter."""
449
450
    def test_adapted_tests(self):
451
        # check that constructor parameters are passed through to the adapted
452
        # test.
453
        # for InterTree tests we want the machinery to bring up two trees in
454
        # each instance: the base one, and the one we are interacting with.
455
        # because each optimiser can be direction specific, we need to test
456
        # each optimiser in its chosen direction.
457
        # unlike the TestProviderAdapter we dont want to automatically add a
3128.1.3 by Vincent Ladeuil
Since we are there s/parameteris.*/parameteriz&/.
458
        # parameterized one for WorkingTree - the optimisers will tell us what
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
459
        # ones to add.
460
        from bzrlib.tests.tree_implementations import (
461
            return_parameter,
462
            revision_tree_from_workingtree
463
            )
464
        from bzrlib.tests.intertree_implementations import (
465
            InterTreeTestProviderAdapter,
466
            )
467
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
468
        input_test = TestInterTreeProviderAdapter(
469
            "test_adapted_tests")
470
        server1 = "a"
471
        server2 = "b"
472
        format1 = WorkingTreeFormat2()
473
        format2 = WorkingTreeFormat3()
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
474
        formats = [(str, format1, format2, "converter1"),
475
            (int, format2, format1, "converter2")]
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
476
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
477
        suite = adapter.adapt(input_test)
478
        tests = list(iter(suite))
479
        self.assertEqual(2, len(tests))
480
        self.assertEqual(tests[0].intertree_class, formats[0][0])
481
        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.
482
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
483
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
3363.1.5 by Aaron Bentley
Update tests
484
        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.
485
        self.assertEqual(tests[0].transport_server, server1)
486
        self.assertEqual(tests[0].transport_readonly_server, server2)
487
        self.assertEqual(tests[1].intertree_class, formats[1][0])
488
        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.
489
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
490
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
3363.1.5 by Aaron Bentley
Update tests
491
        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.
492
        self.assertEqual(tests[1].transport_server, server1)
493
        self.assertEqual(tests[1].transport_readonly_server, server2)
494
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
495
496
class TestTestCaseInTempDir(TestCaseInTempDir):
497
498
    def test_home_is_not_working(self):
499
        self.assertNotEqual(self.test_dir, self.test_home_dir)
500
        cwd = osutils.getcwd()
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
501
        self.assertIsSameRealPath(self.test_dir, cwd)
502
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
1987.1.1 by John Arbash Meinel
Update the test suite to put HOME in a different directory
503
504
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
505
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
506
507
    def test_home_is_non_existant_dir_under_root(self):
508
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
509
510
        This is because TestCaseWithMemoryTransport is for tests that do not
511
        need any disk resources: they should be hooked into bzrlib in such a 
512
        way that no global settings are being changed by the test (only a 
513
        few tests should need to do that), and having a missing dir as home is
514
        an effective way to ensure that this is the case.
515
        """
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
516
        self.assertIsSameRealPath(
517
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
518
            self.test_home_dir)
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
519
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
520
        
521
    def test_cwd_is_TEST_ROOT(self):
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
522
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
523
        cwd = osutils.getcwd()
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
524
        self.assertIsSameRealPath(self.test_dir, cwd)
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
525
526
    def test_make_branch_and_memory_tree(self):
527
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
528
529
        This is hard to comprehensively robustly test, so we settle for making
530
        a branch and checking no directory was created at its relpath.
531
        """
532
        tree = self.make_branch_and_memory_tree('dir')
2227.2.2 by v.ladeuil+lp at free
Cleanup.
533
        # Guard against regression into MemoryTransport leaking
534
        # files to disk instead of keeping them in memory.
535
        self.failIf(osutils.lexists('dir'))
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
536
        self.assertIsInstance(tree, memorytree.MemoryTree)
537
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
538
    def test_make_branch_and_memory_tree_with_format(self):
539
        """make_branch_and_memory_tree should accept a format option."""
540
        format = bzrdir.BzrDirMetaFormat1()
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
541
        format.repository_format = weaverepo.RepositoryFormat7()
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
542
        tree = self.make_branch_and_memory_tree('dir', format=format)
2227.2.2 by v.ladeuil+lp at free
Cleanup.
543
        # Guard against regression into MemoryTransport leaking
544
        # files to disk instead of keeping them in memory.
545
        self.failIf(osutils.lexists('dir'))
1986.4.9 by Robert Collins
``TestCase.make_branch_and_memory_tree`` now takes a format
546
        self.assertIsInstance(tree, memorytree.MemoryTree)
547
        self.assertEqual(format.repository_format.__class__,
548
            tree.branch.repository._format.__class__)
549
2875.1.1 by Vincent Ladeuil
Fix #147986 by monitoring a safety .bzr directory.
550
    def test_safety_net(self):
551
        """No test should modify the safety .bzr directory.
552
553
        We just test that the _check_safety_net private method raises
2875.1.2 by Vincent Ladeuil
Update NEWS, fix typo.
554
        AssertionError, it's easier than building a test suite with the same
2875.1.1 by Vincent Ladeuil
Fix #147986 by monitoring a safety .bzr directory.
555
        test.
556
        """
557
        # Oops, a commit in the current directory (i.e. without local .bzr
558
        # directory) will crawl up the hierarchy to find a .bzr directory.
559
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
560
        # But we have a safety net in place.
561
        self.assertRaises(AssertionError, self._check_safety_net)
562
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
563
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
564
class TestTestCaseWithTransport(TestCaseWithTransport):
565
    """Tests for the convenience functions TestCaseWithTransport introduces."""
566
567
    def test_get_readonly_url_none(self):
568
        from bzrlib.transport import get_transport
569
        from bzrlib.transport.memory import MemoryServer
570
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
571
        self.vfs_transport_factory = MemoryServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
572
        self.transport_readonly_server = None
573
        # calling get_readonly_transport() constructs a decorator on the url
574
        # for the server
575
        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.
576
        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.
577
        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.
578
        t2 = get_transport(url2)
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
579
        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.
580
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
581
        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.
582
583
    def test_get_readonly_url_http(self):
3102.1.1 by Vincent Ladeuil
Rename bzrlib/test/HTTPTestUtils.py to bzrlib/tests/http_utils.py and fix
584
        from bzrlib.tests.http_server import HttpServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
585
        from bzrlib.transport import get_transport
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
586
        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 :)
587
        from bzrlib.transport.http import HttpTransportBase
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
588
        self.transport_server = LocalURLServer
1534.4.10 by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs.
589
        self.transport_readonly_server = HttpServer
590
        # calling get_readonly_transport() gives us a HTTP server instance.
591
        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.
592
        url2 = self.get_readonly_url('foo/bar')
1540.3.6 by Martin Pool
[merge] update from bzr.dev
593
        # 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.
594
        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.
595
        t2 = get_transport(url2)
1540.3.6 by Martin Pool
[merge] update from bzr.dev
596
        self.failUnless(isinstance(t, HttpTransportBase))
597
        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.
598
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
1534.4.31 by Robert Collins
cleanedup test_outside_wt
599
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
600
    def test_is_directory(self):
601
        """Test assertIsDirectory assertion"""
602
        t = self.get_transport()
603
        self.build_tree(['a_dir/', 'a_file'], transport=t)
604
        self.assertIsDirectory('a_dir', t)
605
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
606
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
1534.4.31 by Robert Collins
cleanedup test_outside_wt
607
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
608
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
609
class TestTestCaseTransports(TestCaseWithTransport):
610
611
    def setUp(self):
612
        super(TestTestCaseTransports, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
613
        self.vfs_transport_factory = MemoryServer
1910.13.1 by Andrew Bennetts
Make make_bzrdir preserve the transport.
614
615
    def test_make_bzrdir_preserves_transport(self):
616
        t = self.get_transport()
617
        result_bzrdir = self.make_bzrdir('subdir')
618
        self.assertIsInstance(result_bzrdir.transport, 
619
                              MemoryTransport)
620
        # should not be on disk, should only be in memory
621
        self.failIfExists('subdir')
622
623
1534.4.31 by Robert Collins
cleanedup test_outside_wt
624
class TestChrootedTest(ChrootedTestCase):
625
626
    def test_root_is_root(self):
627
        from bzrlib.transport import get_transport
628
        t = get_transport(self.get_readonly_url())
629
        url = t.base
630
        self.assertEqual(url, t.clone('..').base)
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
631
632
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
633
class MockProgress(_BaseProgressBar):
634
    """Progress-bar standin that records calls.
635
636
    Useful for testing pb using code.
637
    """
638
639
    def __init__(self):
640
        _BaseProgressBar.__init__(self)
641
        self.calls = []
642
643
    def tick(self):
644
        self.calls.append(('tick',))
645
646
    def update(self, msg=None, current=None, total=None):
647
        self.calls.append(('update', msg, current, total))
648
649
    def clear(self):
650
        self.calls.append(('clear',))
651
1864.3.1 by John Arbash Meinel
Print out when a test fails in non verbose mode, run transport tests later
652
    def note(self, msg, *args):
653
        self.calls.append(('note', msg, args))
654
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
655
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
656
class TestTestResult(TestCase):
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
657
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
658
    def check_timing(self, test_case, expected_re):
2095.4.1 by Martin Pool
Better progress bars during tests
659
        result = bzrlib.tests.TextTestResult(self._log_file,
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
660
                descriptions=0,
661
                verbosity=1,
662
                )
663
        test_case.run(result)
664
        timed_string = result._testTimeString(test_case)
665
        self.assertContainsRe(timed_string, expected_re)
666
667
    def test_test_reporting(self):
668
        class ShortDelayTestCase(TestCase):
669
            def test_short_delay(self):
670
                time.sleep(0.003)
671
            def test_short_benchmark(self):
672
                self.time(time.sleep, 0.003)
673
        self.check_timing(ShortDelayTestCase('test_short_delay'),
674
                          r"^ +[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).
675
        # if a benchmark time is given, we want a x of y style result.
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
676
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
677
                          r"^ +[0-9]+ms/ +[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).
678
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
679
    def test_unittest_reporting_unittest_class(self):
680
        # getting the time from a non-bzrlib test works ok
681
        class ShortDelayTestCase(unittest.TestCase):
682
            def test_short_delay(self):
683
                time.sleep(0.003)
684
        self.check_timing(ShortDelayTestCase('test_short_delay'),
685
                          r"^ +[0-9]+ms$")
686
        
1819.1.1 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Give the test result object an optional benchmark
687
    def test_assigned_benchmark_file_stores_date(self):
688
        output = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
689
        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
690
                                        descriptions=0,
691
                                        verbosity=1,
692
                                        bench_history=output
693
                                        )
694
        output_string = output.getvalue()
1819.1.4 by Jan Balster
save the revison id for every benchmark run in .perf-history
695
        # if you are wondering about the regexp please read the comment in
696
        # 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.
697
        # XXX: what comment?  -- Andrew Bennetts
698
        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
699
700
    def test_benchhistory_records_test_times(self):
701
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
702
        result = bzrlib.tests.TextTestResult(
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
703
            self._log_file,
704
            descriptions=0,
705
            verbosity=1,
706
            bench_history=result_stream
707
            )
708
709
        # we want profile a call and check that its test duration is recorded
710
        # make a new test instance that when run will generate a benchmark
711
        example_test_case = TestTestResult("_time_hello_world_encoding")
712
        # execute the test, which should succeed and record times
713
        example_test_case.run(result)
714
        lines = result_stream.getvalue().splitlines()
715
        self.assertEqual(2, len(lines))
716
        self.assertContainsRe(lines[1],
717
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
718
            "._time_hello_world_encoding")
719
 
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
720
    def _time_hello_world_encoding(self):
721
        """Profile two sleep calls
722
        
723
        This is used to exercise the test framework.
724
        """
725
        self.time(unicode, 'hello', errors='replace')
726
        self.time(unicode, 'world', errors='replace')
727
728
    def test_lsprofiling(self):
729
        """Verbose test result prints lsprof statistics from test cases."""
1551.15.28 by Aaron Bentley
Improve Feature usage style w/ lsprof
730
        self.requireFeature(test_lsprof.LSProfFeature)
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
731
        result_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
732
        result = bzrlib.tests.VerboseTestResult(
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
733
            unittest._WritelnDecorator(result_stream),
734
            descriptions=0,
735
            verbosity=2,
736
            )
737
        # we want profile a call of some sort and check it is output by
738
        # addSuccess. We dont care about addError or addFailure as they
739
        # are not that interesting for performance tuning.
740
        # make a new test instance that when run will generate a profile
741
        example_test_case = TestTestResult("_time_hello_world_encoding")
742
        example_test_case._gather_lsprof_in_benchmarks = True
743
        # execute the test, which should succeed and record profiles
744
        example_test_case.run(result)
745
        # lsprofile_something()
746
        # if this worked we want 
747
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
748
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
749
        # (the lsprof header)
750
        # ... an arbitrary number of lines
751
        # and the function call which is time.sleep.
752
        #           1        0            ???         ???       ???(sleep) 
753
        # and then repeated but with 'world', rather than 'hello'.
754
        # this should appear in the output stream of our test result.
1831.2.1 by Martin Pool
[trivial] Simplify & fix up lsprof blackbox test
755
        output = result_stream.getvalue()
756
        self.assertContainsRe(output,
757
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
758
        self.assertContainsRe(output,
759
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
760
        self.assertContainsRe(output,
761
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
762
        self.assertContainsRe(output,
763
            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
764
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
765
    def test_known_failure(self):
766
        """A KnownFailure being raised should trigger several result actions."""
767
        class InstrumentedTestResult(ExtendedTestResult):
768
769
            def report_test_start(self, test): pass
770
            def report_known_failure(self, test, err):
771
                self._call = test, err
772
        result = InstrumentedTestResult(None, None, None, None)
773
        def test_function():
774
            raise KnownFailure('failed!')
775
        test = unittest.FunctionTestCase(test_function)
776
        test.run(result)
777
        # it should invoke 'report_known_failure'.
778
        self.assertEqual(2, len(result._call))
779
        self.assertEqual(test, result._call[0])
780
        self.assertEqual(KnownFailure, result._call[1][0])
781
        self.assertIsInstance(result._call[1][1], KnownFailure)
782
        # we dont introspec the traceback, if the rest is ok, it would be
783
        # exceptional for it not to be.
784
        # it should update the known_failure_count on the object.
785
        self.assertEqual(1, result.known_failure_count)
786
        # the result should be successful.
787
        self.assertTrue(result.wasSuccessful())
788
789
    def test_verbose_report_known_failure(self):
790
        # verbose test output formatting
791
        result_stream = StringIO()
792
        result = bzrlib.tests.VerboseTestResult(
793
            unittest._WritelnDecorator(result_stream),
794
            descriptions=0,
795
            verbosity=2,
796
            )
797
        test = self.get_passing_test()
798
        result.startTest(test)
799
        prefix = len(result_stream.getvalue())
800
        # the err parameter has the shape:
801
        # (class, exception object, traceback)
802
        # KnownFailures dont get their tracebacks shown though, so we
803
        # can skip that.
804
        err = (KnownFailure, KnownFailure('foo'), None)
805
        result.report_known_failure(test, err)
806
        output = result_stream.getvalue()[prefix:]
807
        lines = output.splitlines()
2418.3.1 by John Arbash Meinel
Remove timing dependencies from the selftest tests.
808
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
809
        self.assertEqual(lines[1], '    foo')
810
        self.assertEqual(2, len(lines))
811
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
812
    def test_text_report_known_failure(self):
813
        # text test output formatting
814
        pb = MockProgress()
815
        result = bzrlib.tests.TextTestResult(
816
            None,
817
            descriptions=0,
818
            verbosity=1,
819
            pb=pb,
820
            )
821
        test = self.get_passing_test()
822
        # this seeds the state to handle reporting the test.
823
        result.startTest(test)
824
        # the err parameter has the shape:
825
        # (class, exception object, traceback)
826
        # KnownFailures dont get their tracebacks shown though, so we
827
        # can skip that.
828
        err = (KnownFailure, KnownFailure('foo'), None)
829
        result.report_known_failure(test, err)
830
        self.assertEqual(
831
            [
832
            ('update', '[1 in 0s] passing_test', None, None),
833
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
834
            ],
835
            pb.calls)
836
        # known_failures should be printed in the summary, so if we run a test
837
        # after there are some known failures, the update prefix should match
838
        # this.
839
        result.known_failure_count = 3
840
        test.run(result)
841
        self.assertEqual(
842
            [
3297.1.3 by Martin Pool
Fix up selftest progress tests
843
            ('update', '[2 in 0s] passing_test', None, None),
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
844
            ],
845
            pb.calls[2:])
846
847
    def get_passing_test(self):
848
        """Return a test object that can't be run usefully."""
849
        def passing_test():
850
            pass
851
        return unittest.FunctionTestCase(passing_test)
852
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
853
    def test_add_not_supported(self):
854
        """Test the behaviour of invoking addNotSupported."""
855
        class InstrumentedTestResult(ExtendedTestResult):
856
            def report_test_start(self, test): pass
857
            def report_unsupported(self, test, feature):
858
                self._call = test, feature
859
        result = InstrumentedTestResult(None, None, None, None)
860
        test = SampleTestCase('_test_pass')
861
        feature = Feature()
862
        result.startTest(test)
863
        result.addNotSupported(test, feature)
864
        # it should invoke 'report_unsupported'.
865
        self.assertEqual(2, len(result._call))
866
        self.assertEqual(test, result._call[0])
867
        self.assertEqual(feature, result._call[1])
868
        # the result should be successful.
869
        self.assertTrue(result.wasSuccessful())
870
        # it should record the test against a count of tests not run due to
871
        # this feature.
872
        self.assertEqual(1, result.unsupported['Feature'])
873
        # and invoking it again should increment that counter
874
        result.addNotSupported(test, feature)
875
        self.assertEqual(2, result.unsupported['Feature'])
876
877
    def test_verbose_report_unsupported(self):
878
        # verbose test output formatting
879
        result_stream = StringIO()
880
        result = bzrlib.tests.VerboseTestResult(
881
            unittest._WritelnDecorator(result_stream),
882
            descriptions=0,
883
            verbosity=2,
884
            )
885
        test = self.get_passing_test()
886
        feature = Feature()
887
        result.startTest(test)
888
        prefix = len(result_stream.getvalue())
889
        result.report_unsupported(test, feature)
890
        output = result_stream.getvalue()[prefix:]
891
        lines = output.splitlines()
892
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
893
    
894
    def test_text_report_unsupported(self):
895
        # text test output formatting
896
        pb = MockProgress()
897
        result = bzrlib.tests.TextTestResult(
898
            None,
899
            descriptions=0,
900
            verbosity=1,
901
            pb=pb,
902
            )
903
        test = self.get_passing_test()
904
        feature = Feature()
905
        # this seeds the state to handle reporting the test.
906
        result.startTest(test)
907
        result.report_unsupported(test, feature)
908
        # no output on unsupported features
909
        self.assertEqual(
910
            [('update', '[1 in 0s] passing_test', None, None)
911
            ],
912
            pb.calls)
913
        # the number of missing features should be printed in the progress
914
        # summary, so check for that.
915
        result.unsupported = {'foo':0, 'bar':0}
916
        test.run(result)
917
        self.assertEqual(
918
            [
3297.1.3 by Martin Pool
Fix up selftest progress tests
919
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
920
            ],
921
            pb.calls[1:])
922
    
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
923
    def test_unavailable_exception(self):
924
        """An UnavailableFeature being raised should invoke addNotSupported."""
925
        class InstrumentedTestResult(ExtendedTestResult):
926
927
            def report_test_start(self, test): pass
928
            def addNotSupported(self, test, feature):
929
                self._call = test, feature
930
        result = InstrumentedTestResult(None, None, None, None)
931
        feature = Feature()
932
        def test_function():
933
            raise UnavailableFeature(feature)
934
        test = unittest.FunctionTestCase(test_function)
935
        test.run(result)
936
        # it should invoke 'addNotSupported'.
937
        self.assertEqual(2, len(result._call))
938
        self.assertEqual(test, result._call[0])
939
        self.assertEqual(feature, result._call[1])
940
        # and not count as an error
941
        self.assertEqual(0, result.error_count)
942
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
943
    def test_strict_with_unsupported_feature(self):
944
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
945
                                             verbosity=1)
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
946
        test = self.get_passing_test()
947
        feature = "Unsupported Feature"
948
        result.addNotSupported(test, feature)
949
        self.assertFalse(result.wasStrictlySuccessful())
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
950
        self.assertEqual(None, result._extractBenchmarkTime(test))
951
 
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
952
    def test_strict_with_known_failure(self):
953
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
954
                                             verbosity=1)
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
955
        test = self.get_passing_test()
956
        err = (KnownFailure, KnownFailure('foo'), None)
2695.1.1 by Martin Pool
Fix problem if the first test is missing a dependency
957
        result._addKnownFailure(test, err)
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
958
        self.assertFalse(result.wasStrictlySuccessful())
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
959
        self.assertEqual(None, result._extractBenchmarkTime(test))
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
960
961
    def test_strict_with_success(self):
962
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
963
                                             verbosity=1)
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
964
        test = self.get_passing_test()
965
        result.addSuccess(test)
966
        self.assertTrue(result.wasStrictlySuccessful())
2695.1.3 by Martin Pool
Fix up selftest tests for new extractBenchmarkTime behaviour; remove many unneeded calls to it
967
        self.assertEqual(None, result._extractBenchmarkTime(test))
2658.3.2 by Daniel Watkins
Added tests for ExtendedTestResult.wasStrictlySuccessful.
968
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
969
3477.1.1 by John Arbash Meinel
Move UnicodeFeature into a core 'tests' feature, rather than living in test_diff.
970
class TestUnicodeFilenameFeature(TestCase):
971
972
    def test_probe_passes(self):
3477.1.2 by John Arbash Meinel
Rename UnicodeFilename => UnicodeFilenameFeature
973
        """UnicodeFilenameFeature._probe passes."""
3477.1.1 by John Arbash Meinel
Move UnicodeFeature into a core 'tests' feature, rather than living in test_diff.
974
        # We can't test much more than that because the behaviour depends
975
        # on the platform.
3477.1.2 by John Arbash Meinel
Rename UnicodeFilename => UnicodeFilenameFeature
976
        tests.UnicodeFilenameFeature._probe()
3477.1.1 by John Arbash Meinel
Move UnicodeFeature into a core 'tests' feature, rather than living in test_diff.
977
978
1534.11.1 by Robert Collins
Teach bzr selftest to use a progress bar in non verbose mode.
979
class TestRunner(TestCase):
980
981
    def dummy_test(self):
982
        pass
983
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
984
    def run_test_runner(self, testrunner, test):
985
        """Run suite in testrunner, saving global state and restoring it.
986
987
        This current saves and restores:
988
        TestCaseInTempDir.TEST_ROOT
989
        
990
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
991
        without using this convenience method, because of our use of global state.
992
        """
993
        old_root = TestCaseInTempDir.TEST_ROOT
994
        try:
995
            TestCaseInTempDir.TEST_ROOT = None
996
            return testrunner.run(test)
997
        finally:
998
            TestCaseInTempDir.TEST_ROOT = old_root
999
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1000
    def test_known_failure_failed_run(self):
1001
        # run a test that generates a known failure which should be printed in
1002
        # the final output when real failures occur.
1003
        def known_failure_test():
1004
            raise KnownFailure('failed')
1005
        test = unittest.TestSuite()
1006
        test.addTest(unittest.FunctionTestCase(known_failure_test))
1007
        def failing_test():
1008
            raise AssertionError('foo')
1009
        test.addTest(unittest.FunctionTestCase(failing_test))
1010
        stream = StringIO()
1011
        runner = TextTestRunner(stream=stream)
1012
        result = self.run_test_runner(runner, test)
1013
        lines = stream.getvalue().splitlines()
1014
        self.assertEqual([
1015
            '',
1016
            '======================================================================',
1017
            'FAIL: unittest.FunctionTestCase (failing_test)',
1018
            '----------------------------------------------------------------------',
1019
            'Traceback (most recent call last):',
1020
            '    raise AssertionError(\'foo\')',
1021
            'AssertionError: foo',
1022
            '',
1023
            '----------------------------------------------------------------------',
1024
            '',
1025
            '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
1026
            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
1027
1028
    def test_known_failure_ok_run(self):
1029
        # run a test that generates a known failure which should be printed in the final output.
1030
        def known_failure_test():
1031
            raise KnownFailure('failed')
1032
        test = unittest.FunctionTestCase(known_failure_test)
1033
        stream = StringIO()
1034
        runner = TextTestRunner(stream=stream)
1035
        result = self.run_test_runner(runner, test)
2418.3.1 by John Arbash Meinel
Remove timing dependencies from the selftest tests.
1036
        self.assertContainsRe(stream.getvalue(),
1037
            '\n'
1038
            '-*\n'
1039
            'Ran 1 test in .*\n'
1040
            '\n'
1041
            'OK \\(known_failures=1\\)\n')
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1042
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1043
    def test_skipped_test(self):
1044
        # run a test that is skipped, and check the suite as a whole still
1045
        # succeeds.
1046
        # skipping_test must be hidden in here so it's not run as a real test
1047
        def skipping_test():
1048
            raise TestSkipped('test intentionally skipped')
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1049
2485.6.5 by Martin Pool
Remove keep_output option
1050
        runner = TextTestRunner(stream=self._log_file)
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1051
        test = unittest.FunctionTestCase(skipping_test)
1052
        result = self.run_test_runner(runner, test)
1053
        self.assertTrue(result.wasSuccessful())
1054
1055
    def test_skipped_from_setup(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1056
        calls = []
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1057
        class SkippedSetupTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1058
1059
            def setUp(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1060
                calls.append('setUp')
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1061
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1062
                raise TestSkipped('skipped setup')
1063
1064
            def test_skip(self):
1065
                self.fail('test reached')
1066
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1067
            def cleanup(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1068
                calls.append('cleanup')
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1069
2485.6.5 by Martin Pool
Remove keep_output option
1070
        runner = TextTestRunner(stream=self._log_file)
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1071
        test = SkippedSetupTest('test_skip')
1072
        result = self.run_test_runner(runner, test)
1073
        self.assertTrue(result.wasSuccessful())
1074
        # Check if cleanup was called the right number of times.
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1075
        self.assertEqual(['setUp', 'cleanup'], calls)
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1076
1077
    def test_skipped_from_test(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1078
        calls = []
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1079
        class SkippedTest(TestCase):
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1080
1081
            def setUp(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1082
                calls.append('setUp')
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1083
                self.addCleanup(self.cleanup)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1084
1085
            def test_skip(self):
1086
                raise TestSkipped('skipped test')
1087
2338.4.10 by Marien Zwart
Make a test skipped from setUp run tearDown again. Make calling _runCleanups twice safe. Clean up tests.
1088
            def cleanup(self):
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1089
                calls.append('cleanup')
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1090
2485.6.5 by Martin Pool
Remove keep_output option
1091
        runner = TextTestRunner(stream=self._log_file)
2338.4.8 by Marien Zwart
Fix a bug in selftest causing tearDown to run twice for skipped tests.
1092
        test = SkippedTest('test_skip')
1093
        result = self.run_test_runner(runner, test)
1094
        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.
1095
        # Check if cleanup was called the right number of times.
3224.4.1 by Andrew Bennetts
Prune __dict__ of TestCases after they have run to save memory.
1096
        self.assertEqual(['setUp', 'cleanup'], calls)
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1097
2729.1.1 by Martin Pool
Add TestNotApplicable exception and handling of it; document test parameterization
1098
    def test_not_applicable(self):
1099
        # run a test that is skipped because it's not applicable
1100
        def not_applicable_test():
1101
            from bzrlib.tests import TestNotApplicable
1102
            raise TestNotApplicable('this test never runs')
1103
        out = StringIO()
1104
        runner = TextTestRunner(stream=out, verbosity=2)
1105
        test = unittest.FunctionTestCase(not_applicable_test)
1106
        result = self.run_test_runner(runner, test)
1107
        self._log_file.write(out.getvalue())
1108
        self.assertTrue(result.wasSuccessful())
1109
        self.assertTrue(result.wasStrictlySuccessful())
1110
        self.assertContainsRe(out.getvalue(),
1111
                r'(?m)not_applicable_test   * N/A')
1112
        self.assertContainsRe(out.getvalue(),
1113
                r'(?m)^    this test never runs')
1114
1115
    def test_not_applicable_demo(self):
1116
        # just so you can see it in the test output
1117
        raise TestNotApplicable('this test is just a demonstation')
1118
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1119
    def test_unsupported_features_listed(self):
1120
        """When unsupported features are encountered they are detailed."""
1121
        class Feature1(Feature):
1122
            def _probe(self): return False
1123
        class Feature2(Feature):
1124
            def _probe(self): return False
1125
        # create sample tests
1126
        test1 = SampleTestCase('_test_pass')
1127
        test1._test_needs_features = [Feature1()]
1128
        test2 = SampleTestCase('_test_pass')
1129
        test2._test_needs_features = [Feature2()]
1130
        test = unittest.TestSuite()
1131
        test.addTest(test1)
1132
        test.addTest(test2)
1133
        stream = StringIO()
1134
        runner = TextTestRunner(stream=stream)
1135
        result = self.run_test_runner(runner, test)
1136
        lines = stream.getvalue().splitlines()
1137
        self.assertEqual([
1138
            'OK',
1139
            "Missing feature 'Feature1' skipped 1 tests.",
1140
            "Missing feature 'Feature2' skipped 1 tests.",
1141
            ],
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1142
            lines[-3:])
2367.1.5 by Robert Collins
Implement reporting of Unsupported tests in the bzr test result and runner
1143
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1144
    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.
1145
        # tests that the running the benchmark produces a history file
1146
        # containing a timestamp and the revision id of the bzrlib source which
1147
        # was tested.
1148
        workingtree = _get_bzr_source_tree()
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1149
        test = TestRunner('dummy_test')
1150
        output = StringIO()
1151
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
1152
        result = self.run_test_runner(runner, test)
1153
        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.
1154
        self.assertContainsRe(output_string, "--date [0-9.]+")
1155
        if workingtree is not None:
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
1156
            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.
1157
            self.assertEndsWith(output_string.rstrip(), revision_id)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1158
3199.1.1 by Vincent Ladeuil
Get rid of ~1000 useless log files out of 10.000 tests in /tmp.
1159
    def assertLogDeleted(self, test):
1160
        log = test._get_log()
1161
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1162
        self.assertEqual('', test._log_contents)
1163
        self.assertIs(None, test._log_file_name)
1164
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
1165
    def test_success_log_deleted(self):
1166
        """Successful tests have their log deleted"""
1167
1168
        class LogTester(TestCase):
1169
1170
            def test_success(self):
1171
                self.log('this will be removed\n')
1172
1173
        sio = cStringIO.StringIO()
1174
        runner = TextTestRunner(stream=sio)
1175
        test = LogTester('test_success')
1176
        result = self.run_test_runner(runner, test)
1177
3199.1.1 by Vincent Ladeuil
Get rid of ~1000 useless log files out of 10.000 tests in /tmp.
1178
        self.assertLogDeleted(test)
1179
1180
    def test_skipped_log_deleted(self):
1181
        """Skipped tests have their log deleted"""
1182
1183
        class LogTester(TestCase):
1184
1185
            def test_skipped(self):
1186
                self.log('this will be removed\n')
1187
                raise tests.TestSkipped()
1188
1189
        sio = cStringIO.StringIO()
1190
        runner = TextTestRunner(stream=sio)
1191
        test = LogTester('test_skipped')
1192
        result = self.run_test_runner(runner, test)
1193
1194
        self.assertLogDeleted(test)
1195
1196
    def test_not_aplicable_log_deleted(self):
1197
        """Not applicable tests have their log deleted"""
1198
1199
        class LogTester(TestCase):
1200
1201
            def test_not_applicable(self):
1202
                self.log('this will be removed\n')
1203
                raise tests.TestNotApplicable()
1204
1205
        sio = cStringIO.StringIO()
1206
        runner = TextTestRunner(stream=sio)
1207
        test = LogTester('test_not_applicable')
1208
        result = self.run_test_runner(runner, test)
1209
1210
        self.assertLogDeleted(test)
1211
1212
    def test_known_failure_log_deleted(self):
1213
        """Know failure tests have their log deleted"""
1214
1215
        class LogTester(TestCase):
1216
1217
            def test_known_failure(self):
1218
                self.log('this will be removed\n')
1219
                raise tests.KnownFailure()
1220
1221
        sio = cStringIO.StringIO()
1222
        runner = TextTestRunner(stream=sio)
1223
        test = LogTester('test_known_failure')
1224
        result = self.run_test_runner(runner, test)
1225
1226
        self.assertLogDeleted(test)
2036.1.1 by John Arbash Meinel
test that logs are kept or deleted when appropriate
1227
1228
    def test_fail_log_kept(self):
1229
        """Failed tests have their log kept"""
1230
1231
        class LogTester(TestCase):
1232
1233
            def test_fail(self):
1234
                self.log('this will be kept\n')
1235
                self.fail('this test fails')
1236
1237
        sio = cStringIO.StringIO()
1238
        runner = TextTestRunner(stream=sio)
1239
        test = LogTester('test_fail')
1240
        result = self.run_test_runner(runner, test)
1241
1242
        text = sio.getvalue()
1243
        self.assertContainsRe(text, 'this will be kept')
1244
        self.assertContainsRe(text, 'this test fails')
1245
1246
        log = test._get_log()
1247
        self.assertContainsRe(log, 'this will be kept')
1248
        self.assertEqual(log, test._log_contents)
1249
1250
    def test_error_log_kept(self):
1251
        """Tests with errors have their log kept"""
1252
1253
        class LogTester(TestCase):
1254
1255
            def test_error(self):
1256
                self.log('this will be kept\n')
1257
                raise ValueError('random exception raised')
1258
1259
        sio = cStringIO.StringIO()
1260
        runner = TextTestRunner(stream=sio)
1261
        test = LogTester('test_error')
1262
        result = self.run_test_runner(runner, test)
1263
1264
        text = sio.getvalue()
1265
        self.assertContainsRe(text, 'this will be kept')
1266
        self.assertContainsRe(text, 'random exception raised')
1267
1268
        log = test._get_log()
1269
        self.assertContainsRe(log, 'this will be kept')
1270
        self.assertEqual(log, test._log_contents)
1819.1.2 by Carl Friedrich Bolz
(lifeless, cfbolz, hpk): Add a benchmark output parameter to TextTestRunner.
1271
2036.1.2 by John Arbash Meinel
whitespace fix
1272
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1273
class SampleTestCase(TestCase):
1274
1275
    def _test_pass(self):
1276
        pass
1277
3287.20.1 by John Arbash Meinel
Update assertListRaises so that it returns the exception.
1278
class _TestException(Exception):
1279
    pass
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1280
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1281
class TestTestCase(TestCase):
1282
    """Tests that test the core bzrlib TestCase."""
1283
2560.1.1 by Robert Collins
Make debug.debug_flags be isolated for all tests.
1284
    def test_debug_flags_sanitised(self):
1285
        """The bzrlib debug flags should be sanitised by setUp."""
1286
        # we could set something and run a test that will check
1287
        # it gets santised, but this is probably sufficient for now:
1288
        # if someone runs the test with -Dsomething it will error.
1289
        self.assertEqual(set(), bzrlib.debug.debug_flags)
1290
1534.11.7 by Robert Collins
Test and correct the problem with nested test logs breaking further in-test logs.
1291
    def inner_test(self):
1292
        # the inner child test
1293
        note("inner_test")
1294
1295
    def outer_child(self):
1296
        # the outer child test
1297
        note("outer_start")
1298
        self.inner_test = TestTestCase("inner_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1299
        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.
1300
                                        descriptions=0,
1301
                                        verbosity=1)
1302
        self.inner_test.run(result)
1303
        note("outer finish")
1304
1305
    def test_trace_nesting(self):
1306
        # this tests that each test case nests its trace facility correctly.
1307
        # we do this by running a test case manually. That test case (A)
1308
        # should setup a new log, log content to it, setup a child case (B),
1309
        # which should log independently, then case (A) should log a trailer
1310
        # and return.
1311
        # we do two nested children so that we can verify the state of the 
1312
        # logs after the outer child finishes is correct, which a bad clean
1313
        # up routine in tearDown might trigger a fault in our test with only
1314
        # one child, we should instead see the bad result inside our test with
1315
        # the two children.
1316
        # the outer child test
1317
        original_trace = bzrlib.trace._trace_file
1318
        outer_test = TestTestCase("outer_child")
2095.4.1 by Martin Pool
Better progress bars during tests
1319
        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.
1320
                                        descriptions=0,
1321
                                        verbosity=1)
1322
        outer_test.run(result)
1323
        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)
1324
1325
    def method_that_times_a_bit_twice(self):
1326
        # 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.
1327
        self.time(time.sleep, 0.007)
1328
        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)
1329
1330
    def test_time_creates_benchmark_in_result(self):
1331
        """Test that the TestCase.time() method accumulates a benchmark time."""
1332
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1333
        output_stream = StringIO()
2095.4.1 by Martin Pool
Better progress bars during tests
1334
        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)
1335
            unittest._WritelnDecorator(output_stream),
1336
            descriptions=0,
2095.4.1 by Martin Pool
Better progress bars during tests
1337
            verbosity=2,
1338
            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)
1339
        sample_test.run(result)
1340
        self.assertContainsRe(
1341
            output_stream.getvalue(),
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
1342
            r"\d+ms/ +\d+ms\n$")
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1343
1344
    def test_hooks_sanitised(self):
1345
        """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.
1346
        self.assertEqual(bzrlib.branch.BranchHooks(),
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1347
            bzrlib.branch.Branch.hooks)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
1348
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1349
            bzrlib.smart.server.SmartTCPServer.hooks)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
1350
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
1351
    def test__gather_lsprof_in_benchmarks(self):
1352
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1353
        
1354
        Each self.time() call is individually and separately profiled.
1355
        """
1551.15.28 by Aaron Bentley
Improve Feature usage style w/ lsprof
1356
        self.requireFeature(test_lsprof.LSProfFeature)
1725.1.1 by Robert Collins
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
1357
        # overrides the class member with an instance member so no cleanup 
1358
        # needed.
1359
        self._gather_lsprof_in_benchmarks = True
1360
        self.time(time.sleep, 0.000)
1361
        self.time(time.sleep, 0.003)
1362
        self.assertEqual(2, len(self._benchcalls))
1363
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1364
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1365
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1366
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1367
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1368
    def test_knownFailure(self):
1369
        """Self.knownFailure() should raise a KnownFailure exception."""
1370
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1371
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1372
    def test_requireFeature_available(self):
1373
        """self.requireFeature(available) is a no-op."""
1374
        class Available(Feature):
1375
            def _probe(self):return True
1376
        feature = Available()
1377
        self.requireFeature(feature)
1378
1379
    def test_requireFeature_unavailable(self):
1380
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1381
        class Unavailable(Feature):
1382
            def _probe(self):return False
1383
        feature = Unavailable()
1384
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1385
2367.1.3 by Robert Collins
Add support for calling addNotSupported on TestResults to bzr TestCase's
1386
    def test_run_no_parameters(self):
1387
        test = SampleTestCase('_test_pass')
1388
        test.run()
1389
    
1390
    def test_run_enabled_unittest_result(self):
1391
        """Test we revert to regular behaviour when the test is enabled."""
1392
        test = SampleTestCase('_test_pass')
1393
        class EnabledFeature(object):
1394
            def available(self):
1395
                return True
1396
        test._test_needs_features = [EnabledFeature()]
1397
        result = unittest.TestResult()
1398
        test.run(result)
1399
        self.assertEqual(1, result.testsRun)
1400
        self.assertEqual([], result.errors)
1401
        self.assertEqual([], result.failures)
1402
1403
    def test_run_disabled_unittest_result(self):
1404
        """Test our compatability for disabled tests with unittest results."""
1405
        test = SampleTestCase('_test_pass')
1406
        class DisabledFeature(object):
1407
            def available(self):
1408
                return False
1409
        test._test_needs_features = [DisabledFeature()]
1410
        result = unittest.TestResult()
1411
        test.run(result)
1412
        self.assertEqual(1, result.testsRun)
1413
        self.assertEqual([], result.errors)
1414
        self.assertEqual([], result.failures)
1415
1416
    def test_run_disabled_supporting_result(self):
1417
        """Test disabled tests behaviour with support aware results."""
1418
        test = SampleTestCase('_test_pass')
1419
        class DisabledFeature(object):
1420
            def available(self):
1421
                return False
1422
        the_feature = DisabledFeature()
1423
        test._test_needs_features = [the_feature]
1424
        class InstrumentedTestResult(unittest.TestResult):
1425
            def __init__(self):
1426
                unittest.TestResult.__init__(self)
1427
                self.calls = []
1428
            def startTest(self, test):
1429
                self.calls.append(('startTest', test))
1430
            def stopTest(self, test):
1431
                self.calls.append(('stopTest', test))
1432
            def addNotSupported(self, test, feature):
1433
                self.calls.append(('addNotSupported', test, feature))
1434
        result = InstrumentedTestResult()
1435
        test.run(result)
1436
        self.assertEqual([
1437
            ('startTest', test),
1438
            ('addNotSupported', test, the_feature),
1439
            ('stopTest', test),
1440
            ],
1441
            result.calls)
1442
3287.20.1 by John Arbash Meinel
Update assertListRaises so that it returns the exception.
1443
    def test_assert_list_raises_on_generator(self):
1444
        def generator_which_will_raise():
1445
            # This will not raise until after the first yield
1446
            yield 1
1447
            raise _TestException()
1448
1449
        e = self.assertListRaises(_TestException, generator_which_will_raise)
1450
        self.assertIsInstance(e, _TestException)
1451
1452
        e = self.assertListRaises(Exception, generator_which_will_raise)
1453
        self.assertIsInstance(e, _TestException)
1454
1455
    def test_assert_list_raises_on_plain(self):
1456
        def plain_exception():
1457
            raise _TestException()
1458
            return []
1459
1460
        e = self.assertListRaises(_TestException, plain_exception)
1461
        self.assertIsInstance(e, _TestException)
1462
1463
        e = self.assertListRaises(Exception, plain_exception)
1464
        self.assertIsInstance(e, _TestException)
1465
1466
    def test_assert_list_raises_assert_wrong_exception(self):
1467
        class _NotTestException(Exception):
1468
            pass
1469
1470
        def wrong_exception():
1471
            raise _NotTestException()
1472
1473
        def wrong_exception_generator():
1474
            yield 1
1475
            yield 2
1476
            raise _NotTestException()
1477
1478
        # Wrong exceptions are not intercepted
1479
        self.assertRaises(_NotTestException,
1480
            self.assertListRaises, _TestException, wrong_exception)
1481
        self.assertRaises(_NotTestException,
1482
            self.assertListRaises, _TestException, wrong_exception_generator)
1483
1484
    def test_assert_list_raises_no_exception(self):
1485
        def success():
1486
            return []
1487
1488
        def success_generator():
1489
            yield 1
1490
            yield 2
1491
1492
        self.assertRaises(AssertionError,
1493
            self.assertListRaises, _TestException, success)
1494
1495
        self.assertRaises(AssertionError,
1496
            self.assertListRaises, _TestException, success_generator)
1497
1534.11.4 by Robert Collins
Merge from mainline.
1498
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1499
@symbol_versioning.deprecated_function(zero_eleven)
1500
def sample_deprecated_function():
1501
    """A deprecated function to test applyDeprecated with."""
1502
    return 2
1503
1504
1505
def sample_undeprecated_function(a_param):
1506
    """A undeprecated function to test applyDeprecated with."""
1507
1508
1509
class ApplyDeprecatedHelper(object):
1510
    """A helper class for ApplyDeprecated tests."""
1511
1512
    @symbol_versioning.deprecated_method(zero_eleven)
1513
    def sample_deprecated_method(self, param_one):
1514
        """A deprecated method for testing with."""
1515
        return param_one
1516
1517
    def sample_normal_method(self):
1518
        """A undeprecated method."""
1519
1520
    @symbol_versioning.deprecated_method(zero_ten)
1521
    def sample_nested_deprecation(self):
1522
        return sample_deprecated_function()
1523
1524
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
1525
class TestExtraAssertions(TestCase):
1526
    """Tests for new test assertions in bzrlib test suite"""
1527
1528
    def test_assert_isinstance(self):
1529
        self.assertIsInstance(2, int)
1530
        self.assertIsInstance(u'', basestring)
1531
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1532
        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
1533
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
1534
    def test_assertEndsWith(self):
1535
        self.assertEndsWith('foo', 'oo')
1536
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1537
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1538
    def test_applyDeprecated_not_deprecated(self):
1539
        sample_object = ApplyDeprecatedHelper()
1540
        # calling an undeprecated callable raises an assertion
1541
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1542
            sample_object.sample_normal_method)
1543
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1544
            sample_undeprecated_function, "a param value")
1545
        # calling a deprecated callable (function or method) with the wrong
1546
        # expected deprecation fails.
1547
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1548
            sample_object.sample_deprecated_method, "a param value")
1549
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1550
            sample_deprecated_function)
1551
        # calling a deprecated callable (function or method) with the right
1552
        # expected deprecation returns the functions result.
1553
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1554
            sample_object.sample_deprecated_method, "a param value"))
1555
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
1556
            sample_deprecated_function))
1557
        # calling a nested deprecation with the wrong deprecation version
1558
        # fails even if a deeper nested function was deprecated with the 
1559
        # supplied version.
1560
        self.assertRaises(AssertionError, self.applyDeprecated,
1561
            zero_eleven, sample_object.sample_nested_deprecation)
1562
        # calling a nested deprecation with the right deprecation value
1563
        # returns the calls result.
1564
        self.assertEqual(2, self.applyDeprecated(zero_ten,
1565
            sample_object.sample_nested_deprecation))
1566
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1567
    def test_callDeprecated(self):
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1568
        def testfunc(be_deprecated, result=None):
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1569
            if be_deprecated is True:
1570
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
1571
                                       stacklevel=1)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1572
            return result
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1573
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1574
        self.assertIs(None, result)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1575
        result = self.callDeprecated([], testfunc, False, 'result')
1551.8.8 by Aaron Bentley
Made assertDeprecated return the callable's result
1576
        self.assertEqual('result', result)
1982.3.2 by Robert Collins
New TestCase helper applyDeprecated. This allows you to call a callable
1577
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1551.8.9 by Aaron Bentley
Rename assertDeprecated to callDeprecated
1578
        self.callDeprecated([], testfunc, be_deprecated=False)
1910.2.10 by Aaron Bentley
Add tests for assertDeprecated
1579
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1580
2592.3.242 by Martin Pool
New method TestCase.call_catch_warnings
1581
class TestWarningTests(TestCase):
1582
    """Tests for calling methods that raise warnings."""
1583
1584
    def test_callCatchWarnings(self):
1585
        def meth(a, b):
1586
            warnings.warn("this is your last warning")
1587
            return a + b
1588
        wlist, result = self.callCatchWarnings(meth, 1, 2)
1589
        self.assertEquals(3, result)
1590
        # would like just to compare them, but UserWarning doesn't implement
1591
        # eq well
1592
        w0, = wlist
1593
        self.assertIsInstance(w0, UserWarning)
2592.3.247 by Andrew Bennetts
Fix test_callCatchWarnings to pass when run with Python 2.4.
1594
        self.assertEquals("this is your last warning", str(w0))
2592.3.242 by Martin Pool
New method TestCase.call_catch_warnings
1595
1596
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1597
class TestConvenienceMakers(TestCaseWithTransport):
1598
    """Test for the make_* convenience functions."""
1599
1600
    def test_make_branch_and_tree_with_format(self):
1601
        # we should be able to supply a format to make_branch_and_tree
1602
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1603
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1604
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1605
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1606
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1607
                              bzrlib.bzrdir.BzrDirFormat6)
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1608
1986.2.1 by Robert Collins
Bugfix - the name of the test for make_branch_and_memory_tree was wrong.
1609
    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
1610
        # we should be able to get a new branch and a mutable tree from
1611
        # TestCaseWithTransport
1612
        tree = self.make_branch_and_memory_tree('a')
1613
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1614
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1615
1910.14.1 by Andrew Bennetts
Fix to make_branch_and_tree's behavior when used with an sftp transport.
1616
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1617
1618
    def test_make_tree_for_sftp_branch(self):
1619
        """Transports backed by local directories create local trees."""
1620
1621
        tree = self.make_branch_and_tree('t1')
1622
        base = tree.bzrdir.root_transport.base
1623
        self.failIf(base.startswith('sftp'),
1624
                'base %r is on sftp but should be local' % base)
1625
        self.assertEquals(tree.bzrdir.root_transport,
1626
                tree.branch.bzrdir.root_transport)
1627
        self.assertEquals(tree.bzrdir.root_transport,
1628
                tree.branch.repository.bzrdir.root_transport)
1629
1630
1707.2.1 by Robert Collins
'bzr selftest --benchmark' will run a new benchmarking selftest.
1631
class TestSelftest(TestCase):
1632
    """Tests of bzrlib.tests.selftest."""
1633
1634
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1635
        factory_called = []
1636
        def factory():
1637
            factory_called.append(True)
1638
            return TestSuite()
1639
        out = StringIO()
1640
        err = StringIO()
1641
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1642
            test_suite_factory=factory)
1643
        self.assertEqual([True], factory_called)
2172.4.3 by Alexander Belchenko
Change name of option to '--clean-output' and provide tests
1644
1645
2367.1.2 by Robert Collins
Some minor cleanups of test code, and implement KnownFailure support as
1646
class TestKnownFailure(TestCase):
1647
1648
    def test_known_failure(self):
1649
        """Check that KnownFailure is defined appropriately."""
1650
        # a KnownFailure is an assertion error for compatability with unaware
1651
        # runners.
1652
        self.assertIsInstance(KnownFailure(""), AssertionError)
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1653
1551.13.9 by Aaron Bentley
Implement TestCase.expectFailure
1654
    def test_expect_failure(self):
1655
        try:
1656
            self.expectFailure("Doomed to failure", self.assertTrue, False)
1657
        except KnownFailure, e:
1658
            self.assertEqual('Doomed to failure', e.args[0])
1659
        try:
1660
            self.expectFailure("Doomed to failure", self.assertTrue, True)
1661
        except AssertionError, e:
1662
            self.assertEqual('Unexpected success.  Should have failed:'
1663
                             ' Doomed to failure', e.args[0])
1664
        else:
1665
            self.fail('Assertion not raised')
1666
2367.1.4 by Robert Collins
Add operating system Feature model to bzrlib.tests to allow writing tests
1667
1668
class TestFeature(TestCase):
1669
1670
    def test_caching(self):
1671
        """Feature._probe is called by the feature at most once."""
1672
        class InstrumentedFeature(Feature):
1673
            def __init__(self):
1674
                Feature.__init__(self)
1675
                self.calls = []
1676
            def _probe(self):
1677
                self.calls.append('_probe')
1678
                return False
1679
        feature = InstrumentedFeature()
1680
        feature.available()
1681
        self.assertEqual(['_probe'], feature.calls)
1682
        feature.available()
1683
        self.assertEqual(['_probe'], feature.calls)
1684
1685
    def test_named_str(self):
1686
        """Feature.__str__ should thunk to feature_name()."""
1687
        class NamedFeature(Feature):
1688
            def feature_name(self):
1689
                return 'symlinks'
1690
        feature = NamedFeature()
1691
        self.assertEqual('symlinks', str(feature))
1692
1693
    def test_default_str(self):
1694
        """Feature.__str__ should default to __class__.__name__."""
1695
        class NamedFeature(Feature):
1696
            pass
1697
        feature = NamedFeature()
1698
        self.assertEqual('NamedFeature', str(feature))
2367.1.6 by Robert Collins
Allow per-test-fixture feature requirements via 'requireFeature'.(Robert Collins)
1699
1700
1701
class TestUnavailableFeature(TestCase):
1702
1703
    def test_access_feature(self):
1704
        feature = Feature()
1705
        exception = UnavailableFeature(feature)
1706
        self.assertIs(feature, exception.args[0])
2394.2.5 by Ian Clatworthy
list-only working, include test not
1707
1708
1709
class TestSelftestFiltering(TestCase):
1710
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
1711
    def setUp(self):
1712
        self.suite = TestUtil.TestSuite()
1713
        self.loader = TestUtil.TestLoader()
1714
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1715
            'bzrlib.tests.test_selftest']))
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1716
        self.all_names = _test_ids(self.suite)
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
1717
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
1718
    def test_condition_id_re(self):
1719
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1720
            'test_condition_id_re')
1721
        filtered_suite = filter_suite_by_condition(self.suite,
1722
            condition_id_re('test_condition_id_re'))
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1723
        self.assertEqual([test_name], _test_ids(filtered_suite))
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
1724
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1725
    def test_condition_id_in_list(self):
1726
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1727
                      'test_condition_id_in_list']
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1728
        id_list = tests.TestIdList(test_names)
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1729
        filtered_suite = filter_suite_by_condition(
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1730
            self.suite, tests.condition_id_in_list(id_list))
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1731
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1732
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1733
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1734
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
1735
    def test_condition_id_startswith(self):
1736
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1737
        start = klass + 'test_condition_id_starts'
1738
        test_names = [klass + 'test_condition_id_startswith']
1739
        filtered_suite = filter_suite_by_condition(
1740
            self.suite, tests.condition_id_startswith(start))
1741
        my_pattern = 'TestSelftestFiltering.*test_condition_id_startswith'
1742
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
1743
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1744
2921.6.8 by Robert Collins
* New helper function ``bzrlib.tests.condition_isinstance`` which helps
1745
    def test_condition_isinstance(self):
1746
        filtered_suite = filter_suite_by_condition(self.suite,
1747
            condition_isinstance(self.__class__))
1748
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1749
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1750
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
2921.6.8 by Robert Collins
* New helper function ``bzrlib.tests.condition_isinstance`` which helps
1751
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
1752
    def test_exclude_tests_by_condition(self):
1753
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1754
            'test_exclude_tests_by_condition')
1755
        filtered_suite = exclude_tests_by_condition(self.suite,
1756
            lambda x:x.id() == excluded_name)
1757
        self.assertEqual(len(self.all_names) - 1,
1758
            filtered_suite.countTestCases())
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1759
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
1760
        remaining_names = list(self.all_names)
1761
        remaining_names.remove(excluded_name)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1762
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
2921.6.9 by Robert Collins
* New helper function ``bzrlib.tests.condition_id_re`` which helps
1763
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
1764
    def test_exclude_tests_by_re(self):
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1765
        self.all_names = _test_ids(self.suite)
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
1766
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1767
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1768
            'test_exclude_tests_by_re')
1769
        self.assertEqual(len(self.all_names) - 1,
1770
            filtered_suite.countTestCases())
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1771
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
1772
        remaining_names = list(self.all_names)
1773
        remaining_names.remove(excluded_name)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1774
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
2921.6.2 by Robert Collins
* New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
1775
2921.6.7 by Robert Collins
* New helper function ``bzrlib.tests.filter_suite_by_condition`` which
1776
    def test_filter_suite_by_condition(self):
1777
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1778
            'test_filter_suite_by_condition')
1779
        filtered_suite = filter_suite_by_condition(self.suite,
1780
            lambda x:x.id() == test_name)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1781
        self.assertEqual([test_name], _test_ids(filtered_suite))
2921.6.7 by Robert Collins
* New helper function ``bzrlib.tests.filter_suite_by_condition`` which
1782
2394.2.5 by Ian Clatworthy
list-only working, include test not
1783
    def test_filter_suite_by_re(self):
2921.6.7 by Robert Collins
* New helper function ``bzrlib.tests.filter_suite_by_condition`` which
1784
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1785
        filtered_names = _test_ids(filtered_suite)
2394.2.7 by Ian Clatworthy
Added whitebox tests - filter_suite_by_re and sort_suite_by_re
1786
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1787
            'TestSelftestFiltering.test_filter_suite_by_re'])
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
1788
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1789
    def test_filter_suite_by_id_list(self):
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1790
        test_list = ['bzrlib.tests.test_selftest.'
1791
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1792
        filtered_suite = tests.filter_suite_by_id_list(
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1793
            self.suite, tests.TestIdList(test_list))
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1794
        filtered_names = _test_ids(filtered_suite)
3193.1.2 by Vincent Ladeuil
Add condition_id_in_list and filter_suite_by_id_list capabilities.
1795
        self.assertEqual(
1796
            filtered_names,
1797
            ['bzrlib.tests.test_selftest.'
1798
             'TestSelftestFiltering.test_filter_suite_by_id_list'])
1799
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
1800
    def test_filter_suite_by_id_startswith(self):
3302.11.6 by Vincent Ladeuil
Fixed as per Martin and John reviews. Also fix a bug.
1801
        # By design this test may fail if another test is added whose name also
1802
        # begins with the start value used.
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
1803
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1804
        start = klass + 'test_filter_suite_by_id_starts'
1805
        test_list = [klass + 'test_filter_suite_by_id_startswith']
1806
        filtered_suite = tests.filter_suite_by_id_startswith(self.suite, start)
1807
        filtered_names = _test_ids(filtered_suite)
1808
        self.assertEqual(
1809
            filtered_names,
1810
            ['bzrlib.tests.test_selftest.'
1811
             'TestSelftestFiltering.test_filter_suite_by_id_startswith'])
1812
2921.6.6 by Robert Collins
* The ``exclude_pattern`` and ``random_order`` parameters to the function
1813
    def test_preserve_input(self):
1814
        # NB: Surely this is something in the stdlib to do this?
1815
        self.assertTrue(self.suite is preserve_input(self.suite))
1816
        self.assertTrue("@#$" is preserve_input("@#$"))
1817
3128.1.2 by Vincent Ladeuil
Tweak as per review feedback: s/randomise.*/randomize&/, 0.92 -> 1.0.
1818
    def test_randomize_suite(self):
1819
        randomized_suite = randomize_suite(self.suite)
1820
        # randomizing should not add or remove test names.
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1821
        self.assertEqual(set(_test_ids(self.suite)),
1822
                         set(_test_ids(randomized_suite)))
2921.6.3 by Robert Collins
* New helper method ``bzrlib.tests.randomise_suite`` which returns a
1823
        # Technically, this *can* fail, because random.shuffle(list) can be
1824
        # equal to list. Trying multiple times just pushes the frequency back.
1825
        # As its len(self.all_names)!:1, the failure frequency should be low
1826
        # enough to ignore. RBC 20071021.
1827
        # It should change the order.
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1828
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
2921.6.3 by Robert Collins
* New helper method ``bzrlib.tests.randomise_suite`` which returns a
1829
        # But not the length. (Possibly redundant with the set test, but not
1830
        # necessarily.)
3302.7.4 by Vincent Ladeuil
Cosmetic change.
1831
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
2921.6.3 by Robert Collins
* New helper method ``bzrlib.tests.randomise_suite`` which returns a
1832
3350.5.1 by Robert Collins
* New helper function for splitting test suites ``split_suite_by_condition``.
1833
    def test_split_suit_by_condition(self):
1834
        self.all_names = _test_ids(self.suite)
1835
        condition = condition_id_re('test_filter_suite_by_r')
1836
        split_suite = split_suite_by_condition(self.suite, condition)
1837
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1838
            'test_filter_suite_by_re')
1839
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1840
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1841
        remaining_names = list(self.all_names)
1842
        remaining_names.remove(filtered_name)
1843
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1844
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
1845
    def test_split_suit_by_re(self):
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1846
        self.all_names = _test_ids(self.suite)
2921.6.7 by Robert Collins
* New helper function ``bzrlib.tests.filter_suite_by_condition`` which
1847
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
1848
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1849
            'test_filter_suite_by_re')
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1850
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1851
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
1852
        remaining_names = list(self.all_names)
1853
        remaining_names.remove(filtered_name)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
1854
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2921.6.1 by Robert Collins
* New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
1855
2545.3.2 by James Westby
Add a test for check_inventory_shape.
1856
1857
class TestCheckInventoryShape(TestCaseWithTransport):
1858
1859
    def test_check_inventory_shape(self):
2561.1.2 by Aaron Bentley
Fix indenting in TestCheckInventoryShape
1860
        files = ['a', 'b/', 'b/c']
1861
        tree = self.make_branch_and_tree('.')
1862
        self.build_tree(files)
1863
        tree.add(files)
1864
        tree.lock_read()
1865
        try:
1866
            self.check_inventory_shape(tree.inventory, files)
1867
        finally:
1868
            tree.unlock()
2830.2.1 by Martin Pool
If TestCase.run_bzr hits an internal exception, don't catch it but rather propagate up into the test suite
1869
1870
1871
class TestBlackboxSupport(TestCase):
1872
    """Tests for testsuite blackbox features."""
1873
1874
    def test_run_bzr_failure_not_caught(self):
1875
        # When we run bzr in blackbox mode, we want any unexpected errors to
1876
        # propagate up to the test suite so that it can show the error in the
1877
        # usual way, and we won't get a double traceback.
1878
        e = self.assertRaises(
1879
            AssertionError,
1880
            self.run_bzr, ['assert-fail'])
1881
        # make sure we got the real thing, not an error from somewhere else in
1882
        # the test framework
1883
        self.assertEquals('always fails', str(e))
1884
        # check that there's no traceback in the test log
1885
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
1886
            r'Traceback')
1887
1888
    def test_run_bzr_user_error_caught(self):
1889
        # Running bzr in blackbox mode, normal/expected/user errors should be
1890
        # caught in the regular way and turned into an error message plus exit
1891
        # code.
1892
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
1893
        self.assertEqual(out, '')
3146.4.7 by Aaron Bentley
Remove UNIX path assumption
1894
        self.assertContainsRe(err,
1895
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
1896
1897
1898
class TestTestLoader(TestCase):
1899
    """Tests for the test loader."""
1900
1901
    def _get_loader_and_module(self):
1902
        """Gets a TestLoader and a module with one test in it."""
1903
        loader = TestUtil.TestLoader()
1904
        module = {}
1905
        class Stub(TestCase):
1906
            def test_foo(self):
1907
                pass
1908
        class MyModule(object):
1909
            pass
1910
        MyModule.a_class = Stub
1911
        module = MyModule()
1912
        return loader, module
1913
1914
    def test_module_no_load_tests_attribute_loads_classes(self):
1915
        loader, module = self._get_loader_and_module()
1916
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
1917
1918
    def test_module_load_tests_attribute_gets_called(self):
1919
        loader, module = self._get_loader_and_module()
1920
        # 'self' is here because we're faking the module with a class. Regular
1921
        # load_tests do not need that :)
1922
        def load_tests(self, standard_tests, module, loader):
1923
            result = loader.suiteClass()
1924
            for test in iter_suite_tests(standard_tests):
1925
                result.addTests([test, test])
1926
            return result
1927
        # add a load_tests() method which multiplies the tests from the module.
1928
        module.__class__.load_tests = load_tests
1929
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
1930
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
1931
    def test_load_tests_from_module_name_smoke_test(self):
1932
        loader = TestUtil.TestLoader()
1933
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
1934
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
1935
                          _test_ids(suite))
1936
3302.7.8 by Vincent Ladeuil
Fix typos.
1937
    def test_load_tests_from_module_name_with_bogus_module_name(self):
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
1938
        loader = TestUtil.TestLoader()
1939
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
1940
3193.1.1 by Vincent Ladeuil
Helper to filter test suite building by module when loading a list.
1941
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1942
class TestTestIdList(tests.TestCase):
1943
1944
    def _create_id_list(self, test_list):
1945
        return tests.TestIdList(test_list)
1946
1947
    def _create_suite(self, test_id_list):
3193.1.5 by Vincent Ladeuil
Add helper method to get only listed tests from a module test suite.
1948
1949
        class Stub(TestCase):
1950
            def test_foo(self):
1951
                pass
1952
1953
        def _create_test_id(id):
1954
            return lambda: id
1955
1956
        suite = TestUtil.TestSuite()
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1957
        for id in test_id_list:
3193.1.5 by Vincent Ladeuil
Add helper method to get only listed tests from a module test suite.
1958
            t  = Stub('test_foo')
1959
            t.id = _create_test_id(id)
1960
            suite.addTest(t)
1961
        return suite
1962
1963
    def _test_ids(self, test_suite):
1964
        """Get the ids for the tests in a test suite."""
1965
        return [t.id() for t in iter_suite_tests(test_suite)]
1966
3193.1.1 by Vincent Ladeuil
Helper to filter test suite building by module when loading a list.
1967
    def test_empty_list(self):
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1968
        id_list = self._create_id_list([])
1969
        self.assertEquals({}, id_list.tests)
1970
        self.assertEquals({}, id_list.modules)
3193.1.1 by Vincent Ladeuil
Helper to filter test suite building by module when loading a list.
1971
1972
    def test_valid_list(self):
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1973
        id_list = self._create_id_list(
3193.1.4 by Vincent Ladeuil
Make TestTestIdListFilter aware that a test exists for a module or one of
1974
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1975
             'mod1.func1', 'mod1.cl2.meth2',
1976
             'mod1.submod1',
3193.1.4 by Vincent Ladeuil
Make TestTestIdListFilter aware that a test exists for a module or one of
1977
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
1978
             ])
3302.8.3 by Vincent Ladeuil
Use better names for TestIdList methods.
1979
        self.assertTrue(id_list.refers_to('mod1'))
1980
        self.assertTrue(id_list.refers_to('mod1.submod1'))
1981
        self.assertTrue(id_list.refers_to('mod1.submod2'))
1982
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
1983
        self.assertTrue(id_list.includes('mod1.submod1'))
1984
        self.assertTrue(id_list.includes('mod1.func1'))
3193.1.1 by Vincent Ladeuil
Helper to filter test suite building by module when loading a list.
1985
1986
    def test_bad_chars_in_params(self):
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1987
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
3302.8.3 by Vincent Ladeuil
Use better names for TestIdList methods.
1988
        self.assertTrue(id_list.refers_to('mod1'))
1989
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
3193.1.4 by Vincent Ladeuil
Make TestTestIdListFilter aware that a test exists for a module or one of
1990
1991
    def test_module_used(self):
3193.1.11 by Vincent Ladeuil
Relax constraint on test ids, simplify implementation and update tests.
1992
        id_list = self._create_id_list(['mod.class.meth'])
3302.8.3 by Vincent Ladeuil
Use better names for TestIdList methods.
1993
        self.assertTrue(id_list.refers_to('mod'))
1994
        self.assertTrue(id_list.refers_to('mod.class'))
1995
        self.assertTrue(id_list.refers_to('mod.class.meth'))
3193.1.6 by Vincent Ladeuil
Filter the whole test suite.
1996
1997
    def test_test_suite(self):
1998
        # This test is slow, so we do a single test with one test in each
1999
        # category
2000
        test_list = [
2001
            # testmod_names
3302.9.23 by Vincent Ladeuil
Simplify test_suite().
2002
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
3193.1.12 by Vincent Ladeuil
Fix typo (using test id list is no replacement for running the whole test suite QED).
2003
            'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
3193.1.6 by Vincent Ladeuil
Filter the whole test suite.
2004
            # transport implementations
2005
            'bzrlib.tests.test_transport_implementations.TransportTests'
2006
            '.test_abspath(LocalURLServer)',
3302.9.23 by Vincent Ladeuil
Simplify test_suite().
2007
            # modules_to_doctest
3193.1.6 by Vincent Ladeuil
Filter the whole test suite.
2008
            'bzrlib.timestamp.format_highres_date',
3193.1.8 by Vincent Ladeuil
Add '--load-list' option to selftest.
2009
            # plugins can't be tested that way since selftest may be run with
2010
            # --no-plugins
3193.1.6 by Vincent Ladeuil
Filter the whole test suite.
2011
            ]
2012
        suite = tests.test_suite(test_list)
3302.7.1 by Vincent Ladeuil
Extract _test_ids helper for reuse by other test classes.
2013
        self.assertEquals(test_list, _test_ids(suite))
3193.1.7 by Vincent Ladeuil
Load test id list from a text file.
2014
3302.3.1 by Vincent Ladeuil
Help identify duplicates IDs in test suite and missing tests in id
2015
    def test_test_suite_matches_id_list_with_unknown(self):
2016
        loader = TestUtil.TestLoader()
3302.7.6 by Vincent Ladeuil
Catch up with loadTestsFromModuleName use.
2017
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3302.3.1 by Vincent Ladeuil
Help identify duplicates IDs in test suite and missing tests in id
2018
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2019
                     'bogus']
2020
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2021
        self.assertEquals(['bogus'], not_found)
2022
        self.assertEquals([], duplicates)
2023
2024
    def test_suite_matches_id_list_with_duplicates(self):
2025
        loader = TestUtil.TestLoader()
3302.7.6 by Vincent Ladeuil
Catch up with loadTestsFromModuleName use.
2026
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3302.3.1 by Vincent Ladeuil
Help identify duplicates IDs in test suite and missing tests in id
2027
        dupes = loader.suiteClass()
2028
        for test in iter_suite_tests(suite):
2029
            dupes.addTest(test)
2030
            dupes.addTest(test) # Add it again
2031
2032
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2033
        not_found, duplicates = tests.suite_matches_id_list(
2034
            dupes, test_list)
2035
        self.assertEquals([], not_found)
2036
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2037
                          duplicates)
2038
3193.1.7 by Vincent Ladeuil
Load test id list from a text file.
2039
2040
class TestLoadTestIdList(tests.TestCaseInTempDir):
2041
2042
    def _create_test_list_file(self, file_name, content):
2043
        fl = open(file_name, 'wt')
2044
        fl.write(content)
2045
        fl.close()
2046
2047
    def test_load_unknown(self):
2048
        self.assertRaises(errors.NoSuchFile,
2049
                          tests.load_test_id_list, 'i_do_not_exist')
2050
2051
    def test_load_test_list(self):
2052
        test_list_fname = 'test.list'
2053
        self._create_test_list_file(test_list_fname,
2054
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
2055
        tlist = tests.load_test_id_list(test_list_fname)
2056
        self.assertEquals(2, len(tlist))
2057
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2058
        self.assertEquals('mod2.cl2.meth2', tlist[1])
2059
2060
    def test_load_dirty_file(self):
2061
        test_list_fname = 'test.list'
2062
        self._create_test_list_file(test_list_fname,
2063
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
2064
                                    'bar baz\n')
2065
        tlist = tests.load_test_id_list(test_list_fname)
2066
        self.assertEquals(4, len(tlist))
2067
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2068
        self.assertEquals('', tlist[1])
2069
        self.assertEquals('mod2.cl2.meth2', tlist[2])
2070
        self.assertEquals('bar baz', tlist[3])
2071
2072
3302.8.2 by Vincent Ladeuil
New test loader reducing modules imports and tests loaded.
2073
class TestFilteredByModuleTestLoader(tests.TestCase):
2074
2075
    def _create_loader(self, test_list):
2076
        id_filter = tests.TestIdList(test_list)
3302.8.4 by Vincent Ladeuil
Cosmetic changes.
2077
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
3302.8.2 by Vincent Ladeuil
New test loader reducing modules imports and tests loaded.
2078
        return loader
2079
2080
    def test_load_tests(self):
2081
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2082
        loader = self._create_loader(test_list)
2083
2084
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2085
        self.assertEquals(test_list, _test_ids(suite))
2086
2087
    def test_exclude_tests(self):
2088
        test_list = ['bogus']
2089
        loader = self._create_loader(test_list)
2090
2091
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2092
        self.assertEquals([], _test_ids(suite))
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
2093
2094
2095
class TestFilteredByNameStartTestLoader(tests.TestCase):
2096
2097
    def _create_loader(self, name_start):
3302.11.6 by Vincent Ladeuil
Fixed as per Martin and John reviews. Also fix a bug.
2098
        def needs_module(name):
2099
            return name.startswith(name_start) or name_start.startswith(name)
2100
        loader = TestUtil.FilteredByModuleTestLoader(needs_module)
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
2101
        return loader
2102
2103
    def test_load_tests(self):
2104
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
3302.11.6 by Vincent Ladeuil
Fixed as per Martin and John reviews. Also fix a bug.
2105
        loader = self._create_loader('bzrlib.tests.test_samp')
2106
2107
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2108
        self.assertEquals(test_list, _test_ids(suite))
2109
2110
    def test_load_tests_inside_module(self):
2111
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2112
        loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
3302.11.1 by Vincent Ladeuil
Create a new selftest filter allowing loading only one module/class/test.
2113
2114
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2115
        self.assertEquals(test_list, _test_ids(suite))
2116
2117
    def test_exclude_tests(self):
2118
        test_list = ['bogus']
2119
        loader = self._create_loader('bogus')
2120
2121
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2122
        self.assertEquals([], _test_ids(suite))