/brz/remove-bazaar

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