/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Alexander Belchenko
  • Date: 2007-03-28 14:48:07 UTC
  • mto: This revision was merged to the branch mainline in revision 2407.
  • Revision ID: bialix@ukr.net-20070328144807-dhektxjnz7axuq8j
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32

Show diffs side-by-side

added added

removed removed

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