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,
 
 
53
                          clean_selftest_output,
 
 
60
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
 
61
from bzrlib.tests.TestUtil import _load_module_by_name
 
 
62
from bzrlib.trace import note
 
 
63
from bzrlib.transport.memory import MemoryServer, MemoryTransport
 
 
64
from bzrlib.version import _get_bzr_source_tree
 
 
67
class SelftestTests(TestCase):
 
 
69
    def test_import_tests(self):
 
 
70
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
 
 
71
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
 
73
    def test_import_test_failure(self):
 
 
74
        self.assertRaises(ImportError,
 
 
78
class MetaTestLog(TestCase):
 
 
80
    def test_logging(self):
 
 
81
        """Test logs are captured when a test fails."""
 
 
82
        self.log('a test message')
 
 
83
        self._log_file.flush()
 
 
84
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
 
88
class TestTreeShape(TestCaseInTempDir):
 
 
90
    def test_unicode_paths(self):
 
 
91
        filename = u'hell\u00d8'
 
 
93
            self.build_tree_contents([(filename, 'contents of hello')])
 
 
94
        except UnicodeEncodeError:
 
 
95
            raise TestSkipped("can't build unicode working tree in "
 
 
96
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
 
97
        self.failUnlessExists(filename)
 
 
100
class TestTransportProviderAdapter(TestCase):
 
 
101
    """A group of tests that test the transport implementation adaption core.
 
 
103
    This is a meta test that the tests are applied to all available 
 
 
106
    This will be generalised in the future which is why it is in this 
 
 
107
    test file even though it is specific to transport tests at the moment.
 
 
110
    def test_get_transport_permutations(self):
 
 
111
        # this checks that we the module get_test_permutations call
 
 
112
        # is made by the adapter get_transport_test_permitations method.
 
 
113
        class MockModule(object):
 
 
114
            def get_test_permutations(self):
 
 
115
                return sample_permutation
 
 
116
        sample_permutation = [(1,2), (3,4)]
 
 
117
        from bzrlib.transport 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.transport import (TransportTestProviderAdapter,
 
 
129
                                      _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
        input_test = TestTransportProviderAdapter(
 
 
154
            "test_adapter_sets_transport_class")
 
 
155
        from bzrlib.transport import TransportTestProviderAdapter
 
 
156
        suite = TransportTestProviderAdapter().adapt(input_test)
 
 
157
        tests = list(iter(suite))
 
 
158
        self.assertTrue(len(tests) > 6)
 
 
159
        # there are at least that many builtin transports
 
 
161
        self.assertTrue(issubclass(one_test.transport_class, 
 
 
162
                                   bzrlib.transport.Transport))
 
 
163
        self.assertTrue(issubclass(one_test.transport_server, 
 
 
164
                                   bzrlib.transport.Server))
 
 
167
class TestBranchProviderAdapter(TestCase):
 
 
168
    """A group of tests that test the branch implementation test adapter."""
 
 
170
    def test_adapted_tests(self):
 
 
171
        # check that constructor parameters are passed through to the adapted
 
 
173
        from bzrlib.branch import BranchTestProviderAdapter
 
 
174
        input_test = TestBranchProviderAdapter(
 
 
175
            "test_adapted_tests")
 
 
178
        formats = [("c", "C"), ("d", "D")]
 
 
179
        adapter = BranchTestProviderAdapter(server1, server2, formats)
 
 
180
        suite = adapter.adapt(input_test)
 
 
181
        tests = list(iter(suite))
 
 
182
        self.assertEqual(2, len(tests))
 
 
183
        self.assertEqual(tests[0].branch_format, formats[0][0])
 
 
184
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
 
185
        self.assertEqual(tests[0].transport_server, server1)
 
 
186
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
187
        self.assertEqual(tests[1].branch_format, formats[1][0])
 
 
188
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
 
189
        self.assertEqual(tests[1].transport_server, server1)
 
 
190
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
193
class TestBzrDirProviderAdapter(TestCase):
 
 
194
    """A group of tests that test the bzr dir implementation test adapter."""
 
 
196
    def test_adapted_tests(self):
 
 
197
        # check that constructor parameters are passed through to the adapted
 
 
199
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
 
 
200
        input_test = TestBzrDirProviderAdapter(
 
 
201
            "test_adapted_tests")
 
 
206
        adapter = BzrDirTestProviderAdapter(vfs_factory,
 
 
207
            server1, server2, formats)
 
 
208
        suite = adapter.adapt(input_test)
 
 
209
        tests = list(iter(suite))
 
 
210
        self.assertEqual(2, len(tests))
 
 
211
        self.assertEqual(tests[0].bzrdir_format, formats[0])
 
 
212
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
 
 
213
        self.assertEqual(tests[0].transport_server, server1)
 
 
214
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
215
        self.assertEqual(tests[1].bzrdir_format, formats[1])
 
 
216
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
 
 
217
        self.assertEqual(tests[1].transport_server, server1)
 
 
218
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
221
class TestRepositoryProviderAdapter(TestCase):
 
 
222
    """A group of tests that test the repository implementation test adapter."""
 
 
224
    def test_adapted_tests(self):
 
 
225
        # check that constructor parameters are passed through to the adapted
 
 
227
        from bzrlib.repository import RepositoryTestProviderAdapter
 
 
228
        input_test = TestRepositoryProviderAdapter(
 
 
229
            "test_adapted_tests")
 
 
232
        formats = [("c", "C"), ("d", "D")]
 
 
233
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
 
 
234
        suite = adapter.adapt(input_test)
 
 
235
        tests = list(iter(suite))
 
 
236
        self.assertEqual(2, len(tests))
 
 
237
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
 
238
        self.assertEqual(tests[0].repository_format, formats[0][0])
 
 
239
        self.assertEqual(tests[0].transport_server, server1)
 
 
240
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
241
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
 
242
        self.assertEqual(tests[1].repository_format, formats[1][0])
 
 
243
        self.assertEqual(tests[1].transport_server, server1)
 
 
244
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
246
    def test_setting_vfs_transport(self):
 
 
247
        """The vfs_transport_factory can be set optionally."""
 
 
248
        from bzrlib.repository import RepositoryTestProviderAdapter
 
 
249
        input_test = TestRepositoryProviderAdapter(
 
 
250
            "test_adapted_tests")
 
 
251
        formats = [("c", "C")]
 
 
252
        adapter = RepositoryTestProviderAdapter(None, None, formats,
 
 
253
            vfs_transport_factory="vfs")
 
 
254
        suite = adapter.adapt(input_test)
 
 
255
        tests = list(iter(suite))
 
 
256
        self.assertEqual(1, len(tests))
 
 
257
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
 
 
260
class TestInterRepositoryProviderAdapter(TestCase):
 
 
261
    """A group of tests that test the InterRepository test adapter."""
 
 
263
    def test_adapted_tests(self):
 
 
264
        # check that constructor parameters are passed through to the adapted
 
 
266
        from bzrlib.repository import InterRepositoryTestProviderAdapter
 
 
267
        input_test = TestInterRepositoryProviderAdapter(
 
 
268
            "test_adapted_tests")
 
 
271
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
 
272
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
 
 
273
        suite = adapter.adapt(input_test)
 
 
274
        tests = list(iter(suite))
 
 
275
        self.assertEqual(2, len(tests))
 
 
276
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
 
 
277
        self.assertEqual(tests[0].repository_format, formats[0][1])
 
 
278
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
 
 
279
        self.assertEqual(tests[0].transport_server, server1)
 
 
280
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
281
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
 
 
282
        self.assertEqual(tests[1].repository_format, formats[1][1])
 
 
283
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
 
 
284
        self.assertEqual(tests[1].transport_server, server1)
 
 
285
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
288
class TestInterVersionedFileProviderAdapter(TestCase):
 
 
289
    """A group of tests that test the InterVersionedFile test adapter."""
 
 
291
    def test_adapted_tests(self):
 
 
292
        # check that constructor parameters are passed through to the adapted
 
 
294
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
 
 
295
        input_test = TestInterRepositoryProviderAdapter(
 
 
296
            "test_adapted_tests")
 
 
299
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
 
300
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
 
 
301
        suite = adapter.adapt(input_test)
 
 
302
        tests = list(iter(suite))
 
 
303
        self.assertEqual(2, len(tests))
 
 
304
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
 
 
305
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
 
 
306
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
 
 
307
        self.assertEqual(tests[0].transport_server, server1)
 
 
308
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
309
        self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
 
 
310
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
 
 
311
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
 
 
312
        self.assertEqual(tests[1].transport_server, server1)
 
 
313
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
316
class TestRevisionStoreProviderAdapter(TestCase):
 
 
317
    """A group of tests that test the RevisionStore test adapter."""
 
 
319
    def test_adapted_tests(self):
 
 
320
        # check that constructor parameters are passed through to the adapted
 
 
322
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
 
 
323
        input_test = TestRevisionStoreProviderAdapter(
 
 
324
            "test_adapted_tests")
 
 
325
        # revision stores need a store factory - i.e. RevisionKnit
 
 
326
        #, a readonly and rw transport 
 
 
330
        store_factories = ["c", "d"]
 
 
331
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
 
 
332
        suite = adapter.adapt(input_test)
 
 
333
        tests = list(iter(suite))
 
 
334
        self.assertEqual(2, len(tests))
 
 
335
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
 
 
336
        self.assertEqual(tests[0].transport_server, server1)
 
 
337
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
338
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
 
 
339
        self.assertEqual(tests[1].transport_server, server1)
 
 
340
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
343
class TestWorkingTreeProviderAdapter(TestCase):
 
 
344
    """A group of tests that test the workingtree implementation test adapter."""
 
 
346
    def test_adapted_tests(self):
 
 
347
        # check that constructor parameters are passed through to the adapted
 
 
349
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
 
 
350
        input_test = TestWorkingTreeProviderAdapter(
 
 
351
            "test_adapted_tests")
 
 
354
        formats = [("c", "C"), ("d", "D")]
 
 
355
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
 
 
356
        suite = adapter.adapt(input_test)
 
 
357
        tests = list(iter(suite))
 
 
358
        self.assertEqual(2, len(tests))
 
 
359
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
 
360
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
 
361
        self.assertEqual(tests[0].transport_server, server1)
 
 
362
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
363
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
 
364
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
 
365
        self.assertEqual(tests[1].transport_server, server1)
 
 
366
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
369
class TestTreeProviderAdapter(TestCase):
 
 
370
    """Test the setup of tree_implementation tests."""
 
 
372
    def test_adapted_tests(self):
 
 
373
        # the tree implementation adapter is meant to setup one instance for
 
 
374
        # each working tree format, and one additional instance that will
 
 
375
        # use the default wt format, but create a revision tree for the tests.
 
 
376
        # this means that the wt ones should have the workingtree_to_test_tree
 
 
377
        # attribute set to 'return_parameter' and the revision one set to
 
 
378
        # revision_tree_from_workingtree.
 
 
380
        from bzrlib.tests.tree_implementations import (
 
 
381
            TreeTestProviderAdapter,
 
 
383
            revision_tree_from_workingtree
 
 
385
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
 
 
386
        input_test = TestTreeProviderAdapter(
 
 
387
            "test_adapted_tests")
 
 
390
        formats = [("c", "C"), ("d", "D")]
 
 
391
        adapter = TreeTestProviderAdapter(server1, server2, formats)
 
 
392
        suite = adapter.adapt(input_test)
 
 
393
        tests = list(iter(suite))
 
 
394
        self.assertEqual(4, len(tests))
 
 
395
        # this must match the default format setp up in
 
 
396
        # TreeTestProviderAdapter.adapt
 
 
397
        default_format = WorkingTreeFormat3
 
 
398
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
 
399
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
 
400
        self.assertEqual(tests[0].transport_server, server1)
 
 
401
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
402
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
 
403
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
 
404
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
 
405
        self.assertEqual(tests[1].transport_server, server1)
 
 
406
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
407
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
 
408
        self.assertIsInstance(tests[2].workingtree_format, default_format)
 
 
409
        #self.assertEqual(tests[2].bzrdir_format,
 
 
410
        #                 default_format._matchingbzrdir)
 
 
411
        self.assertEqual(tests[2].transport_server, server1)
 
 
412
        self.assertEqual(tests[2].transport_readonly_server, server2)
 
 
413
        self.assertEqual(tests[2].workingtree_to_test_tree,
 
 
414
            revision_tree_from_workingtree)
 
 
417
class TestInterTreeProviderAdapter(TestCase):
 
 
418
    """A group of tests that test the InterTreeTestAdapter."""
 
 
420
    def test_adapted_tests(self):
 
 
421
        # check that constructor parameters are passed through to the adapted
 
 
423
        # for InterTree tests we want the machinery to bring up two trees in
 
 
424
        # each instance: the base one, and the one we are interacting with.
 
 
425
        # because each optimiser can be direction specific, we need to test
 
 
426
        # each optimiser in its chosen direction.
 
 
427
        # unlike the TestProviderAdapter we dont want to automatically add a
 
 
428
        # parameterised one for WorkingTree - the optimisers will tell us what
 
 
430
        from bzrlib.tests.tree_implementations import (
 
 
432
            revision_tree_from_workingtree
 
 
434
        from bzrlib.tests.intertree_implementations import (
 
 
435
            InterTreeTestProviderAdapter,
 
 
437
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
 
 
438
        input_test = TestInterTreeProviderAdapter(
 
 
439
            "test_adapted_tests")
 
 
442
        format1 = WorkingTreeFormat2()
 
 
443
        format2 = WorkingTreeFormat3()
 
 
444
        formats = [(str, format1, format2, "converter1"),
 
 
445
            (int, format2, format1, "converter2")]
 
 
446
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
 
 
447
        suite = adapter.adapt(input_test)
 
 
448
        tests = list(iter(suite))
 
 
449
        self.assertEqual(2, len(tests))
 
 
450
        self.assertEqual(tests[0].intertree_class, formats[0][0])
 
 
451
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
 
 
452
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
 
 
453
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
 
 
454
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
 
455
        self.assertEqual(tests[0].transport_server, server1)
 
 
456
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
 
457
        self.assertEqual(tests[1].intertree_class, formats[1][0])
 
 
458
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
 
 
459
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
 
 
460
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
 
 
461
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
 
462
        self.assertEqual(tests[1].transport_server, server1)
 
 
463
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
 
466
class TestTestCaseInTempDir(TestCaseInTempDir):
 
 
468
    def test_home_is_not_working(self):
 
 
469
        self.assertNotEqual(self.test_dir, self.test_home_dir)
 
 
470
        cwd = osutils.getcwd()
 
 
471
        self.assertEqual(self.test_dir, cwd)
 
 
472
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
 
475
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
 
 
477
    def test_home_is_non_existant_dir_under_root(self):
 
 
478
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
 
 
480
        This is because TestCaseWithMemoryTransport is for tests that do not
 
 
481
        need any disk resources: they should be hooked into bzrlib in such a 
 
 
482
        way that no global settings are being changed by the test (only a 
 
 
483
        few tests should need to do that), and having a missing dir as home is
 
 
484
        an effective way to ensure that this is the case.
 
 
486
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
 
488
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
 
490
    def test_cwd_is_TEST_ROOT(self):
 
 
491
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
 
492
        cwd = osutils.getcwd()
 
 
493
        self.assertEqual(self.test_dir, cwd)
 
 
495
    def test_make_branch_and_memory_tree(self):
 
 
496
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
 
 
498
        This is hard to comprehensively robustly test, so we settle for making
 
 
499
        a branch and checking no directory was created at its relpath.
 
 
501
        tree = self.make_branch_and_memory_tree('dir')
 
 
502
        # Guard against regression into MemoryTransport leaking
 
 
503
        # files to disk instead of keeping them in memory.
 
 
504
        self.failIf(osutils.lexists('dir'))
 
 
505
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
 
507
    def test_make_branch_and_memory_tree_with_format(self):
 
 
508
        """make_branch_and_memory_tree should accept a format option."""
 
 
509
        format = bzrdir.BzrDirMetaFormat1()
 
 
510
        format.repository_format = weaverepo.RepositoryFormat7()
 
 
511
        tree = self.make_branch_and_memory_tree('dir', format=format)
 
 
512
        # Guard against regression into MemoryTransport leaking
 
 
513
        # files to disk instead of keeping them in memory.
 
 
514
        self.failIf(osutils.lexists('dir'))
 
 
515
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
 
516
        self.assertEqual(format.repository_format.__class__,
 
 
517
            tree.branch.repository._format.__class__)
 
 
520
class TestTestCaseWithTransport(TestCaseWithTransport):
 
 
521
    """Tests for the convenience functions TestCaseWithTransport introduces."""
 
 
523
    def test_get_readonly_url_none(self):
 
 
524
        from bzrlib.transport import get_transport
 
 
525
        from bzrlib.transport.memory import MemoryServer
 
 
526
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
 
 
527
        self.vfs_transport_factory = MemoryServer
 
 
528
        self.transport_readonly_server = None
 
 
529
        # calling get_readonly_transport() constructs a decorator on the url
 
 
531
        url = self.get_readonly_url()
 
 
532
        url2 = self.get_readonly_url('foo/bar')
 
 
533
        t = get_transport(url)
 
 
534
        t2 = get_transport(url2)
 
 
535
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
 
536
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
 
 
537
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
 
539
    def test_get_readonly_url_http(self):
 
 
540
        from bzrlib.tests.HttpServer import HttpServer
 
 
541
        from bzrlib.transport import get_transport
 
 
542
        from bzrlib.transport.local import LocalURLServer
 
 
543
        from bzrlib.transport.http import HttpTransportBase
 
 
544
        self.transport_server = LocalURLServer
 
 
545
        self.transport_readonly_server = HttpServer
 
 
546
        # calling get_readonly_transport() gives us a HTTP server instance.
 
 
547
        url = self.get_readonly_url()
 
 
548
        url2 = self.get_readonly_url('foo/bar')
 
 
549
        # the transport returned may be any HttpTransportBase subclass
 
 
550
        t = get_transport(url)
 
 
551
        t2 = get_transport(url2)
 
 
552
        self.failUnless(isinstance(t, HttpTransportBase))
 
 
553
        self.failUnless(isinstance(t2, HttpTransportBase))
 
 
554
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
 
556
    def test_is_directory(self):
 
 
557
        """Test assertIsDirectory assertion"""
 
 
558
        t = self.get_transport()
 
 
559
        self.build_tree(['a_dir/', 'a_file'], transport=t)
 
 
560
        self.assertIsDirectory('a_dir', t)
 
 
561
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
 
 
562
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
 
 
565
class TestTestCaseTransports(TestCaseWithTransport):
 
 
568
        super(TestTestCaseTransports, self).setUp()
 
 
569
        self.vfs_transport_factory = MemoryServer
 
 
571
    def test_make_bzrdir_preserves_transport(self):
 
 
572
        t = self.get_transport()
 
 
573
        result_bzrdir = self.make_bzrdir('subdir')
 
 
574
        self.assertIsInstance(result_bzrdir.transport, 
 
 
576
        # should not be on disk, should only be in memory
 
 
577
        self.failIfExists('subdir')
 
 
580
class TestChrootedTest(ChrootedTestCase):
 
 
582
    def test_root_is_root(self):
 
 
583
        from bzrlib.transport import get_transport
 
 
584
        t = get_transport(self.get_readonly_url())
 
 
586
        self.assertEqual(url, t.clone('..').base)
 
 
589
class MockProgress(_BaseProgressBar):
 
 
590
    """Progress-bar standin that records calls.
 
 
592
    Useful for testing pb using code.
 
 
596
        _BaseProgressBar.__init__(self)
 
 
600
        self.calls.append(('tick',))
 
 
602
    def update(self, msg=None, current=None, total=None):
 
 
603
        self.calls.append(('update', msg, current, total))
 
 
606
        self.calls.append(('clear',))
 
 
608
    def note(self, msg, *args):
 
 
609
        self.calls.append(('note', msg, args))
 
 
612
class TestTestResult(TestCase):
 
 
614
    def test_elapsed_time_with_benchmarking(self):
 
 
615
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
619
        result._recordTestStartTime()
 
 
621
        result.extractBenchmarkTime(self)
 
 
622
        timed_string = result._testTimeString()
 
 
623
        # without explicit benchmarking, we should get a simple time.
 
 
624
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
 
 
625
        # if a benchmark time is given, we want a x of y style result.
 
 
626
        self.time(time.sleep, 0.001)
 
 
627
        result.extractBenchmarkTime(self)
 
 
628
        timed_string = result._testTimeString()
 
 
629
        self.assertContainsRe(
 
 
630
            timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
 
 
631
        # extracting the time from a non-bzrlib testcase sets to None
 
 
632
        result._recordTestStartTime()
 
 
633
        result.extractBenchmarkTime(
 
 
634
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
 
 
635
        timed_string = result._testTimeString()
 
 
636
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
 
 
637
        # cheat. Yes, wash thy mouth out with soap.
 
 
638
        self._benchtime = None
 
 
640
    def test_assigned_benchmark_file_stores_date(self):
 
 
642
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
647
        output_string = output.getvalue()
 
 
649
        # if you are wondering about the regexp please read the comment in
 
 
650
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
 
 
651
        # XXX: what comment?  -- Andrew Bennetts
 
 
652
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
 
654
    def test_benchhistory_records_test_times(self):
 
 
655
        result_stream = StringIO()
 
 
656
        result = bzrlib.tests.TextTestResult(
 
 
660
            bench_history=result_stream
 
 
663
        # we want profile a call and check that its test duration is recorded
 
 
664
        # make a new test instance that when run will generate a benchmark
 
 
665
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
 
666
        # execute the test, which should succeed and record times
 
 
667
        example_test_case.run(result)
 
 
668
        lines = result_stream.getvalue().splitlines()
 
 
669
        self.assertEqual(2, len(lines))
 
 
670
        self.assertContainsRe(lines[1],
 
 
671
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
 
 
672
            "._time_hello_world_encoding")
 
 
674
    def _time_hello_world_encoding(self):
 
 
675
        """Profile two sleep calls
 
 
677
        This is used to exercise the test framework.
 
 
679
        self.time(unicode, 'hello', errors='replace')
 
 
680
        self.time(unicode, 'world', errors='replace')
 
 
682
    def test_lsprofiling(self):
 
 
683
        """Verbose test result prints lsprof statistics from test cases."""
 
 
684
        self.requireFeature(test_lsprof.LSProfFeature)
 
 
685
        result_stream = StringIO()
 
 
686
        result = bzrlib.tests.VerboseTestResult(
 
 
687
            unittest._WritelnDecorator(result_stream),
 
 
691
        # we want profile a call of some sort and check it is output by
 
 
692
        # addSuccess. We dont care about addError or addFailure as they
 
 
693
        # are not that interesting for performance tuning.
 
 
694
        # make a new test instance that when run will generate a profile
 
 
695
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
 
696
        example_test_case._gather_lsprof_in_benchmarks = True
 
 
697
        # execute the test, which should succeed and record profiles
 
 
698
        example_test_case.run(result)
 
 
699
        # lsprofile_something()
 
 
700
        # if this worked we want 
 
 
701
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
 
 
702
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
 
 
703
        # (the lsprof header)
 
 
704
        # ... an arbitrary number of lines
 
 
705
        # and the function call which is time.sleep.
 
 
706
        #           1        0            ???         ???       ???(sleep) 
 
 
707
        # and then repeated but with 'world', rather than 'hello'.
 
 
708
        # this should appear in the output stream of our test result.
 
 
709
        output = result_stream.getvalue()
 
 
710
        self.assertContainsRe(output,
 
 
711
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
 
 
712
        self.assertContainsRe(output,
 
 
713
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
 
 
714
        self.assertContainsRe(output,
 
 
715
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
 
 
716
        self.assertContainsRe(output,
 
 
717
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
 
 
719
    def test_known_failure(self):
 
 
720
        """A KnownFailure being raised should trigger several result actions."""
 
 
721
        class InstrumentedTestResult(ExtendedTestResult):
 
 
723
            def report_test_start(self, test): pass
 
 
724
            def report_known_failure(self, test, err):
 
 
725
                self._call = test, err
 
 
726
        result = InstrumentedTestResult(None, None, None, None)
 
 
728
            raise KnownFailure('failed!')
 
 
729
        test = unittest.FunctionTestCase(test_function)
 
 
731
        # it should invoke 'report_known_failure'.
 
 
732
        self.assertEqual(2, len(result._call))
 
 
733
        self.assertEqual(test, result._call[0])
 
 
734
        self.assertEqual(KnownFailure, result._call[1][0])
 
 
735
        self.assertIsInstance(result._call[1][1], KnownFailure)
 
 
736
        # we dont introspec the traceback, if the rest is ok, it would be
 
 
737
        # exceptional for it not to be.
 
 
738
        # it should update the known_failure_count on the object.
 
 
739
        self.assertEqual(1, result.known_failure_count)
 
 
740
        # the result should be successful.
 
 
741
        self.assertTrue(result.wasSuccessful())
 
 
743
    def test_verbose_report_known_failure(self):
 
 
744
        # verbose test output formatting
 
 
745
        result_stream = StringIO()
 
 
746
        result = bzrlib.tests.VerboseTestResult(
 
 
747
            unittest._WritelnDecorator(result_stream),
 
 
751
        test = self.get_passing_test()
 
 
752
        result.startTest(test)
 
 
753
        result.extractBenchmarkTime(test)
 
 
754
        prefix = len(result_stream.getvalue())
 
 
755
        # the err parameter has the shape:
 
 
756
        # (class, exception object, traceback)
 
 
757
        # KnownFailures dont get their tracebacks shown though, so we
 
 
759
        err = (KnownFailure, KnownFailure('foo'), None)
 
 
760
        result.report_known_failure(test, err)
 
 
761
        output = result_stream.getvalue()[prefix:]
 
 
762
        lines = output.splitlines()
 
 
763
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
 
 
764
        self.assertEqual(lines[1], '    foo')
 
 
765
        self.assertEqual(2, len(lines))
 
 
767
    def test_text_report_known_failure(self):
 
 
768
        # text test output formatting
 
 
770
        result = bzrlib.tests.TextTestResult(
 
 
776
        test = self.get_passing_test()
 
 
777
        # this seeds the state to handle reporting the test.
 
 
778
        result.startTest(test)
 
 
779
        result.extractBenchmarkTime(test)
 
 
780
        # the err parameter has the shape:
 
 
781
        # (class, exception object, traceback)
 
 
782
        # KnownFailures dont get their tracebacks shown though, so we
 
 
784
        err = (KnownFailure, KnownFailure('foo'), None)
 
 
785
        result.report_known_failure(test, err)
 
 
788
            ('update', '[1 in 0s] passing_test', None, None),
 
 
789
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
 
 
792
        # known_failures should be printed in the summary, so if we run a test
 
 
793
        # after there are some known failures, the update prefix should match
 
 
795
        result.known_failure_count = 3
 
 
799
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
 
 
803
    def get_passing_test(self):
 
 
804
        """Return a test object that can't be run usefully."""
 
 
807
        return unittest.FunctionTestCase(passing_test)
 
 
809
    def test_add_not_supported(self):
 
 
810
        """Test the behaviour of invoking addNotSupported."""
 
 
811
        class InstrumentedTestResult(ExtendedTestResult):
 
 
812
            def report_test_start(self, test): pass
 
 
813
            def report_unsupported(self, test, feature):
 
 
814
                self._call = test, feature
 
 
815
        result = InstrumentedTestResult(None, None, None, None)
 
 
816
        test = SampleTestCase('_test_pass')
 
 
818
        result.startTest(test)
 
 
819
        result.addNotSupported(test, feature)
 
 
820
        # it should invoke 'report_unsupported'.
 
 
821
        self.assertEqual(2, len(result._call))
 
 
822
        self.assertEqual(test, result._call[0])
 
 
823
        self.assertEqual(feature, result._call[1])
 
 
824
        # the result should be successful.
 
 
825
        self.assertTrue(result.wasSuccessful())
 
 
826
        # it should record the test against a count of tests not run due to
 
 
828
        self.assertEqual(1, result.unsupported['Feature'])
 
 
829
        # and invoking it again should increment that counter
 
 
830
        result.addNotSupported(test, feature)
 
 
831
        self.assertEqual(2, result.unsupported['Feature'])
 
 
833
    def test_verbose_report_unsupported(self):
 
 
834
        # verbose test output formatting
 
 
835
        result_stream = StringIO()
 
 
836
        result = bzrlib.tests.VerboseTestResult(
 
 
837
            unittest._WritelnDecorator(result_stream),
 
 
841
        test = self.get_passing_test()
 
 
843
        result.startTest(test)
 
 
844
        result.extractBenchmarkTime(test)
 
 
845
        prefix = len(result_stream.getvalue())
 
 
846
        result.report_unsupported(test, feature)
 
 
847
        output = result_stream.getvalue()[prefix:]
 
 
848
        lines = output.splitlines()
 
 
849
        self.assertEqual(lines, ['NODEP                   0ms', "    The feature 'Feature' is not available."])
 
 
851
    def test_text_report_unsupported(self):
 
 
852
        # text test output formatting
 
 
854
        result = bzrlib.tests.TextTestResult(
 
 
860
        test = self.get_passing_test()
 
 
862
        # this seeds the state to handle reporting the test.
 
 
863
        result.startTest(test)
 
 
864
        result.extractBenchmarkTime(test)
 
 
865
        result.report_unsupported(test, feature)
 
 
866
        # no output on unsupported features
 
 
868
            [('update', '[1 in 0s] passing_test', None, None)
 
 
871
        # the number of missing features should be printed in the progress
 
 
872
        # summary, so check for that.
 
 
873
        result.unsupported = {'foo':0, 'bar':0}
 
 
877
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
 
 
881
    def test_unavailable_exception(self):
 
 
882
        """An UnavailableFeature being raised should invoke addNotSupported."""
 
 
883
        class InstrumentedTestResult(ExtendedTestResult):
 
 
885
            def report_test_start(self, test): pass
 
 
886
            def addNotSupported(self, test, feature):
 
 
887
                self._call = test, feature
 
 
888
        result = InstrumentedTestResult(None, None, None, None)
 
 
891
            raise UnavailableFeature(feature)
 
 
892
        test = unittest.FunctionTestCase(test_function)
 
 
894
        # it should invoke 'addNotSupported'.
 
 
895
        self.assertEqual(2, len(result._call))
 
 
896
        self.assertEqual(test, result._call[0])
 
 
897
        self.assertEqual(feature, result._call[1])
 
 
898
        # and not count as an error
 
 
899
        self.assertEqual(0, result.error_count)
 
 
902
class TestRunner(TestCase):
 
 
904
    def dummy_test(self):
 
 
907
    def run_test_runner(self, testrunner, test):
 
 
908
        """Run suite in testrunner, saving global state and restoring it.
 
 
910
        This current saves and restores:
 
 
911
        TestCaseInTempDir.TEST_ROOT
 
 
913
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
 
 
914
        without using this convenience method, because of our use of global state.
 
 
916
        old_root = TestCaseInTempDir.TEST_ROOT
 
 
918
            TestCaseInTempDir.TEST_ROOT = None
 
 
919
            return testrunner.run(test)
 
 
921
            TestCaseInTempDir.TEST_ROOT = old_root
 
 
923
    def test_known_failure_failed_run(self):
 
 
924
        # run a test that generates a known failure which should be printed in
 
 
925
        # the final output when real failures occur.
 
 
926
        def known_failure_test():
 
 
927
            raise KnownFailure('failed')
 
 
928
        test = unittest.TestSuite()
 
 
929
        test.addTest(unittest.FunctionTestCase(known_failure_test))
 
 
931
            raise AssertionError('foo')
 
 
932
        test.addTest(unittest.FunctionTestCase(failing_test))
 
 
934
        runner = TextTestRunner(stream=stream)
 
 
935
        result = self.run_test_runner(runner, test)
 
 
936
        lines = stream.getvalue().splitlines()
 
 
939
            '======================================================================',
 
 
940
            'FAIL: unittest.FunctionTestCase (failing_test)',
 
 
941
            '----------------------------------------------------------------------',
 
 
942
            'Traceback (most recent call last):',
 
 
943
            '    raise AssertionError(\'foo\')',
 
 
944
            'AssertionError: foo',
 
 
946
            '----------------------------------------------------------------------',
 
 
948
            'FAILED (failures=1, known_failure_count=1)'],
 
 
949
            lines[0:5] + lines[6:10] + lines[11:])
 
 
951
    def test_known_failure_ok_run(self):
 
 
952
        # run a test that generates a known failure which should be printed in the final output.
 
 
953
        def known_failure_test():
 
 
954
            raise KnownFailure('failed')
 
 
955
        test = unittest.FunctionTestCase(known_failure_test)
 
 
957
        runner = TextTestRunner(stream=stream)
 
 
958
        result = self.run_test_runner(runner, test)
 
 
959
        self.assertContainsRe(stream.getvalue(),
 
 
964
            'OK \\(known_failures=1\\)\n')
 
 
966
    def test_skipped_test(self):
 
 
967
        # run a test that is skipped, and check the suite as a whole still
 
 
969
        # skipping_test must be hidden in here so it's not run as a real test
 
 
971
            raise TestSkipped('test intentionally skipped')
 
 
973
        runner = TextTestRunner(stream=self._log_file)
 
 
974
        test = unittest.FunctionTestCase(skipping_test)
 
 
975
        result = self.run_test_runner(runner, test)
 
 
976
        self.assertTrue(result.wasSuccessful())
 
 
978
    def test_skipped_from_setup(self):
 
 
979
        class SkippedSetupTest(TestCase):
 
 
983
                self.addCleanup(self.cleanup)
 
 
984
                raise TestSkipped('skipped setup')
 
 
987
                self.fail('test reached')
 
 
992
        runner = TextTestRunner(stream=self._log_file)
 
 
993
        test = SkippedSetupTest('test_skip')
 
 
994
        result = self.run_test_runner(runner, test)
 
 
995
        self.assertTrue(result.wasSuccessful())
 
 
996
        # Check if cleanup was called the right number of times.
 
 
997
        self.assertEqual(0, test.counter)
 
 
999
    def test_skipped_from_test(self):
 
 
1000
        class SkippedTest(TestCase):
 
 
1004
                self.addCleanup(self.cleanup)
 
 
1006
            def test_skip(self):
 
 
1007
                raise TestSkipped('skipped test')
 
 
1012
        runner = TextTestRunner(stream=self._log_file)
 
 
1013
        test = SkippedTest('test_skip')
 
 
1014
        result = self.run_test_runner(runner, test)
 
 
1015
        self.assertTrue(result.wasSuccessful())
 
 
1016
        # Check if cleanup was called the right number of times.
 
 
1017
        self.assertEqual(0, test.counter)
 
 
1019
    def test_unsupported_features_listed(self):
 
 
1020
        """When unsupported features are encountered they are detailed."""
 
 
1021
        class Feature1(Feature):
 
 
1022
            def _probe(self): return False
 
 
1023
        class Feature2(Feature):
 
 
1024
            def _probe(self): return False
 
 
1025
        # create sample tests
 
 
1026
        test1 = SampleTestCase('_test_pass')
 
 
1027
        test1._test_needs_features = [Feature1()]
 
 
1028
        test2 = SampleTestCase('_test_pass')
 
 
1029
        test2._test_needs_features = [Feature2()]
 
 
1030
        test = unittest.TestSuite()
 
 
1034
        runner = TextTestRunner(stream=stream)
 
 
1035
        result = self.run_test_runner(runner, test)
 
 
1036
        lines = stream.getvalue().splitlines()
 
 
1039
            "Missing feature 'Feature1' skipped 1 tests.",
 
 
1040
            "Missing feature 'Feature2' skipped 1 tests.",
 
 
1044
    def test_bench_history(self):
 
 
1045
        # tests that the running the benchmark produces a history file
 
 
1046
        # containing a timestamp and the revision id of the bzrlib source which
 
 
1048
        workingtree = _get_bzr_source_tree()
 
 
1049
        test = TestRunner('dummy_test')
 
 
1051
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
 
 
1052
        result = self.run_test_runner(runner, test)
 
 
1053
        output_string = output.getvalue()
 
 
1054
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
 
1055
        if workingtree is not None:
 
 
1056
            revision_id = workingtree.get_parent_ids()[0]
 
 
1057
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
 
1059
    def test_success_log_deleted(self):
 
 
1060
        """Successful tests have their log deleted"""
 
 
1062
        class LogTester(TestCase):
 
 
1064
            def test_success(self):
 
 
1065
                self.log('this will be removed\n')
 
 
1067
        sio = cStringIO.StringIO()
 
 
1068
        runner = TextTestRunner(stream=sio)
 
 
1069
        test = LogTester('test_success')
 
 
1070
        result = self.run_test_runner(runner, test)
 
 
1072
        log = test._get_log()
 
 
1073
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
 
1074
        self.assertEqual('', test._log_contents)
 
 
1075
        self.assertIs(None, test._log_file_name)
 
 
1077
    def test_fail_log_kept(self):
 
 
1078
        """Failed tests have their log kept"""
 
 
1080
        class LogTester(TestCase):
 
 
1082
            def test_fail(self):
 
 
1083
                self.log('this will be kept\n')
 
 
1084
                self.fail('this test fails')
 
 
1086
        sio = cStringIO.StringIO()
 
 
1087
        runner = TextTestRunner(stream=sio)
 
 
1088
        test = LogTester('test_fail')
 
 
1089
        result = self.run_test_runner(runner, test)
 
 
1091
        text = sio.getvalue()
 
 
1092
        self.assertContainsRe(text, 'this will be kept')
 
 
1093
        self.assertContainsRe(text, 'this test fails')
 
 
1095
        log = test._get_log()
 
 
1096
        self.assertContainsRe(log, 'this will be kept')
 
 
1097
        self.assertEqual(log, test._log_contents)
 
 
1099
    def test_error_log_kept(self):
 
 
1100
        """Tests with errors have their log kept"""
 
 
1102
        class LogTester(TestCase):
 
 
1104
            def test_error(self):
 
 
1105
                self.log('this will be kept\n')
 
 
1106
                raise ValueError('random exception raised')
 
 
1108
        sio = cStringIO.StringIO()
 
 
1109
        runner = TextTestRunner(stream=sio)
 
 
1110
        test = LogTester('test_error')
 
 
1111
        result = self.run_test_runner(runner, test)
 
 
1113
        text = sio.getvalue()
 
 
1114
        self.assertContainsRe(text, 'this will be kept')
 
 
1115
        self.assertContainsRe(text, 'random exception raised')
 
 
1117
        log = test._get_log()
 
 
1118
        self.assertContainsRe(log, 'this will be kept')
 
 
1119
        self.assertEqual(log, test._log_contents)
 
 
1122
class SampleTestCase(TestCase):
 
 
1124
    def _test_pass(self):
 
 
1128
class TestTestCase(TestCase):
 
 
1129
    """Tests that test the core bzrlib TestCase."""
 
 
1131
    def test_debug_flags_sanitised(self):
 
 
1132
        """The bzrlib debug flags should be sanitised by setUp."""
 
 
1133
        # we could set something and run a test that will check
 
 
1134
        # it gets santised, but this is probably sufficient for now:
 
 
1135
        # if someone runs the test with -Dsomething it will error.
 
 
1136
        self.assertEqual(set(), bzrlib.debug.debug_flags)
 
 
1138
    def inner_test(self):
 
 
1139
        # the inner child test
 
 
1142
    def outer_child(self):
 
 
1143
        # the outer child test
 
 
1145
        self.inner_test = TestTestCase("inner_child")
 
 
1146
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
1149
        self.inner_test.run(result)
 
 
1150
        note("outer finish")
 
 
1152
    def test_trace_nesting(self):
 
 
1153
        # this tests that each test case nests its trace facility correctly.
 
 
1154
        # we do this by running a test case manually. That test case (A)
 
 
1155
        # should setup a new log, log content to it, setup a child case (B),
 
 
1156
        # which should log independently, then case (A) should log a trailer
 
 
1158
        # we do two nested children so that we can verify the state of the 
 
 
1159
        # logs after the outer child finishes is correct, which a bad clean
 
 
1160
        # up routine in tearDown might trigger a fault in our test with only
 
 
1161
        # one child, we should instead see the bad result inside our test with
 
 
1163
        # the outer child test
 
 
1164
        original_trace = bzrlib.trace._trace_file
 
 
1165
        outer_test = TestTestCase("outer_child")
 
 
1166
        result = bzrlib.tests.TextTestResult(self._log_file,
 
 
1169
        outer_test.run(result)
 
 
1170
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
 
 
1172
    def method_that_times_a_bit_twice(self):
 
 
1173
        # call self.time twice to ensure it aggregates
 
 
1174
        self.time(time.sleep, 0.007)
 
 
1175
        self.time(time.sleep, 0.007)
 
 
1177
    def test_time_creates_benchmark_in_result(self):
 
 
1178
        """Test that the TestCase.time() method accumulates a benchmark time."""
 
 
1179
        sample_test = TestTestCase("method_that_times_a_bit_twice")
 
 
1180
        output_stream = StringIO()
 
 
1181
        result = bzrlib.tests.VerboseTestResult(
 
 
1182
            unittest._WritelnDecorator(output_stream),
 
 
1185
            num_tests=sample_test.countTestCases())
 
 
1186
        sample_test.run(result)
 
 
1187
        self.assertContainsRe(
 
 
1188
            output_stream.getvalue(),
 
 
1189
            r"\d+ms/ +\d+ms\n$")
 
 
1191
    def test_hooks_sanitised(self):
 
 
1192
        """The bzrlib hooks should be sanitised by setUp."""
 
 
1193
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
 
1194
            bzrlib.branch.Branch.hooks)
 
 
1195
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
 
 
1196
            bzrlib.smart.server.SmartTCPServer.hooks)
 
 
1198
    def test__gather_lsprof_in_benchmarks(self):
 
 
1199
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
 
 
1201
        Each self.time() call is individually and separately profiled.
 
 
1203
        self.requireFeature(test_lsprof.LSProfFeature)
 
 
1204
        # overrides the class member with an instance member so no cleanup 
 
 
1206
        self._gather_lsprof_in_benchmarks = True
 
 
1207
        self.time(time.sleep, 0.000)
 
 
1208
        self.time(time.sleep, 0.003)
 
 
1209
        self.assertEqual(2, len(self._benchcalls))
 
 
1210
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
 
 
1211
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
 
 
1212
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
 
 
1213
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
 
 
1215
    def test_knownFailure(self):
 
 
1216
        """Self.knownFailure() should raise a KnownFailure exception."""
 
 
1217
        self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
 
 
1219
    def test_requireFeature_available(self):
 
 
1220
        """self.requireFeature(available) is a no-op."""
 
 
1221
        class Available(Feature):
 
 
1222
            def _probe(self):return True
 
 
1223
        feature = Available()
 
 
1224
        self.requireFeature(feature)
 
 
1226
    def test_requireFeature_unavailable(self):
 
 
1227
        """self.requireFeature(unavailable) raises UnavailableFeature."""
 
 
1228
        class Unavailable(Feature):
 
 
1229
            def _probe(self):return False
 
 
1230
        feature = Unavailable()
 
 
1231
        self.assertRaises(UnavailableFeature, self.requireFeature, feature)
 
 
1233
    def test_run_no_parameters(self):
 
 
1234
        test = SampleTestCase('_test_pass')
 
 
1237
    def test_run_enabled_unittest_result(self):
 
 
1238
        """Test we revert to regular behaviour when the test is enabled."""
 
 
1239
        test = SampleTestCase('_test_pass')
 
 
1240
        class EnabledFeature(object):
 
 
1241
            def available(self):
 
 
1243
        test._test_needs_features = [EnabledFeature()]
 
 
1244
        result = unittest.TestResult()
 
 
1246
        self.assertEqual(1, result.testsRun)
 
 
1247
        self.assertEqual([], result.errors)
 
 
1248
        self.assertEqual([], result.failures)
 
 
1250
    def test_run_disabled_unittest_result(self):
 
 
1251
        """Test our compatability for disabled tests with unittest results."""
 
 
1252
        test = SampleTestCase('_test_pass')
 
 
1253
        class DisabledFeature(object):
 
 
1254
            def available(self):
 
 
1256
        test._test_needs_features = [DisabledFeature()]
 
 
1257
        result = unittest.TestResult()
 
 
1259
        self.assertEqual(1, result.testsRun)
 
 
1260
        self.assertEqual([], result.errors)
 
 
1261
        self.assertEqual([], result.failures)
 
 
1263
    def test_run_disabled_supporting_result(self):
 
 
1264
        """Test disabled tests behaviour with support aware results."""
 
 
1265
        test = SampleTestCase('_test_pass')
 
 
1266
        class DisabledFeature(object):
 
 
1267
            def available(self):
 
 
1269
        the_feature = DisabledFeature()
 
 
1270
        test._test_needs_features = [the_feature]
 
 
1271
        class InstrumentedTestResult(unittest.TestResult):
 
 
1273
                unittest.TestResult.__init__(self)
 
 
1275
            def startTest(self, test):
 
 
1276
                self.calls.append(('startTest', test))
 
 
1277
            def stopTest(self, test):
 
 
1278
                self.calls.append(('stopTest', test))
 
 
1279
            def addNotSupported(self, test, feature):
 
 
1280
                self.calls.append(('addNotSupported', test, feature))
 
 
1281
        result = InstrumentedTestResult()
 
 
1284
            ('startTest', test),
 
 
1285
            ('addNotSupported', test, the_feature),
 
 
1291
@symbol_versioning.deprecated_function(zero_eleven)
 
 
1292
def sample_deprecated_function():
 
 
1293
    """A deprecated function to test applyDeprecated with."""
 
 
1297
def sample_undeprecated_function(a_param):
 
 
1298
    """A undeprecated function to test applyDeprecated with."""
 
 
1301
class ApplyDeprecatedHelper(object):
 
 
1302
    """A helper class for ApplyDeprecated tests."""
 
 
1304
    @symbol_versioning.deprecated_method(zero_eleven)
 
 
1305
    def sample_deprecated_method(self, param_one):
 
 
1306
        """A deprecated method for testing with."""
 
 
1309
    def sample_normal_method(self):
 
 
1310
        """A undeprecated method."""
 
 
1312
    @symbol_versioning.deprecated_method(zero_ten)
 
 
1313
    def sample_nested_deprecation(self):
 
 
1314
        return sample_deprecated_function()
 
 
1317
class TestExtraAssertions(TestCase):
 
 
1318
    """Tests for new test assertions in bzrlib test suite"""
 
 
1320
    def test_assert_isinstance(self):
 
 
1321
        self.assertIsInstance(2, int)
 
 
1322
        self.assertIsInstance(u'', basestring)
 
 
1323
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
 
 
1324
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
 
 
1326
    def test_assertEndsWith(self):
 
 
1327
        self.assertEndsWith('foo', 'oo')
 
 
1328
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
 
 
1330
    def test_applyDeprecated_not_deprecated(self):
 
 
1331
        sample_object = ApplyDeprecatedHelper()
 
 
1332
        # calling an undeprecated callable raises an assertion
 
 
1333
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
 
1334
            sample_object.sample_normal_method)
 
 
1335
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
 
1336
            sample_undeprecated_function, "a param value")
 
 
1337
        # calling a deprecated callable (function or method) with the wrong
 
 
1338
        # expected deprecation fails.
 
 
1339
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
 
1340
            sample_object.sample_deprecated_method, "a param value")
 
 
1341
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
 
1342
            sample_deprecated_function)
 
 
1343
        # calling a deprecated callable (function or method) with the right
 
 
1344
        # expected deprecation returns the functions result.
 
 
1345
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
 
 
1346
            sample_object.sample_deprecated_method, "a param value"))
 
 
1347
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
 
 
1348
            sample_deprecated_function))
 
 
1349
        # calling a nested deprecation with the wrong deprecation version
 
 
1350
        # fails even if a deeper nested function was deprecated with the 
 
 
1352
        self.assertRaises(AssertionError, self.applyDeprecated,
 
 
1353
            zero_eleven, sample_object.sample_nested_deprecation)
 
 
1354
        # calling a nested deprecation with the right deprecation value
 
 
1355
        # returns the calls result.
 
 
1356
        self.assertEqual(2, self.applyDeprecated(zero_ten,
 
 
1357
            sample_object.sample_nested_deprecation))
 
 
1359
    def test_callDeprecated(self):
 
 
1360
        def testfunc(be_deprecated, result=None):
 
 
1361
            if be_deprecated is True:
 
 
1362
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
 
 
1365
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
 
 
1366
        self.assertIs(None, result)
 
 
1367
        result = self.callDeprecated([], testfunc, False, 'result')
 
 
1368
        self.assertEqual('result', result)
 
 
1369
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
 
 
1370
        self.callDeprecated([], testfunc, be_deprecated=False)
 
 
1373
class TestConvenienceMakers(TestCaseWithTransport):
 
 
1374
    """Test for the make_* convenience functions."""
 
 
1376
    def test_make_branch_and_tree_with_format(self):
 
 
1377
        # we should be able to supply a format to make_branch_and_tree
 
 
1378
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
 
 
1379
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
 
 
1380
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
 
 
1381
                              bzrlib.bzrdir.BzrDirMetaFormat1)
 
 
1382
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
 
 
1383
                              bzrlib.bzrdir.BzrDirFormat6)
 
 
1385
    def test_make_branch_and_memory_tree(self):
 
 
1386
        # we should be able to get a new branch and a mutable tree from
 
 
1387
        # TestCaseWithTransport
 
 
1388
        tree = self.make_branch_and_memory_tree('a')
 
 
1389
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
 
 
1392
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
 
 
1394
    def test_make_tree_for_sftp_branch(self):
 
 
1395
        """Transports backed by local directories create local trees."""
 
 
1397
        tree = self.make_branch_and_tree('t1')
 
 
1398
        base = tree.bzrdir.root_transport.base
 
 
1399
        self.failIf(base.startswith('sftp'),
 
 
1400
                'base %r is on sftp but should be local' % base)
 
 
1401
        self.assertEquals(tree.bzrdir.root_transport,
 
 
1402
                tree.branch.bzrdir.root_transport)
 
 
1403
        self.assertEquals(tree.bzrdir.root_transport,
 
 
1404
                tree.branch.repository.bzrdir.root_transport)
 
 
1407
class TestSelftest(TestCase):
 
 
1408
    """Tests of bzrlib.tests.selftest."""
 
 
1410
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
 
 
1413
            factory_called.append(True)
 
 
1417
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
 
 
1418
            test_suite_factory=factory)
 
 
1419
        self.assertEqual([True], factory_called)
 
 
1422
class TestSelftestCleanOutput(TestCaseInTempDir):
 
 
1424
    def test_clean_output(self):
 
 
1425
        # test functionality of clean_selftest_output()
 
 
1426
        self.build_tree(['test0000.tmp/', 'test0001.tmp/',
 
 
1427
                         'bzrlib/', 'tests/',
 
 
1428
                         'bzr', 'setup.py', 'test9999.tmp'])
 
 
1431
        before = os.listdir(root)
 
 
1433
        self.assertEquals(['bzr','bzrlib','setup.py',
 
 
1434
                           'test0000.tmp','test0001.tmp',
 
 
1435
                           'test9999.tmp','tests'],
 
 
1437
        clean_selftest_output(root, quiet=True)
 
 
1438
        after = os.listdir(root)
 
 
1440
        self.assertEquals(['bzr','bzrlib','setup.py',
 
 
1441
                           'test9999.tmp','tests'],
 
 
1444
    def test_clean_readonly(self):
 
 
1445
        # test for delete read-only files
 
 
1446
        self.build_tree(['test0000.tmp/', 'test0000.tmp/foo'])
 
 
1447
        osutils.make_readonly('test0000.tmp/foo')
 
 
1449
        before = os.listdir(root);  before.sort()
 
 
1450
        self.assertEquals(['test0000.tmp'], before)
 
 
1451
        clean_selftest_output(root, quiet=True)
 
 
1452
        after = os.listdir(root);   after.sort()
 
 
1453
        self.assertEquals([], after)
 
 
1456
class TestKnownFailure(TestCase):
 
 
1458
    def test_known_failure(self):
 
 
1459
        """Check that KnownFailure is defined appropriately."""
 
 
1460
        # a KnownFailure is an assertion error for compatability with unaware
 
 
1462
        self.assertIsInstance(KnownFailure(""), AssertionError)
 
 
1464
    def test_expect_failure(self):
 
 
1466
            self.expectFailure("Doomed to failure", self.assertTrue, False)
 
 
1467
        except KnownFailure, e:
 
 
1468
            self.assertEqual('Doomed to failure', e.args[0])
 
 
1470
            self.expectFailure("Doomed to failure", self.assertTrue, True)
 
 
1471
        except AssertionError, e:
 
 
1472
            self.assertEqual('Unexpected success.  Should have failed:'
 
 
1473
                             ' Doomed to failure', e.args[0])
 
 
1475
            self.fail('Assertion not raised')
 
 
1478
class TestFeature(TestCase):
 
 
1480
    def test_caching(self):
 
 
1481
        """Feature._probe is called by the feature at most once."""
 
 
1482
        class InstrumentedFeature(Feature):
 
 
1484
                Feature.__init__(self)
 
 
1487
                self.calls.append('_probe')
 
 
1489
        feature = InstrumentedFeature()
 
 
1491
        self.assertEqual(['_probe'], feature.calls)
 
 
1493
        self.assertEqual(['_probe'], feature.calls)
 
 
1495
    def test_named_str(self):
 
 
1496
        """Feature.__str__ should thunk to feature_name()."""
 
 
1497
        class NamedFeature(Feature):
 
 
1498
            def feature_name(self):
 
 
1500
        feature = NamedFeature()
 
 
1501
        self.assertEqual('symlinks', str(feature))
 
 
1503
    def test_default_str(self):
 
 
1504
        """Feature.__str__ should default to __class__.__name__."""
 
 
1505
        class NamedFeature(Feature):
 
 
1507
        feature = NamedFeature()
 
 
1508
        self.assertEqual('NamedFeature', str(feature))
 
 
1511
class TestUnavailableFeature(TestCase):
 
 
1513
    def test_access_feature(self):
 
 
1515
        exception = UnavailableFeature(feature)
 
 
1516
        self.assertIs(feature, exception.args[0])
 
 
1519
class TestSelftestFiltering(TestCase):
 
 
1522
        self.suite = TestUtil.TestSuite()
 
 
1523
        self.loader = TestUtil.TestLoader()
 
 
1524
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
 
 
1525
            'bzrlib.tests.test_selftest']))
 
 
1526
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
 
 
1528
    def test_filter_suite_by_re(self):
 
 
1529
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
 
 
1530
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
 
 
1531
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
 
 
1532
            'TestSelftestFiltering.test_filter_suite_by_re'])
 
 
1534
    def test_sort_suite_by_re(self):
 
 
1535
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
 
 
1536
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
 
 
1537
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
 
 
1538
            'TestSelftestFiltering.test_filter_suite_by_re')
 
 
1539
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
 
 
1542
class TestCheckInventoryShape(TestCaseWithTransport):
 
 
1544
    def test_check_inventory_shape(self):
 
 
1545
        files = ['a', 'b/', 'b/c']
 
 
1546
        tree = self.make_branch_and_tree('.')
 
 
1547
        self.build_tree(files)
 
 
1551
            self.check_inventory_shape(tree.inventory, files)