~bzr-pqm/bzr/bzr.dev

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