/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Martin Pool
  • Date: 2008-06-18 05:35:02 UTC
  • mto: This revision was merged to the branch mainline in revision 3510.
  • Revision ID: mbp@sourcefrog.net-20080618053502-9ogi5d5tx7w5ybf6
Change stray pdb calls to exceptions

Show diffs side-by-side

added added

removed removed

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