1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
17
"""Tests for the test framework."""
 
 
21
from StringIO import StringIO
 
 
36
from bzrlib.progress import _BaseProgressBar
 
 
37
from bzrlib.repofmt import weaverepo
 
 
38
from bzrlib.symbol_versioning import zero_ten, zero_eleven
 
 
39
from bzrlib.tests import (
 
 
46
                          TestCaseWithMemoryTransport,
 
 
47
                          TestCaseWithTransport,
 
 
59
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
 
60
from bzrlib.tests.TestUtil import _load_module_by_name
 
 
61
from bzrlib.trace import note
 
 
62
from bzrlib.transport.memory import MemoryServer, MemoryTransport
 
 
63
from bzrlib.version import _get_bzr_source_tree
 
 
66
class SelftestTests(TestCase):
 
 
68
    def test_import_tests(self):
 
 
69
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
 
 
70
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
 
72
    def test_import_test_failure(self):
 
 
73
        self.assertRaises(ImportError,
 
 
77
class MetaTestLog(TestCase):
 
 
79
    def test_logging(self):
 
 
80
        """Test logs are captured when a test fails."""
 
 
81
        self.log('a test message')
 
 
82
        self._log_file.flush()
 
 
83
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
 
87
class TestTreeShape(TestCaseInTempDir):
 
 
89
    def test_unicode_paths(self):
 
 
90
        filename = u'hell\u00d8'
 
 
92
            self.build_tree_contents([(filename, 'contents of hello')])
 
 
93
        except UnicodeEncodeError:
 
 
94
            raise TestSkipped("can't build unicode working tree in "
 
 
95
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
 
96
        self.failUnlessExists(filename)
 
 
99
class TestTransportProviderAdapter(TestCase):
 
 
100
    """A group of tests that test the transport implementation adaption core.
 
 
102
    This is a meta test that the tests are applied to all available 
 
 
105
    This will be generalised in the future which is why it is in this 
 
 
106
    test file even though it is specific to transport tests at the moment.
 
 
109
    def test_get_transport_permutations(self):
 
 
110
        # this checks that we the module get_test_permutations call
 
 
111
        # is made by the adapter get_transport_test_permitations method.
 
 
112
        class MockModule(object):
 
 
113
            def get_test_permutations(self):
 
 
114
                return sample_permutation
 
 
115
        sample_permutation = [(1,2), (3,4)]
 
 
116
        from bzrlib.tests.test_transport_implementations \
 
 
117
            import TransportTestProviderAdapter
 
 
118
        adapter = TransportTestProviderAdapter()
 
 
119
        self.assertEqual(sample_permutation,
 
 
120
                         adapter.get_transport_test_permutations(MockModule()))
 
 
122
    def test_adapter_checks_all_modules(self):
 
 
123
        # this checks that the adapter returns as many permurtations as
 
 
124
        # there are in all the registered# transport modules for there
 
 
125
        # - we assume if this matches its probably doing the right thing
 
 
126
        # especially in combination with the tests for setting the right
 
 
128
        from bzrlib.tests.test_transport_implementations \
 
 
129
            import TransportTestProviderAdapter
 
 
130
        from bzrlib.transport import _get_transport_modules
 
 
131
        modules = _get_transport_modules()
 
 
132
        permutation_count = 0
 
 
133
        for module in modules:
 
 
135
                permutation_count += len(reduce(getattr, 
 
 
136
                    (module + ".get_test_permutations").split('.')[1:],
 
 
137
                     __import__(module))())
 
 
138
            except errors.DependencyNotPresent:
 
 
140
        input_test = TestTransportProviderAdapter(
 
 
141
            "test_adapter_sets_transport_class")
 
 
142
        adapter = TransportTestProviderAdapter()
 
 
143
        self.assertEqual(permutation_count,
 
 
144
                         len(list(iter(adapter.adapt(input_test)))))
 
 
146
    def test_adapter_sets_transport_class(self):
 
 
147
        # Check that the test adapter inserts a transport and server into the
 
 
150
        # This test used to know about all the possible transports and the
 
 
151
        # order they were returned but that seems overly brittle (mbp
 
 
153
        from bzrlib.tests.test_transport_implementations \
 
 
154
            import TransportTestProviderAdapter
 
 
155
        scenarios = TransportTestProviderAdapter().scenarios
 
 
156
        # there are at least that many builtin transports
 
 
157
        self.assertTrue(len(scenarios) > 6)
 
 
158
        one_scenario = scenarios[0]
 
 
159
        self.assertIsInstance(one_scenario[0], str)
 
 
160
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
 
 
161
                                   bzrlib.transport.Transport))
 
 
162
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
 
 
163
                                   bzrlib.transport.Server))
 
 
166
class TestBranchProviderAdapter(TestCase):
 
 
167
    """A group of tests that test the branch implementation test adapter."""
 
 
169
    def test_constructor(self):
 
 
170
        # check that constructor parameters are passed through to the adapted
 
 
172
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
 
 
175
        formats = [("c", "C"), ("d", "D")]
 
 
176
        adapter = BranchTestProviderAdapter(server1, server2, formats)
 
 
177
        self.assertEqual(2, len(adapter.scenarios))
 
 
180
             {'branch_format': 'c',
 
 
181
              'bzrdir_format': 'C',
 
 
182
              'transport_readonly_server': 'b',
 
 
183
              'transport_server': 'a'}),
 
 
185
             {'branch_format': 'd',
 
 
186
              'bzrdir_format': 'D',
 
 
187
              'transport_readonly_server': 'b',
 
 
188
              'transport_server': 'a'})],
 
 
192
class TestBzrDirProviderAdapter(TestCase):
 
 
193
    """A group of tests that test the bzr dir implementation test adapter."""
 
 
195
    def test_adapted_tests(self):
 
 
196
        # check that constructor parameters are passed through to the adapted
 
 
198
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
 
 
203
        adapter = BzrDirTestProviderAdapter(vfs_factory,
 
 
204
            server1, server2, formats)
 
 
207
             {'bzrdir_format': 'c',
 
 
208
              'transport_readonly_server': 'b',
 
 
209
              'transport_server': 'a',
 
 
210
              'vfs_transport_factory': 'v'}),
 
 
212
             {'bzrdir_format': 'd',
 
 
213
              'transport_readonly_server': 'b',
 
 
214
              'transport_server': 'a',
 
 
215
              'vfs_transport_factory': 'v'})],
 
 
219
class TestRepositoryProviderAdapter(TestCase):
 
 
220
    """A group of tests that test the repository implementation test adapter."""
 
 
222
    def test_constructor(self):
 
 
223
        # check that constructor parameters are passed through to the
 
 
225
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
 
228
        formats = [("c", "C"), ("d", "D")]
 
 
229
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
 
 
232
             {'bzrdir_format': 'C',
 
 
233
              'repository_format': 'c',
 
 
234
              'transport_readonly_server': 'b',
 
 
235
              'transport_server': 'a'}),
 
 
237
             {'bzrdir_format': 'D',
 
 
238
              'repository_format': 'd',
 
 
239
              'transport_readonly_server': 'b',
 
 
240
              'transport_server': 'a'})],
 
 
243
    def test_setting_vfs_transport(self):
 
 
244
        """The vfs_transport_factory can be set optionally."""
 
 
245
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
 
246
        formats = [("a", "b"), ("c", "d")]
 
 
247
        adapter = RepositoryTestProviderAdapter(None, None, formats,
 
 
248
            vfs_transport_factory="vfs")
 
 
251
             {'bzrdir_format': 'b',
 
 
252
              'repository_format': 'a',
 
 
253
              'transport_readonly_server': None,
 
 
254
              'transport_server': None,
 
 
255
              'vfs_transport_factory': 'vfs'}),
 
 
257
             {'bzrdir_format': 'd',
 
 
258
              'repository_format': 'c',
 
 
259
              'transport_readonly_server': None,
 
 
260
              'transport_server': None,
 
 
261
              'vfs_transport_factory': 'vfs'})],
 
 
264
    def test_formats_to_scenarios(self):
 
 
265
        """The adapter can generate all the scenarios needed."""
 
 
266
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
 
267
        no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
 
 
269
        vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
 
 
270
            [], vfs_transport_factory="vfs")
 
 
271
        # no_vfs generate scenarios without vfs_transport_factor
 
 
272
        formats = [("c", "C"), (1, "D")]
 
 
275
             {'bzrdir_format': 'C',
 
 
276
              'repository_format': 'c',
 
 
277
              'transport_readonly_server': 'readonly',
 
 
278
              'transport_server': 'server'}),
 
 
280
             {'bzrdir_format': 'D',
 
 
281
              'repository_format': 1,
 
 
282
              'transport_readonly_server': 'readonly',
 
 
283
              'transport_server': 'server'})],
 
 
284
            no_vfs_adapter.formats_to_scenarios(formats))
 
 
287
             {'bzrdir_format': 'C',
 
 
288
              'repository_format': 'c',
 
 
289
              'transport_readonly_server': 'readonly',
 
 
290
              'transport_server': 'server',
 
 
291
              'vfs_transport_factory': 'vfs'}),
 
 
293
             {'bzrdir_format': 'D',
 
 
294
              'repository_format': 1,
 
 
295
              'transport_readonly_server': 'readonly',
 
 
296
              'transport_server': 'server',
 
 
297
              'vfs_transport_factory': 'vfs'})],
 
 
298
            vfs_adapter.formats_to_scenarios(formats))
 
 
301
class TestTestScenarioApplier(TestCase):
 
 
302
    """Tests for the test adaption facilities."""
 
 
304
    def test_adapt_applies_scenarios(self):
 
 
305
        from bzrlib.tests.repository_implementations import TestScenarioApplier
 
 
306
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
 
 
307
        adapter = TestScenarioApplier()
 
 
308
        adapter.scenarios = [("1", "dict"), ("2", "settings")]
 
 
310
        def capture_call(test, scenario):
 
 
311
            calls.append((test, scenario))
 
 
313
        adapter.adapt_test_to_scenario = capture_call
 
 
314
        adapter.adapt(input_test)
 
 
315
        self.assertEqual([(input_test, ("1", "dict")),
 
 
316
            (input_test, ("2", "settings"))], calls)
 
 
318
    def test_adapt_test_to_scenario(self):
 
 
319
        from bzrlib.tests.repository_implementations import TestScenarioApplier
 
 
320
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
 
 
321
        adapter = TestScenarioApplier()
 
 
322
        # setup two adapted tests
 
 
323
        adapted_test1 = adapter.adapt_test_to_scenario(input_test,
 
 
325
            {"bzrdir_format":"bzr_format",
 
 
326
             "repository_format":"repo_fmt",
 
 
327
             "transport_server":"transport_server",
 
 
328
             "transport_readonly_server":"readonly-server"}))
 
 
329
        adapted_test2 = adapter.adapt_test_to_scenario(input_test,
 
 
330
            ("new id 2", {"bzrdir_format":None}))
 
 
331
        # input_test should have been altered.
 
 
332
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
 
 
333
        # the new tests are mutually incompatible, ensuring it has 
 
 
334
        # made new ones, and unspecified elements in the scenario
 
 
335
        # should not have been altered.
 
 
336
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
 
 
337
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
 
 
338
        self.assertEqual("transport_server", adapted_test1.transport_server)
 
 
339
        self.assertEqual("readonly-server",
 
 
340
            adapted_test1.transport_readonly_server)
 
 
342
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
 
343
            "test_adapt_test_to_scenario(new id)",
 
 
345
        self.assertEqual(None, adapted_test2.bzrdir_format)
 
 
347
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
 
348
            "test_adapt_test_to_scenario(new id 2)",
 
 
352
class TestInterRepositoryProviderAdapter(TestCase):
 
 
353
    """A group of tests that test the InterRepository test adapter."""
 
 
355
    def test_adapted_tests(self):
 
 
356
        # check that constructor parameters are passed through to the adapted
 
 
358
        from bzrlib.tests.interrepository_implementations import \
 
 
359
            InterRepositoryTestProviderAdapter
 
 
362
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
 
363
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
 
 
366
             {'interrepo_class': str,
 
 
367
              'repository_format': 'C1',
 
 
368
              'repository_format_to': 'C2',
 
 
369
              'transport_readonly_server': 'b',
 
 
370
              'transport_server': 'a'}),
 
 
372
             {'interrepo_class': int,
 
 
373
              'repository_format': 'D1',
 
 
374
              'repository_format_to': 'D2',
 
 
375
              'transport_readonly_server': 'b',
 
 
376
              'transport_server': 'a'})],
 
 
377
            adapter.formats_to_scenarios(formats))
 
 
380
class TestInterVersionedFileProviderAdapter(TestCase):
 
 
381
    """A group of tests that test the InterVersionedFile test adapter."""
 
 
383
    def test_scenarios(self):
 
 
384
        # check that constructor parameters are passed through to the adapted
 
 
386
        from bzrlib.tests.interversionedfile_implementations \
 
 
387
            import InterVersionedFileTestProviderAdapter
 
 
390
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
 
391
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
 
 
394
             {'interversionedfile_class':str,
 
 
395
              'transport_readonly_server': 'b',
 
 
396
              'transport_server': 'a',
 
 
397
              'versionedfile_factory': 'C1',
 
 
398
              'versionedfile_factory_to': 'C2'}),
 
 
400
             {'interversionedfile_class': int,
 
 
401
              'transport_readonly_server': 'b',
 
 
402
              'transport_server': 'a',
 
 
403
              'versionedfile_factory': 'D1',
 
 
404
              'versionedfile_factory_to': 'D2'})],
 
 
408
class TestRevisionStoreProviderAdapter(TestCase):
 
 
409
    """A group of tests that test the RevisionStore test adapter."""
 
 
411
    def test_scenarios(self):
 
 
412
        # check that constructor parameters are passed through to the adapted
 
 
414
        from bzrlib.tests.revisionstore_implementations \
 
 
415
            import RevisionStoreTestProviderAdapter
 
 
416
        # revision stores need a store factory - i.e. RevisionKnit
 
 
417
        #, a readonly and rw transport 
 
 
421
        store_factories = ["c", "d"]
 
 
422
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
 
 
425
             {'store_factory': 'c',
 
 
426
              'transport_readonly_server': 'b',
 
 
427
              'transport_server': 'a'}),
 
 
429
             {'store_factory': 'd',
 
 
430
              'transport_readonly_server': 'b',
 
 
431
              'transport_server': 'a'})],
 
 
435
class TestWorkingTreeProviderAdapter(TestCase):
 
 
436
    """A group of tests that test the workingtree implementation test adapter."""
 
 
438
    def test_scenarios(self):
 
 
439
        # check that constructor parameters are passed through to the adapted
 
 
441
        from bzrlib.tests.workingtree_implementations \
 
 
442
            import WorkingTreeTestProviderAdapter
 
 
445
        formats = [("c", "C"), ("d", "D")]
 
 
446
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
 
 
449
             {'bzrdir_format': 'C',
 
 
450
              'transport_readonly_server': 'b',
 
 
451
              'transport_server': 'a',
 
 
452
              'workingtree_format': 'c'}),
 
 
454
             {'bzrdir_format': 'D',
 
 
455
              'transport_readonly_server': 'b',
 
 
456
              'transport_server': 'a',
 
 
457
              'workingtree_format': 'd'})],
 
 
461
class TestTreeProviderAdapter(TestCase):
 
 
462
    """Test the setup of tree_implementation tests."""
 
 
464
    def test_adapted_tests(self):
 
 
465
        # the tree implementation adapter is meant to setup one instance for
 
 
466
        # each working tree format, and one additional instance that will
 
 
467
        # use the default wt format, but create a revision tree for the tests.
 
 
468
        # this means that the wt ones should have the workingtree_to_test_tree
 
 
469
        # attribute set to 'return_parameter' and the revision one set to
 
 
470
        # revision_tree_from_workingtree.
 
 
472
        from bzrlib.tests.tree_implementations import (
 
 
473
            TreeTestProviderAdapter,
 
 
475
            revision_tree_from_workingtree
 
 
477
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
 
 
478
        input_test = TestTreeProviderAdapter(
 
 
479
            "test_adapted_tests")
 
 
482
        formats = [("c", "C"), ("d", "D")]
 
 
483
        adapter = TreeTestProviderAdapter(server1, server2, formats)
 
 
484
        suite = adapter.adapt(input_test)
 
 
485
        tests = list(iter(suite))
 
 
486
        self.assertEqual(4, len(tests))
 
 
487
        # this must match the default format setp up in
 
 
488
        # TreeTestProviderAdapter.adapt
 
 
489
        default_format = WorkingTreeFormat3
 
 
490
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
 
491
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
 
492
        self.assertEqual(tests[0].transport_server, server1)
 
 
493
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
494
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
 
495
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
 
496
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
 
497
        self.assertEqual(tests[1].transport_server, server1)
 
 
498
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
499
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
 
500
        self.assertIsInstance(tests[2].workingtree_format, default_format)
 
 
501
        #self.assertEqual(tests[2].bzrdir_format,
 
 
502
        #                 default_format._matchingbzrdir)
 
 
503
        self.assertEqual(tests[2].transport_server, server1)
 
 
504
        self.assertEqual(tests[2].transport_readonly_server, server2)
 
 
505
        self.assertEqual(tests[2].workingtree_to_test_tree,
 
 
506
            revision_tree_from_workingtree)
 
 
509
class TestInterTreeProviderAdapter(TestCase):
 
 
510
    """A group of tests that test the InterTreeTestAdapter."""
 
 
512
    def test_adapted_tests(self):
 
 
513
        # check that constructor parameters are passed through to the adapted
 
 
515
        # for InterTree tests we want the machinery to bring up two trees in
 
 
516
        # each instance: the base one, and the one we are interacting with.
 
 
517
        # because each optimiser can be direction specific, we need to test
 
 
518
        # each optimiser in its chosen direction.
 
 
519
        # unlike the TestProviderAdapter we dont want to automatically add a
 
 
520
        # parameterised one for WorkingTree - the optimisers will tell us what
 
 
522
        from bzrlib.tests.tree_implementations import (
 
 
524
            revision_tree_from_workingtree
 
 
526
        from bzrlib.tests.intertree_implementations import (
 
 
527
            InterTreeTestProviderAdapter,
 
 
529
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
 
 
530
        input_test = TestInterTreeProviderAdapter(
 
 
531
            "test_adapted_tests")
 
 
534
        format1 = WorkingTreeFormat2()
 
 
535
        format2 = WorkingTreeFormat3()
 
 
536
        formats = [(str, format1, format2, "converter1"),
 
 
537
            (int, format2, format1, "converter2")]
 
 
538
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
 
 
539
        suite = adapter.adapt(input_test)
 
 
540
        tests = list(iter(suite))
 
 
541
        self.assertEqual(2, len(tests))
 
 
542
        self.assertEqual(tests[0].intertree_class, formats[0][0])
 
 
543
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
 
 
544
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
 
 
545
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
 
 
546
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
 
547
        self.assertEqual(tests[0].transport_server, server1)
 
 
548
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
549
        self.assertEqual(tests[1].intertree_class, formats[1][0])
 
 
550
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
 
 
551
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
 
 
552
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
 
 
553
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
 
554
        self.assertEqual(tests[1].transport_server, server1)
 
 
555
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
558
class TestTestCaseInTempDir(TestCaseInTempDir):
 
 
560
    def test_home_is_not_working(self):
 
 
561
        self.assertNotEqual(self.test_dir, self.test_home_dir)
 
 
562
        cwd = osutils.getcwd()
 
 
563
        self.assertEqual(self.test_dir, cwd)
 
 
564
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
 
567
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
 
 
569
    def test_home_is_non_existant_dir_under_root(self):
 
 
570
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
 
 
572
        This is because TestCaseWithMemoryTransport is for tests that do not
 
 
573
        need any disk resources: they should be hooked into bzrlib in such a 
 
 
574
        way that no global settings are being changed by the test (only a 
 
 
575
        few tests should need to do that), and having a missing dir as home is
 
 
576
        an effective way to ensure that this is the case.
 
 
578
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
 
580
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
 
582
    def test_cwd_is_TEST_ROOT(self):
 
 
583
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
 
584
        cwd = osutils.getcwd()
 
 
585
        self.assertEqual(self.test_dir, cwd)
 
 
587
    def test_make_branch_and_memory_tree(self):
 
 
588
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
 
 
590
        This is hard to comprehensively robustly test, so we settle for making
 
 
591
        a branch and checking no directory was created at its relpath.
 
 
593
        tree = self.make_branch_and_memory_tree('dir')
 
 
594
        # Guard against regression into MemoryTransport leaking
 
 
595
        # files to disk instead of keeping them in memory.
 
 
596
        self.failIf(osutils.lexists('dir'))
 
 
597
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
 
599
    def test_make_branch_and_memory_tree_with_format(self):
 
 
600
        """make_branch_and_memory_tree should accept a format option."""
 
 
601
        format = bzrdir.BzrDirMetaFormat1()
 
 
602
        format.repository_format = weaverepo.RepositoryFormat7()
 
 
603
        tree = self.make_branch_and_memory_tree('dir', format=format)
 
 
604
        # Guard against regression into MemoryTransport leaking
 
 
605
        # files to disk instead of keeping them in memory.
 
 
606
        self.failIf(osutils.lexists('dir'))
 
 
607
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
 
608
        self.assertEqual(format.repository_format.__class__,
 
 
609
            tree.branch.repository._format.__class__)
 
 
612
class TestTestCaseWithTransport(TestCaseWithTransport):
 
 
613
    """Tests for the convenience functions TestCaseWithTransport introduces."""
 
 
615
    def test_get_readonly_url_none(self):
 
 
616
        from bzrlib.transport import get_transport
 
 
617
        from bzrlib.transport.memory import MemoryServer
 
 
618
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
 
 
619
        self.vfs_transport_factory = MemoryServer
 
 
620
        self.transport_readonly_server = None
 
 
621
        # calling get_readonly_transport() constructs a decorator on the url
 
 
623
        url = self.get_readonly_url()
 
 
624
        url2 = self.get_readonly_url('foo/bar')
 
 
625
        t = get_transport(url)
 
 
626
        t2 = get_transport(url2)
 
 
627
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
 
628
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
 
 
629
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
 
631
    def test_get_readonly_url_http(self):
 
 
632
        from bzrlib.tests.HttpServer import HttpServer
 
 
633
        from bzrlib.transport import get_transport
 
 
634
        from bzrlib.transport.local import LocalURLServer
 
 
635
        from bzrlib.transport.http import HttpTransportBase
 
 
636
        self.transport_server = LocalURLServer
 
 
637
        self.transport_readonly_server = HttpServer
 
 
638
        # calling get_readonly_transport() gives us a HTTP server instance.
 
 
639
        url = self.get_readonly_url()
 
 
640
        url2 = self.get_readonly_url('foo/bar')
 
 
641
        # the transport returned may be any HttpTransportBase subclass
 
 
642
        t = get_transport(url)
 
 
643
        t2 = get_transport(url2)
 
 
644
        self.failUnless(isinstance(t, HttpTransportBase))
 
 
645
        self.failUnless(isinstance(t2, HttpTransportBase))
 
 
646
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
 
648
    def test_is_directory(self):
 
 
649
        """Test assertIsDirectory assertion"""
 
 
650
        t = self.get_transport()
 
 
651
        self.build_tree(['a_dir/', 'a_file'], transport=t)
 
 
652
        self.assertIsDirectory('a_dir', t)
 
 
653
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
 
 
654
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
 
 
657
class TestTestCaseTransports(TestCaseWithTransport):
 
 
660
        super(TestTestCaseTransports, self).setUp()
 
 
661
        self.vfs_transport_factory = MemoryServer
 
 
663
    def test_make_bzrdir_preserves_transport(self):
 
 
664
        t = self.get_transport()
 
 
665
        result_bzrdir = self.make_bzrdir('subdir')
 
 
666
        self.assertIsInstance(result_bzrdir.transport, 
 
 
668
        # should not be on disk, should only be in memory
 
 
669
        self.failIfExists('subdir')
 
 
672
class TestChrootedTest(ChrootedTestCase):
 
 
674
    def test_root_is_root(self):
 
 
675
        from bzrlib.transport import get_transport
 
 
676
        t = get_transport(self.get_readonly_url())
 
 
678
        self.assertEqual(url, t.clone('..').base)
 
 
681
class MockProgress(_BaseProgressBar):
 
 
682
    """Progress-bar standin that records calls.
 
 
684
    Useful for testing pb using code.
 
 
688
        _BaseProgressBar.__init__(self)
 
 
692
        self.calls.append(('tick',))
 
 
694
    def update(self, msg=None, current=None, total=None):
 
 
695
        self.calls.append(('update', msg, current, total))
 
 
698
        self.calls.append(('clear',))
 
 
700
    def note(self, msg, *args):
 
 
701
        self.calls.append(('note', msg, args))
 
 
704
class TestTestResult(TestCase):
 
 
706
    def test_elapsed_time_with_benchmarking(self):
 
 
707
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
711
        result._recordTestStartTime()
 
 
713
        result.extractBenchmarkTime(self)
 
 
714
        timed_string = result._testTimeString()
 
 
715
        # without explicit benchmarking, we should get a simple time.
 
 
716
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
 
 
717
        # if a benchmark time is given, we want a x of y style result.
 
 
718
        self.time(time.sleep, 0.001)
 
 
719
        result.extractBenchmarkTime(self)
 
 
720
        timed_string = result._testTimeString()
 
 
721
        self.assertContainsRe(
 
 
722
            timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
 
 
723
        # extracting the time from a non-bzrlib testcase sets to None
 
 
724
        result._recordTestStartTime()
 
 
725
        result.extractBenchmarkTime(
 
 
726
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
 
 
727
        timed_string = result._testTimeString()
 
 
728
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
 
 
729
        # cheat. Yes, wash thy mouth out with soap.
 
 
730
        self._benchtime = None
 
 
732
    def test_assigned_benchmark_file_stores_date(self):
 
 
734
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
739
        output_string = output.getvalue()
 
 
741
        # if you are wondering about the regexp please read the comment in
 
 
742
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
 
 
743
        # XXX: what comment?  -- Andrew Bennetts
 
 
744
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
 
746
    def test_benchhistory_records_test_times(self):
 
 
747
        result_stream = StringIO()
 
 
748
        result = bzrlib.tests.TextTestResult(
 
 
752
            bench_history=result_stream
 
 
755
        # we want profile a call and check that its test duration is recorded
 
 
756
        # make a new test instance that when run will generate a benchmark
 
 
757
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
 
758
        # execute the test, which should succeed and record times
 
 
759
        example_test_case.run(result)
 
 
760
        lines = result_stream.getvalue().splitlines()
 
 
761
        self.assertEqual(2, len(lines))
 
 
762
        self.assertContainsRe(lines[1],
 
 
763
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
 
 
764
            "._time_hello_world_encoding")
 
 
766
    def _time_hello_world_encoding(self):
 
 
767
        """Profile two sleep calls
 
 
769
        This is used to exercise the test framework.
 
 
771
        self.time(unicode, 'hello', errors='replace')
 
 
772
        self.time(unicode, 'world', errors='replace')
 
 
774
    def test_lsprofiling(self):
 
 
775
        """Verbose test result prints lsprof statistics from test cases."""
 
 
776
        self.requireFeature(test_lsprof.LSProfFeature)
 
 
777
        result_stream = StringIO()
 
 
778
        result = bzrlib.tests.VerboseTestResult(
 
 
779
            unittest._WritelnDecorator(result_stream),
 
 
783
        # we want profile a call of some sort and check it is output by
 
 
784
        # addSuccess. We dont care about addError or addFailure as they
 
 
785
        # are not that interesting for performance tuning.
 
 
786
        # make a new test instance that when run will generate a profile
 
 
787
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
 
788
        example_test_case._gather_lsprof_in_benchmarks = True
 
 
789
        # execute the test, which should succeed and record profiles
 
 
790
        example_test_case.run(result)
 
 
791
        # lsprofile_something()
 
 
792
        # if this worked we want 
 
 
793
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
 
 
794
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
 
 
795
        # (the lsprof header)
 
 
796
        # ... an arbitrary number of lines
 
 
797
        # and the function call which is time.sleep.
 
 
798
        #           1        0            ???         ???       ???(sleep) 
 
 
799
        # and then repeated but with 'world', rather than 'hello'.
 
 
800
        # this should appear in the output stream of our test result.
 
 
801
        output = result_stream.getvalue()
 
 
802
        self.assertContainsRe(output,
 
 
803
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
 
 
804
        self.assertContainsRe(output,
 
 
805
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
 
 
806
        self.assertContainsRe(output,
 
 
807
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
 
 
808
        self.assertContainsRe(output,
 
 
809
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
 
 
811
    def test_known_failure(self):
 
 
812
        """A KnownFailure being raised should trigger several result actions."""
 
 
813
        class InstrumentedTestResult(ExtendedTestResult):
 
 
815
            def report_test_start(self, test): pass
 
 
816
            def report_known_failure(self, test, err):
 
 
817
                self._call = test, err
 
 
818
        result = InstrumentedTestResult(None, None, None, None)
 
 
820
            raise KnownFailure('failed!')
 
 
821
        test = unittest.FunctionTestCase(test_function)
 
 
823
        # it should invoke 'report_known_failure'.
 
 
824
        self.assertEqual(2, len(result._call))
 
 
825
        self.assertEqual(test, result._call[0])
 
 
826
        self.assertEqual(KnownFailure, result._call[1][0])
 
 
827
        self.assertIsInstance(result._call[1][1], KnownFailure)
 
 
828
        # we dont introspec the traceback, if the rest is ok, it would be
 
 
829
        # exceptional for it not to be.
 
 
830
        # it should update the known_failure_count on the object.
 
 
831
        self.assertEqual(1, result.known_failure_count)
 
 
832
        # the result should be successful.
 
 
833
        self.assertTrue(result.wasSuccessful())
 
 
835
    def test_verbose_report_known_failure(self):
 
 
836
        # verbose test output formatting
 
 
837
        result_stream = StringIO()
 
 
838
        result = bzrlib.tests.VerboseTestResult(
 
 
839
            unittest._WritelnDecorator(result_stream),
 
 
843
        test = self.get_passing_test()
 
 
844
        result.startTest(test)
 
 
845
        result.extractBenchmarkTime(test)
 
 
846
        prefix = len(result_stream.getvalue())
 
 
847
        # the err parameter has the shape:
 
 
848
        # (class, exception object, traceback)
 
 
849
        # KnownFailures dont get their tracebacks shown though, so we
 
 
851
        err = (KnownFailure, KnownFailure('foo'), None)
 
 
852
        result.report_known_failure(test, err)
 
 
853
        output = result_stream.getvalue()[prefix:]
 
 
854
        lines = output.splitlines()
 
 
855
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
 
 
856
        self.assertEqual(lines[1], '    foo')
 
 
857
        self.assertEqual(2, len(lines))
 
 
859
    def test_text_report_known_failure(self):
 
 
860
        # text test output formatting
 
 
862
        result = bzrlib.tests.TextTestResult(
 
 
868
        test = self.get_passing_test()
 
 
869
        # this seeds the state to handle reporting the test.
 
 
870
        result.startTest(test)
 
 
871
        result.extractBenchmarkTime(test)
 
 
872
        # the err parameter has the shape:
 
 
873
        # (class, exception object, traceback)
 
 
874
        # KnownFailures dont get their tracebacks shown though, so we
 
 
876
        err = (KnownFailure, KnownFailure('foo'), None)
 
 
877
        result.report_known_failure(test, err)
 
 
880
            ('update', '[1 in 0s] passing_test', None, None),
 
 
881
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
 
 
884
        # known_failures should be printed in the summary, so if we run a test
 
 
885
        # after there are some known failures, the update prefix should match
 
 
887
        result.known_failure_count = 3
 
 
891
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
 
 
895
    def get_passing_test(self):
 
 
896
        """Return a test object that can't be run usefully."""
 
 
899
        return unittest.FunctionTestCase(passing_test)
 
 
901
    def test_add_not_supported(self):
 
 
902
        """Test the behaviour of invoking addNotSupported."""
 
 
903
        class InstrumentedTestResult(ExtendedTestResult):
 
 
904
            def report_test_start(self, test): pass
 
 
905
            def report_unsupported(self, test, feature):
 
 
906
                self._call = test, feature
 
 
907
        result = InstrumentedTestResult(None, None, None, None)
 
 
908
        test = SampleTestCase('_test_pass')
 
 
910
        result.startTest(test)
 
 
911
        result.addNotSupported(test, feature)
 
 
912
        # it should invoke 'report_unsupported'.
 
 
913
        self.assertEqual(2, len(result._call))
 
 
914
        self.assertEqual(test, result._call[0])
 
 
915
        self.assertEqual(feature, result._call[1])
 
 
916
        # the result should be successful.
 
 
917
        self.assertTrue(result.wasSuccessful())
 
 
918
        # it should record the test against a count of tests not run due to
 
 
920
        self.assertEqual(1, result.unsupported['Feature'])
 
 
921
        # and invoking it again should increment that counter
 
 
922
        result.addNotSupported(test, feature)
 
 
923
        self.assertEqual(2, result.unsupported['Feature'])
 
 
925
    def test_verbose_report_unsupported(self):
 
 
926
        # verbose test output formatting
 
 
927
        result_stream = StringIO()
 
 
928
        result = bzrlib.tests.VerboseTestResult(
 
 
929
            unittest._WritelnDecorator(result_stream),
 
 
933
        test = self.get_passing_test()
 
 
935
        result.startTest(test)
 
 
936
        result.extractBenchmarkTime(test)
 
 
937
        prefix = len(result_stream.getvalue())
 
 
938
        result.report_unsupported(test, feature)
 
 
939
        output = result_stream.getvalue()[prefix:]
 
 
940
        lines = output.splitlines()
 
 
941
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
 
 
943
    def test_text_report_unsupported(self):
 
 
944
        # text test output formatting
 
 
946
        result = bzrlib.tests.TextTestResult(
 
 
952
        test = self.get_passing_test()
 
 
954
        # this seeds the state to handle reporting the test.
 
 
955
        result.startTest(test)
 
 
956
        result.extractBenchmarkTime(test)
 
 
957
        result.report_unsupported(test, feature)
 
 
958
        # no output on unsupported features
 
 
960
            [('update', '[1 in 0s] passing_test', None, None)
 
 
963
        # the number of missing features should be printed in the progress
 
 
964
        # summary, so check for that.
 
 
965
        result.unsupported = {'foo':0, 'bar':0}
 
 
969
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
 
 
973
    def test_unavailable_exception(self):
 
 
974
        """An UnavailableFeature being raised should invoke addNotSupported."""
 
 
975
        class InstrumentedTestResult(ExtendedTestResult):
 
 
977
            def report_test_start(self, test): pass
 
 
978
            def addNotSupported(self, test, feature):
 
 
979
                self._call = test, feature
 
 
980
        result = InstrumentedTestResult(None, None, None, None)
 
 
983
            raise UnavailableFeature(feature)
 
 
984
        test = unittest.FunctionTestCase(test_function)
 
 
986
        # it should invoke 'addNotSupported'.
 
 
987
        self.assertEqual(2, len(result._call))
 
 
988
        self.assertEqual(test, result._call[0])
 
 
989
        self.assertEqual(feature, result._call[1])
 
 
990
        # and not count as an error
 
 
991
        self.assertEqual(0, result.error_count)
 
 
994
class TestRunner(TestCase):
 
 
996
    def dummy_test(self):
 
 
999
    def run_test_runner(self, testrunner, test):
 
 
1000
        """Run suite in testrunner, saving global state and restoring it.
 
 
1002
        This current saves and restores:
 
 
1003
        TestCaseInTempDir.TEST_ROOT
 
 
1005
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
 
 
1006
        without using this convenience method, because of our use of global state.
 
 
1008
        old_root = TestCaseInTempDir.TEST_ROOT
 
 
1010
            TestCaseInTempDir.TEST_ROOT = None
 
 
1011
            return testrunner.run(test)
 
 
1013
            TestCaseInTempDir.TEST_ROOT = old_root
 
 
1015
    def test_known_failure_failed_run(self):
 
 
1016
        # run a test that generates a known failure which should be printed in
 
 
1017
        # the final output when real failures occur.
 
 
1018
        def known_failure_test():
 
 
1019
            raise KnownFailure('failed')
 
 
1020
        test = unittest.TestSuite()
 
 
1021
        test.addTest(unittest.FunctionTestCase(known_failure_test))
 
 
1023
            raise AssertionError('foo')
 
 
1024
        test.addTest(unittest.FunctionTestCase(failing_test))
 
 
1026
        runner = TextTestRunner(stream=stream)
 
 
1027
        result = self.run_test_runner(runner, test)
 
 
1028
        lines = stream.getvalue().splitlines()
 
 
1031
            '======================================================================',
 
 
1032
            'FAIL: unittest.FunctionTestCase (failing_test)',
 
 
1033
            '----------------------------------------------------------------------',
 
 
1034
            'Traceback (most recent call last):',
 
 
1035
            '    raise AssertionError(\'foo\')',
 
 
1036
            'AssertionError: foo',
 
 
1038
            '----------------------------------------------------------------------',
 
 
1040
            'FAILED (failures=1, known_failure_count=1)'],
 
 
1041
            lines[0:5] + lines[6:10] + lines[11:])
 
 
1043
    def test_known_failure_ok_run(self):
 
 
1044
        # run a test that generates a known failure which should be printed in the final output.
 
 
1045
        def known_failure_test():
 
 
1046
            raise KnownFailure('failed')
 
 
1047
        test = unittest.FunctionTestCase(known_failure_test)
 
 
1049
        runner = TextTestRunner(stream=stream)
 
 
1050
        result = self.run_test_runner(runner, test)
 
 
1051
        self.assertContainsRe(stream.getvalue(),
 
 
1054
            'Ran 1 test in .*\n'
 
 
1056
            'OK \\(known_failures=1\\)\n')
 
 
1058
    def test_skipped_test(self):
 
 
1059
        # run a test that is skipped, and check the suite as a whole still
 
 
1061
        # skipping_test must be hidden in here so it's not run as a real test
 
 
1062
        def skipping_test():
 
 
1063
            raise TestSkipped('test intentionally skipped')
 
 
1065
        runner = TextTestRunner(stream=self._log_file)
 
 
1066
        test = unittest.FunctionTestCase(skipping_test)
 
 
1067
        result = self.run_test_runner(runner, test)
 
 
1068
        self.assertTrue(result.wasSuccessful())
 
 
1070
    def test_skipped_from_setup(self):
 
 
1071
        class SkippedSetupTest(TestCase):
 
 
1075
                self.addCleanup(self.cleanup)
 
 
1076
                raise TestSkipped('skipped setup')
 
 
1078
            def test_skip(self):
 
 
1079
                self.fail('test reached')
 
 
1084
        runner = TextTestRunner(stream=self._log_file)
 
 
1085
        test = SkippedSetupTest('test_skip')
 
 
1086
        result = self.run_test_runner(runner, test)
 
 
1087
        self.assertTrue(result.wasSuccessful())
 
 
1088
        # Check if cleanup was called the right number of times.
 
 
1089
        self.assertEqual(0, test.counter)
 
 
1091
    def test_skipped_from_test(self):
 
 
1092
        class SkippedTest(TestCase):
 
 
1096
                self.addCleanup(self.cleanup)
 
 
1098
            def test_skip(self):
 
 
1099
                raise TestSkipped('skipped test')
 
 
1104
        runner = TextTestRunner(stream=self._log_file)
 
 
1105
        test = SkippedTest('test_skip')
 
 
1106
        result = self.run_test_runner(runner, test)
 
 
1107
        self.assertTrue(result.wasSuccessful())
 
 
1108
        # Check if cleanup was called the right number of times.
 
 
1109
        self.assertEqual(0, test.counter)
 
 
1111
    def test_unsupported_features_listed(self):
 
 
1112
        """When unsupported features are encountered they are detailed."""
 
 
1113
        class Feature1(Feature):
 
 
1114
            def _probe(self): return False
 
 
1115
        class Feature2(Feature):
 
 
1116
            def _probe(self): return False
 
 
1117
        # create sample tests
 
 
1118
        test1 = SampleTestCase('_test_pass')
 
 
1119
        test1._test_needs_features = [Feature1()]
 
 
1120
        test2 = SampleTestCase('_test_pass')
 
 
1121
        test2._test_needs_features = [Feature2()]
 
 
1122
        test = unittest.TestSuite()
 
 
1126
        runner = TextTestRunner(stream=stream)
 
 
1127
        result = self.run_test_runner(runner, test)
 
 
1128
        lines = stream.getvalue().splitlines()
 
 
1131
            "Missing feature 'Feature1' skipped 1 tests.",
 
 
1132
            "Missing feature 'Feature2' skipped 1 tests.",
 
 
1136
    def test_bench_history(self):
 
 
1137
        # tests that the running the benchmark produces a history file
 
 
1138
        # containing a timestamp and the revision id of the bzrlib source which
 
 
1140
        workingtree = _get_bzr_source_tree()
 
 
1141
        test = TestRunner('dummy_test')
 
 
1143
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
 
 
1144
        result = self.run_test_runner(runner, test)
 
 
1145
        output_string = output.getvalue()
 
 
1146
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
 
1147
        if workingtree is not None:
 
 
1148
            revision_id = workingtree.get_parent_ids()[0]
 
 
1149
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
 
1151
    def test_success_log_deleted(self):
 
 
1152
        """Successful tests have their log deleted"""
 
 
1154
        class LogTester(TestCase):
 
 
1156
            def test_success(self):
 
 
1157
                self.log('this will be removed\n')
 
 
1159
        sio = cStringIO.StringIO()
 
 
1160
        runner = TextTestRunner(stream=sio)
 
 
1161
        test = LogTester('test_success')
 
 
1162
        result = self.run_test_runner(runner, test)
 
 
1164
        log = test._get_log()
 
 
1165
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
 
1166
        self.assertEqual('', test._log_contents)
 
 
1167
        self.assertIs(None, test._log_file_name)
 
 
1169
    def test_fail_log_kept(self):
 
 
1170
        """Failed tests have their log kept"""
 
 
1172
        class LogTester(TestCase):
 
 
1174
            def test_fail(self):
 
 
1175
                self.log('this will be kept\n')
 
 
1176
                self.fail('this test fails')
 
 
1178
        sio = cStringIO.StringIO()
 
 
1179
        runner = TextTestRunner(stream=sio)
 
 
1180
        test = LogTester('test_fail')
 
 
1181
        result = self.run_test_runner(runner, test)
 
 
1183
        text = sio.getvalue()
 
 
1184
        self.assertContainsRe(text, 'this will be kept')
 
 
1185
        self.assertContainsRe(text, 'this test fails')
 
 
1187
        log = test._get_log()
 
 
1188
        self.assertContainsRe(log, 'this will be kept')
 
 
1189
        self.assertEqual(log, test._log_contents)
 
 
1191
    def test_error_log_kept(self):
 
 
1192
        """Tests with errors have their log kept"""
 
 
1194
        class LogTester(TestCase):
 
 
1196
            def test_error(self):
 
 
1197
                self.log('this will be kept\n')
 
 
1198
                raise ValueError('random exception raised')
 
 
1200
        sio = cStringIO.StringIO()
 
 
1201
        runner = TextTestRunner(stream=sio)
 
 
1202
        test = LogTester('test_error')
 
 
1203
        result = self.run_test_runner(runner, test)
 
 
1205
        text = sio.getvalue()
 
 
1206
        self.assertContainsRe(text, 'this will be kept')
 
 
1207
        self.assertContainsRe(text, 'random exception raised')
 
 
1209
        log = test._get_log()
 
 
1210
        self.assertContainsRe(log, 'this will be kept')
 
 
1211
        self.assertEqual(log, test._log_contents)
 
 
1214
class SampleTestCase(TestCase):
 
 
1216
    def _test_pass(self):
 
 
1220
class TestTestCase(TestCase):
 
 
1221
    """Tests that test the core bzrlib TestCase."""
 
 
1223
    def test_debug_flags_sanitised(self):
 
 
1224
        """The bzrlib debug flags should be sanitised by setUp."""
 
 
1225
        # we could set something and run a test that will check
 
 
1226
        # it gets santised, but this is probably sufficient for now:
 
 
1227
        # if someone runs the test with -Dsomething it will error.
 
 
1228
        self.assertEqual(set(), bzrlib.debug.debug_flags)
 
 
1230
    def inner_test(self):
 
 
1231
        # the inner child test
 
 
1234
    def outer_child(self):
 
 
1235
        # the outer child test
 
 
1237
        self.inner_test = TestTestCase("inner_child")
 
 
1238
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
1241
        self.inner_test.run(result)
 
 
1242
        note("outer finish")
 
 
1244
    def test_trace_nesting(self):
 
 
1245
        # this tests that each test case nests its trace facility correctly.
 
 
1246
        # we do this by running a test case manually. That test case (A)
 
 
1247
        # should setup a new log, log content to it, setup a child case (B),
 
 
1248
        # which should log independently, then case (A) should log a trailer
 
 
1250
        # we do two nested children so that we can verify the state of the 
 
 
1251
        # logs after the outer child finishes is correct, which a bad clean
 
 
1252
        # up routine in tearDown might trigger a fault in our test with only
 
 
1253
        # one child, we should instead see the bad result inside our test with
 
 
1255
        # the outer child test
 
 
1256
        original_trace = bzrlib.trace._trace_file
 
 
1257
        outer_test = TestTestCase("outer_child")
 
 
1258
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
1261
        outer_test.run(result)
 
 
1262
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
 
 
1264
    def method_that_times_a_bit_twice(self):
 
 
1265
        # call self.time twice to ensure it aggregates
 
 
1266
        self.time(time.sleep, 0.007)
 
 
1267
        self.time(time.sleep, 0.007)
 
 
1269
    def test_time_creates_benchmark_in_result(self):
 
 
1270
        """Test that the TestCase.time() method accumulates a benchmark time."""
 
 
1271
        sample_test = TestTestCase("method_that_times_a_bit_twice")
 
 
1272
        output_stream = StringIO()
 
 
1273
        result = bzrlib.tests.VerboseTestResult(
 
 
1274
            unittest._WritelnDecorator(output_stream),
 
 
1277
            num_tests=sample_test.countTestCases())
 
 
1278
        sample_test.run(result)
 
 
1279
        self.assertContainsRe(
 
 
1280
            output_stream.getvalue(),
 
 
1281
            r"\d+ms/ +\d+ms\n$")
 
 
1283
    def test_hooks_sanitised(self):
 
 
1284
        """The bzrlib hooks should be sanitised by setUp."""
 
 
1285
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
 
1286
            bzrlib.branch.Branch.hooks)
 
 
1287
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
 
 
1288
            bzrlib.smart.server.SmartTCPServer.hooks)
 
 
1290
    def test__gather_lsprof_in_benchmarks(self):
 
 
1291
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
 
 
1293
        Each self.time() call is individually and separately profiled.
 
 
1295
        self.requireFeature(test_lsprof.LSProfFeature)
 
 
1296
        # overrides the class member with an instance member so no cleanup 
 
 
1298
        self._gather_lsprof_in_benchmarks = True
 
 
1299
        self.time(time.sleep, 0.000)
 
 
1300
        self.time(time.sleep, 0.003)
 
 
1301
        self.assertEqual(2, len(self._benchcalls))
 
 
1302
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
 
 
1303
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
 
 
1304
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
 
 
1305
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
 
 
1307
    def test_knownFailure(self):
 
 
1308
        """Self.knownFailure() should raise a KnownFailure exception."""
 
 
1309
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
 
 
1311
    def test_requireFeature_available(self):
 
 
1312
        """self.requireFeature(available) is a no-op."""
 
 
1313
        class Available(Feature):
 
 
1314
            def _probe(self):return True
 
 
1315
        feature = Available()
 
 
1316
        self.requireFeature(feature)
 
 
1318
    def test_requireFeature_unavailable(self):
 
 
1319
        """self.requireFeature(unavailable) raises UnavailableFeature."""
 
 
1320
        class Unavailable(Feature):
 
 
1321
            def _probe(self):return False
 
 
1322
        feature = Unavailable()
 
 
1323
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
 
 
1325
    def test_run_no_parameters(self):
 
 
1326
        test = SampleTestCase('_test_pass')
 
 
1329
    def test_run_enabled_unittest_result(self):
 
 
1330
        """Test we revert to regular behaviour when the test is enabled."""
 
 
1331
        test = SampleTestCase('_test_pass')
 
 
1332
        class EnabledFeature(object):
 
 
1333
            def available(self):
 
 
1335
        test._test_needs_features = [EnabledFeature()]
 
 
1336
        result = unittest.TestResult()
 
 
1338
        self.assertEqual(1, result.testsRun)
 
 
1339
        self.assertEqual([], result.errors)
 
 
1340
        self.assertEqual([], result.failures)
 
 
1342
    def test_run_disabled_unittest_result(self):
 
 
1343
        """Test our compatability for disabled tests with unittest results."""
 
 
1344
        test = SampleTestCase('_test_pass')
 
 
1345
        class DisabledFeature(object):
 
 
1346
            def available(self):
 
 
1348
        test._test_needs_features = [DisabledFeature()]
 
 
1349
        result = unittest.TestResult()
 
 
1351
        self.assertEqual(1, result.testsRun)
 
 
1352
        self.assertEqual([], result.errors)
 
 
1353
        self.assertEqual([], result.failures)
 
 
1355
    def test_run_disabled_supporting_result(self):
 
 
1356
        """Test disabled tests behaviour with support aware results."""
 
 
1357
        test = SampleTestCase('_test_pass')
 
 
1358
        class DisabledFeature(object):
 
 
1359
            def available(self):
 
 
1361
        the_feature = DisabledFeature()
 
 
1362
        test._test_needs_features = [the_feature]
 
 
1363
        class InstrumentedTestResult(unittest.TestResult):
 
 
1365
                unittest.TestResult.__init__(self)
 
 
1367
            def startTest(self, test):
 
 
1368
                self.calls.append(('startTest', test))
 
 
1369
            def stopTest(self, test):
 
 
1370
                self.calls.append(('stopTest', test))
 
 
1371
            def addNotSupported(self, test, feature):
 
 
1372
                self.calls.append(('addNotSupported', test, feature))
 
 
1373
        result = InstrumentedTestResult()
 
 
1376
            ('startTest', test),
 
 
1377
            ('addNotSupported', test, the_feature),
 
 
1383
@symbol_versioning.deprecated_function(zero_eleven)
 
 
1384
def sample_deprecated_function():
 
 
1385
    """A deprecated function to test applyDeprecated with."""
 
 
1389
def sample_undeprecated_function(a_param):
 
 
1390
    """A undeprecated function to test applyDeprecated with."""
 
 
1393
class ApplyDeprecatedHelper(object):
 
 
1394
    """A helper class for ApplyDeprecated tests."""
 
 
1396
    @symbol_versioning.deprecated_method(zero_eleven)
 
 
1397
    def sample_deprecated_method(self, param_one):
 
 
1398
        """A deprecated method for testing with."""
 
 
1401
    def sample_normal_method(self):
 
 
1402
        """A undeprecated method."""
 
 
1404
    @symbol_versioning.deprecated_method(zero_ten)
 
 
1405
    def sample_nested_deprecation(self):
 
 
1406
        return sample_deprecated_function()
 
 
1409
class TestExtraAssertions(TestCase):
 
 
1410
    """Tests for new test assertions in bzrlib test suite"""
 
 
1412
    def test_assert_isinstance(self):
 
 
1413
        self.assertIsInstance(2, int)
 
 
1414
        self.assertIsInstance(u'', basestring)
 
 
1415
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
 
 
1416
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
 
 
1418
    def test_assertEndsWith(self):
 
 
1419
        self.assertEndsWith('foo', 'oo')
 
 
1420
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
 
 
1422
    def test_applyDeprecated_not_deprecated(self):
 
 
1423
        sample_object = ApplyDeprecatedHelper()
 
 
1424
        # calling an undeprecated callable raises an assertion
 
 
1425
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
 
1426
            sample_object.sample_normal_method)
 
 
1427
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
 
1428
            sample_undeprecated_function, "a param value")
 
 
1429
        # calling a deprecated callable (function or method) with the wrong
 
 
1430
        # expected deprecation fails.
 
 
1431
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
 
1432
            sample_object.sample_deprecated_method, "a param value")
 
 
1433
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
 
1434
            sample_deprecated_function)
 
 
1435
        # calling a deprecated callable (function or method) with the right
 
 
1436
        # expected deprecation returns the functions result.
 
 
1437
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
 
 
1438
            sample_object.sample_deprecated_method, "a param value"))
 
 
1439
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
 
 
1440
            sample_deprecated_function))
 
 
1441
        # calling a nested deprecation with the wrong deprecation version
 
 
1442
        # fails even if a deeper nested function was deprecated with the 
 
 
1444
        self.assertRaises(AssertionError, self.applyDeprecated,
 
 
1445
            zero_eleven, sample_object.sample_nested_deprecation)
 
 
1446
        # calling a nested deprecation with the right deprecation value
 
 
1447
        # returns the calls result.
 
 
1448
        self.assertEqual(2, self.applyDeprecated(zero_ten,
 
 
1449
            sample_object.sample_nested_deprecation))
 
 
1451
    def test_callDeprecated(self):
 
 
1452
        def testfunc(be_deprecated, result=None):
 
 
1453
            if be_deprecated is True:
 
 
1454
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
 
 
1457
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
 
 
1458
        self.assertIs(None, result)
 
 
1459
        result = self.callDeprecated([], testfunc, False, 'result')
 
 
1460
        self.assertEqual('result', result)
 
 
1461
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
 
 
1462
        self.callDeprecated([], testfunc, be_deprecated=False)
 
 
1465
class TestConvenienceMakers(TestCaseWithTransport):
 
 
1466
    """Test for the make_* convenience functions."""
 
 
1468
    def test_make_branch_and_tree_with_format(self):
 
 
1469
        # we should be able to supply a format to make_branch_and_tree
 
 
1470
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
 
 
1471
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
 
 
1472
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
 
 
1473
                              bzrlib.bzrdir.BzrDirMetaFormat1)
 
 
1474
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
 
 
1475
                              bzrlib.bzrdir.BzrDirFormat6)
 
 
1477
    def test_make_branch_and_memory_tree(self):
 
 
1478
        # we should be able to get a new branch and a mutable tree from
 
 
1479
        # TestCaseWithTransport
 
 
1480
        tree = self.make_branch_and_memory_tree('a')
 
 
1481
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
 
 
1484
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
 
 
1486
    def test_make_tree_for_sftp_branch(self):
 
 
1487
        """Transports backed by local directories create local trees."""
 
 
1489
        tree = self.make_branch_and_tree('t1')
 
 
1490
        base = tree.bzrdir.root_transport.base
 
 
1491
        self.failIf(base.startswith('sftp'),
 
 
1492
                'base %r is on sftp but should be local' % base)
 
 
1493
        self.assertEquals(tree.bzrdir.root_transport,
 
 
1494
                tree.branch.bzrdir.root_transport)
 
 
1495
        self.assertEquals(tree.bzrdir.root_transport,
 
 
1496
                tree.branch.repository.bzrdir.root_transport)
 
 
1499
class TestSelftest(TestCase):
 
 
1500
    """Tests of bzrlib.tests.selftest."""
 
 
1502
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
 
 
1505
            factory_called.append(True)
 
 
1509
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
 
 
1510
            test_suite_factory=factory)
 
 
1511
        self.assertEqual([True], factory_called)
 
 
1514
class TestKnownFailure(TestCase):
 
 
1516
    def test_known_failure(self):
 
 
1517
        """Check that KnownFailure is defined appropriately."""
 
 
1518
        # a KnownFailure is an assertion error for compatability with unaware
 
 
1520
        self.assertIsInstance(KnownFailure(""), AssertionError)
 
 
1522
    def test_expect_failure(self):
 
 
1524
            self.expectFailure("Doomed to failure", self.assertTrue, False)
 
 
1525
        except KnownFailure, e:
 
 
1526
            self.assertEqual('Doomed to failure', e.args[0])
 
 
1528
            self.expectFailure("Doomed to failure", self.assertTrue, True)
 
 
1529
        except AssertionError, e:
 
 
1530
            self.assertEqual('Unexpected success.  Should have failed:'
 
 
1531
                             ' Doomed to failure', e.args[0])
 
 
1533
            self.fail('Assertion not raised')
 
 
1536
class TestFeature(TestCase):
 
 
1538
    def test_caching(self):
 
 
1539
        """Feature._probe is called by the feature at most once."""
 
 
1540
        class InstrumentedFeature(Feature):
 
 
1542
                Feature.__init__(self)
 
 
1545
                self.calls.append('_probe')
 
 
1547
        feature = InstrumentedFeature()
 
 
1549
        self.assertEqual(['_probe'], feature.calls)
 
 
1551
        self.assertEqual(['_probe'], feature.calls)
 
 
1553
    def test_named_str(self):
 
 
1554
        """Feature.__str__ should thunk to feature_name()."""
 
 
1555
        class NamedFeature(Feature):
 
 
1556
            def feature_name(self):
 
 
1558
        feature = NamedFeature()
 
 
1559
        self.assertEqual('symlinks', str(feature))
 
 
1561
    def test_default_str(self):
 
 
1562
        """Feature.__str__ should default to __class__.__name__."""
 
 
1563
        class NamedFeature(Feature):
 
 
1565
        feature = NamedFeature()
 
 
1566
        self.assertEqual('NamedFeature', str(feature))
 
 
1569
class TestUnavailableFeature(TestCase):
 
 
1571
    def test_access_feature(self):
 
 
1573
        exception = UnavailableFeature(feature)
 
 
1574
        self.assertIs(feature, exception.args[0])
 
 
1577
class TestSelftestFiltering(TestCase):
 
 
1580
        self.suite = TestUtil.TestSuite()
 
 
1581
        self.loader = TestUtil.TestLoader()
 
 
1582
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
 
 
1583
            'bzrlib.tests.test_selftest']))
 
 
1584
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
 
 
1586
    def test_filter_suite_by_re(self):
 
 
1587
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
 
 
1588
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
 
 
1589
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
 
 
1590
            'TestSelftestFiltering.test_filter_suite_by_re'])
 
 
1592
    def test_sort_suite_by_re(self):
 
 
1593
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
 
 
1594
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
 
 
1595
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
 
 
1596
            'TestSelftestFiltering.test_filter_suite_by_re')
 
 
1597
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
 
 
1600
class TestCheckInventoryShape(TestCaseWithTransport):
 
 
1602
    def test_check_inventory_shape(self):
 
 
1603
        files = ['a', 'b/', 'b/c']
 
 
1604
        tree = self.make_branch_and_tree('.')
 
 
1605
        self.build_tree(files)
 
 
1609
            self.check_inventory_shape(tree.inventory, files)