~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Jelmer Vernooij
  • Date: 2009-01-28 18:42:55 UTC
  • mto: This revision was merged to the branch mainline in revision 3968.
  • Revision ID: jelmer@samba.org-20090128184255-bdmklkvm83ltk191
Update NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
26
26
 
27
27
import bzrlib
28
28
from bzrlib import (
 
29
    branchbuilder,
29
30
    bzrdir,
30
31
    errors,
31
32
    memorytree,
32
33
    osutils,
 
34
    remote,
33
35
    repository,
34
36
    symbol_versioning,
 
37
    tests,
 
38
    workingtree,
35
39
    )
36
40
from bzrlib.progress import _BaseProgressBar
37
 
from bzrlib.repofmt import weaverepo
38
 
from bzrlib.symbol_versioning import zero_ten, zero_eleven
 
41
from bzrlib.repofmt import (
 
42
    pack_repo,
 
43
    weaverepo,
 
44
    )
 
45
from bzrlib.symbol_versioning import (
 
46
    one_zero,
 
47
    zero_eleven,
 
48
    zero_ten,
 
49
    )
39
50
from bzrlib.tests import (
40
51
                          ChrootedTestCase,
 
52
                          ExtendedTestResult,
 
53
                          Feature,
 
54
                          KnownFailure,
41
55
                          TestCase,
42
56
                          TestCaseInTempDir,
43
57
                          TestCaseWithMemoryTransport,
44
58
                          TestCaseWithTransport,
 
59
                          TestNotApplicable,
45
60
                          TestSkipped,
46
61
                          TestSuite,
 
62
                          TestUtil,
47
63
                          TextTestRunner,
 
64
                          UnavailableFeature,
 
65
                          condition_id_re,
 
66
                          condition_isinstance,
 
67
                          exclude_tests_by_condition,
 
68
                          exclude_tests_by_re,
 
69
                          filter_suite_by_condition,
 
70
                          filter_suite_by_re,
 
71
                          iter_suite_tests,
 
72
                          preserve_input,
 
73
                          randomize_suite,
 
74
                          split_suite_by_condition,
 
75
                          split_suite_by_re,
 
76
                          test_lsprof,
 
77
                          test_suite,
48
78
                          )
49
79
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
50
80
from bzrlib.tests.TestUtil import _load_module_by_name
53
83
from bzrlib.version import _get_bzr_source_tree
54
84
 
55
85
 
 
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
 
56
91
class SelftestTests(TestCase):
57
92
 
58
93
    def test_import_tests(self):
74
109
                              'a test message\n')
75
110
 
76
111
 
 
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
 
77
121
class TestTreeShape(TestCaseInTempDir):
78
122
 
79
123
    def test_unicode_paths(self):
 
124
        self.requireFeature(tests.UnicodeFilename)
 
125
 
80
126
        filename = u'hell\u00d8'
81
 
        try:
82
 
            self.build_tree_contents([(filename, 'contents of hello')])
83
 
        except UnicodeEncodeError:
84
 
            raise TestSkipped("can't build unicode working tree in "
85
 
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
127
        self.build_tree_contents([(filename, 'contents of hello')])
86
128
        self.failUnlessExists(filename)
87
129
 
88
130
 
97
139
    """
98
140
 
99
141
    def test_get_transport_permutations(self):
100
 
        # this checks that we the module get_test_permutations call
101
 
        # is made by the adapter get_transport_test_permitations method.
 
142
        # this checks that get_test_permutations defined by the module is
 
143
        # called by the adapter get_transport_test_permutations method.
102
144
        class MockModule(object):
103
145
            def get_test_permutations(self):
104
146
                return sample_permutation
105
147
        sample_permutation = [(1,2), (3,4)]
106
 
        from bzrlib.transport import TransportTestProviderAdapter
 
148
        from bzrlib.tests.test_transport_implementations \
 
149
            import TransportTestProviderAdapter
107
150
        adapter = TransportTestProviderAdapter()
108
151
        self.assertEqual(sample_permutation,
109
152
                         adapter.get_transport_test_permutations(MockModule()))
110
153
 
111
154
    def test_adapter_checks_all_modules(self):
112
 
        # this checks that the adapter returns as many permurtations as
113
 
        # there are in all the registered# transport modules for there
114
 
        # - we assume if this matches its probably doing the right thing
115
 
        # especially in combination with the tests for setting the right
116
 
        # classes below.
117
 
        from bzrlib.transport import (TransportTestProviderAdapter,
118
 
                                      _get_transport_modules
119
 
                                      )
 
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.
 
159
        from bzrlib.tests.test_transport_implementations \
 
160
            import TransportTestProviderAdapter
 
161
        from bzrlib.transport import _get_transport_modules
120
162
        modules = _get_transport_modules()
121
163
        permutation_count = 0
122
164
        for module in modules:
139
181
        # This test used to know about all the possible transports and the
140
182
        # order they were returned but that seems overly brittle (mbp
141
183
        # 20060307)
142
 
        input_test = TestTransportProviderAdapter(
143
 
            "test_adapter_sets_transport_class")
144
 
        from bzrlib.transport import TransportTestProviderAdapter
145
 
        suite = TransportTestProviderAdapter().adapt(input_test)
146
 
        tests = list(iter(suite))
147
 
        self.assertTrue(len(tests) > 6)
 
184
        from bzrlib.tests.test_transport_implementations \
 
185
            import TransportTestProviderAdapter
 
186
        scenarios = TransportTestProviderAdapter().scenarios
148
187
        # there are at least that many builtin transports
149
 
        one_test = tests[0]
150
 
        self.assertTrue(issubclass(one_test.transport_class, 
 
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"],
151
192
                                   bzrlib.transport.Transport))
152
 
        self.assertTrue(issubclass(one_test.transport_server, 
 
193
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
153
194
                                   bzrlib.transport.Server))
154
195
 
155
196
 
156
197
class TestBranchProviderAdapter(TestCase):
157
198
    """A group of tests that test the branch implementation test adapter."""
158
199
 
159
 
    def test_adapted_tests(self):
 
200
    def test_constructor(self):
160
201
        # check that constructor parameters are passed through to the adapted
161
202
        # test.
162
 
        from bzrlib.branch import BranchTestProviderAdapter
163
 
        input_test = TestBranchProviderAdapter(
164
 
            "test_adapted_tests")
 
203
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
165
204
        server1 = "a"
166
205
        server2 = "b"
167
206
        formats = [("c", "C"), ("d", "D")]
168
207
        adapter = BranchTestProviderAdapter(server1, server2, formats)
169
 
        suite = adapter.adapt(input_test)
170
 
        tests = list(iter(suite))
171
 
        self.assertEqual(2, len(tests))
172
 
        self.assertEqual(tests[0].branch_format, formats[0][0])
173
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
174
 
        self.assertEqual(tests[0].transport_server, server1)
175
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
176
 
        self.assertEqual(tests[1].branch_format, formats[1][0])
177
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
178
 
        self.assertEqual(tests[1].transport_server, server1)
179
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
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)
180
221
 
181
222
 
182
223
class TestBzrDirProviderAdapter(TestCase):
185
226
    def test_adapted_tests(self):
186
227
        # check that constructor parameters are passed through to the adapted
187
228
        # test.
188
 
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
189
 
        input_test = TestBzrDirProviderAdapter(
190
 
            "test_adapted_tests")
 
229
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
 
230
        vfs_factory = "v"
191
231
        server1 = "a"
192
232
        server2 = "b"
193
233
        formats = ["c", "d"]
194
 
        adapter = BzrDirTestProviderAdapter(server1, server2, formats)
195
 
        suite = adapter.adapt(input_test)
196
 
        tests = list(iter(suite))
197
 
        self.assertEqual(2, len(tests))
198
 
        self.assertEqual(tests[0].bzrdir_format, formats[0])
199
 
        self.assertEqual(tests[0].transport_server, server1)
200
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
201
 
        self.assertEqual(tests[1].bzrdir_format, formats[1])
202
 
        self.assertEqual(tests[1].transport_server, server1)
203
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
204
 
 
205
 
 
206
 
class TestRepositoryProviderAdapter(TestCase):
 
234
        adapter = BzrDirTestProviderAdapter(vfs_factory,
 
235
            server1, server2, formats)
 
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)
 
248
 
 
249
 
 
250
class TestRepositoryParameterisation(TestCase):
207
251
    """A group of tests that test the repository implementation test adapter."""
208
252
 
209
 
    def test_adapted_tests(self):
210
 
        # check that constructor parameters are passed through to the adapted
211
 
        # test.
212
 
        from bzrlib.repository import RepositoryTestProviderAdapter
213
 
        input_test = TestRepositoryProviderAdapter(
214
 
            "test_adapted_tests")
215
 
        server1 = "a"
216
 
        server2 = "b"
217
 
        formats = [("c", "C"), ("d", "D")]
218
 
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
219
 
        suite = adapter.adapt(input_test)
220
 
        tests = list(iter(suite))
221
 
        self.assertEqual(2, len(tests))
222
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
223
 
        self.assertEqual(tests[0].repository_format, formats[0][0])
224
 
        self.assertEqual(tests[0].transport_server, server1)
225
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
226
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
227
 
        self.assertEqual(tests[1].repository_format, formats[1][0])
228
 
        self.assertEqual(tests[1].transport_server, server1)
229
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
253
    def test_formats_to_scenarios(self):
 
254
        """The adapter can generate all the scenarios needed."""
 
255
        from bzrlib.tests.per_repository import formats_to_scenarios
 
256
        formats = [("(c)", remote.RemoteRepositoryFormat()),
 
257
                   ("(d)", repository.format_registry.get(
 
258
                        'Bazaar pack repository format 1 (needs bzr 0.92)\n'))]
 
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")
 
263
        # no_vfs generate scenarios without vfs_transport_factory
 
264
        self.assertEqual([
 
265
            ('RemoteRepositoryFormat(c)',
 
266
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
 
267
              'repository_format': remote.RemoteRepositoryFormat(),
 
268
              'transport_readonly_server': 'readonly',
 
269
              'transport_server': 'server'}),
 
270
            ('RepositoryFormatKnitPack1(d)',
 
271
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
 
272
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
 
273
              'transport_readonly_server': 'readonly',
 
274
              'transport_server': 'server'})],
 
275
            no_vfs_scenarios)
 
276
        self.assertEqual([
 
277
            ('RemoteRepositoryFormat(c)',
 
278
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
 
279
              'repository_format': remote.RemoteRepositoryFormat(),
 
280
              'transport_readonly_server': 'readonly',
 
281
              'transport_server': 'server',
 
282
              'vfs_transport_factory': 'vfs'}),
 
283
            ('RepositoryFormatKnitPack1(d)',
 
284
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
 
285
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
 
286
              'transport_readonly_server': 'readonly',
 
287
              'transport_server': 'server',
 
288
              'vfs_transport_factory': 'vfs'})],
 
289
            vfs_scenarios)
 
290
 
 
291
 
 
292
class TestTestScenarioApplier(TestCase):
 
293
    """Tests for the test adaption facilities."""
 
294
 
 
295
    def test_adapt_applies_scenarios(self):
 
296
        from bzrlib.tests.per_repository 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
 
 
309
    def test_adapt_test_to_scenario(self):
 
310
        from bzrlib.tests.per_repository import TestScenarioApplier
 
311
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
 
312
        adapter = TestScenarioApplier()
 
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(
 
333
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
334
            "test_adapt_test_to_scenario(new id)",
 
335
            adapted_test1.id())
 
336
        self.assertEqual(None, adapted_test2.bzrdir_format)
 
337
        self.assertEqual(
 
338
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
339
            "test_adapt_test_to_scenario(new id 2)",
 
340
            adapted_test2.id())
230
341
 
231
342
 
232
343
class TestInterRepositoryProviderAdapter(TestCase):
235
346
    def test_adapted_tests(self):
236
347
        # check that constructor parameters are passed through to the adapted
237
348
        # test.
238
 
        from bzrlib.repository import InterRepositoryTestProviderAdapter
239
 
        input_test = TestInterRepositoryProviderAdapter(
240
 
            "test_adapted_tests")
 
349
        from bzrlib.tests.interrepository_implementations import \
 
350
            InterRepositoryTestProviderAdapter
241
351
        server1 = "a"
242
352
        server2 = "b"
243
353
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
244
354
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
245
 
        suite = adapter.adapt(input_test)
246
 
        tests = list(iter(suite))
247
 
        self.assertEqual(2, len(tests))
248
 
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
249
 
        self.assertEqual(tests[0].repository_format, formats[0][1])
250
 
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
251
 
        self.assertEqual(tests[0].transport_server, server1)
252
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
253
 
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
254
 
        self.assertEqual(tests[1].repository_format, formats[1][1])
255
 
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
256
 
        self.assertEqual(tests[1].transport_server, server1)
257
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
258
 
 
259
 
 
260
 
class TestInterVersionedFileProviderAdapter(TestCase):
261
 
    """A group of tests that test the InterVersionedFile test adapter."""
262
 
 
263
 
    def test_adapted_tests(self):
264
 
        # check that constructor parameters are passed through to the adapted
265
 
        # test.
266
 
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
267
 
        input_test = TestInterRepositoryProviderAdapter(
268
 
            "test_adapted_tests")
269
 
        server1 = "a"
270
 
        server2 = "b"
271
 
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
272
 
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
273
 
        suite = adapter.adapt(input_test)
274
 
        tests = list(iter(suite))
275
 
        self.assertEqual(2, len(tests))
276
 
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
277
 
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
278
 
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
279
 
        self.assertEqual(tests[0].transport_server, server1)
280
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
281
 
        self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
282
 
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
283
 
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
284
 
        self.assertEqual(tests[1].transport_server, server1)
285
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
286
 
 
287
 
 
288
 
class TestRevisionStoreProviderAdapter(TestCase):
289
 
    """A group of tests that test the RevisionStore test adapter."""
290
 
 
291
 
    def test_adapted_tests(self):
292
 
        # check that constructor parameters are passed through to the adapted
293
 
        # test.
294
 
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
295
 
        input_test = TestRevisionStoreProviderAdapter(
296
 
            "test_adapted_tests")
297
 
        # revision stores need a store factory - i.e. RevisionKnit
298
 
        #, a readonly and rw transport 
299
 
        # transport servers:
300
 
        server1 = "a"
301
 
        server2 = "b"
302
 
        store_factories = ["c", "d"]
303
 
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
304
 
        suite = adapter.adapt(input_test)
305
 
        tests = list(iter(suite))
306
 
        self.assertEqual(2, len(tests))
307
 
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
308
 
        self.assertEqual(tests[0].transport_server, server1)
309
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
310
 
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
311
 
        self.assertEqual(tests[1].transport_server, server1)
312
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
355
        self.assertEqual([
 
356
            ('str,str,str',
 
357
             {'interrepo_class': str,
 
358
              'repository_format': 'C1',
 
359
              'repository_format_to': 'C2',
 
360
              'transport_readonly_server': 'b',
 
361
              'transport_server': 'a'}),
 
362
            ('int,str,str',
 
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))
313
369
 
314
370
 
315
371
class TestWorkingTreeProviderAdapter(TestCase):
316
372
    """A group of tests that test the workingtree implementation test adapter."""
317
373
 
318
 
    def test_adapted_tests(self):
 
374
    def test_scenarios(self):
319
375
        # check that constructor parameters are passed through to the adapted
320
376
        # test.
321
 
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
322
 
        input_test = TestWorkingTreeProviderAdapter(
323
 
            "test_adapted_tests")
 
377
        from bzrlib.tests.workingtree_implementations \
 
378
            import WorkingTreeTestProviderAdapter
324
379
        server1 = "a"
325
380
        server2 = "b"
326
 
        formats = [("c", "C"), ("d", "D")]
 
381
        formats = [workingtree.WorkingTreeFormat2(),
 
382
                   workingtree.WorkingTreeFormat3(),]
327
383
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
328
 
        suite = adapter.adapt(input_test)
329
 
        tests = list(iter(suite))
330
 
        self.assertEqual(2, len(tests))
331
 
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
332
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
333
 
        self.assertEqual(tests[0].transport_server, server1)
334
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
335
 
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
336
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
337
 
        self.assertEqual(tests[1].transport_server, server1)
338
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
384
        self.assertEqual([
 
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]})],
 
395
            adapter.scenarios)
339
396
 
340
397
 
341
398
class TestTreeProviderAdapter(TestCase):
354
411
            return_parameter,
355
412
            revision_tree_from_workingtree
356
413
            )
357
 
        from bzrlib.workingtree import WorkingTreeFormat
358
414
        input_test = TestTreeProviderAdapter(
359
415
            "test_adapted_tests")
360
416
        server1 = "a"
361
417
        server2 = "b"
362
 
        formats = [("c", "C"), ("d", "D")]
 
418
        formats = [workingtree.WorkingTreeFormat2(),
 
419
                   workingtree.WorkingTreeFormat3(),]
363
420
        adapter = TreeTestProviderAdapter(server1, server2, formats)
364
421
        suite = adapter.adapt(input_test)
365
422
        tests = list(iter(suite))
366
 
        self.assertEqual(4, len(tests))
367
 
        default_format = WorkingTreeFormat.get_default_format()
368
 
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
369
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
423
        # XXX We should not have tests fail as we add more scenarios
 
424
        # abentley 20080412
 
425
        self.assertEqual(7, len(tests))
 
426
        # this must match the default format setp up in
 
427
        # TreeTestProviderAdapter.adapt
 
428
        default_format = workingtree.WorkingTreeFormat3
 
429
        self.assertEqual(tests[0].workingtree_format, formats[0])
 
430
        self.assertEqual(tests[0].bzrdir_format, formats[0]._matchingbzrdir)
370
431
        self.assertEqual(tests[0].transport_server, server1)
371
432
        self.assertEqual(tests[0].transport_readonly_server, server2)
372
 
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
373
 
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
374
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
433
        self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
 
434
        self.assertEqual(tests[1].workingtree_format, formats[1])
 
435
        self.assertEqual(tests[1].bzrdir_format, formats[1]._matchingbzrdir)
375
436
        self.assertEqual(tests[1].transport_server, server1)
376
437
        self.assertEqual(tests[1].transport_readonly_server, server2)
377
 
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
378
 
        self.assertEqual(tests[2].workingtree_format, default_format)
379
 
        self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
 
438
        self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
 
439
        self.assertIsInstance(tests[2].workingtree_format, default_format)
 
440
        #self.assertEqual(tests[2].bzrdir_format,
 
441
        #                 default_format._matchingbzrdir)
380
442
        self.assertEqual(tests[2].transport_server, server1)
381
443
        self.assertEqual(tests[2].transport_readonly_server, server2)
382
 
        self.assertEqual(tests[2].workingtree_to_test_tree,
 
444
        self.assertEqual(tests[2]._workingtree_to_test_tree,
383
445
            revision_tree_from_workingtree)
384
446
 
385
447
 
394
456
        # because each optimiser can be direction specific, we need to test
395
457
        # each optimiser in its chosen direction.
396
458
        # unlike the TestProviderAdapter we dont want to automatically add a
397
 
        # parameterised one for WorkingTree - the optimisers will tell us what
 
459
        # parameterized one for WorkingTree - the optimisers will tell us what
398
460
        # ones to add.
399
461
        from bzrlib.tests.tree_implementations import (
400
462
            return_parameter,
410
472
        server2 = "b"
411
473
        format1 = WorkingTreeFormat2()
412
474
        format2 = WorkingTreeFormat3()
413
 
        formats = [(str, format1, format2, "converter1"),
414
 
            (int, format2, format1, "converter2")]
 
475
        formats = [("1", str, format1, format2, "converter1"),
 
476
            ("2", int, format2, format1, "converter2")]
415
477
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
416
478
        suite = adapter.adapt(input_test)
417
479
        tests = list(iter(suite))
418
480
        self.assertEqual(2, len(tests))
419
 
        self.assertEqual(tests[0].intertree_class, formats[0][0])
420
 
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
421
 
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
422
 
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
423
 
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
481
        self.assertEqual(tests[0].intertree_class, formats[0][1])
 
482
        self.assertEqual(tests[0].workingtree_format, formats[0][2])
 
483
        self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
 
484
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][4])
 
485
        self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
424
486
        self.assertEqual(tests[0].transport_server, server1)
425
487
        self.assertEqual(tests[0].transport_readonly_server, server2)
426
 
        self.assertEqual(tests[1].intertree_class, formats[1][0])
427
 
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
428
 
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
429
 
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
430
 
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
488
        self.assertEqual(tests[1].intertree_class, formats[1][1])
 
489
        self.assertEqual(tests[1].workingtree_format, formats[1][2])
 
490
        self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
 
491
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][4])
 
492
        self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
431
493
        self.assertEqual(tests[1].transport_server, server1)
432
494
        self.assertEqual(tests[1].transport_readonly_server, server2)
433
495
 
437
499
    def test_home_is_not_working(self):
438
500
        self.assertNotEqual(self.test_dir, self.test_home_dir)
439
501
        cwd = osutils.getcwd()
440
 
        self.assertEqual(self.test_dir, cwd)
441
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
502
        self.assertIsSameRealPath(self.test_dir, cwd)
 
503
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
 
504
 
 
505
    def test_assertEqualStat_equal(self):
 
506
        from bzrlib.tests.test_dirstate import _FakeStat
 
507
        self.build_tree(["foo"])
 
508
        real = os.lstat("foo")
 
509
        fake = _FakeStat(real.st_size, real.st_mtime, real.st_ctime,
 
510
            real.st_dev, real.st_ino, real.st_mode)
 
511
        self.assertEqualStat(real, fake)
 
512
 
 
513
    def test_assertEqualStat_notequal(self):
 
514
        self.build_tree(["foo", "bar"])
 
515
        self.assertRaises(AssertionError, self.assertEqualStat,
 
516
            os.lstat("foo"), os.lstat("bar"))
442
517
 
443
518
 
444
519
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
452
527
        few tests should need to do that), and having a missing dir as home is
453
528
        an effective way to ensure that this is the case.
454
529
        """
455
 
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
530
        self.assertIsSameRealPath(
 
531
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
456
532
            self.test_home_dir)
457
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
533
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
458
534
        
459
535
    def test_cwd_is_TEST_ROOT(self):
460
 
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
536
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
461
537
        cwd = osutils.getcwd()
462
 
        self.assertEqual(self.test_dir, cwd)
 
538
        self.assertIsSameRealPath(self.test_dir, cwd)
463
539
 
464
540
    def test_make_branch_and_memory_tree(self):
465
541
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
485
561
        self.assertEqual(format.repository_format.__class__,
486
562
            tree.branch.repository._format.__class__)
487
563
 
 
564
    def test_make_branch_builder(self):
 
565
        builder = self.make_branch_builder('dir')
 
566
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
 
567
        # Guard against regression into MemoryTransport leaking
 
568
        # files to disk instead of keeping them in memory.
 
569
        self.failIf(osutils.lexists('dir'))
 
570
 
 
571
    def test_make_branch_builder_with_format(self):
 
572
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
 
573
        # that the format objects are used.
 
574
        format = bzrdir.BzrDirMetaFormat1()
 
575
        repo_format = weaverepo.RepositoryFormat7()
 
576
        format.repository_format = repo_format
 
577
        builder = self.make_branch_builder('dir', format=format)
 
578
        the_branch = builder.get_branch()
 
579
        # Guard against regression into MemoryTransport leaking
 
580
        # files to disk instead of keeping them in memory.
 
581
        self.failIf(osutils.lexists('dir'))
 
582
        self.assertEqual(format.repository_format.__class__,
 
583
                         the_branch.repository._format.__class__)
 
584
        self.assertEqual(repo_format.get_format_string(),
 
585
                         self.get_transport().get_bytes(
 
586
                            'dir/.bzr/repository/format'))
 
587
 
 
588
    def test_make_branch_builder_with_format_name(self):
 
589
        builder = self.make_branch_builder('dir', format='knit')
 
590
        the_branch = builder.get_branch()
 
591
        # Guard against regression into MemoryTransport leaking
 
592
        # files to disk instead of keeping them in memory.
 
593
        self.failIf(osutils.lexists('dir'))
 
594
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
 
595
        self.assertEqual(dir_format.repository_format.__class__,
 
596
                         the_branch.repository._format.__class__)
 
597
        self.assertEqual('Bazaar-NG Knit Repository Format 1',
 
598
                         self.get_transport().get_bytes(
 
599
                            'dir/.bzr/repository/format'))
 
600
 
 
601
    def test_safety_net(self):
 
602
        """No test should modify the safety .bzr directory.
 
603
 
 
604
        We just test that the _check_safety_net private method raises
 
605
        AssertionError, it's easier than building a test suite with the same
 
606
        test.
 
607
        """
 
608
        # Oops, a commit in the current directory (i.e. without local .bzr
 
609
        # directory) will crawl up the hierarchy to find a .bzr directory.
 
610
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
 
611
        # But we have a safety net in place.
 
612
        self.assertRaises(AssertionError, self._check_safety_net)
 
613
 
488
614
 
489
615
class TestTestCaseWithTransport(TestCaseWithTransport):
490
616
    """Tests for the convenience functions TestCaseWithTransport introduces."""
493
619
        from bzrlib.transport import get_transport
494
620
        from bzrlib.transport.memory import MemoryServer
495
621
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
496
 
        self.transport_server = MemoryServer
 
622
        self.vfs_transport_factory = MemoryServer
497
623
        self.transport_readonly_server = None
498
624
        # calling get_readonly_transport() constructs a decorator on the url
499
625
        # for the server
506
632
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
507
633
 
508
634
    def test_get_readonly_url_http(self):
509
 
        from bzrlib.tests.HttpServer import HttpServer
 
635
        from bzrlib.tests.http_server import HttpServer
510
636
        from bzrlib.transport import get_transport
511
637
        from bzrlib.transport.local import LocalURLServer
512
638
        from bzrlib.transport.http import HttpTransportBase
530
656
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
531
657
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
532
658
 
 
659
    def test_make_branch_builder(self):
 
660
        builder = self.make_branch_builder('dir')
 
661
        rev_id = builder.build_commit()
 
662
        self.failUnlessExists('dir')
 
663
        a_dir = bzrdir.BzrDir.open('dir')
 
664
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
 
665
        a_branch = a_dir.open_branch()
 
666
        builder_branch = builder.get_branch()
 
667
        self.assertEqual(a_branch.base, builder_branch.base)
 
668
        self.assertEqual((1, rev_id), builder_branch.last_revision_info())
 
669
        self.assertEqual((1, rev_id), a_branch.last_revision_info())
 
670
 
533
671
 
534
672
class TestTestCaseTransports(TestCaseWithTransport):
535
673
 
536
674
    def setUp(self):
537
675
        super(TestTestCaseTransports, self).setUp()
538
 
        self.transport_server = MemoryServer
 
676
        self.vfs_transport_factory = MemoryServer
539
677
 
540
678
    def test_make_bzrdir_preserves_transport(self):
541
679
        t = self.get_transport()
580
718
 
581
719
class TestTestResult(TestCase):
582
720
 
583
 
    def test_elapsed_time_with_benchmarking(self):
 
721
    def check_timing(self, test_case, expected_re):
584
722
        result = bzrlib.tests.TextTestResult(self._log_file,
585
 
                                        descriptions=0,
586
 
                                        verbosity=1,
587
 
                                        )
588
 
        result._recordTestStartTime()
589
 
        time.sleep(0.003)
590
 
        result.extractBenchmarkTime(self)
591
 
        timed_string = result._testTimeString()
592
 
        # without explicit benchmarking, we should get a simple time.
593
 
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
 
723
                descriptions=0,
 
724
                verbosity=1,
 
725
                )
 
726
        test_case.run(result)
 
727
        timed_string = result._testTimeString(test_case)
 
728
        self.assertContainsRe(timed_string, expected_re)
 
729
 
 
730
    def test_test_reporting(self):
 
731
        class ShortDelayTestCase(TestCase):
 
732
            def test_short_delay(self):
 
733
                time.sleep(0.003)
 
734
            def test_short_benchmark(self):
 
735
                self.time(time.sleep, 0.003)
 
736
        self.check_timing(ShortDelayTestCase('test_short_delay'),
 
737
                          r"^ +[0-9]+ms$")
594
738
        # if a benchmark time is given, we want a x of y style result.
595
 
        self.time(time.sleep, 0.001)
596
 
        result.extractBenchmarkTime(self)
597
 
        timed_string = result._testTimeString()
598
 
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
599
 
        # extracting the time from a non-bzrlib testcase sets to None
600
 
        result._recordTestStartTime()
601
 
        result.extractBenchmarkTime(
602
 
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
603
 
        timed_string = result._testTimeString()
604
 
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
605
 
        # cheat. Yes, wash thy mouth out with soap.
606
 
        self._benchtime = None
 
739
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
 
740
                          r"^ +[0-9]+ms/ +[0-9]+ms$")
607
741
 
 
742
    def test_unittest_reporting_unittest_class(self):
 
743
        # getting the time from a non-bzrlib test works ok
 
744
        class ShortDelayTestCase(unittest.TestCase):
 
745
            def test_short_delay(self):
 
746
                time.sleep(0.003)
 
747
        self.check_timing(ShortDelayTestCase('test_short_delay'),
 
748
                          r"^ +[0-9]+ms$")
 
749
        
608
750
    def test_assigned_benchmark_file_stores_date(self):
609
751
        output = StringIO()
610
752
        result = bzrlib.tests.TextTestResult(self._log_file,
613
755
                                        bench_history=output
614
756
                                        )
615
757
        output_string = output.getvalue()
616
 
        
617
758
        # if you are wondering about the regexp please read the comment in
618
759
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
619
760
        # XXX: what comment?  -- Andrew Bennetts
649
790
 
650
791
    def test_lsprofiling(self):
651
792
        """Verbose test result prints lsprof statistics from test cases."""
652
 
        try:
653
 
            import bzrlib.lsprof
654
 
        except ImportError:
655
 
            raise TestSkipped("lsprof not installed.")
 
793
        self.requireFeature(test_lsprof.LSProfFeature)
656
794
        result_stream = StringIO()
657
795
        result = bzrlib.tests.VerboseTestResult(
658
796
            unittest._WritelnDecorator(result_stream),
687
825
        self.assertContainsRe(output,
688
826
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
689
827
 
 
828
    def test_known_failure(self):
 
829
        """A KnownFailure being raised should trigger several result actions."""
 
830
        class InstrumentedTestResult(ExtendedTestResult):
 
831
 
 
832
            def report_test_start(self, test): pass
 
833
            def report_known_failure(self, test, err):
 
834
                self._call = test, err
 
835
        result = InstrumentedTestResult(None, None, None, None)
 
836
        def test_function():
 
837
            raise KnownFailure('failed!')
 
838
        test = unittest.FunctionTestCase(test_function)
 
839
        test.run(result)
 
840
        # it should invoke 'report_known_failure'.
 
841
        self.assertEqual(2, len(result._call))
 
842
        self.assertEqual(test, result._call[0])
 
843
        self.assertEqual(KnownFailure, result._call[1][0])
 
844
        self.assertIsInstance(result._call[1][1], KnownFailure)
 
845
        # we dont introspec the traceback, if the rest is ok, it would be
 
846
        # exceptional for it not to be.
 
847
        # it should update the known_failure_count on the object.
 
848
        self.assertEqual(1, result.known_failure_count)
 
849
        # the result should be successful.
 
850
        self.assertTrue(result.wasSuccessful())
 
851
 
 
852
    def test_verbose_report_known_failure(self):
 
853
        # verbose test output formatting
 
854
        result_stream = StringIO()
 
855
        result = bzrlib.tests.VerboseTestResult(
 
856
            unittest._WritelnDecorator(result_stream),
 
857
            descriptions=0,
 
858
            verbosity=2,
 
859
            )
 
860
        test = self.get_passing_test()
 
861
        result.startTest(test)
 
862
        prefix = len(result_stream.getvalue())
 
863
        # the err parameter has the shape:
 
864
        # (class, exception object, traceback)
 
865
        # KnownFailures dont get their tracebacks shown though, so we
 
866
        # can skip that.
 
867
        err = (KnownFailure, KnownFailure('foo'), None)
 
868
        result.report_known_failure(test, err)
 
869
        output = result_stream.getvalue()[prefix:]
 
870
        lines = output.splitlines()
 
871
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
 
872
        self.assertEqual(lines[1], '    foo')
 
873
        self.assertEqual(2, len(lines))
 
874
 
 
875
    def test_text_report_known_failure(self):
 
876
        # text test output formatting
 
877
        pb = MockProgress()
 
878
        result = bzrlib.tests.TextTestResult(
 
879
            None,
 
880
            descriptions=0,
 
881
            verbosity=1,
 
882
            pb=pb,
 
883
            )
 
884
        test = self.get_passing_test()
 
885
        # this seeds the state to handle reporting the test.
 
886
        result.startTest(test)
 
887
        # the err parameter has the shape:
 
888
        # (class, exception object, traceback)
 
889
        # KnownFailures dont get their tracebacks shown though, so we
 
890
        # can skip that.
 
891
        err = (KnownFailure, KnownFailure('foo'), None)
 
892
        result.report_known_failure(test, err)
 
893
        self.assertEqual(
 
894
            [
 
895
            ('update', '[1 in 0s] passing_test', None, None),
 
896
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
 
897
            ],
 
898
            pb.calls)
 
899
        # known_failures should be printed in the summary, so if we run a test
 
900
        # after there are some known failures, the update prefix should match
 
901
        # this.
 
902
        result.known_failure_count = 3
 
903
        test.run(result)
 
904
        self.assertEqual(
 
905
            [
 
906
            ('update', '[2 in 0s] passing_test', None, None),
 
907
            ],
 
908
            pb.calls[2:])
 
909
 
 
910
    def get_passing_test(self):
 
911
        """Return a test object that can't be run usefully."""
 
912
        def passing_test():
 
913
            pass
 
914
        return unittest.FunctionTestCase(passing_test)
 
915
 
 
916
    def test_add_not_supported(self):
 
917
        """Test the behaviour of invoking addNotSupported."""
 
918
        class InstrumentedTestResult(ExtendedTestResult):
 
919
            def report_test_start(self, test): pass
 
920
            def report_unsupported(self, test, feature):
 
921
                self._call = test, feature
 
922
        result = InstrumentedTestResult(None, None, None, None)
 
923
        test = SampleTestCase('_test_pass')
 
924
        feature = Feature()
 
925
        result.startTest(test)
 
926
        result.addNotSupported(test, feature)
 
927
        # it should invoke 'report_unsupported'.
 
928
        self.assertEqual(2, len(result._call))
 
929
        self.assertEqual(test, result._call[0])
 
930
        self.assertEqual(feature, result._call[1])
 
931
        # the result should be successful.
 
932
        self.assertTrue(result.wasSuccessful())
 
933
        # it should record the test against a count of tests not run due to
 
934
        # this feature.
 
935
        self.assertEqual(1, result.unsupported['Feature'])
 
936
        # and invoking it again should increment that counter
 
937
        result.addNotSupported(test, feature)
 
938
        self.assertEqual(2, result.unsupported['Feature'])
 
939
 
 
940
    def test_verbose_report_unsupported(self):
 
941
        # verbose test output formatting
 
942
        result_stream = StringIO()
 
943
        result = bzrlib.tests.VerboseTestResult(
 
944
            unittest._WritelnDecorator(result_stream),
 
945
            descriptions=0,
 
946
            verbosity=2,
 
947
            )
 
948
        test = self.get_passing_test()
 
949
        feature = Feature()
 
950
        result.startTest(test)
 
951
        prefix = len(result_stream.getvalue())
 
952
        result.report_unsupported(test, feature)
 
953
        output = result_stream.getvalue()[prefix:]
 
954
        lines = output.splitlines()
 
955
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
 
956
    
 
957
    def test_text_report_unsupported(self):
 
958
        # text test output formatting
 
959
        pb = MockProgress()
 
960
        result = bzrlib.tests.TextTestResult(
 
961
            None,
 
962
            descriptions=0,
 
963
            verbosity=1,
 
964
            pb=pb,
 
965
            )
 
966
        test = self.get_passing_test()
 
967
        feature = Feature()
 
968
        # this seeds the state to handle reporting the test.
 
969
        result.startTest(test)
 
970
        result.report_unsupported(test, feature)
 
971
        # no output on unsupported features
 
972
        self.assertEqual(
 
973
            [('update', '[1 in 0s] passing_test', None, None)
 
974
            ],
 
975
            pb.calls)
 
976
        # the number of missing features should be printed in the progress
 
977
        # summary, so check for that.
 
978
        result.unsupported = {'foo':0, 'bar':0}
 
979
        test.run(result)
 
980
        self.assertEqual(
 
981
            [
 
982
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
 
983
            ],
 
984
            pb.calls[1:])
 
985
    
 
986
    def test_unavailable_exception(self):
 
987
        """An UnavailableFeature being raised should invoke addNotSupported."""
 
988
        class InstrumentedTestResult(ExtendedTestResult):
 
989
 
 
990
            def report_test_start(self, test): pass
 
991
            def addNotSupported(self, test, feature):
 
992
                self._call = test, feature
 
993
        result = InstrumentedTestResult(None, None, None, None)
 
994
        feature = Feature()
 
995
        def test_function():
 
996
            raise UnavailableFeature(feature)
 
997
        test = unittest.FunctionTestCase(test_function)
 
998
        test.run(result)
 
999
        # it should invoke 'addNotSupported'.
 
1000
        self.assertEqual(2, len(result._call))
 
1001
        self.assertEqual(test, result._call[0])
 
1002
        self.assertEqual(feature, result._call[1])
 
1003
        # and not count as an error
 
1004
        self.assertEqual(0, result.error_count)
 
1005
 
 
1006
    def test_strict_with_unsupported_feature(self):
 
1007
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
 
1008
                                             verbosity=1)
 
1009
        test = self.get_passing_test()
 
1010
        feature = "Unsupported Feature"
 
1011
        result.addNotSupported(test, feature)
 
1012
        self.assertFalse(result.wasStrictlySuccessful())
 
1013
        self.assertEqual(None, result._extractBenchmarkTime(test))
 
1014
 
 
1015
    def test_strict_with_known_failure(self):
 
1016
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
 
1017
                                             verbosity=1)
 
1018
        test = self.get_passing_test()
 
1019
        err = (KnownFailure, KnownFailure('foo'), None)
 
1020
        result._addKnownFailure(test, err)
 
1021
        self.assertFalse(result.wasStrictlySuccessful())
 
1022
        self.assertEqual(None, result._extractBenchmarkTime(test))
 
1023
 
 
1024
    def test_strict_with_success(self):
 
1025
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
 
1026
                                             verbosity=1)
 
1027
        test = self.get_passing_test()
 
1028
        result.addSuccess(test)
 
1029
        self.assertTrue(result.wasStrictlySuccessful())
 
1030
        self.assertEqual(None, result._extractBenchmarkTime(test))
 
1031
 
 
1032
 
 
1033
class TestUnicodeFilenameFeature(TestCase):
 
1034
 
 
1035
    def test_probe_passes(self):
 
1036
        """UnicodeFilenameFeature._probe passes."""
 
1037
        # We can't test much more than that because the behaviour depends
 
1038
        # on the platform.
 
1039
        tests.UnicodeFilenameFeature._probe()
 
1040
 
690
1041
 
691
1042
class TestRunner(TestCase):
692
1043
 
709
1060
        finally:
710
1061
            TestCaseInTempDir.TEST_ROOT = old_root
711
1062
 
 
1063
    def test_known_failure_failed_run(self):
 
1064
        # run a test that generates a known failure which should be printed in
 
1065
        # the final output when real failures occur.
 
1066
        def known_failure_test():
 
1067
            raise KnownFailure('failed')
 
1068
        test = unittest.TestSuite()
 
1069
        test.addTest(unittest.FunctionTestCase(known_failure_test))
 
1070
        def failing_test():
 
1071
            raise AssertionError('foo')
 
1072
        test.addTest(unittest.FunctionTestCase(failing_test))
 
1073
        stream = StringIO()
 
1074
        runner = TextTestRunner(stream=stream)
 
1075
        result = self.run_test_runner(runner, test)
 
1076
        lines = stream.getvalue().splitlines()
 
1077
        self.assertEqual([
 
1078
            '',
 
1079
            '======================================================================',
 
1080
            'FAIL: unittest.FunctionTestCase (failing_test)',
 
1081
            '----------------------------------------------------------------------',
 
1082
            'Traceback (most recent call last):',
 
1083
            '    raise AssertionError(\'foo\')',
 
1084
            'AssertionError: foo',
 
1085
            '',
 
1086
            '----------------------------------------------------------------------',
 
1087
            '',
 
1088
            'FAILED (failures=1, known_failure_count=1)'],
 
1089
            lines[0:5] + lines[6:10] + lines[11:])
 
1090
 
 
1091
    def test_known_failure_ok_run(self):
 
1092
        # run a test that generates a known failure which should be printed in the final output.
 
1093
        def known_failure_test():
 
1094
            raise KnownFailure('failed')
 
1095
        test = unittest.FunctionTestCase(known_failure_test)
 
1096
        stream = StringIO()
 
1097
        runner = TextTestRunner(stream=stream)
 
1098
        result = self.run_test_runner(runner, test)
 
1099
        self.assertContainsRe(stream.getvalue(),
 
1100
            '\n'
 
1101
            '-*\n'
 
1102
            'Ran 1 test in .*\n'
 
1103
            '\n'
 
1104
            'OK \\(known_failures=1\\)\n')
 
1105
 
712
1106
    def test_skipped_test(self):
713
1107
        # run a test that is skipped, and check the suite as a whole still
714
1108
        # succeeds.
715
1109
        # skipping_test must be hidden in here so it's not run as a real test
716
1110
        def skipping_test():
717
1111
            raise TestSkipped('test intentionally skipped')
718
 
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
1112
 
 
1113
        runner = TextTestRunner(stream=self._log_file)
719
1114
        test = unittest.FunctionTestCase(skipping_test)
720
1115
        result = self.run_test_runner(runner, test)
721
1116
        self.assertTrue(result.wasSuccessful())
722
1117
 
 
1118
    def test_skipped_from_setup(self):
 
1119
        calls = []
 
1120
        class SkippedSetupTest(TestCase):
 
1121
 
 
1122
            def setUp(self):
 
1123
                calls.append('setUp')
 
1124
                self.addCleanup(self.cleanup)
 
1125
                raise TestSkipped('skipped setup')
 
1126
 
 
1127
            def test_skip(self):
 
1128
                self.fail('test reached')
 
1129
 
 
1130
            def cleanup(self):
 
1131
                calls.append('cleanup')
 
1132
 
 
1133
        runner = TextTestRunner(stream=self._log_file)
 
1134
        test = SkippedSetupTest('test_skip')
 
1135
        result = self.run_test_runner(runner, test)
 
1136
        self.assertTrue(result.wasSuccessful())
 
1137
        # Check if cleanup was called the right number of times.
 
1138
        self.assertEqual(['setUp', 'cleanup'], calls)
 
1139
 
 
1140
    def test_skipped_from_test(self):
 
1141
        calls = []
 
1142
        class SkippedTest(TestCase):
 
1143
 
 
1144
            def setUp(self):
 
1145
                calls.append('setUp')
 
1146
                self.addCleanup(self.cleanup)
 
1147
 
 
1148
            def test_skip(self):
 
1149
                raise TestSkipped('skipped test')
 
1150
 
 
1151
            def cleanup(self):
 
1152
                calls.append('cleanup')
 
1153
 
 
1154
        runner = TextTestRunner(stream=self._log_file)
 
1155
        test = SkippedTest('test_skip')
 
1156
        result = self.run_test_runner(runner, test)
 
1157
        self.assertTrue(result.wasSuccessful())
 
1158
        # Check if cleanup was called the right number of times.
 
1159
        self.assertEqual(['setUp', 'cleanup'], calls)
 
1160
 
 
1161
    def test_not_applicable(self):
 
1162
        # run a test that is skipped because it's not applicable
 
1163
        def not_applicable_test():
 
1164
            from bzrlib.tests import TestNotApplicable
 
1165
            raise TestNotApplicable('this test never runs')
 
1166
        out = StringIO()
 
1167
        runner = TextTestRunner(stream=out, verbosity=2)
 
1168
        test = unittest.FunctionTestCase(not_applicable_test)
 
1169
        result = self.run_test_runner(runner, test)
 
1170
        self._log_file.write(out.getvalue())
 
1171
        self.assertTrue(result.wasSuccessful())
 
1172
        self.assertTrue(result.wasStrictlySuccessful())
 
1173
        self.assertContainsRe(out.getvalue(),
 
1174
                r'(?m)not_applicable_test   * N/A')
 
1175
        self.assertContainsRe(out.getvalue(),
 
1176
                r'(?m)^    this test never runs')
 
1177
 
 
1178
    def test_not_applicable_demo(self):
 
1179
        # just so you can see it in the test output
 
1180
        raise TestNotApplicable('this test is just a demonstation')
 
1181
 
 
1182
    def test_unsupported_features_listed(self):
 
1183
        """When unsupported features are encountered they are detailed."""
 
1184
        class Feature1(Feature):
 
1185
            def _probe(self): return False
 
1186
        class Feature2(Feature):
 
1187
            def _probe(self): return False
 
1188
        # create sample tests
 
1189
        test1 = SampleTestCase('_test_pass')
 
1190
        test1._test_needs_features = [Feature1()]
 
1191
        test2 = SampleTestCase('_test_pass')
 
1192
        test2._test_needs_features = [Feature2()]
 
1193
        test = unittest.TestSuite()
 
1194
        test.addTest(test1)
 
1195
        test.addTest(test2)
 
1196
        stream = StringIO()
 
1197
        runner = TextTestRunner(stream=stream)
 
1198
        result = self.run_test_runner(runner, test)
 
1199
        lines = stream.getvalue().splitlines()
 
1200
        self.assertEqual([
 
1201
            'OK',
 
1202
            "Missing feature 'Feature1' skipped 1 tests.",
 
1203
            "Missing feature 'Feature2' skipped 1 tests.",
 
1204
            ],
 
1205
            lines[-3:])
 
1206
 
723
1207
    def test_bench_history(self):
724
1208
        # tests that the running the benchmark produces a history file
725
1209
        # containing a timestamp and the revision id of the bzrlib source which
735
1219
            revision_id = workingtree.get_parent_ids()[0]
736
1220
            self.assertEndsWith(output_string.rstrip(), revision_id)
737
1221
 
 
1222
    def assertLogDeleted(self, test):
 
1223
        log = test._get_log()
 
1224
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
1225
        self.assertEqual('', test._log_contents)
 
1226
        self.assertIs(None, test._log_file_name)
 
1227
 
738
1228
    def test_success_log_deleted(self):
739
1229
        """Successful tests have their log deleted"""
740
1230
 
748
1238
        test = LogTester('test_success')
749
1239
        result = self.run_test_runner(runner, test)
750
1240
 
751
 
        log = test._get_log()
752
 
        self.assertEqual("DELETED log file to reduce memory footprint", log)
753
 
        self.assertEqual('', test._log_contents)
754
 
        self.assertIs(None, test._log_file_name)
 
1241
        self.assertLogDeleted(test)
 
1242
 
 
1243
    def test_skipped_log_deleted(self):
 
1244
        """Skipped tests have their log deleted"""
 
1245
 
 
1246
        class LogTester(TestCase):
 
1247
 
 
1248
            def test_skipped(self):
 
1249
                self.log('this will be removed\n')
 
1250
                raise tests.TestSkipped()
 
1251
 
 
1252
        sio = cStringIO.StringIO()
 
1253
        runner = TextTestRunner(stream=sio)
 
1254
        test = LogTester('test_skipped')
 
1255
        result = self.run_test_runner(runner, test)
 
1256
 
 
1257
        self.assertLogDeleted(test)
 
1258
 
 
1259
    def test_not_aplicable_log_deleted(self):
 
1260
        """Not applicable tests have their log deleted"""
 
1261
 
 
1262
        class LogTester(TestCase):
 
1263
 
 
1264
            def test_not_applicable(self):
 
1265
                self.log('this will be removed\n')
 
1266
                raise tests.TestNotApplicable()
 
1267
 
 
1268
        sio = cStringIO.StringIO()
 
1269
        runner = TextTestRunner(stream=sio)
 
1270
        test = LogTester('test_not_applicable')
 
1271
        result = self.run_test_runner(runner, test)
 
1272
 
 
1273
        self.assertLogDeleted(test)
 
1274
 
 
1275
    def test_known_failure_log_deleted(self):
 
1276
        """Know failure tests have their log deleted"""
 
1277
 
 
1278
        class LogTester(TestCase):
 
1279
 
 
1280
            def test_known_failure(self):
 
1281
                self.log('this will be removed\n')
 
1282
                raise tests.KnownFailure()
 
1283
 
 
1284
        sio = cStringIO.StringIO()
 
1285
        runner = TextTestRunner(stream=sio)
 
1286
        test = LogTester('test_known_failure')
 
1287
        result = self.run_test_runner(runner, test)
 
1288
 
 
1289
        self.assertLogDeleted(test)
755
1290
 
756
1291
    def test_fail_log_kept(self):
757
1292
        """Failed tests have their log kept"""
798
1333
        self.assertEqual(log, test._log_contents)
799
1334
 
800
1335
 
 
1336
class SampleTestCase(TestCase):
 
1337
 
 
1338
    def _test_pass(self):
 
1339
        pass
 
1340
 
 
1341
class _TestException(Exception):
 
1342
    pass
 
1343
 
801
1344
class TestTestCase(TestCase):
802
1345
    """Tests that test the core bzrlib TestCase."""
803
1346
 
 
1347
    def test_debug_flags_sanitised(self):
 
1348
        """The bzrlib debug flags should be sanitised by setUp."""
 
1349
        if 'allow_debug' in tests.selftest_debug_flags:
 
1350
            raise TestNotApplicable(
 
1351
                '-Eallow_debug option prevents debug flag sanitisation')
 
1352
        # we could set something and run a test that will check
 
1353
        # it gets santised, but this is probably sufficient for now:
 
1354
        # if someone runs the test with -Dsomething it will error.
 
1355
        self.assertEqual(set(), bzrlib.debug.debug_flags)
 
1356
 
 
1357
    def change_selftest_debug_flags(self, new_flags):
 
1358
        orig_selftest_flags = tests.selftest_debug_flags
 
1359
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
 
1360
        tests.selftest_debug_flags = set(new_flags)
 
1361
        
 
1362
    def _restore_selftest_debug_flags(self, flags):
 
1363
        tests.selftest_debug_flags = flags
 
1364
 
 
1365
    def test_allow_debug_flag(self):
 
1366
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
 
1367
        sanitised (i.e. cleared) before running a test.
 
1368
        """
 
1369
        self.change_selftest_debug_flags(set(['allow_debug']))
 
1370
        bzrlib.debug.debug_flags = set(['a-flag'])
 
1371
        class TestThatRecordsFlags(TestCase):
 
1372
            def test_foo(nested_self):
 
1373
                self.flags = set(bzrlib.debug.debug_flags)
 
1374
        test = TestThatRecordsFlags('test_foo')
 
1375
        test.run(self.make_test_result())
 
1376
        self.assertEqual(set(['a-flag']), self.flags)
 
1377
 
 
1378
    def test_debug_flags_restored(self):
 
1379
        """The bzrlib debug flags should be restored to their original state
 
1380
        after the test was run, even if allow_debug is set.
 
1381
        """
 
1382
        self.change_selftest_debug_flags(set(['allow_debug']))
 
1383
        # Now run a test that modifies debug.debug_flags.
 
1384
        bzrlib.debug.debug_flags = set(['original-state'])
 
1385
        class TestThatModifiesFlags(TestCase):
 
1386
            def test_foo(self):
 
1387
                bzrlib.debug.debug_flags = set(['modified'])
 
1388
        test = TestThatModifiesFlags('test_foo')
 
1389
        test.run(self.make_test_result())
 
1390
        self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)
 
1391
 
 
1392
    def make_test_result(self):
 
1393
        return bzrlib.tests.TextTestResult(
 
1394
            self._log_file, descriptions=0, verbosity=1)
 
1395
 
804
1396
    def inner_test(self):
805
1397
        # the inner child test
806
1398
        note("inner_test")
809
1401
        # the outer child test
810
1402
        note("outer_start")
811
1403
        self.inner_test = TestTestCase("inner_child")
812
 
        result = bzrlib.tests.TextTestResult(self._log_file,
813
 
                                        descriptions=0,
814
 
                                        verbosity=1)
 
1404
        result = self.make_test_result()
815
1405
        self.inner_test.run(result)
816
1406
        note("outer finish")
817
1407
 
829
1419
        # the outer child test
830
1420
        original_trace = bzrlib.trace._trace_file
831
1421
        outer_test = TestTestCase("outer_child")
832
 
        result = bzrlib.tests.TextTestResult(self._log_file,
833
 
                                        descriptions=0,
834
 
                                        verbosity=1)
 
1422
        result = self.make_test_result()
835
1423
        outer_test.run(result)
836
1424
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
837
1425
 
858
1446
        """The bzrlib hooks should be sanitised by setUp."""
859
1447
        self.assertEqual(bzrlib.branch.BranchHooks(),
860
1448
            bzrlib.branch.Branch.hooks)
 
1449
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
 
1450
            bzrlib.smart.server.SmartTCPServer.hooks)
861
1451
 
862
1452
    def test__gather_lsprof_in_benchmarks(self):
863
1453
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
864
1454
        
865
1455
        Each self.time() call is individually and separately profiled.
866
1456
        """
867
 
        try:
868
 
            import bzrlib.lsprof
869
 
        except ImportError:
870
 
            raise TestSkipped("lsprof not installed.")
 
1457
        self.requireFeature(test_lsprof.LSProfFeature)
871
1458
        # overrides the class member with an instance member so no cleanup 
872
1459
        # needed.
873
1460
        self._gather_lsprof_in_benchmarks = True
879
1466
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
880
1467
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
881
1468
 
 
1469
    def test_knownFailure(self):
 
1470
        """Self.knownFailure() should raise a KnownFailure exception."""
 
1471
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
 
1472
 
 
1473
    def test_requireFeature_available(self):
 
1474
        """self.requireFeature(available) is a no-op."""
 
1475
        class Available(Feature):
 
1476
            def _probe(self):return True
 
1477
        feature = Available()
 
1478
        self.requireFeature(feature)
 
1479
 
 
1480
    def test_requireFeature_unavailable(self):
 
1481
        """self.requireFeature(unavailable) raises UnavailableFeature."""
 
1482
        class Unavailable(Feature):
 
1483
            def _probe(self):return False
 
1484
        feature = Unavailable()
 
1485
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
 
1486
 
 
1487
    def test_run_no_parameters(self):
 
1488
        test = SampleTestCase('_test_pass')
 
1489
        test.run()
 
1490
    
 
1491
    def test_run_enabled_unittest_result(self):
 
1492
        """Test we revert to regular behaviour when the test is enabled."""
 
1493
        test = SampleTestCase('_test_pass')
 
1494
        class EnabledFeature(object):
 
1495
            def available(self):
 
1496
                return True
 
1497
        test._test_needs_features = [EnabledFeature()]
 
1498
        result = unittest.TestResult()
 
1499
        test.run(result)
 
1500
        self.assertEqual(1, result.testsRun)
 
1501
        self.assertEqual([], result.errors)
 
1502
        self.assertEqual([], result.failures)
 
1503
 
 
1504
    def test_run_disabled_unittest_result(self):
 
1505
        """Test our compatability for disabled tests with unittest results."""
 
1506
        test = SampleTestCase('_test_pass')
 
1507
        class DisabledFeature(object):
 
1508
            def available(self):
 
1509
                return False
 
1510
        test._test_needs_features = [DisabledFeature()]
 
1511
        result = unittest.TestResult()
 
1512
        test.run(result)
 
1513
        self.assertEqual(1, result.testsRun)
 
1514
        self.assertEqual([], result.errors)
 
1515
        self.assertEqual([], result.failures)
 
1516
 
 
1517
    def test_run_disabled_supporting_result(self):
 
1518
        """Test disabled tests behaviour with support aware results."""
 
1519
        test = SampleTestCase('_test_pass')
 
1520
        class DisabledFeature(object):
 
1521
            def available(self):
 
1522
                return False
 
1523
        the_feature = DisabledFeature()
 
1524
        test._test_needs_features = [the_feature]
 
1525
        class InstrumentedTestResult(unittest.TestResult):
 
1526
            def __init__(self):
 
1527
                unittest.TestResult.__init__(self)
 
1528
                self.calls = []
 
1529
            def startTest(self, test):
 
1530
                self.calls.append(('startTest', test))
 
1531
            def stopTest(self, test):
 
1532
                self.calls.append(('stopTest', test))
 
1533
            def addNotSupported(self, test, feature):
 
1534
                self.calls.append(('addNotSupported', test, feature))
 
1535
        result = InstrumentedTestResult()
 
1536
        test.run(result)
 
1537
        self.assertEqual([
 
1538
            ('startTest', test),
 
1539
            ('addNotSupported', test, the_feature),
 
1540
            ('stopTest', test),
 
1541
            ],
 
1542
            result.calls)
 
1543
 
 
1544
    def test_assert_list_raises_on_generator(self):
 
1545
        def generator_which_will_raise():
 
1546
            # This will not raise until after the first yield
 
1547
            yield 1
 
1548
            raise _TestException()
 
1549
 
 
1550
        e = self.assertListRaises(_TestException, generator_which_will_raise)
 
1551
        self.assertIsInstance(e, _TestException)
 
1552
 
 
1553
        e = self.assertListRaises(Exception, generator_which_will_raise)
 
1554
        self.assertIsInstance(e, _TestException)
 
1555
 
 
1556
    def test_assert_list_raises_on_plain(self):
 
1557
        def plain_exception():
 
1558
            raise _TestException()
 
1559
            return []
 
1560
 
 
1561
        e = self.assertListRaises(_TestException, plain_exception)
 
1562
        self.assertIsInstance(e, _TestException)
 
1563
 
 
1564
        e = self.assertListRaises(Exception, plain_exception)
 
1565
        self.assertIsInstance(e, _TestException)
 
1566
 
 
1567
    def test_assert_list_raises_assert_wrong_exception(self):
 
1568
        class _NotTestException(Exception):
 
1569
            pass
 
1570
 
 
1571
        def wrong_exception():
 
1572
            raise _NotTestException()
 
1573
 
 
1574
        def wrong_exception_generator():
 
1575
            yield 1
 
1576
            yield 2
 
1577
            raise _NotTestException()
 
1578
 
 
1579
        # Wrong exceptions are not intercepted
 
1580
        self.assertRaises(_NotTestException,
 
1581
            self.assertListRaises, _TestException, wrong_exception)
 
1582
        self.assertRaises(_NotTestException,
 
1583
            self.assertListRaises, _TestException, wrong_exception_generator)
 
1584
 
 
1585
    def test_assert_list_raises_no_exception(self):
 
1586
        def success():
 
1587
            return []
 
1588
 
 
1589
        def success_generator():
 
1590
            yield 1
 
1591
            yield 2
 
1592
 
 
1593
        self.assertRaises(AssertionError,
 
1594
            self.assertListRaises, _TestException, success)
 
1595
 
 
1596
        self.assertRaises(AssertionError,
 
1597
            self.assertListRaises, _TestException, success_generator)
 
1598
 
882
1599
 
883
1600
@symbol_versioning.deprecated_function(zero_eleven)
884
1601
def sample_deprecated_function():
962
1679
        self.callDeprecated([], testfunc, be_deprecated=False)
963
1680
 
964
1681
 
 
1682
class TestWarningTests(TestCase):
 
1683
    """Tests for calling methods that raise warnings."""
 
1684
 
 
1685
    def test_callCatchWarnings(self):
 
1686
        def meth(a, b):
 
1687
            warnings.warn("this is your last warning")
 
1688
            return a + b
 
1689
        wlist, result = self.callCatchWarnings(meth, 1, 2)
 
1690
        self.assertEquals(3, result)
 
1691
        # would like just to compare them, but UserWarning doesn't implement
 
1692
        # eq well
 
1693
        w0, = wlist
 
1694
        self.assertIsInstance(w0, UserWarning)
 
1695
        self.assertEquals("this is your last warning", str(w0))
 
1696
 
 
1697
 
965
1698
class TestConvenienceMakers(TestCaseWithTransport):
966
1699
    """Test for the make_* convenience functions."""
967
1700
 
1011
1744
        self.assertEqual([True], factory_called)
1012
1745
 
1013
1746
 
1014
 
class TestSelftestCleanOutput(TestCaseInTempDir):
1015
 
 
1016
 
    def test_clean_output(self):
1017
 
        # test functionality of clean_selftest_output()
1018
 
        from bzrlib.tests import clean_selftest_output
1019
 
 
1020
 
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
1021
 
        files = ('bzr', 'setup.py', 'test9999.tmp')
1022
 
        for i in dirs:
1023
 
            os.mkdir(i)
1024
 
        for i in files:
1025
 
            f = file(i, 'wb')
1026
 
            f.write('content of ')
1027
 
            f.write(i)
1028
 
            f.close()
1029
 
 
1030
 
        root = os.getcwdu()
1031
 
        before = os.listdir(root)
1032
 
        before.sort()
1033
 
        self.assertEquals(['bzr','bzrlib','setup.py',
1034
 
                           'test0000.tmp','test0001.tmp',
1035
 
                           'test9999.tmp','tests'],
1036
 
                           before)
1037
 
        clean_selftest_output(root, quiet=True)
1038
 
        after = os.listdir(root)
1039
 
        after.sort()
1040
 
        self.assertEquals(['bzr','bzrlib','setup.py',
1041
 
                           'test9999.tmp','tests'],
1042
 
                           after)
 
1747
class TestKnownFailure(TestCase):
 
1748
 
 
1749
    def test_known_failure(self):
 
1750
        """Check that KnownFailure is defined appropriately."""
 
1751
        # a KnownFailure is an assertion error for compatability with unaware
 
1752
        # runners.
 
1753
        self.assertIsInstance(KnownFailure(""), AssertionError)
 
1754
 
 
1755
    def test_expect_failure(self):
 
1756
        try:
 
1757
            self.expectFailure("Doomed to failure", self.assertTrue, False)
 
1758
        except KnownFailure, e:
 
1759
            self.assertEqual('Doomed to failure', e.args[0])
 
1760
        try:
 
1761
            self.expectFailure("Doomed to failure", self.assertTrue, True)
 
1762
        except AssertionError, e:
 
1763
            self.assertEqual('Unexpected success.  Should have failed:'
 
1764
                             ' Doomed to failure', e.args[0])
 
1765
        else:
 
1766
            self.fail('Assertion not raised')
 
1767
 
 
1768
 
 
1769
class TestFeature(TestCase):
 
1770
 
 
1771
    def test_caching(self):
 
1772
        """Feature._probe is called by the feature at most once."""
 
1773
        class InstrumentedFeature(Feature):
 
1774
            def __init__(self):
 
1775
                Feature.__init__(self)
 
1776
                self.calls = []
 
1777
            def _probe(self):
 
1778
                self.calls.append('_probe')
 
1779
                return False
 
1780
        feature = InstrumentedFeature()
 
1781
        feature.available()
 
1782
        self.assertEqual(['_probe'], feature.calls)
 
1783
        feature.available()
 
1784
        self.assertEqual(['_probe'], feature.calls)
 
1785
 
 
1786
    def test_named_str(self):
 
1787
        """Feature.__str__ should thunk to feature_name()."""
 
1788
        class NamedFeature(Feature):
 
1789
            def feature_name(self):
 
1790
                return 'symlinks'
 
1791
        feature = NamedFeature()
 
1792
        self.assertEqual('symlinks', str(feature))
 
1793
 
 
1794
    def test_default_str(self):
 
1795
        """Feature.__str__ should default to __class__.__name__."""
 
1796
        class NamedFeature(Feature):
 
1797
            pass
 
1798
        feature = NamedFeature()
 
1799
        self.assertEqual('NamedFeature', str(feature))
 
1800
 
 
1801
 
 
1802
class TestUnavailableFeature(TestCase):
 
1803
 
 
1804
    def test_access_feature(self):
 
1805
        feature = Feature()
 
1806
        exception = UnavailableFeature(feature)
 
1807
        self.assertIs(feature, exception.args[0])
 
1808
 
 
1809
 
 
1810
class TestSelftestFiltering(TestCase):
 
1811
 
 
1812
    def setUp(self):
 
1813
        self.suite = TestUtil.TestSuite()
 
1814
        self.loader = TestUtil.TestLoader()
 
1815
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
 
1816
            'bzrlib.tests.test_selftest']))
 
1817
        self.all_names = _test_ids(self.suite)
 
1818
 
 
1819
    def test_condition_id_re(self):
 
1820
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1821
            'test_condition_id_re')
 
1822
        filtered_suite = filter_suite_by_condition(self.suite,
 
1823
            condition_id_re('test_condition_id_re'))
 
1824
        self.assertEqual([test_name], _test_ids(filtered_suite))
 
1825
 
 
1826
    def test_condition_id_in_list(self):
 
1827
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1828
                      'test_condition_id_in_list']
 
1829
        id_list = tests.TestIdList(test_names)
 
1830
        filtered_suite = filter_suite_by_condition(
 
1831
            self.suite, tests.condition_id_in_list(id_list))
 
1832
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
 
1833
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
 
1834
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
 
1835
 
 
1836
    def test_condition_id_startswith(self):
 
1837
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1838
        start1 = klass + 'test_condition_id_starts'
 
1839
        start2 = klass + 'test_condition_id_in'
 
1840
        test_names = [ klass + 'test_condition_id_in_list',
 
1841
                      klass + 'test_condition_id_startswith',
 
1842
                     ]
 
1843
        filtered_suite = filter_suite_by_condition(
 
1844
            self.suite, tests.condition_id_startswith([start1, start2]))
 
1845
        self.assertEqual(test_names, _test_ids(filtered_suite))
 
1846
 
 
1847
    def test_condition_isinstance(self):
 
1848
        filtered_suite = filter_suite_by_condition(self.suite,
 
1849
            condition_isinstance(self.__class__))
 
1850
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1851
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
 
1852
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
 
1853
 
 
1854
    def test_exclude_tests_by_condition(self):
 
1855
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1856
            'test_exclude_tests_by_condition')
 
1857
        filtered_suite = exclude_tests_by_condition(self.suite,
 
1858
            lambda x:x.id() == excluded_name)
 
1859
        self.assertEqual(len(self.all_names) - 1,
 
1860
            filtered_suite.countTestCases())
 
1861
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
 
1862
        remaining_names = list(self.all_names)
 
1863
        remaining_names.remove(excluded_name)
 
1864
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
 
1865
 
 
1866
    def test_exclude_tests_by_re(self):
 
1867
        self.all_names = _test_ids(self.suite)
 
1868
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
 
1869
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1870
            'test_exclude_tests_by_re')
 
1871
        self.assertEqual(len(self.all_names) - 1,
 
1872
            filtered_suite.countTestCases())
 
1873
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
 
1874
        remaining_names = list(self.all_names)
 
1875
        remaining_names.remove(excluded_name)
 
1876
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
 
1877
 
 
1878
    def test_filter_suite_by_condition(self):
 
1879
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1880
            'test_filter_suite_by_condition')
 
1881
        filtered_suite = filter_suite_by_condition(self.suite,
 
1882
            lambda x:x.id() == test_name)
 
1883
        self.assertEqual([test_name], _test_ids(filtered_suite))
 
1884
 
 
1885
    def test_filter_suite_by_re(self):
 
1886
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1887
        filtered_names = _test_ids(filtered_suite)
 
1888
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
 
1889
            'TestSelftestFiltering.test_filter_suite_by_re'])
 
1890
 
 
1891
    def test_filter_suite_by_id_list(self):
 
1892
        test_list = ['bzrlib.tests.test_selftest.'
 
1893
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
 
1894
        filtered_suite = tests.filter_suite_by_id_list(
 
1895
            self.suite, tests.TestIdList(test_list))
 
1896
        filtered_names = _test_ids(filtered_suite)
 
1897
        self.assertEqual(
 
1898
            filtered_names,
 
1899
            ['bzrlib.tests.test_selftest.'
 
1900
             'TestSelftestFiltering.test_filter_suite_by_id_list'])
 
1901
 
 
1902
    def test_filter_suite_by_id_startswith(self):
 
1903
        # By design this test may fail if another test is added whose name also
 
1904
        # begins with one of the start value used.
 
1905
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1906
        start1 = klass + 'test_filter_suite_by_id_starts'
 
1907
        start2 = klass + 'test_filter_suite_by_id_li'
 
1908
        test_list = [klass + 'test_filter_suite_by_id_list',
 
1909
                     klass + 'test_filter_suite_by_id_startswith',
 
1910
                     ]
 
1911
        filtered_suite = tests.filter_suite_by_id_startswith(
 
1912
            self.suite, [start1, start2])
 
1913
        self.assertEqual(
 
1914
            test_list,
 
1915
            _test_ids(filtered_suite),
 
1916
            )
 
1917
 
 
1918
    def test_preserve_input(self):
 
1919
        # NB: Surely this is something in the stdlib to do this?
 
1920
        self.assertTrue(self.suite is preserve_input(self.suite))
 
1921
        self.assertTrue("@#$" is preserve_input("@#$"))
 
1922
 
 
1923
    def test_randomize_suite(self):
 
1924
        randomized_suite = randomize_suite(self.suite)
 
1925
        # randomizing should not add or remove test names.
 
1926
        self.assertEqual(set(_test_ids(self.suite)),
 
1927
                         set(_test_ids(randomized_suite)))
 
1928
        # Technically, this *can* fail, because random.shuffle(list) can be
 
1929
        # equal to list. Trying multiple times just pushes the frequency back.
 
1930
        # As its len(self.all_names)!:1, the failure frequency should be low
 
1931
        # enough to ignore. RBC 20071021.
 
1932
        # It should change the order.
 
1933
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
 
1934
        # But not the length. (Possibly redundant with the set test, but not
 
1935
        # necessarily.)
 
1936
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
 
1937
 
 
1938
    def test_split_suit_by_condition(self):
 
1939
        self.all_names = _test_ids(self.suite)
 
1940
        condition = condition_id_re('test_filter_suite_by_r')
 
1941
        split_suite = split_suite_by_condition(self.suite, condition)
 
1942
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1943
            'test_filter_suite_by_re')
 
1944
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
 
1945
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
 
1946
        remaining_names = list(self.all_names)
 
1947
        remaining_names.remove(filtered_name)
 
1948
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
 
1949
 
 
1950
    def test_split_suit_by_re(self):
 
1951
        self.all_names = _test_ids(self.suite)
 
1952
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1953
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1954
            'test_filter_suite_by_re')
 
1955
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
 
1956
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
 
1957
        remaining_names = list(self.all_names)
 
1958
        remaining_names.remove(filtered_name)
 
1959
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
 
1960
 
 
1961
 
 
1962
class TestCheckInventoryShape(TestCaseWithTransport):
 
1963
 
 
1964
    def test_check_inventory_shape(self):
 
1965
        files = ['a', 'b/', 'b/c']
 
1966
        tree = self.make_branch_and_tree('.')
 
1967
        self.build_tree(files)
 
1968
        tree.add(files)
 
1969
        tree.lock_read()
 
1970
        try:
 
1971
            self.check_inventory_shape(tree.inventory, files)
 
1972
        finally:
 
1973
            tree.unlock()
 
1974
 
 
1975
 
 
1976
class TestBlackboxSupport(TestCase):
 
1977
    """Tests for testsuite blackbox features."""
 
1978
 
 
1979
    def test_run_bzr_failure_not_caught(self):
 
1980
        # When we run bzr in blackbox mode, we want any unexpected errors to
 
1981
        # propagate up to the test suite so that it can show the error in the
 
1982
        # usual way, and we won't get a double traceback.
 
1983
        e = self.assertRaises(
 
1984
            AssertionError,
 
1985
            self.run_bzr, ['assert-fail'])
 
1986
        # make sure we got the real thing, not an error from somewhere else in
 
1987
        # the test framework
 
1988
        self.assertEquals('always fails', str(e))
 
1989
        # check that there's no traceback in the test log
 
1990
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
 
1991
            r'Traceback')
 
1992
 
 
1993
    def test_run_bzr_user_error_caught(self):
 
1994
        # Running bzr in blackbox mode, normal/expected/user errors should be
 
1995
        # caught in the regular way and turned into an error message plus exit
 
1996
        # code.
 
1997
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
 
1998
        self.assertEqual(out, '')
 
1999
        self.assertContainsRe(err,
 
2000
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
 
2001
 
 
2002
 
 
2003
class TestTestLoader(TestCase):
 
2004
    """Tests for the test loader."""
 
2005
 
 
2006
    def _get_loader_and_module(self):
 
2007
        """Gets a TestLoader and a module with one test in it."""
 
2008
        loader = TestUtil.TestLoader()
 
2009
        module = {}
 
2010
        class Stub(TestCase):
 
2011
            def test_foo(self):
 
2012
                pass
 
2013
        class MyModule(object):
 
2014
            pass
 
2015
        MyModule.a_class = Stub
 
2016
        module = MyModule()
 
2017
        return loader, module
 
2018
 
 
2019
    def test_module_no_load_tests_attribute_loads_classes(self):
 
2020
        loader, module = self._get_loader_and_module()
 
2021
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
 
2022
 
 
2023
    def test_module_load_tests_attribute_gets_called(self):
 
2024
        loader, module = self._get_loader_and_module()
 
2025
        # 'self' is here because we're faking the module with a class. Regular
 
2026
        # load_tests do not need that :)
 
2027
        def load_tests(self, standard_tests, module, loader):
 
2028
            result = loader.suiteClass()
 
2029
            for test in iter_suite_tests(standard_tests):
 
2030
                result.addTests([test, test])
 
2031
            return result
 
2032
        # add a load_tests() method which multiplies the tests from the module.
 
2033
        module.__class__.load_tests = load_tests
 
2034
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
 
2035
 
 
2036
    def test_load_tests_from_module_name_smoke_test(self):
 
2037
        loader = TestUtil.TestLoader()
 
2038
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2039
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
2040
                          _test_ids(suite))
 
2041
 
 
2042
    def test_load_tests_from_module_name_with_bogus_module_name(self):
 
2043
        loader = TestUtil.TestLoader()
 
2044
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
 
2045
 
 
2046
 
 
2047
class TestTestIdList(tests.TestCase):
 
2048
 
 
2049
    def _create_id_list(self, test_list):
 
2050
        return tests.TestIdList(test_list)
 
2051
 
 
2052
    def _create_suite(self, test_id_list):
 
2053
 
 
2054
        class Stub(TestCase):
 
2055
            def test_foo(self):
 
2056
                pass
 
2057
 
 
2058
        def _create_test_id(id):
 
2059
            return lambda: id
 
2060
 
 
2061
        suite = TestUtil.TestSuite()
 
2062
        for id in test_id_list:
 
2063
            t  = Stub('test_foo')
 
2064
            t.id = _create_test_id(id)
 
2065
            suite.addTest(t)
 
2066
        return suite
 
2067
 
 
2068
    def _test_ids(self, test_suite):
 
2069
        """Get the ids for the tests in a test suite."""
 
2070
        return [t.id() for t in iter_suite_tests(test_suite)]
 
2071
 
 
2072
    def test_empty_list(self):
 
2073
        id_list = self._create_id_list([])
 
2074
        self.assertEquals({}, id_list.tests)
 
2075
        self.assertEquals({}, id_list.modules)
 
2076
 
 
2077
    def test_valid_list(self):
 
2078
        id_list = self._create_id_list(
 
2079
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
 
2080
             'mod1.func1', 'mod1.cl2.meth2',
 
2081
             'mod1.submod1',
 
2082
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
 
2083
             ])
 
2084
        self.assertTrue(id_list.refers_to('mod1'))
 
2085
        self.assertTrue(id_list.refers_to('mod1.submod1'))
 
2086
        self.assertTrue(id_list.refers_to('mod1.submod2'))
 
2087
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
 
2088
        self.assertTrue(id_list.includes('mod1.submod1'))
 
2089
        self.assertTrue(id_list.includes('mod1.func1'))
 
2090
 
 
2091
    def test_bad_chars_in_params(self):
 
2092
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
 
2093
        self.assertTrue(id_list.refers_to('mod1'))
 
2094
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
 
2095
 
 
2096
    def test_module_used(self):
 
2097
        id_list = self._create_id_list(['mod.class.meth'])
 
2098
        self.assertTrue(id_list.refers_to('mod'))
 
2099
        self.assertTrue(id_list.refers_to('mod.class'))
 
2100
        self.assertTrue(id_list.refers_to('mod.class.meth'))
 
2101
 
 
2102
    def test_test_suite(self):
 
2103
        # This test is slow, so we do a single test with one test in each
 
2104
        # category
 
2105
        test_list = [
 
2106
            # testmod_names
 
2107
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
 
2108
            'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
 
2109
            # transport implementations
 
2110
            'bzrlib.tests.test_transport_implementations.TransportTests'
 
2111
            '.test_abspath(LocalURLServer)',
 
2112
            # modules_to_doctest
 
2113
            'bzrlib.timestamp.format_highres_date',
 
2114
            # plugins can't be tested that way since selftest may be run with
 
2115
            # --no-plugins
 
2116
            ]
 
2117
        suite = tests.test_suite(test_list)
 
2118
        self.assertEquals(test_list, _test_ids(suite))
 
2119
 
 
2120
    def test_test_suite_matches_id_list_with_unknown(self):
 
2121
        loader = TestUtil.TestLoader()
 
2122
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2123
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
 
2124
                     'bogus']
 
2125
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
 
2126
        self.assertEquals(['bogus'], not_found)
 
2127
        self.assertEquals([], duplicates)
 
2128
 
 
2129
    def test_suite_matches_id_list_with_duplicates(self):
 
2130
        loader = TestUtil.TestLoader()
 
2131
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2132
        dupes = loader.suiteClass()
 
2133
        for test in iter_suite_tests(suite):
 
2134
            dupes.addTest(test)
 
2135
            dupes.addTest(test) # Add it again
 
2136
 
 
2137
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
 
2138
        not_found, duplicates = tests.suite_matches_id_list(
 
2139
            dupes, test_list)
 
2140
        self.assertEquals([], not_found)
 
2141
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
2142
                          duplicates)
 
2143
 
 
2144
 
 
2145
class TestLoadTestIdList(tests.TestCaseInTempDir):
 
2146
 
 
2147
    def _create_test_list_file(self, file_name, content):
 
2148
        fl = open(file_name, 'wt')
 
2149
        fl.write(content)
 
2150
        fl.close()
 
2151
 
 
2152
    def test_load_unknown(self):
 
2153
        self.assertRaises(errors.NoSuchFile,
 
2154
                          tests.load_test_id_list, 'i_do_not_exist')
 
2155
 
 
2156
    def test_load_test_list(self):
 
2157
        test_list_fname = 'test.list'
 
2158
        self._create_test_list_file(test_list_fname,
 
2159
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
 
2160
        tlist = tests.load_test_id_list(test_list_fname)
 
2161
        self.assertEquals(2, len(tlist))
 
2162
        self.assertEquals('mod1.cl1.meth1', tlist[0])
 
2163
        self.assertEquals('mod2.cl2.meth2', tlist[1])
 
2164
 
 
2165
    def test_load_dirty_file(self):
 
2166
        test_list_fname = 'test.list'
 
2167
        self._create_test_list_file(test_list_fname,
 
2168
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
 
2169
                                    'bar baz\n')
 
2170
        tlist = tests.load_test_id_list(test_list_fname)
 
2171
        self.assertEquals(4, len(tlist))
 
2172
        self.assertEquals('mod1.cl1.meth1', tlist[0])
 
2173
        self.assertEquals('', tlist[1])
 
2174
        self.assertEquals('mod2.cl2.meth2', tlist[2])
 
2175
        self.assertEquals('bar baz', tlist[3])
 
2176
 
 
2177
 
 
2178
class TestFilteredByModuleTestLoader(tests.TestCase):
 
2179
 
 
2180
    def _create_loader(self, test_list):
 
2181
        id_filter = tests.TestIdList(test_list)
 
2182
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
 
2183
        return loader
 
2184
 
 
2185
    def test_load_tests(self):
 
2186
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
 
2187
        loader = self._create_loader(test_list)
 
2188
 
 
2189
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2190
        self.assertEquals(test_list, _test_ids(suite))
 
2191
 
 
2192
    def test_exclude_tests(self):
 
2193
        test_list = ['bogus']
 
2194
        loader = self._create_loader(test_list)
 
2195
 
 
2196
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2197
        self.assertEquals([], _test_ids(suite))
 
2198
 
 
2199
 
 
2200
class TestFilteredByNameStartTestLoader(tests.TestCase):
 
2201
 
 
2202
    def _create_loader(self, name_start):
 
2203
        def needs_module(name):
 
2204
            return name.startswith(name_start) or name_start.startswith(name)
 
2205
        loader = TestUtil.FilteredByModuleTestLoader(needs_module)
 
2206
        return loader
 
2207
 
 
2208
    def test_load_tests(self):
 
2209
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
 
2210
        loader = self._create_loader('bzrlib.tests.test_samp')
 
2211
 
 
2212
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2213
        self.assertEquals(test_list, _test_ids(suite))
 
2214
 
 
2215
    def test_load_tests_inside_module(self):
 
2216
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
 
2217
        loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
 
2218
 
 
2219
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2220
        self.assertEquals(test_list, _test_ids(suite))
 
2221
 
 
2222
    def test_exclude_tests(self):
 
2223
        test_list = ['bogus']
 
2224
        loader = self._create_loader('bogus')
 
2225
 
 
2226
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2227
        self.assertEquals([], _test_ids(suite))
 
2228
 
 
2229
 
 
2230
class TestTestPrefixRegistry(tests.TestCase):
 
2231
 
 
2232
    def _get_registry(self):
 
2233
        tp_registry = tests.TestPrefixAliasRegistry()
 
2234
        return tp_registry
 
2235
 
 
2236
    def test_register_new_prefix(self):
 
2237
        tpr = self._get_registry()
 
2238
        tpr.register('foo', 'fff.ooo.ooo')
 
2239
        self.assertEquals('fff.ooo.ooo', tpr.get('foo'))
 
2240
 
 
2241
    def test_register_existing_prefix(self):
 
2242
        tpr = self._get_registry()
 
2243
        tpr.register('bar', 'bbb.aaa.rrr')
 
2244
        tpr.register('bar', 'bBB.aAA.rRR')
 
2245
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
 
2246
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
2247
                              r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')
 
2248
 
 
2249
    def test_get_unknown_prefix(self):
 
2250
        tpr = self._get_registry()
 
2251
        self.assertRaises(KeyError, tpr.get, 'I am not a prefix')
 
2252
 
 
2253
    def test_resolve_prefix(self):
 
2254
        tpr = self._get_registry()
 
2255
        tpr.register('bar', 'bb.aa.rr')
 
2256
        self.assertEquals('bb.aa.rr', tpr.resolve_alias('bar'))
 
2257
 
 
2258
    def test_resolve_unknown_alias(self):
 
2259
        tpr = self._get_registry()
 
2260
        self.assertRaises(errors.BzrCommandError,
 
2261
                          tpr.resolve_alias, 'I am not a prefix')
 
2262
 
 
2263
    def test_predefined_prefixes(self):
 
2264
        tpr = tests.test_prefix_alias_registry
 
2265
        self.assertEquals('bzrlib', tpr.resolve_alias('bzrlib'))
 
2266
        self.assertEquals('bzrlib.doc', tpr.resolve_alias('bd'))
 
2267
        self.assertEquals('bzrlib.utils', tpr.resolve_alias('bu'))
 
2268
        self.assertEquals('bzrlib.tests', tpr.resolve_alias('bt'))
 
2269
        self.assertEquals('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
 
2270
        self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))