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