~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2007-11-13 20:37:09 UTC
  • mto: This revision was merged to the branch mainline in revision 3001.
  • Revision ID: john@arbash-meinel.com-20071113203709-kysdte0emqv84pnj
Fix bug #162486, by having RemoteBranch properly initialize self._revision_id_to_revno_map.

Show diffs side-by-side

added added

removed removed

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