/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

Merge in the BranchBuilder updates, which brings in a newer bzr.dev

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