/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-07-18 03:07:07 UTC
  • mfrom: (3535.5.1 lean_imports)
  • Revision ID: pqm@pqm.ubuntu.com-20080718030707-qimx9fv1d4xhlk1m
(jam) Some small import cleanups, to reduce unused dependencies.

Show diffs side-by-side

added added

removed removed

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