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