~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-24 00:44:18 UTC
  • Revision ID: mbp@sourcefrog.net-20050324004418-b4a050f656c07f5f
show space usage for various stores in the info command

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
 
    )
36
 
from bzrlib.progress import _BaseProgressBar
37
 
from bzrlib.repofmt import weaverepo
38
 
from bzrlib.symbol_versioning import (
39
 
        zero_ten,
40
 
        zero_eleven,
41
 
        )
42
 
from bzrlib.tests import (
43
 
                          ChrootedTestCase,
44
 
                          ExtendedTestResult,
45
 
                          Feature,
46
 
                          KnownFailure,
47
 
                          TestCase,
48
 
                          TestCaseInTempDir,
49
 
                          TestCaseWithMemoryTransport,
50
 
                          TestCaseWithTransport,
51
 
                          TestSkipped,
52
 
                          TestSuite,
53
 
                          TestUtil,
54
 
                          TextTestRunner,
55
 
                          UnavailableFeature,
56
 
                          iter_suite_tests,
57
 
                          filter_suite_by_re,
58
 
                          sort_suite_by_re,
59
 
                          test_lsprof,
60
 
                          test_suite,
61
 
                          )
62
 
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
63
 
from bzrlib.tests.TestUtil import _load_module_by_name
64
 
from bzrlib.trace import note
65
 
from bzrlib.transport.memory import MemoryServer, MemoryTransport
66
 
from bzrlib.version import _get_bzr_source_tree
67
 
 
68
 
 
69
 
class SelftestTests(TestCase):
70
 
 
71
 
    def test_import_tests(self):
72
 
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
73
 
        self.assertEqual(mod.SelftestTests, SelftestTests)
74
 
 
75
 
    def test_import_test_failure(self):
76
 
        self.assertRaises(ImportError,
77
 
                          _load_module_by_name,
78
 
                          'bzrlib.no-name-yet')
79
 
 
80
 
class MetaTestLog(TestCase):
81
 
 
82
 
    def test_logging(self):
83
 
        """Test logs are captured when a test fails."""
84
 
        self.log('a test message')
85
 
        self._log_file.flush()
86
 
        self.assertContainsRe(self._get_log(keep_log_file=True),
87
 
                              'a test message\n')
88
 
 
89
 
 
90
 
class TestTreeShape(TestCaseInTempDir):
91
 
 
92
 
    def test_unicode_paths(self):
93
 
        filename = u'hell\u00d8'
94
 
        try:
95
 
            self.build_tree_contents([(filename, 'contents of hello')])
96
 
        except UnicodeEncodeError:
97
 
            raise TestSkipped("can't build unicode working tree in "
98
 
                "filesystem encoding %s" % sys.getfilesystemencoding())
99
 
        self.failUnlessExists(filename)
100
 
 
101
 
 
102
 
class TestTransportProviderAdapter(TestCase):
103
 
    """A group of tests that test the transport implementation adaption core.
104
 
 
105
 
    This is a meta test that the tests are applied to all available 
106
 
    transports.
107
 
 
108
 
    This will be generalised in the future which is why it is in this 
109
 
    test file even though it is specific to transport tests at the moment.
110
 
    """
111
 
 
112
 
    def test_get_transport_permutations(self):
113
 
        # this checks that we the module get_test_permutations call
114
 
        # is made by the adapter get_transport_test_permitations method.
115
 
        class MockModule(object):
116
 
            def get_test_permutations(self):
117
 
                return sample_permutation
118
 
        sample_permutation = [(1,2), (3,4)]
119
 
        from bzrlib.tests.test_transport_implementations \
120
 
            import TransportTestProviderAdapter
121
 
        adapter = TransportTestProviderAdapter()
122
 
        self.assertEqual(sample_permutation,
123
 
                         adapter.get_transport_test_permutations(MockModule()))
124
 
 
125
 
    def test_adapter_checks_all_modules(self):
126
 
        # this checks that the adapter returns as many permurtations as
127
 
        # there are in all the registered# transport modules for there
128
 
        # - we assume if this matches its probably doing the right thing
129
 
        # especially in combination with the tests for setting the right
130
 
        # classes below.
131
 
        from bzrlib.tests.test_transport_implementations \
132
 
            import TransportTestProviderAdapter
133
 
        from bzrlib.transport import _get_transport_modules
134
 
        modules = _get_transport_modules()
135
 
        permutation_count = 0
136
 
        for module in modules:
137
 
            try:
138
 
                permutation_count += len(reduce(getattr, 
139
 
                    (module + ".get_test_permutations").split('.')[1:],
140
 
                     __import__(module))())
141
 
            except errors.DependencyNotPresent:
142
 
                pass
143
 
        input_test = TestTransportProviderAdapter(
144
 
            "test_adapter_sets_transport_class")
145
 
        adapter = TransportTestProviderAdapter()
146
 
        self.assertEqual(permutation_count,
147
 
                         len(list(iter(adapter.adapt(input_test)))))
148
 
 
149
 
    def test_adapter_sets_transport_class(self):
150
 
        # Check that the test adapter inserts a transport and server into the
151
 
        # generated test.
152
 
        #
153
 
        # This test used to know about all the possible transports and the
154
 
        # order they were returned but that seems overly brittle (mbp
155
 
        # 20060307)
156
 
        from bzrlib.tests.test_transport_implementations \
157
 
            import TransportTestProviderAdapter
158
 
        scenarios = TransportTestProviderAdapter().scenarios
159
 
        # there are at least that many builtin transports
160
 
        self.assertTrue(len(scenarios) > 6)
161
 
        one_scenario = scenarios[0]
162
 
        self.assertIsInstance(one_scenario[0], str)
163
 
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
164
 
                                   bzrlib.transport.Transport))
165
 
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
166
 
                                   bzrlib.transport.Server))
167
 
 
168
 
 
169
 
class TestBranchProviderAdapter(TestCase):
170
 
    """A group of tests that test the branch implementation test adapter."""
171
 
 
172
 
    def test_constructor(self):
173
 
        # check that constructor parameters are passed through to the adapted
174
 
        # test.
175
 
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
176
 
        server1 = "a"
177
 
        server2 = "b"
178
 
        formats = [("c", "C"), ("d", "D")]
179
 
        adapter = BranchTestProviderAdapter(server1, server2, formats)
180
 
        self.assertEqual(2, len(adapter.scenarios))
181
 
        self.assertEqual([
182
 
            ('str',
183
 
             {'branch_format': 'c',
184
 
              'bzrdir_format': 'C',
185
 
              'transport_readonly_server': 'b',
186
 
              'transport_server': 'a'}),
187
 
            ('str',
188
 
             {'branch_format': 'd',
189
 
              'bzrdir_format': 'D',
190
 
              'transport_readonly_server': 'b',
191
 
              'transport_server': 'a'})],
192
 
            adapter.scenarios)
193
 
 
194
 
 
195
 
class TestBzrDirProviderAdapter(TestCase):
196
 
    """A group of tests that test the bzr dir implementation test adapter."""
197
 
 
198
 
    def test_adapted_tests(self):
199
 
        # check that constructor parameters are passed through to the adapted
200
 
        # test.
201
 
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
202
 
        vfs_factory = "v"
203
 
        server1 = "a"
204
 
        server2 = "b"
205
 
        formats = ["c", "d"]
206
 
        adapter = BzrDirTestProviderAdapter(vfs_factory,
207
 
            server1, server2, formats)
208
 
        self.assertEqual([
209
 
            ('str',
210
 
             {'bzrdir_format': 'c',
211
 
              'transport_readonly_server': 'b',
212
 
              'transport_server': 'a',
213
 
              'vfs_transport_factory': 'v'}),
214
 
            ('str',
215
 
             {'bzrdir_format': 'd',
216
 
              'transport_readonly_server': 'b',
217
 
              'transport_server': 'a',
218
 
              'vfs_transport_factory': 'v'})],
219
 
            adapter.scenarios)
220
 
 
221
 
 
222
 
class TestRepositoryProviderAdapter(TestCase):
223
 
    """A group of tests that test the repository implementation test adapter."""
224
 
 
225
 
    def test_constructor(self):
226
 
        # check that constructor parameters are passed through to the
227
 
        # scenarios.
228
 
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
229
 
        server1 = "a"
230
 
        server2 = "b"
231
 
        formats = [("c", "C"), ("d", "D")]
232
 
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
233
 
        self.assertEqual([
234
 
            ('str',
235
 
             {'bzrdir_format': 'C',
236
 
              'repository_format': 'c',
237
 
              'transport_readonly_server': 'b',
238
 
              'transport_server': 'a'}),
239
 
            ('str',
240
 
             {'bzrdir_format': 'D',
241
 
              'repository_format': 'd',
242
 
              'transport_readonly_server': 'b',
243
 
              'transport_server': 'a'})],
244
 
            adapter.scenarios)
245
 
 
246
 
    def test_setting_vfs_transport(self):
247
 
        """The vfs_transport_factory can be set optionally."""
248
 
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
249
 
        formats = [("a", "b"), ("c", "d")]
250
 
        adapter = RepositoryTestProviderAdapter(None, None, formats,
251
 
            vfs_transport_factory="vfs")
252
 
        self.assertEqual([
253
 
            ('str',
254
 
             {'bzrdir_format': 'b',
255
 
              'repository_format': 'a',
256
 
              'transport_readonly_server': None,
257
 
              'transport_server': None,
258
 
              'vfs_transport_factory': 'vfs'}),
259
 
            ('str',
260
 
             {'bzrdir_format': 'd',
261
 
              'repository_format': 'c',
262
 
              'transport_readonly_server': None,
263
 
              'transport_server': None,
264
 
              'vfs_transport_factory': 'vfs'})],
265
 
            adapter.scenarios)
266
 
 
267
 
    def test_formats_to_scenarios(self):
268
 
        """The adapter can generate all the scenarios needed."""
269
 
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
270
 
        no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
271
 
            [], None)
272
 
        vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
273
 
            [], vfs_transport_factory="vfs")
274
 
        # no_vfs generate scenarios without vfs_transport_factor
275
 
        formats = [("c", "C"), (1, "D")]
276
 
        self.assertEqual([
277
 
            ('str',
278
 
             {'bzrdir_format': 'C',
279
 
              'repository_format': 'c',
280
 
              'transport_readonly_server': 'readonly',
281
 
              'transport_server': 'server'}),
282
 
            ('int',
283
 
             {'bzrdir_format': 'D',
284
 
              'repository_format': 1,
285
 
              'transport_readonly_server': 'readonly',
286
 
              'transport_server': 'server'})],
287
 
            no_vfs_adapter.formats_to_scenarios(formats))
288
 
        self.assertEqual([
289
 
            ('str',
290
 
             {'bzrdir_format': 'C',
291
 
              'repository_format': 'c',
292
 
              'transport_readonly_server': 'readonly',
293
 
              'transport_server': 'server',
294
 
              'vfs_transport_factory': 'vfs'}),
295
 
            ('int',
296
 
             {'bzrdir_format': 'D',
297
 
              'repository_format': 1,
298
 
              'transport_readonly_server': 'readonly',
299
 
              'transport_server': 'server',
300
 
              'vfs_transport_factory': 'vfs'})],
301
 
            vfs_adapter.formats_to_scenarios(formats))
302
 
 
303
 
 
304
 
class TestTestScenarioApplier(TestCase):
305
 
    """Tests for the test adaption facilities."""
306
 
 
307
 
    def test_adapt_applies_scenarios(self):
308
 
        from bzrlib.tests.repository_implementations import TestScenarioApplier
309
 
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
310
 
        adapter = TestScenarioApplier()
311
 
        adapter.scenarios = [("1", "dict"), ("2", "settings")]
312
 
        calls = []
313
 
        def capture_call(test, scenario):
314
 
            calls.append((test, scenario))
315
 
            return test
316
 
        adapter.adapt_test_to_scenario = capture_call
317
 
        adapter.adapt(input_test)
318
 
        self.assertEqual([(input_test, ("1", "dict")),
319
 
            (input_test, ("2", "settings"))], calls)
320
 
 
321
 
    def test_adapt_test_to_scenario(self):
322
 
        from bzrlib.tests.repository_implementations import TestScenarioApplier
323
 
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
324
 
        adapter = TestScenarioApplier()
325
 
        # setup two adapted tests
326
 
        adapted_test1 = adapter.adapt_test_to_scenario(input_test,
327
 
            ("new id",
328
 
            {"bzrdir_format":"bzr_format",
329
 
             "repository_format":"repo_fmt",
330
 
             "transport_server":"transport_server",
331
 
             "transport_readonly_server":"readonly-server"}))
332
 
        adapted_test2 = adapter.adapt_test_to_scenario(input_test,
333
 
            ("new id 2", {"bzrdir_format":None}))
334
 
        # input_test should have been altered.
335
 
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
336
 
        # the new tests are mutually incompatible, ensuring it has 
337
 
        # made new ones, and unspecified elements in the scenario
338
 
        # should not have been altered.
339
 
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
340
 
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
341
 
        self.assertEqual("transport_server", adapted_test1.transport_server)
342
 
        self.assertEqual("readonly-server",
343
 
            adapted_test1.transport_readonly_server)
344
 
        self.assertEqual(
345
 
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
346
 
            "test_adapt_test_to_scenario(new id)",
347
 
            adapted_test1.id())
348
 
        self.assertEqual(None, adapted_test2.bzrdir_format)
349
 
        self.assertEqual(
350
 
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
351
 
            "test_adapt_test_to_scenario(new id 2)",
352
 
            adapted_test2.id())
353
 
 
354
 
 
355
 
class TestInterRepositoryProviderAdapter(TestCase):
356
 
    """A group of tests that test the InterRepository test adapter."""
357
 
 
358
 
    def test_adapted_tests(self):
359
 
        # check that constructor parameters are passed through to the adapted
360
 
        # test.
361
 
        from bzrlib.tests.interrepository_implementations import \
362
 
            InterRepositoryTestProviderAdapter
363
 
        server1 = "a"
364
 
        server2 = "b"
365
 
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
366
 
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
367
 
        self.assertEqual([
368
 
            ('str',
369
 
             {'interrepo_class': str,
370
 
              'repository_format': 'C1',
371
 
              'repository_format_to': 'C2',
372
 
              'transport_readonly_server': 'b',
373
 
              'transport_server': 'a'}),
374
 
            ('int',
375
 
             {'interrepo_class': int,
376
 
              'repository_format': 'D1',
377
 
              'repository_format_to': 'D2',
378
 
              'transport_readonly_server': 'b',
379
 
              'transport_server': 'a'})],
380
 
            adapter.formats_to_scenarios(formats))
381
 
 
382
 
 
383
 
class TestInterVersionedFileProviderAdapter(TestCase):
384
 
    """A group of tests that test the InterVersionedFile test adapter."""
385
 
 
386
 
    def test_scenarios(self):
387
 
        # check that constructor parameters are passed through to the adapted
388
 
        # test.
389
 
        from bzrlib.tests.interversionedfile_implementations \
390
 
            import InterVersionedFileTestProviderAdapter
391
 
        server1 = "a"
392
 
        server2 = "b"
393
 
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
394
 
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
395
 
        self.assertEqual([
396
 
            ('str',
397
 
             {'interversionedfile_class':str,
398
 
              'transport_readonly_server': 'b',
399
 
              'transport_server': 'a',
400
 
              'versionedfile_factory': 'C1',
401
 
              'versionedfile_factory_to': 'C2'}),
402
 
            ('int',
403
 
             {'interversionedfile_class': int,
404
 
              'transport_readonly_server': 'b',
405
 
              'transport_server': 'a',
406
 
              'versionedfile_factory': 'D1',
407
 
              'versionedfile_factory_to': 'D2'})],
408
 
            adapter.scenarios)
409
 
 
410
 
 
411
 
class TestRevisionStoreProviderAdapter(TestCase):
412
 
    """A group of tests that test the RevisionStore test adapter."""
413
 
 
414
 
    def test_scenarios(self):
415
 
        # check that constructor parameters are passed through to the adapted
416
 
        # test.
417
 
        from bzrlib.tests.revisionstore_implementations \
418
 
            import RevisionStoreTestProviderAdapter
419
 
        # revision stores need a store factory - i.e. RevisionKnit
420
 
        #, a readonly and rw transport 
421
 
        # transport servers:
422
 
        server1 = "a"
423
 
        server2 = "b"
424
 
        store_factories = ["c", "d"]
425
 
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
426
 
        self.assertEqual([
427
 
            ('c',
428
 
             {'store_factory': 'c',
429
 
              'transport_readonly_server': 'b',
430
 
              'transport_server': 'a'}),
431
 
            ('d',
432
 
             {'store_factory': 'd',
433
 
              'transport_readonly_server': 'b',
434
 
              'transport_server': 'a'})],
435
 
            adapter.scenarios)
436
 
 
437
 
 
438
 
class TestWorkingTreeProviderAdapter(TestCase):
439
 
    """A group of tests that test the workingtree implementation test adapter."""
440
 
 
441
 
    def test_scenarios(self):
442
 
        # check that constructor parameters are passed through to the adapted
443
 
        # test.
444
 
        from bzrlib.tests.workingtree_implementations \
445
 
            import WorkingTreeTestProviderAdapter
446
 
        server1 = "a"
447
 
        server2 = "b"
448
 
        formats = [("c", "C"), ("d", "D")]
449
 
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
450
 
        self.assertEqual([
451
 
            ('str',
452
 
             {'bzrdir_format': 'C',
453
 
              'transport_readonly_server': 'b',
454
 
              'transport_server': 'a',
455
 
              'workingtree_format': 'c'}),
456
 
            ('str',
457
 
             {'bzrdir_format': 'D',
458
 
              'transport_readonly_server': 'b',
459
 
              'transport_server': 'a',
460
 
              'workingtree_format': 'd'})],
461
 
            adapter.scenarios)
462
 
 
463
 
 
464
 
class TestTreeProviderAdapter(TestCase):
465
 
    """Test the setup of tree_implementation tests."""
466
 
 
467
 
    def test_adapted_tests(self):
468
 
        # the tree implementation adapter is meant to setup one instance for
469
 
        # each working tree format, and one additional instance that will
470
 
        # use the default wt format, but create a revision tree for the tests.
471
 
        # this means that the wt ones should have the workingtree_to_test_tree
472
 
        # attribute set to 'return_parameter' and the revision one set to
473
 
        # revision_tree_from_workingtree.
474
 
 
475
 
        from bzrlib.tests.tree_implementations import (
476
 
            TreeTestProviderAdapter,
477
 
            return_parameter,
478
 
            revision_tree_from_workingtree
479
 
            )
480
 
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
481
 
        input_test = TestTreeProviderAdapter(
482
 
            "test_adapted_tests")
483
 
        server1 = "a"
484
 
        server2 = "b"
485
 
        formats = [("c", "C"), ("d", "D")]
486
 
        adapter = TreeTestProviderAdapter(server1, server2, formats)
487
 
        suite = adapter.adapt(input_test)
488
 
        tests = list(iter(suite))
489
 
        self.assertEqual(4, len(tests))
490
 
        # this must match the default format setp up in
491
 
        # TreeTestProviderAdapter.adapt
492
 
        default_format = WorkingTreeFormat3
493
 
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
494
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
495
 
        self.assertEqual(tests[0].transport_server, server1)
496
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
497
 
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
498
 
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
499
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
500
 
        self.assertEqual(tests[1].transport_server, server1)
501
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
502
 
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
503
 
        self.assertIsInstance(tests[2].workingtree_format, default_format)
504
 
        #self.assertEqual(tests[2].bzrdir_format,
505
 
        #                 default_format._matchingbzrdir)
506
 
        self.assertEqual(tests[2].transport_server, server1)
507
 
        self.assertEqual(tests[2].transport_readonly_server, server2)
508
 
        self.assertEqual(tests[2].workingtree_to_test_tree,
509
 
            revision_tree_from_workingtree)
510
 
 
511
 
 
512
 
class TestInterTreeProviderAdapter(TestCase):
513
 
    """A group of tests that test the InterTreeTestAdapter."""
514
 
 
515
 
    def test_adapted_tests(self):
516
 
        # check that constructor parameters are passed through to the adapted
517
 
        # test.
518
 
        # for InterTree tests we want the machinery to bring up two trees in
519
 
        # each instance: the base one, and the one we are interacting with.
520
 
        # because each optimiser can be direction specific, we need to test
521
 
        # each optimiser in its chosen direction.
522
 
        # unlike the TestProviderAdapter we dont want to automatically add a
523
 
        # parameterised one for WorkingTree - the optimisers will tell us what
524
 
        # ones to add.
525
 
        from bzrlib.tests.tree_implementations import (
526
 
            return_parameter,
527
 
            revision_tree_from_workingtree
528
 
            )
529
 
        from bzrlib.tests.intertree_implementations import (
530
 
            InterTreeTestProviderAdapter,
531
 
            )
532
 
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
533
 
        input_test = TestInterTreeProviderAdapter(
534
 
            "test_adapted_tests")
535
 
        server1 = "a"
536
 
        server2 = "b"
537
 
        format1 = WorkingTreeFormat2()
538
 
        format2 = WorkingTreeFormat3()
539
 
        formats = [(str, format1, format2, "converter1"),
540
 
            (int, format2, format1, "converter2")]
541
 
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
542
 
        suite = adapter.adapt(input_test)
543
 
        tests = list(iter(suite))
544
 
        self.assertEqual(2, len(tests))
545
 
        self.assertEqual(tests[0].intertree_class, formats[0][0])
546
 
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
547
 
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
548
 
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
549
 
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
550
 
        self.assertEqual(tests[0].transport_server, server1)
551
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
552
 
        self.assertEqual(tests[1].intertree_class, formats[1][0])
553
 
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
554
 
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
555
 
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
556
 
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
557
 
        self.assertEqual(tests[1].transport_server, server1)
558
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
559
 
 
560
 
 
561
 
class TestTestCaseInTempDir(TestCaseInTempDir):
562
 
 
563
 
    def test_home_is_not_working(self):
564
 
        self.assertNotEqual(self.test_dir, self.test_home_dir)
565
 
        cwd = osutils.getcwd()
566
 
        self.assertEqual(self.test_dir, cwd)
567
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
568
 
 
569
 
 
570
 
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
571
 
 
572
 
    def test_home_is_non_existant_dir_under_root(self):
573
 
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
574
 
 
575
 
        This is because TestCaseWithMemoryTransport is for tests that do not
576
 
        need any disk resources: they should be hooked into bzrlib in such a 
577
 
        way that no global settings are being changed by the test (only a 
578
 
        few tests should need to do that), and having a missing dir as home is
579
 
        an effective way to ensure that this is the case.
580
 
        """
581
 
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
582
 
            self.test_home_dir)
583
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
584
 
        
585
 
    def test_cwd_is_TEST_ROOT(self):
586
 
        self.assertEqual(self.test_dir, self.TEST_ROOT)
587
 
        cwd = osutils.getcwd()
588
 
        self.assertEqual(self.test_dir, cwd)
589
 
 
590
 
    def test_make_branch_and_memory_tree(self):
591
 
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
592
 
 
593
 
        This is hard to comprehensively robustly test, so we settle for making
594
 
        a branch and checking no directory was created at its relpath.
595
 
        """
596
 
        tree = self.make_branch_and_memory_tree('dir')
597
 
        # Guard against regression into MemoryTransport leaking
598
 
        # files to disk instead of keeping them in memory.
599
 
        self.failIf(osutils.lexists('dir'))
600
 
        self.assertIsInstance(tree, memorytree.MemoryTree)
601
 
 
602
 
    def test_make_branch_and_memory_tree_with_format(self):
603
 
        """make_branch_and_memory_tree should accept a format option."""
604
 
        format = bzrdir.BzrDirMetaFormat1()
605
 
        format.repository_format = weaverepo.RepositoryFormat7()
606
 
        tree = self.make_branch_and_memory_tree('dir', format=format)
607
 
        # Guard against regression into MemoryTransport leaking
608
 
        # files to disk instead of keeping them in memory.
609
 
        self.failIf(osutils.lexists('dir'))
610
 
        self.assertIsInstance(tree, memorytree.MemoryTree)
611
 
        self.assertEqual(format.repository_format.__class__,
612
 
            tree.branch.repository._format.__class__)
613
 
 
614
 
 
615
 
class TestTestCaseWithTransport(TestCaseWithTransport):
616
 
    """Tests for the convenience functions TestCaseWithTransport introduces."""
617
 
 
618
 
    def test_get_readonly_url_none(self):
619
 
        from bzrlib.transport import get_transport
620
 
        from bzrlib.transport.memory import MemoryServer
621
 
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
622
 
        self.vfs_transport_factory = MemoryServer
623
 
        self.transport_readonly_server = None
624
 
        # calling get_readonly_transport() constructs a decorator on the url
625
 
        # for the server
626
 
        url = self.get_readonly_url()
627
 
        url2 = self.get_readonly_url('foo/bar')
628
 
        t = get_transport(url)
629
 
        t2 = get_transport(url2)
630
 
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
631
 
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
632
 
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
633
 
 
634
 
    def test_get_readonly_url_http(self):
635
 
        from bzrlib.tests.HttpServer import HttpServer
636
 
        from bzrlib.transport import get_transport
637
 
        from bzrlib.transport.local import LocalURLServer
638
 
        from bzrlib.transport.http import HttpTransportBase
639
 
        self.transport_server = LocalURLServer
640
 
        self.transport_readonly_server = HttpServer
641
 
        # calling get_readonly_transport() gives us a HTTP server instance.
642
 
        url = self.get_readonly_url()
643
 
        url2 = self.get_readonly_url('foo/bar')
644
 
        # the transport returned may be any HttpTransportBase subclass
645
 
        t = get_transport(url)
646
 
        t2 = get_transport(url2)
647
 
        self.failUnless(isinstance(t, HttpTransportBase))
648
 
        self.failUnless(isinstance(t2, HttpTransportBase))
649
 
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
650
 
 
651
 
    def test_is_directory(self):
652
 
        """Test assertIsDirectory assertion"""
653
 
        t = self.get_transport()
654
 
        self.build_tree(['a_dir/', 'a_file'], transport=t)
655
 
        self.assertIsDirectory('a_dir', t)
656
 
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
657
 
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
658
 
 
659
 
 
660
 
class TestTestCaseTransports(TestCaseWithTransport):
661
 
 
662
 
    def setUp(self):
663
 
        super(TestTestCaseTransports, self).setUp()
664
 
        self.vfs_transport_factory = MemoryServer
665
 
 
666
 
    def test_make_bzrdir_preserves_transport(self):
667
 
        t = self.get_transport()
668
 
        result_bzrdir = self.make_bzrdir('subdir')
669
 
        self.assertIsInstance(result_bzrdir.transport, 
670
 
                              MemoryTransport)
671
 
        # should not be on disk, should only be in memory
672
 
        self.failIfExists('subdir')
673
 
 
674
 
 
675
 
class TestChrootedTest(ChrootedTestCase):
676
 
 
677
 
    def test_root_is_root(self):
678
 
        from bzrlib.transport import get_transport
679
 
        t = get_transport(self.get_readonly_url())
680
 
        url = t.base
681
 
        self.assertEqual(url, t.clone('..').base)
682
 
 
683
 
 
684
 
class MockProgress(_BaseProgressBar):
685
 
    """Progress-bar standin that records calls.
686
 
 
687
 
    Useful for testing pb using code.
688
 
    """
689
 
 
690
 
    def __init__(self):
691
 
        _BaseProgressBar.__init__(self)
692
 
        self.calls = []
693
 
 
694
 
    def tick(self):
695
 
        self.calls.append(('tick',))
696
 
 
697
 
    def update(self, msg=None, current=None, total=None):
698
 
        self.calls.append(('update', msg, current, total))
699
 
 
700
 
    def clear(self):
701
 
        self.calls.append(('clear',))
702
 
 
703
 
    def note(self, msg, *args):
704
 
        self.calls.append(('note', msg, args))
705
 
 
706
 
 
707
 
class TestTestResult(TestCase):
708
 
 
709
 
    def test_elapsed_time_with_benchmarking(self):
710
 
        result = bzrlib.tests.TextTestResult(self._log_file,
711
 
                                        descriptions=0,
712
 
                                        verbosity=1,
713
 
                                        )
714
 
        result._recordTestStartTime()
715
 
        time.sleep(0.003)
716
 
        result.extractBenchmarkTime(self)
717
 
        timed_string = result._testTimeString()
718
 
        # without explicit benchmarking, we should get a simple time.
719
 
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
720
 
        # if a benchmark time is given, we want a x of y style result.
721
 
        self.time(time.sleep, 0.001)
722
 
        result.extractBenchmarkTime(self)
723
 
        timed_string = result._testTimeString()
724
 
        self.assertContainsRe(
725
 
            timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
726
 
        # extracting the time from a non-bzrlib testcase sets to None
727
 
        result._recordTestStartTime()
728
 
        result.extractBenchmarkTime(
729
 
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
730
 
        timed_string = result._testTimeString()
731
 
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
732
 
        # cheat. Yes, wash thy mouth out with soap.
733
 
        self._benchtime = None
734
 
 
735
 
    def test_assigned_benchmark_file_stores_date(self):
736
 
        output = StringIO()
737
 
        result = bzrlib.tests.TextTestResult(self._log_file,
738
 
                                        descriptions=0,
739
 
                                        verbosity=1,
740
 
                                        bench_history=output
741
 
                                        )
742
 
        output_string = output.getvalue()
743
 
        
744
 
        # if you are wondering about the regexp please read the comment in
745
 
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
746
 
        # XXX: what comment?  -- Andrew Bennetts
747
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
748
 
 
749
 
    def test_benchhistory_records_test_times(self):
750
 
        result_stream = StringIO()
751
 
        result = bzrlib.tests.TextTestResult(
752
 
            self._log_file,
753
 
            descriptions=0,
754
 
            verbosity=1,
755
 
            bench_history=result_stream
756
 
            )
757
 
 
758
 
        # we want profile a call and check that its test duration is recorded
759
 
        # make a new test instance that when run will generate a benchmark
760
 
        example_test_case = TestTestResult("_time_hello_world_encoding")
761
 
        # execute the test, which should succeed and record times
762
 
        example_test_case.run(result)
763
 
        lines = result_stream.getvalue().splitlines()
764
 
        self.assertEqual(2, len(lines))
765
 
        self.assertContainsRe(lines[1],
766
 
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
767
 
            "._time_hello_world_encoding")
768
 
 
769
 
    def _time_hello_world_encoding(self):
770
 
        """Profile two sleep calls
771
 
        
772
 
        This is used to exercise the test framework.
773
 
        """
774
 
        self.time(unicode, 'hello', errors='replace')
775
 
        self.time(unicode, 'world', errors='replace')
776
 
 
777
 
    def test_lsprofiling(self):
778
 
        """Verbose test result prints lsprof statistics from test cases."""
779
 
        self.requireFeature(test_lsprof.LSProfFeature)
780
 
        result_stream = StringIO()
781
 
        result = bzrlib.tests.VerboseTestResult(
782
 
            unittest._WritelnDecorator(result_stream),
783
 
            descriptions=0,
784
 
            verbosity=2,
785
 
            )
786
 
        # we want profile a call of some sort and check it is output by
787
 
        # addSuccess. We dont care about addError or addFailure as they
788
 
        # are not that interesting for performance tuning.
789
 
        # make a new test instance that when run will generate a profile
790
 
        example_test_case = TestTestResult("_time_hello_world_encoding")
791
 
        example_test_case._gather_lsprof_in_benchmarks = True
792
 
        # execute the test, which should succeed and record profiles
793
 
        example_test_case.run(result)
794
 
        # lsprofile_something()
795
 
        # if this worked we want 
796
 
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
797
 
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
798
 
        # (the lsprof header)
799
 
        # ... an arbitrary number of lines
800
 
        # and the function call which is time.sleep.
801
 
        #           1        0            ???         ???       ???(sleep) 
802
 
        # and then repeated but with 'world', rather than 'hello'.
803
 
        # this should appear in the output stream of our test result.
804
 
        output = result_stream.getvalue()
805
 
        self.assertContainsRe(output,
806
 
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
807
 
        self.assertContainsRe(output,
808
 
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
809
 
        self.assertContainsRe(output,
810
 
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
811
 
        self.assertContainsRe(output,
812
 
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
813
 
 
814
 
    def test_known_failure(self):
815
 
        """A KnownFailure being raised should trigger several result actions."""
816
 
        class InstrumentedTestResult(ExtendedTestResult):
817
 
 
818
 
            def report_test_start(self, test): pass
819
 
            def report_known_failure(self, test, err):
820
 
                self._call = test, err
821
 
        result = InstrumentedTestResult(None, None, None, None)
822
 
        def test_function():
823
 
            raise KnownFailure('failed!')
824
 
        test = unittest.FunctionTestCase(test_function)
825
 
        test.run(result)
826
 
        # it should invoke 'report_known_failure'.
827
 
        self.assertEqual(2, len(result._call))
828
 
        self.assertEqual(test, result._call[0])
829
 
        self.assertEqual(KnownFailure, result._call[1][0])
830
 
        self.assertIsInstance(result._call[1][1], KnownFailure)
831
 
        # we dont introspec the traceback, if the rest is ok, it would be
832
 
        # exceptional for it not to be.
833
 
        # it should update the known_failure_count on the object.
834
 
        self.assertEqual(1, result.known_failure_count)
835
 
        # the result should be successful.
836
 
        self.assertTrue(result.wasSuccessful())
837
 
 
838
 
    def test_verbose_report_known_failure(self):
839
 
        # verbose test output formatting
840
 
        result_stream = StringIO()
841
 
        result = bzrlib.tests.VerboseTestResult(
842
 
            unittest._WritelnDecorator(result_stream),
843
 
            descriptions=0,
844
 
            verbosity=2,
845
 
            )
846
 
        test = self.get_passing_test()
847
 
        result.startTest(test)
848
 
        result.extractBenchmarkTime(test)
849
 
        prefix = len(result_stream.getvalue())
850
 
        # the err parameter has the shape:
851
 
        # (class, exception object, traceback)
852
 
        # KnownFailures dont get their tracebacks shown though, so we
853
 
        # can skip that.
854
 
        err = (KnownFailure, KnownFailure('foo'), None)
855
 
        result.report_known_failure(test, err)
856
 
        output = result_stream.getvalue()[prefix:]
857
 
        lines = output.splitlines()
858
 
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
859
 
        self.assertEqual(lines[1], '    foo')
860
 
        self.assertEqual(2, len(lines))
861
 
 
862
 
    def test_text_report_known_failure(self):
863
 
        # text test output formatting
864
 
        pb = MockProgress()
865
 
        result = bzrlib.tests.TextTestResult(
866
 
            None,
867
 
            descriptions=0,
868
 
            verbosity=1,
869
 
            pb=pb,
870
 
            )
871
 
        test = self.get_passing_test()
872
 
        # this seeds the state to handle reporting the test.
873
 
        result.startTest(test)
874
 
        result.extractBenchmarkTime(test)
875
 
        # the err parameter has the shape:
876
 
        # (class, exception object, traceback)
877
 
        # KnownFailures dont get their tracebacks shown though, so we
878
 
        # can skip that.
879
 
        err = (KnownFailure, KnownFailure('foo'), None)
880
 
        result.report_known_failure(test, err)
881
 
        self.assertEqual(
882
 
            [
883
 
            ('update', '[1 in 0s] passing_test', None, None),
884
 
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
885
 
            ],
886
 
            pb.calls)
887
 
        # known_failures should be printed in the summary, so if we run a test
888
 
        # after there are some known failures, the update prefix should match
889
 
        # this.
890
 
        result.known_failure_count = 3
891
 
        test.run(result)
892
 
        self.assertEqual(
893
 
            [
894
 
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
895
 
            ],
896
 
            pb.calls[2:])
897
 
 
898
 
    def get_passing_test(self):
899
 
        """Return a test object that can't be run usefully."""
900
 
        def passing_test():
901
 
            pass
902
 
        return unittest.FunctionTestCase(passing_test)
903
 
 
904
 
    def test_add_not_supported(self):
905
 
        """Test the behaviour of invoking addNotSupported."""
906
 
        class InstrumentedTestResult(ExtendedTestResult):
907
 
            def report_test_start(self, test): pass
908
 
            def report_unsupported(self, test, feature):
909
 
                self._call = test, feature
910
 
        result = InstrumentedTestResult(None, None, None, None)
911
 
        test = SampleTestCase('_test_pass')
912
 
        feature = Feature()
913
 
        result.startTest(test)
914
 
        result.addNotSupported(test, feature)
915
 
        # it should invoke 'report_unsupported'.
916
 
        self.assertEqual(2, len(result._call))
917
 
        self.assertEqual(test, result._call[0])
918
 
        self.assertEqual(feature, result._call[1])
919
 
        # the result should be successful.
920
 
        self.assertTrue(result.wasSuccessful())
921
 
        # it should record the test against a count of tests not run due to
922
 
        # this feature.
923
 
        self.assertEqual(1, result.unsupported['Feature'])
924
 
        # and invoking it again should increment that counter
925
 
        result.addNotSupported(test, feature)
926
 
        self.assertEqual(2, result.unsupported['Feature'])
927
 
 
928
 
    def test_verbose_report_unsupported(self):
929
 
        # verbose test output formatting
930
 
        result_stream = StringIO()
931
 
        result = bzrlib.tests.VerboseTestResult(
932
 
            unittest._WritelnDecorator(result_stream),
933
 
            descriptions=0,
934
 
            verbosity=2,
935
 
            )
936
 
        test = self.get_passing_test()
937
 
        feature = Feature()
938
 
        result.startTest(test)
939
 
        result.extractBenchmarkTime(test)
940
 
        prefix = len(result_stream.getvalue())
941
 
        result.report_unsupported(test, feature)
942
 
        output = result_stream.getvalue()[prefix:]
943
 
        lines = output.splitlines()
944
 
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
945
 
    
946
 
    def test_text_report_unsupported(self):
947
 
        # text test output formatting
948
 
        pb = MockProgress()
949
 
        result = bzrlib.tests.TextTestResult(
950
 
            None,
951
 
            descriptions=0,
952
 
            verbosity=1,
953
 
            pb=pb,
954
 
            )
955
 
        test = self.get_passing_test()
956
 
        feature = Feature()
957
 
        # this seeds the state to handle reporting the test.
958
 
        result.startTest(test)
959
 
        result.extractBenchmarkTime(test)
960
 
        result.report_unsupported(test, feature)
961
 
        # no output on unsupported features
962
 
        self.assertEqual(
963
 
            [('update', '[1 in 0s] passing_test', None, None)
964
 
            ],
965
 
            pb.calls)
966
 
        # the number of missing features should be printed in the progress
967
 
        # summary, so check for that.
968
 
        result.unsupported = {'foo':0, 'bar':0}
969
 
        test.run(result)
970
 
        self.assertEqual(
971
 
            [
972
 
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
973
 
            ],
974
 
            pb.calls[1:])
975
 
    
976
 
    def test_unavailable_exception(self):
977
 
        """An UnavailableFeature being raised should invoke addNotSupported."""
978
 
        class InstrumentedTestResult(ExtendedTestResult):
979
 
 
980
 
            def report_test_start(self, test): pass
981
 
            def addNotSupported(self, test, feature):
982
 
                self._call = test, feature
983
 
        result = InstrumentedTestResult(None, None, None, None)
984
 
        feature = Feature()
985
 
        def test_function():
986
 
            raise UnavailableFeature(feature)
987
 
        test = unittest.FunctionTestCase(test_function)
988
 
        test.run(result)
989
 
        # it should invoke 'addNotSupported'.
990
 
        self.assertEqual(2, len(result._call))
991
 
        self.assertEqual(test, result._call[0])
992
 
        self.assertEqual(feature, result._call[1])
993
 
        # and not count as an error
994
 
        self.assertEqual(0, result.error_count)
995
 
 
996
 
    def test_strict_with_unsupported_feature(self):
997
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
998
 
                                                verbosity=1)
999
 
        test = self.get_passing_test()
1000
 
        feature = "Unsupported Feature"
1001
 
        result.addNotSupported(test, feature)
1002
 
        self.assertFalse(result.wasStrictlySuccessful())
1003
 
 
1004
 
    def test_strict_with_known_failure(self):
1005
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1006
 
                                                verbosity=1)
1007
 
        test = self.get_passing_test()
1008
 
        err = (KnownFailure, KnownFailure('foo'), None)
1009
 
        result.addKnownFailure(test, err)
1010
 
        self.assertFalse(result.wasStrictlySuccessful())
1011
 
 
1012
 
    def test_strict_with_success(self):
1013
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1014
 
                                                verbosity=1)
1015
 
        test = self.get_passing_test()
1016
 
        result.addSuccess(test)
1017
 
        self.assertTrue(result.wasStrictlySuccessful())
1018
 
 
1019
 
 
1020
 
class TestRunner(TestCase):
1021
 
 
1022
 
    def dummy_test(self):
1023
 
        pass
1024
 
 
1025
 
    def run_test_runner(self, testrunner, test):
1026
 
        """Run suite in testrunner, saving global state and restoring it.
1027
 
 
1028
 
        This current saves and restores:
1029
 
        TestCaseInTempDir.TEST_ROOT
1030
 
        
1031
 
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
1032
 
        without using this convenience method, because of our use of global state.
1033
 
        """
1034
 
        old_root = TestCaseInTempDir.TEST_ROOT
1035
 
        try:
1036
 
            TestCaseInTempDir.TEST_ROOT = None
1037
 
            return testrunner.run(test)
1038
 
        finally:
1039
 
            TestCaseInTempDir.TEST_ROOT = old_root
1040
 
 
1041
 
    def test_known_failure_failed_run(self):
1042
 
        # run a test that generates a known failure which should be printed in
1043
 
        # the final output when real failures occur.
1044
 
        def known_failure_test():
1045
 
            raise KnownFailure('failed')
1046
 
        test = unittest.TestSuite()
1047
 
        test.addTest(unittest.FunctionTestCase(known_failure_test))
1048
 
        def failing_test():
1049
 
            raise AssertionError('foo')
1050
 
        test.addTest(unittest.FunctionTestCase(failing_test))
1051
 
        stream = StringIO()
1052
 
        runner = TextTestRunner(stream=stream)
1053
 
        result = self.run_test_runner(runner, test)
1054
 
        lines = stream.getvalue().splitlines()
1055
 
        self.assertEqual([
1056
 
            '',
1057
 
            '======================================================================',
1058
 
            'FAIL: unittest.FunctionTestCase (failing_test)',
1059
 
            '----------------------------------------------------------------------',
1060
 
            'Traceback (most recent call last):',
1061
 
            '    raise AssertionError(\'foo\')',
1062
 
            'AssertionError: foo',
1063
 
            '',
1064
 
            '----------------------------------------------------------------------',
1065
 
            '',
1066
 
            'FAILED (failures=1, known_failure_count=1)'],
1067
 
            lines[0:5] + lines[6:10] + lines[11:])
1068
 
 
1069
 
    def test_known_failure_ok_run(self):
1070
 
        # run a test that generates a known failure which should be printed in the final output.
1071
 
        def known_failure_test():
1072
 
            raise KnownFailure('failed')
1073
 
        test = unittest.FunctionTestCase(known_failure_test)
1074
 
        stream = StringIO()
1075
 
        runner = TextTestRunner(stream=stream)
1076
 
        result = self.run_test_runner(runner, test)
1077
 
        self.assertContainsRe(stream.getvalue(),
1078
 
            '\n'
1079
 
            '-*\n'
1080
 
            'Ran 1 test in .*\n'
1081
 
            '\n'
1082
 
            'OK \\(known_failures=1\\)\n')
1083
 
 
1084
 
    def test_skipped_test(self):
1085
 
        # run a test that is skipped, and check the suite as a whole still
1086
 
        # succeeds.
1087
 
        # skipping_test must be hidden in here so it's not run as a real test
1088
 
        def skipping_test():
1089
 
            raise TestSkipped('test intentionally skipped')
1090
 
 
1091
 
        runner = TextTestRunner(stream=self._log_file)
1092
 
        test = unittest.FunctionTestCase(skipping_test)
1093
 
        result = self.run_test_runner(runner, test)
1094
 
        self.assertTrue(result.wasSuccessful())
1095
 
 
1096
 
    def test_skipped_from_setup(self):
1097
 
        class SkippedSetupTest(TestCase):
1098
 
 
1099
 
            def setUp(self):
1100
 
                self.counter = 1
1101
 
                self.addCleanup(self.cleanup)
1102
 
                raise TestSkipped('skipped setup')
1103
 
 
1104
 
            def test_skip(self):
1105
 
                self.fail('test reached')
1106
 
 
1107
 
            def cleanup(self):
1108
 
                self.counter -= 1
1109
 
 
1110
 
        runner = TextTestRunner(stream=self._log_file)
1111
 
        test = SkippedSetupTest('test_skip')
1112
 
        result = self.run_test_runner(runner, test)
1113
 
        self.assertTrue(result.wasSuccessful())
1114
 
        # Check if cleanup was called the right number of times.
1115
 
        self.assertEqual(0, test.counter)
1116
 
 
1117
 
    def test_skipped_from_test(self):
1118
 
        class SkippedTest(TestCase):
1119
 
 
1120
 
            def setUp(self):
1121
 
                self.counter = 1
1122
 
                self.addCleanup(self.cleanup)
1123
 
 
1124
 
            def test_skip(self):
1125
 
                raise TestSkipped('skipped test')
1126
 
 
1127
 
            def cleanup(self):
1128
 
                self.counter -= 1
1129
 
 
1130
 
        runner = TextTestRunner(stream=self._log_file)
1131
 
        test = SkippedTest('test_skip')
1132
 
        result = self.run_test_runner(runner, test)
1133
 
        self.assertTrue(result.wasSuccessful())
1134
 
        # Check if cleanup was called the right number of times.
1135
 
        self.assertEqual(0, test.counter)
1136
 
 
1137
 
    def test_unsupported_features_listed(self):
1138
 
        """When unsupported features are encountered they are detailed."""
1139
 
        class Feature1(Feature):
1140
 
            def _probe(self): return False
1141
 
        class Feature2(Feature):
1142
 
            def _probe(self): return False
1143
 
        # create sample tests
1144
 
        test1 = SampleTestCase('_test_pass')
1145
 
        test1._test_needs_features = [Feature1()]
1146
 
        test2 = SampleTestCase('_test_pass')
1147
 
        test2._test_needs_features = [Feature2()]
1148
 
        test = unittest.TestSuite()
1149
 
        test.addTest(test1)
1150
 
        test.addTest(test2)
1151
 
        stream = StringIO()
1152
 
        runner = TextTestRunner(stream=stream)
1153
 
        result = self.run_test_runner(runner, test)
1154
 
        lines = stream.getvalue().splitlines()
1155
 
        self.assertEqual([
1156
 
            'OK',
1157
 
            "Missing feature 'Feature1' skipped 1 tests.",
1158
 
            "Missing feature 'Feature2' skipped 1 tests.",
1159
 
            ],
1160
 
            lines[-3:])
1161
 
 
1162
 
    def test_bench_history(self):
1163
 
        # tests that the running the benchmark produces a history file
1164
 
        # containing a timestamp and the revision id of the bzrlib source which
1165
 
        # was tested.
1166
 
        workingtree = _get_bzr_source_tree()
1167
 
        test = TestRunner('dummy_test')
1168
 
        output = StringIO()
1169
 
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
1170
 
        result = self.run_test_runner(runner, test)
1171
 
        output_string = output.getvalue()
1172
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
1173
 
        if workingtree is not None:
1174
 
            revision_id = workingtree.get_parent_ids()[0]
1175
 
            self.assertEndsWith(output_string.rstrip(), revision_id)
1176
 
 
1177
 
    def test_success_log_deleted(self):
1178
 
        """Successful tests have their log deleted"""
1179
 
 
1180
 
        class LogTester(TestCase):
1181
 
 
1182
 
            def test_success(self):
1183
 
                self.log('this will be removed\n')
1184
 
 
1185
 
        sio = cStringIO.StringIO()
1186
 
        runner = TextTestRunner(stream=sio)
1187
 
        test = LogTester('test_success')
1188
 
        result = self.run_test_runner(runner, test)
1189
 
 
1190
 
        log = test._get_log()
1191
 
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1192
 
        self.assertEqual('', test._log_contents)
1193
 
        self.assertIs(None, test._log_file_name)
1194
 
 
1195
 
    def test_fail_log_kept(self):
1196
 
        """Failed tests have their log kept"""
1197
 
 
1198
 
        class LogTester(TestCase):
1199
 
 
1200
 
            def test_fail(self):
1201
 
                self.log('this will be kept\n')
1202
 
                self.fail('this test fails')
1203
 
 
1204
 
        sio = cStringIO.StringIO()
1205
 
        runner = TextTestRunner(stream=sio)
1206
 
        test = LogTester('test_fail')
1207
 
        result = self.run_test_runner(runner, test)
1208
 
 
1209
 
        text = sio.getvalue()
1210
 
        self.assertContainsRe(text, 'this will be kept')
1211
 
        self.assertContainsRe(text, 'this test fails')
1212
 
 
1213
 
        log = test._get_log()
1214
 
        self.assertContainsRe(log, 'this will be kept')
1215
 
        self.assertEqual(log, test._log_contents)
1216
 
 
1217
 
    def test_error_log_kept(self):
1218
 
        """Tests with errors have their log kept"""
1219
 
 
1220
 
        class LogTester(TestCase):
1221
 
 
1222
 
            def test_error(self):
1223
 
                self.log('this will be kept\n')
1224
 
                raise ValueError('random exception raised')
1225
 
 
1226
 
        sio = cStringIO.StringIO()
1227
 
        runner = TextTestRunner(stream=sio)
1228
 
        test = LogTester('test_error')
1229
 
        result = self.run_test_runner(runner, test)
1230
 
 
1231
 
        text = sio.getvalue()
1232
 
        self.assertContainsRe(text, 'this will be kept')
1233
 
        self.assertContainsRe(text, 'random exception raised')
1234
 
 
1235
 
        log = test._get_log()
1236
 
        self.assertContainsRe(log, 'this will be kept')
1237
 
        self.assertEqual(log, test._log_contents)
1238
 
 
1239
 
 
1240
 
class SampleTestCase(TestCase):
1241
 
 
1242
 
    def _test_pass(self):
1243
 
        pass
1244
 
 
1245
 
 
1246
 
class TestTestCase(TestCase):
1247
 
    """Tests that test the core bzrlib TestCase."""
1248
 
 
1249
 
    def test_debug_flags_sanitised(self):
1250
 
        """The bzrlib debug flags should be sanitised by setUp."""
1251
 
        # we could set something and run a test that will check
1252
 
        # it gets santised, but this is probably sufficient for now:
1253
 
        # if someone runs the test with -Dsomething it will error.
1254
 
        self.assertEqual(set(), bzrlib.debug.debug_flags)
1255
 
 
1256
 
    def inner_test(self):
1257
 
        # the inner child test
1258
 
        note("inner_test")
1259
 
 
1260
 
    def outer_child(self):
1261
 
        # the outer child test
1262
 
        note("outer_start")
1263
 
        self.inner_test = TestTestCase("inner_child")
1264
 
        result = bzrlib.tests.TextTestResult(self._log_file,
1265
 
                                        descriptions=0,
1266
 
                                        verbosity=1)
1267
 
        self.inner_test.run(result)
1268
 
        note("outer finish")
1269
 
 
1270
 
    def test_trace_nesting(self):
1271
 
        # this tests that each test case nests its trace facility correctly.
1272
 
        # we do this by running a test case manually. That test case (A)
1273
 
        # should setup a new log, log content to it, setup a child case (B),
1274
 
        # which should log independently, then case (A) should log a trailer
1275
 
        # and return.
1276
 
        # we do two nested children so that we can verify the state of the 
1277
 
        # logs after the outer child finishes is correct, which a bad clean
1278
 
        # up routine in tearDown might trigger a fault in our test with only
1279
 
        # one child, we should instead see the bad result inside our test with
1280
 
        # the two children.
1281
 
        # the outer child test
1282
 
        original_trace = bzrlib.trace._trace_file
1283
 
        outer_test = TestTestCase("outer_child")
1284
 
        result = bzrlib.tests.TextTestResult(self._log_file,
1285
 
                                        descriptions=0,
1286
 
                                        verbosity=1)
1287
 
        outer_test.run(result)
1288
 
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
1289
 
 
1290
 
    def method_that_times_a_bit_twice(self):
1291
 
        # call self.time twice to ensure it aggregates
1292
 
        self.time(time.sleep, 0.007)
1293
 
        self.time(time.sleep, 0.007)
1294
 
 
1295
 
    def test_time_creates_benchmark_in_result(self):
1296
 
        """Test that the TestCase.time() method accumulates a benchmark time."""
1297
 
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1298
 
        output_stream = StringIO()
1299
 
        result = bzrlib.tests.VerboseTestResult(
1300
 
            unittest._WritelnDecorator(output_stream),
1301
 
            descriptions=0,
1302
 
            verbosity=2,
1303
 
            num_tests=sample_test.countTestCases())
1304
 
        sample_test.run(result)
1305
 
        self.assertContainsRe(
1306
 
            output_stream.getvalue(),
1307
 
            r"\d+ms/ +\d+ms\n$")
1308
 
 
1309
 
    def test_hooks_sanitised(self):
1310
 
        """The bzrlib hooks should be sanitised by setUp."""
1311
 
        self.assertEqual(bzrlib.branch.BranchHooks(),
1312
 
            bzrlib.branch.Branch.hooks)
1313
 
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1314
 
            bzrlib.smart.server.SmartTCPServer.hooks)
1315
 
 
1316
 
    def test__gather_lsprof_in_benchmarks(self):
1317
 
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1318
 
        
1319
 
        Each self.time() call is individually and separately profiled.
1320
 
        """
1321
 
        self.requireFeature(test_lsprof.LSProfFeature)
1322
 
        # overrides the class member with an instance member so no cleanup 
1323
 
        # needed.
1324
 
        self._gather_lsprof_in_benchmarks = True
1325
 
        self.time(time.sleep, 0.000)
1326
 
        self.time(time.sleep, 0.003)
1327
 
        self.assertEqual(2, len(self._benchcalls))
1328
 
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1329
 
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1330
 
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1331
 
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1332
 
 
1333
 
    def test_knownFailure(self):
1334
 
        """Self.knownFailure() should raise a KnownFailure exception."""
1335
 
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1336
 
 
1337
 
    def test_requireFeature_available(self):
1338
 
        """self.requireFeature(available) is a no-op."""
1339
 
        class Available(Feature):
1340
 
            def _probe(self):return True
1341
 
        feature = Available()
1342
 
        self.requireFeature(feature)
1343
 
 
1344
 
    def test_requireFeature_unavailable(self):
1345
 
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1346
 
        class Unavailable(Feature):
1347
 
            def _probe(self):return False
1348
 
        feature = Unavailable()
1349
 
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1350
 
 
1351
 
    def test_run_no_parameters(self):
1352
 
        test = SampleTestCase('_test_pass')
1353
 
        test.run()
1354
 
    
1355
 
    def test_run_enabled_unittest_result(self):
1356
 
        """Test we revert to regular behaviour when the test is enabled."""
1357
 
        test = SampleTestCase('_test_pass')
1358
 
        class EnabledFeature(object):
1359
 
            def available(self):
1360
 
                return True
1361
 
        test._test_needs_features = [EnabledFeature()]
1362
 
        result = unittest.TestResult()
1363
 
        test.run(result)
1364
 
        self.assertEqual(1, result.testsRun)
1365
 
        self.assertEqual([], result.errors)
1366
 
        self.assertEqual([], result.failures)
1367
 
 
1368
 
    def test_run_disabled_unittest_result(self):
1369
 
        """Test our compatability for disabled tests with unittest results."""
1370
 
        test = SampleTestCase('_test_pass')
1371
 
        class DisabledFeature(object):
1372
 
            def available(self):
1373
 
                return False
1374
 
        test._test_needs_features = [DisabledFeature()]
1375
 
        result = unittest.TestResult()
1376
 
        test.run(result)
1377
 
        self.assertEqual(1, result.testsRun)
1378
 
        self.assertEqual([], result.errors)
1379
 
        self.assertEqual([], result.failures)
1380
 
 
1381
 
    def test_run_disabled_supporting_result(self):
1382
 
        """Test disabled tests behaviour with support aware results."""
1383
 
        test = SampleTestCase('_test_pass')
1384
 
        class DisabledFeature(object):
1385
 
            def available(self):
1386
 
                return False
1387
 
        the_feature = DisabledFeature()
1388
 
        test._test_needs_features = [the_feature]
1389
 
        class InstrumentedTestResult(unittest.TestResult):
1390
 
            def __init__(self):
1391
 
                unittest.TestResult.__init__(self)
1392
 
                self.calls = []
1393
 
            def startTest(self, test):
1394
 
                self.calls.append(('startTest', test))
1395
 
            def stopTest(self, test):
1396
 
                self.calls.append(('stopTest', test))
1397
 
            def addNotSupported(self, test, feature):
1398
 
                self.calls.append(('addNotSupported', test, feature))
1399
 
        result = InstrumentedTestResult()
1400
 
        test.run(result)
1401
 
        self.assertEqual([
1402
 
            ('startTest', test),
1403
 
            ('addNotSupported', test, the_feature),
1404
 
            ('stopTest', test),
1405
 
            ],
1406
 
            result.calls)
1407
 
 
1408
 
 
1409
 
@symbol_versioning.deprecated_function(zero_eleven)
1410
 
def sample_deprecated_function():
1411
 
    """A deprecated function to test applyDeprecated with."""
1412
 
    return 2
1413
 
 
1414
 
 
1415
 
def sample_undeprecated_function(a_param):
1416
 
    """A undeprecated function to test applyDeprecated with."""
1417
 
 
1418
 
 
1419
 
class ApplyDeprecatedHelper(object):
1420
 
    """A helper class for ApplyDeprecated tests."""
1421
 
 
1422
 
    @symbol_versioning.deprecated_method(zero_eleven)
1423
 
    def sample_deprecated_method(self, param_one):
1424
 
        """A deprecated method for testing with."""
1425
 
        return param_one
1426
 
 
1427
 
    def sample_normal_method(self):
1428
 
        """A undeprecated method."""
1429
 
 
1430
 
    @symbol_versioning.deprecated_method(zero_ten)
1431
 
    def sample_nested_deprecation(self):
1432
 
        return sample_deprecated_function()
1433
 
 
1434
 
 
1435
 
class TestExtraAssertions(TestCase):
1436
 
    """Tests for new test assertions in bzrlib test suite"""
1437
 
 
1438
 
    def test_assert_isinstance(self):
1439
 
        self.assertIsInstance(2, int)
1440
 
        self.assertIsInstance(u'', basestring)
1441
 
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1442
 
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1443
 
 
1444
 
    def test_assertEndsWith(self):
1445
 
        self.assertEndsWith('foo', 'oo')
1446
 
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1447
 
 
1448
 
    def test_applyDeprecated_not_deprecated(self):
1449
 
        sample_object = ApplyDeprecatedHelper()
1450
 
        # calling an undeprecated callable raises an assertion
1451
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1452
 
            sample_object.sample_normal_method)
1453
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1454
 
            sample_undeprecated_function, "a param value")
1455
 
        # calling a deprecated callable (function or method) with the wrong
1456
 
        # expected deprecation fails.
1457
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1458
 
            sample_object.sample_deprecated_method, "a param value")
1459
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1460
 
            sample_deprecated_function)
1461
 
        # calling a deprecated callable (function or method) with the right
1462
 
        # expected deprecation returns the functions result.
1463
 
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1464
 
            sample_object.sample_deprecated_method, "a param value"))
1465
 
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
1466
 
            sample_deprecated_function))
1467
 
        # calling a nested deprecation with the wrong deprecation version
1468
 
        # fails even if a deeper nested function was deprecated with the 
1469
 
        # supplied version.
1470
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1471
 
            zero_eleven, sample_object.sample_nested_deprecation)
1472
 
        # calling a nested deprecation with the right deprecation value
1473
 
        # returns the calls result.
1474
 
        self.assertEqual(2, self.applyDeprecated(zero_ten,
1475
 
            sample_object.sample_nested_deprecation))
1476
 
 
1477
 
    def test_callDeprecated(self):
1478
 
        def testfunc(be_deprecated, result=None):
1479
 
            if be_deprecated is True:
1480
 
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
1481
 
                                       stacklevel=1)
1482
 
            return result
1483
 
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1484
 
        self.assertIs(None, result)
1485
 
        result = self.callDeprecated([], testfunc, False, 'result')
1486
 
        self.assertEqual('result', result)
1487
 
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1488
 
        self.callDeprecated([], testfunc, be_deprecated=False)
1489
 
 
1490
 
 
1491
 
class TestConvenienceMakers(TestCaseWithTransport):
1492
 
    """Test for the make_* convenience functions."""
1493
 
 
1494
 
    def test_make_branch_and_tree_with_format(self):
1495
 
        # we should be able to supply a format to make_branch_and_tree
1496
 
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1497
 
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1498
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1499
 
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1500
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1501
 
                              bzrlib.bzrdir.BzrDirFormat6)
1502
 
 
1503
 
    def test_make_branch_and_memory_tree(self):
1504
 
        # we should be able to get a new branch and a mutable tree from
1505
 
        # TestCaseWithTransport
1506
 
        tree = self.make_branch_and_memory_tree('a')
1507
 
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1508
 
 
1509
 
 
1510
 
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1511
 
 
1512
 
    def test_make_tree_for_sftp_branch(self):
1513
 
        """Transports backed by local directories create local trees."""
1514
 
 
1515
 
        tree = self.make_branch_and_tree('t1')
1516
 
        base = tree.bzrdir.root_transport.base
1517
 
        self.failIf(base.startswith('sftp'),
1518
 
                'base %r is on sftp but should be local' % base)
1519
 
        self.assertEquals(tree.bzrdir.root_transport,
1520
 
                tree.branch.bzrdir.root_transport)
1521
 
        self.assertEquals(tree.bzrdir.root_transport,
1522
 
                tree.branch.repository.bzrdir.root_transport)
1523
 
 
1524
 
 
1525
 
class TestSelftest(TestCase):
1526
 
    """Tests of bzrlib.tests.selftest."""
1527
 
 
1528
 
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1529
 
        factory_called = []
1530
 
        def factory():
1531
 
            factory_called.append(True)
1532
 
            return TestSuite()
1533
 
        out = StringIO()
1534
 
        err = StringIO()
1535
 
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1536
 
            test_suite_factory=factory)
1537
 
        self.assertEqual([True], factory_called)
1538
 
 
1539
 
 
1540
 
class TestKnownFailure(TestCase):
1541
 
 
1542
 
    def test_known_failure(self):
1543
 
        """Check that KnownFailure is defined appropriately."""
1544
 
        # a KnownFailure is an assertion error for compatability with unaware
1545
 
        # runners.
1546
 
        self.assertIsInstance(KnownFailure(""), AssertionError)
1547
 
 
1548
 
    def test_expect_failure(self):
1549
 
        try:
1550
 
            self.expectFailure("Doomed to failure", self.assertTrue, False)
1551
 
        except KnownFailure, e:
1552
 
            self.assertEqual('Doomed to failure', e.args[0])
1553
 
        try:
1554
 
            self.expectFailure("Doomed to failure", self.assertTrue, True)
1555
 
        except AssertionError, e:
1556
 
            self.assertEqual('Unexpected success.  Should have failed:'
1557
 
                             ' Doomed to failure', e.args[0])
1558
 
        else:
1559
 
            self.fail('Assertion not raised')
1560
 
 
1561
 
 
1562
 
class TestFeature(TestCase):
1563
 
 
1564
 
    def test_caching(self):
1565
 
        """Feature._probe is called by the feature at most once."""
1566
 
        class InstrumentedFeature(Feature):
1567
 
            def __init__(self):
1568
 
                Feature.__init__(self)
1569
 
                self.calls = []
1570
 
            def _probe(self):
1571
 
                self.calls.append('_probe')
1572
 
                return False
1573
 
        feature = InstrumentedFeature()
1574
 
        feature.available()
1575
 
        self.assertEqual(['_probe'], feature.calls)
1576
 
        feature.available()
1577
 
        self.assertEqual(['_probe'], feature.calls)
1578
 
 
1579
 
    def test_named_str(self):
1580
 
        """Feature.__str__ should thunk to feature_name()."""
1581
 
        class NamedFeature(Feature):
1582
 
            def feature_name(self):
1583
 
                return 'symlinks'
1584
 
        feature = NamedFeature()
1585
 
        self.assertEqual('symlinks', str(feature))
1586
 
 
1587
 
    def test_default_str(self):
1588
 
        """Feature.__str__ should default to __class__.__name__."""
1589
 
        class NamedFeature(Feature):
1590
 
            pass
1591
 
        feature = NamedFeature()
1592
 
        self.assertEqual('NamedFeature', str(feature))
1593
 
 
1594
 
 
1595
 
class TestUnavailableFeature(TestCase):
1596
 
 
1597
 
    def test_access_feature(self):
1598
 
        feature = Feature()
1599
 
        exception = UnavailableFeature(feature)
1600
 
        self.assertIs(feature, exception.args[0])
1601
 
 
1602
 
 
1603
 
class TestSelftestFiltering(TestCase):
1604
 
 
1605
 
    def setUp(self):
1606
 
        self.suite = TestUtil.TestSuite()
1607
 
        self.loader = TestUtil.TestLoader()
1608
 
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1609
 
            'bzrlib.tests.test_selftest']))
1610
 
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
1611
 
 
1612
 
    def test_filter_suite_by_re(self):
1613
 
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1614
 
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
1615
 
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1616
 
            'TestSelftestFiltering.test_filter_suite_by_re'])
1617
 
            
1618
 
    def test_sort_suite_by_re(self):
1619
 
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1620
 
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
1621
 
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1622
 
            'TestSelftestFiltering.test_filter_suite_by_re')
1623
 
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1624
 
 
1625
 
 
1626
 
class TestCheckInventoryShape(TestCaseWithTransport):
1627
 
 
1628
 
    def test_check_inventory_shape(self):
1629
 
        files = ['a', 'b/', 'b/c']
1630
 
        tree = self.make_branch_and_tree('.')
1631
 
        self.build_tree(files)
1632
 
        tree.add(files)
1633
 
        tree.lock_read()
1634
 
        try:
1635
 
            self.check_inventory_shape(tree.inventory, files)
1636
 
        finally:
1637
 
            tree.unlock()