~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2007-03-15 22:35:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2363.
  • Revision ID: john@arbash-meinel.com-20070315223535-d3d4964oe1hc8zhg
Add an overzealous test, for Unicode support of _iter_changes.
For both knowns and unknowns.
And include a basic, if suboptimal, fix.
I would rather defer the decoding until we've determined that we are going to return the tuple.
There is still something broken with added files, but I'll get to that.

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
                          TestCase,
 
42
                          TestCaseInTempDir,
 
43
                          TestCaseWithMemoryTransport,
 
44
                          TestCaseWithTransport,
 
45
                          TestSkipped,
 
46
                          TestSuite,
 
47
                          TextTestRunner,
 
48
                          )
 
49
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
50
from bzrlib.tests.TestUtil import _load_module_by_name
 
51
from bzrlib.trace import note
 
52
from bzrlib.transport.memory import MemoryServer, MemoryTransport
 
53
from bzrlib.version import _get_bzr_source_tree
 
54
 
 
55
 
 
56
class SelftestTests(TestCase):
 
57
 
 
58
    def test_import_tests(self):
 
59
        mod = _load_module_by_name('bzrlib.tests.test_selftest')
 
60
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
61
 
 
62
    def test_import_test_failure(self):
 
63
        self.assertRaises(ImportError,
 
64
                          _load_module_by_name,
 
65
                          'bzrlib.no-name-yet')
 
66
 
 
67
class MetaTestLog(TestCase):
 
68
 
 
69
    def test_logging(self):
 
70
        """Test logs are captured when a test fails."""
 
71
        self.log('a test message')
 
72
        self._log_file.flush()
 
73
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
74
                              'a test message\n')
 
75
 
 
76
 
 
77
class TestTreeShape(TestCaseInTempDir):
 
78
 
 
79
    def test_unicode_paths(self):
 
80
        filename = u'hell\u00d8'
 
81
        try:
 
82
            self.build_tree_contents([(filename, 'contents of hello')])
 
83
        except UnicodeEncodeError:
 
84
            raise TestSkipped("can't build unicode working tree in "
 
85
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
86
        self.failUnlessExists(filename)
 
87
 
 
88
 
 
89
class TestTransportProviderAdapter(TestCase):
 
90
    """A group of tests that test the transport implementation adaption core.
 
91
 
 
92
    This is a meta test that the tests are applied to all available 
 
93
    transports.
 
94
 
 
95
    This will be generalised in the future which is why it is in this 
 
96
    test file even though it is specific to transport tests at the moment.
 
97
    """
 
98
 
 
99
    def test_get_transport_permutations(self):
 
100
        # this checks that we the module get_test_permutations call
 
101
        # is made by the adapter get_transport_test_permitations method.
 
102
        class MockModule(object):
 
103
            def get_test_permutations(self):
 
104
                return sample_permutation
 
105
        sample_permutation = [(1,2), (3,4)]
 
106
        from bzrlib.transport import TransportTestProviderAdapter
 
107
        adapter = TransportTestProviderAdapter()
 
108
        self.assertEqual(sample_permutation,
 
109
                         adapter.get_transport_test_permutations(MockModule()))
 
110
 
 
111
    def test_adapter_checks_all_modules(self):
 
112
        # this checks that the adapter returns as many permurtations as
 
113
        # there are in all the registered# transport modules for there
 
114
        # - we assume if this matches its probably doing the right thing
 
115
        # especially in combination with the tests for setting the right
 
116
        # classes below.
 
117
        from bzrlib.transport import (TransportTestProviderAdapter,
 
118
                                      _get_transport_modules
 
119
                                      )
 
120
        modules = _get_transport_modules()
 
121
        permutation_count = 0
 
122
        for module in modules:
 
123
            try:
 
124
                permutation_count += len(reduce(getattr, 
 
125
                    (module + ".get_test_permutations").split('.')[1:],
 
126
                     __import__(module))())
 
127
            except errors.DependencyNotPresent:
 
128
                pass
 
129
        input_test = TestTransportProviderAdapter(
 
130
            "test_adapter_sets_transport_class")
 
131
        adapter = TransportTestProviderAdapter()
 
132
        self.assertEqual(permutation_count,
 
133
                         len(list(iter(adapter.adapt(input_test)))))
 
134
 
 
135
    def test_adapter_sets_transport_class(self):
 
136
        # Check that the test adapter inserts a transport and server into the
 
137
        # generated test.
 
138
        #
 
139
        # This test used to know about all the possible transports and the
 
140
        # order they were returned but that seems overly brittle (mbp
 
141
        # 20060307)
 
142
        input_test = TestTransportProviderAdapter(
 
143
            "test_adapter_sets_transport_class")
 
144
        from bzrlib.transport import TransportTestProviderAdapter
 
145
        suite = TransportTestProviderAdapter().adapt(input_test)
 
146
        tests = list(iter(suite))
 
147
        self.assertTrue(len(tests) > 6)
 
148
        # there are at least that many builtin transports
 
149
        one_test = tests[0]
 
150
        self.assertTrue(issubclass(one_test.transport_class, 
 
151
                                   bzrlib.transport.Transport))
 
152
        self.assertTrue(issubclass(one_test.transport_server, 
 
153
                                   bzrlib.transport.Server))
 
154
 
 
155
 
 
156
class TestBranchProviderAdapter(TestCase):
 
157
    """A group of tests that test the branch implementation test adapter."""
 
158
 
 
159
    def test_adapted_tests(self):
 
160
        # check that constructor parameters are passed through to the adapted
 
161
        # test.
 
162
        from bzrlib.branch import BranchTestProviderAdapter
 
163
        input_test = TestBranchProviderAdapter(
 
164
            "test_adapted_tests")
 
165
        server1 = "a"
 
166
        server2 = "b"
 
167
        formats = [("c", "C"), ("d", "D")]
 
168
        adapter = BranchTestProviderAdapter(server1, server2, formats)
 
169
        suite = adapter.adapt(input_test)
 
170
        tests = list(iter(suite))
 
171
        self.assertEqual(2, len(tests))
 
172
        self.assertEqual(tests[0].branch_format, formats[0][0])
 
173
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
174
        self.assertEqual(tests[0].transport_server, server1)
 
175
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
176
        self.assertEqual(tests[1].branch_format, formats[1][0])
 
177
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
178
        self.assertEqual(tests[1].transport_server, server1)
 
179
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
180
 
 
181
 
 
182
class TestBzrDirProviderAdapter(TestCase):
 
183
    """A group of tests that test the bzr dir implementation test adapter."""
 
184
 
 
185
    def test_adapted_tests(self):
 
186
        # check that constructor parameters are passed through to the adapted
 
187
        # test.
 
188
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
 
189
        input_test = TestBzrDirProviderAdapter(
 
190
            "test_adapted_tests")
 
191
        server1 = "a"
 
192
        server2 = "b"
 
193
        formats = ["c", "d"]
 
194
        adapter = BzrDirTestProviderAdapter(server1, server2, formats)
 
195
        suite = adapter.adapt(input_test)
 
196
        tests = list(iter(suite))
 
197
        self.assertEqual(2, len(tests))
 
198
        self.assertEqual(tests[0].bzrdir_format, formats[0])
 
199
        self.assertEqual(tests[0].transport_server, server1)
 
200
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
201
        self.assertEqual(tests[1].bzrdir_format, formats[1])
 
202
        self.assertEqual(tests[1].transport_server, server1)
 
203
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
204
 
 
205
 
 
206
class TestRepositoryProviderAdapter(TestCase):
 
207
    """A group of tests that test the repository implementation test adapter."""
 
208
 
 
209
    def test_adapted_tests(self):
 
210
        # check that constructor parameters are passed through to the adapted
 
211
        # test.
 
212
        from bzrlib.repository import RepositoryTestProviderAdapter
 
213
        input_test = TestRepositoryProviderAdapter(
 
214
            "test_adapted_tests")
 
215
        server1 = "a"
 
216
        server2 = "b"
 
217
        formats = [("c", "C"), ("d", "D")]
 
218
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
 
219
        suite = adapter.adapt(input_test)
 
220
        tests = list(iter(suite))
 
221
        self.assertEqual(2, len(tests))
 
222
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
223
        self.assertEqual(tests[0].repository_format, formats[0][0])
 
224
        self.assertEqual(tests[0].transport_server, server1)
 
225
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
226
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
227
        self.assertEqual(tests[1].repository_format, formats[1][0])
 
228
        self.assertEqual(tests[1].transport_server, server1)
 
229
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
230
 
 
231
 
 
232
class TestInterRepositoryProviderAdapter(TestCase):
 
233
    """A group of tests that test the InterRepository test adapter."""
 
234
 
 
235
    def test_adapted_tests(self):
 
236
        # check that constructor parameters are passed through to the adapted
 
237
        # test.
 
238
        from bzrlib.repository import InterRepositoryTestProviderAdapter
 
239
        input_test = TestInterRepositoryProviderAdapter(
 
240
            "test_adapted_tests")
 
241
        server1 = "a"
 
242
        server2 = "b"
 
243
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
244
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
 
245
        suite = adapter.adapt(input_test)
 
246
        tests = list(iter(suite))
 
247
        self.assertEqual(2, len(tests))
 
248
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
 
249
        self.assertEqual(tests[0].repository_format, formats[0][1])
 
250
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
 
251
        self.assertEqual(tests[0].transport_server, server1)
 
252
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
253
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
 
254
        self.assertEqual(tests[1].repository_format, formats[1][1])
 
255
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
 
256
        self.assertEqual(tests[1].transport_server, server1)
 
257
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
258
 
 
259
 
 
260
class TestInterVersionedFileProviderAdapter(TestCase):
 
261
    """A group of tests that test the InterVersionedFile test adapter."""
 
262
 
 
263
    def test_adapted_tests(self):
 
264
        # check that constructor parameters are passed through to the adapted
 
265
        # test.
 
266
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
 
267
        input_test = TestInterRepositoryProviderAdapter(
 
268
            "test_adapted_tests")
 
269
        server1 = "a"
 
270
        server2 = "b"
 
271
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
 
272
        adapter = InterVersionedFileTestProviderAdapter(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].interversionedfile_class, formats[0][0])
 
277
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
 
278
        self.assertEqual(tests[0].versionedfile_factory_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].interversionedfile_class, formats[1][0])
 
282
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
 
283
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
 
284
        self.assertEqual(tests[1].transport_server, server1)
 
285
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
286
 
 
287
 
 
288
class TestRevisionStoreProviderAdapter(TestCase):
 
289
    """A group of tests that test the RevisionStore test adapter."""
 
290
 
 
291
    def test_adapted_tests(self):
 
292
        # check that constructor parameters are passed through to the adapted
 
293
        # test.
 
294
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
 
295
        input_test = TestRevisionStoreProviderAdapter(
 
296
            "test_adapted_tests")
 
297
        # revision stores need a store factory - i.e. RevisionKnit
 
298
        #, a readonly and rw transport 
 
299
        # transport servers:
 
300
        server1 = "a"
 
301
        server2 = "b"
 
302
        store_factories = ["c", "d"]
 
303
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
 
304
        suite = adapter.adapt(input_test)
 
305
        tests = list(iter(suite))
 
306
        self.assertEqual(2, len(tests))
 
307
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
 
308
        self.assertEqual(tests[0].transport_server, server1)
 
309
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
310
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
 
311
        self.assertEqual(tests[1].transport_server, server1)
 
312
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
313
 
 
314
 
 
315
class TestWorkingTreeProviderAdapter(TestCase):
 
316
    """A group of tests that test the workingtree implementation test adapter."""
 
317
 
 
318
    def test_adapted_tests(self):
 
319
        # check that constructor parameters are passed through to the adapted
 
320
        # test.
 
321
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
 
322
        input_test = TestWorkingTreeProviderAdapter(
 
323
            "test_adapted_tests")
 
324
        server1 = "a"
 
325
        server2 = "b"
 
326
        formats = [("c", "C"), ("d", "D")]
 
327
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
 
328
        suite = adapter.adapt(input_test)
 
329
        tests = list(iter(suite))
 
330
        self.assertEqual(2, len(tests))
 
331
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
332
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
333
        self.assertEqual(tests[0].transport_server, server1)
 
334
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
335
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
336
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
337
        self.assertEqual(tests[1].transport_server, server1)
 
338
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
339
 
 
340
 
 
341
class TestTreeProviderAdapter(TestCase):
 
342
    """Test the setup of tree_implementation tests."""
 
343
 
 
344
    def test_adapted_tests(self):
 
345
        # the tree implementation adapter is meant to setup one instance for
 
346
        # each working tree format, and one additional instance that will
 
347
        # use the default wt format, but create a revision tree for the tests.
 
348
        # this means that the wt ones should have the workingtree_to_test_tree
 
349
        # attribute set to 'return_parameter' and the revision one set to
 
350
        # revision_tree_from_workingtree.
 
351
 
 
352
        from bzrlib.tests.tree_implementations import (
 
353
            TreeTestProviderAdapter,
 
354
            return_parameter,
 
355
            revision_tree_from_workingtree
 
356
            )
 
357
        from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
 
358
        input_test = TestTreeProviderAdapter(
 
359
            "test_adapted_tests")
 
360
        server1 = "a"
 
361
        server2 = "b"
 
362
        formats = [("c", "C"), ("d", "D")]
 
363
        adapter = TreeTestProviderAdapter(server1, server2, formats)
 
364
        suite = adapter.adapt(input_test)
 
365
        tests = list(iter(suite))
 
366
        self.assertEqual(4, len(tests))
 
367
        # this must match the default format setp up in
 
368
        # TreeTestProviderAdapter.adapt
 
369
        default_format = WorkingTreeFormat3
 
370
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
371
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
372
        self.assertEqual(tests[0].transport_server, server1)
 
373
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
374
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
375
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
376
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
377
        self.assertEqual(tests[1].transport_server, server1)
 
378
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
379
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
380
        self.assertIsInstance(tests[2].workingtree_format, default_format)
 
381
        #self.assertEqual(tests[2].bzrdir_format,
 
382
        #                 default_format._matchingbzrdir)
 
383
        self.assertEqual(tests[2].transport_server, server1)
 
384
        self.assertEqual(tests[2].transport_readonly_server, server2)
 
385
        self.assertEqual(tests[2].workingtree_to_test_tree,
 
386
            revision_tree_from_workingtree)
 
387
 
 
388
 
 
389
class TestInterTreeProviderAdapter(TestCase):
 
390
    """A group of tests that test the InterTreeTestAdapter."""
 
391
 
 
392
    def test_adapted_tests(self):
 
393
        # check that constructor parameters are passed through to the adapted
 
394
        # test.
 
395
        # for InterTree tests we want the machinery to bring up two trees in
 
396
        # each instance: the base one, and the one we are interacting with.
 
397
        # because each optimiser can be direction specific, we need to test
 
398
        # each optimiser in its chosen direction.
 
399
        # unlike the TestProviderAdapter we dont want to automatically add a
 
400
        # parameterised one for WorkingTree - the optimisers will tell us what
 
401
        # ones to add.
 
402
        from bzrlib.tests.tree_implementations import (
 
403
            return_parameter,
 
404
            revision_tree_from_workingtree
 
405
            )
 
406
        from bzrlib.tests.intertree_implementations import (
 
407
            InterTreeTestProviderAdapter,
 
408
            )
 
409
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
 
410
        input_test = TestInterTreeProviderAdapter(
 
411
            "test_adapted_tests")
 
412
        server1 = "a"
 
413
        server2 = "b"
 
414
        format1 = WorkingTreeFormat2()
 
415
        format2 = WorkingTreeFormat3()
 
416
        formats = [(str, format1, format2, "converter1"),
 
417
            (int, format2, format1, "converter2")]
 
418
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
 
419
        suite = adapter.adapt(input_test)
 
420
        tests = list(iter(suite))
 
421
        self.assertEqual(2, len(tests))
 
422
        self.assertEqual(tests[0].intertree_class, formats[0][0])
 
423
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
 
424
        self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
 
425
        self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
 
426
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
427
        self.assertEqual(tests[0].transport_server, server1)
 
428
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
429
        self.assertEqual(tests[1].intertree_class, formats[1][0])
 
430
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
 
431
        self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
 
432
        self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
 
433
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
434
        self.assertEqual(tests[1].transport_server, server1)
 
435
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
436
 
 
437
 
 
438
class TestTestCaseInTempDir(TestCaseInTempDir):
 
439
 
 
440
    def test_home_is_not_working(self):
 
441
        self.assertNotEqual(self.test_dir, self.test_home_dir)
 
442
        cwd = osutils.getcwd()
 
443
        self.assertEqual(self.test_dir, cwd)
 
444
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
445
 
 
446
 
 
447
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
 
448
 
 
449
    def test_home_is_non_existant_dir_under_root(self):
 
450
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
 
451
 
 
452
        This is because TestCaseWithMemoryTransport is for tests that do not
 
453
        need any disk resources: they should be hooked into bzrlib in such a 
 
454
        way that no global settings are being changed by the test (only a 
 
455
        few tests should need to do that), and having a missing dir as home is
 
456
        an effective way to ensure that this is the case.
 
457
        """
 
458
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
459
            self.test_home_dir)
 
460
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
461
        
 
462
    def test_cwd_is_TEST_ROOT(self):
 
463
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
464
        cwd = osutils.getcwd()
 
465
        self.assertEqual(self.test_dir, cwd)
 
466
 
 
467
    def test_make_branch_and_memory_tree(self):
 
468
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
 
469
 
 
470
        This is hard to comprehensively robustly test, so we settle for making
 
471
        a branch and checking no directory was created at its relpath.
 
472
        """
 
473
        tree = self.make_branch_and_memory_tree('dir')
 
474
        # Guard against regression into MemoryTransport leaking
 
475
        # files to disk instead of keeping them in memory.
 
476
        self.failIf(osutils.lexists('dir'))
 
477
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
478
 
 
479
    def test_make_branch_and_memory_tree_with_format(self):
 
480
        """make_branch_and_memory_tree should accept a format option."""
 
481
        format = bzrdir.BzrDirMetaFormat1()
 
482
        format.repository_format = weaverepo.RepositoryFormat7()
 
483
        tree = self.make_branch_and_memory_tree('dir', format=format)
 
484
        # Guard against regression into MemoryTransport leaking
 
485
        # files to disk instead of keeping them in memory.
 
486
        self.failIf(osutils.lexists('dir'))
 
487
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
488
        self.assertEqual(format.repository_format.__class__,
 
489
            tree.branch.repository._format.__class__)
 
490
 
 
491
 
 
492
class TestTestCaseWithTransport(TestCaseWithTransport):
 
493
    """Tests for the convenience functions TestCaseWithTransport introduces."""
 
494
 
 
495
    def test_get_readonly_url_none(self):
 
496
        from bzrlib.transport import get_transport
 
497
        from bzrlib.transport.memory import MemoryServer
 
498
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
 
499
        self.transport_server = MemoryServer
 
500
        self.transport_readonly_server = None
 
501
        # calling get_readonly_transport() constructs a decorator on the url
 
502
        # for the server
 
503
        url = self.get_readonly_url()
 
504
        url2 = self.get_readonly_url('foo/bar')
 
505
        t = get_transport(url)
 
506
        t2 = get_transport(url2)
 
507
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
508
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
 
509
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
510
 
 
511
    def test_get_readonly_url_http(self):
 
512
        from bzrlib.tests.HttpServer import HttpServer
 
513
        from bzrlib.transport import get_transport
 
514
        from bzrlib.transport.local import LocalURLServer
 
515
        from bzrlib.transport.http import HttpTransportBase
 
516
        self.transport_server = LocalURLServer
 
517
        self.transport_readonly_server = HttpServer
 
518
        # calling get_readonly_transport() gives us a HTTP server instance.
 
519
        url = self.get_readonly_url()
 
520
        url2 = self.get_readonly_url('foo/bar')
 
521
        # the transport returned may be any HttpTransportBase subclass
 
522
        t = get_transport(url)
 
523
        t2 = get_transport(url2)
 
524
        self.failUnless(isinstance(t, HttpTransportBase))
 
525
        self.failUnless(isinstance(t2, HttpTransportBase))
 
526
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
 
527
 
 
528
    def test_is_directory(self):
 
529
        """Test assertIsDirectory assertion"""
 
530
        t = self.get_transport()
 
531
        self.build_tree(['a_dir/', 'a_file'], transport=t)
 
532
        self.assertIsDirectory('a_dir', t)
 
533
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
 
534
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
 
535
 
 
536
 
 
537
class TestTestCaseTransports(TestCaseWithTransport):
 
538
 
 
539
    def setUp(self):
 
540
        super(TestTestCaseTransports, self).setUp()
 
541
        self.transport_server = MemoryServer
 
542
 
 
543
    def test_make_bzrdir_preserves_transport(self):
 
544
        t = self.get_transport()
 
545
        result_bzrdir = self.make_bzrdir('subdir')
 
546
        self.assertIsInstance(result_bzrdir.transport, 
 
547
                              MemoryTransport)
 
548
        # should not be on disk, should only be in memory
 
549
        self.failIfExists('subdir')
 
550
 
 
551
 
 
552
class TestChrootedTest(ChrootedTestCase):
 
553
 
 
554
    def test_root_is_root(self):
 
555
        from bzrlib.transport import get_transport
 
556
        t = get_transport(self.get_readonly_url())
 
557
        url = t.base
 
558
        self.assertEqual(url, t.clone('..').base)
 
559
 
 
560
 
 
561
class MockProgress(_BaseProgressBar):
 
562
    """Progress-bar standin that records calls.
 
563
 
 
564
    Useful for testing pb using code.
 
565
    """
 
566
 
 
567
    def __init__(self):
 
568
        _BaseProgressBar.__init__(self)
 
569
        self.calls = []
 
570
 
 
571
    def tick(self):
 
572
        self.calls.append(('tick',))
 
573
 
 
574
    def update(self, msg=None, current=None, total=None):
 
575
        self.calls.append(('update', msg, current, total))
 
576
 
 
577
    def clear(self):
 
578
        self.calls.append(('clear',))
 
579
 
 
580
    def note(self, msg, *args):
 
581
        self.calls.append(('note', msg, args))
 
582
 
 
583
 
 
584
class TestTestResult(TestCase):
 
585
 
 
586
    def test_elapsed_time_with_benchmarking(self):
 
587
        result = bzrlib.tests.TextTestResult(self._log_file,
 
588
                                        descriptions=0,
 
589
                                        verbosity=1,
 
590
                                        )
 
591
        result._recordTestStartTime()
 
592
        time.sleep(0.003)
 
593
        result.extractBenchmarkTime(self)
 
594
        timed_string = result._testTimeString()
 
595
        # without explicit benchmarking, we should get a simple time.
 
596
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
 
597
        # if a benchmark time is given, we want a x of y style result.
 
598
        self.time(time.sleep, 0.001)
 
599
        result.extractBenchmarkTime(self)
 
600
        timed_string = result._testTimeString()
 
601
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
 
602
        # extracting the time from a non-bzrlib testcase sets to None
 
603
        result._recordTestStartTime()
 
604
        result.extractBenchmarkTime(
 
605
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
 
606
        timed_string = result._testTimeString()
 
607
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
 
608
        # cheat. Yes, wash thy mouth out with soap.
 
609
        self._benchtime = None
 
610
 
 
611
    def test_assigned_benchmark_file_stores_date(self):
 
612
        output = StringIO()
 
613
        result = bzrlib.tests.TextTestResult(self._log_file,
 
614
                                        descriptions=0,
 
615
                                        verbosity=1,
 
616
                                        bench_history=output
 
617
                                        )
 
618
        output_string = output.getvalue()
 
619
        
 
620
        # if you are wondering about the regexp please read the comment in
 
621
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
 
622
        # XXX: what comment?  -- Andrew Bennetts
 
623
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
624
 
 
625
    def test_benchhistory_records_test_times(self):
 
626
        result_stream = StringIO()
 
627
        result = bzrlib.tests.TextTestResult(
 
628
            self._log_file,
 
629
            descriptions=0,
 
630
            verbosity=1,
 
631
            bench_history=result_stream
 
632
            )
 
633
 
 
634
        # we want profile a call and check that its test duration is recorded
 
635
        # make a new test instance that when run will generate a benchmark
 
636
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
637
        # execute the test, which should succeed and record times
 
638
        example_test_case.run(result)
 
639
        lines = result_stream.getvalue().splitlines()
 
640
        self.assertEqual(2, len(lines))
 
641
        self.assertContainsRe(lines[1],
 
642
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
 
643
            "._time_hello_world_encoding")
 
644
 
 
645
    def _time_hello_world_encoding(self):
 
646
        """Profile two sleep calls
 
647
        
 
648
        This is used to exercise the test framework.
 
649
        """
 
650
        self.time(unicode, 'hello', errors='replace')
 
651
        self.time(unicode, 'world', errors='replace')
 
652
 
 
653
    def test_lsprofiling(self):
 
654
        """Verbose test result prints lsprof statistics from test cases."""
 
655
        try:
 
656
            import bzrlib.lsprof
 
657
        except ImportError:
 
658
            raise TestSkipped("lsprof not installed.")
 
659
        result_stream = StringIO()
 
660
        result = bzrlib.tests.VerboseTestResult(
 
661
            unittest._WritelnDecorator(result_stream),
 
662
            descriptions=0,
 
663
            verbosity=2,
 
664
            )
 
665
        # we want profile a call of some sort and check it is output by
 
666
        # addSuccess. We dont care about addError or addFailure as they
 
667
        # are not that interesting for performance tuning.
 
668
        # make a new test instance that when run will generate a profile
 
669
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
670
        example_test_case._gather_lsprof_in_benchmarks = True
 
671
        # execute the test, which should succeed and record profiles
 
672
        example_test_case.run(result)
 
673
        # lsprofile_something()
 
674
        # if this worked we want 
 
675
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
 
676
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
 
677
        # (the lsprof header)
 
678
        # ... an arbitrary number of lines
 
679
        # and the function call which is time.sleep.
 
680
        #           1        0            ???         ???       ???(sleep) 
 
681
        # and then repeated but with 'world', rather than 'hello'.
 
682
        # this should appear in the output stream of our test result.
 
683
        output = result_stream.getvalue()
 
684
        self.assertContainsRe(output,
 
685
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
 
686
        self.assertContainsRe(output,
 
687
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
 
688
        self.assertContainsRe(output,
 
689
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
 
690
        self.assertContainsRe(output,
 
691
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
 
692
 
 
693
 
 
694
class TestRunner(TestCase):
 
695
 
 
696
    def dummy_test(self):
 
697
        pass
 
698
 
 
699
    def run_test_runner(self, testrunner, test):
 
700
        """Run suite in testrunner, saving global state and restoring it.
 
701
 
 
702
        This current saves and restores:
 
703
        TestCaseInTempDir.TEST_ROOT
 
704
        
 
705
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
 
706
        without using this convenience method, because of our use of global state.
 
707
        """
 
708
        old_root = TestCaseInTempDir.TEST_ROOT
 
709
        try:
 
710
            TestCaseInTempDir.TEST_ROOT = None
 
711
            return testrunner.run(test)
 
712
        finally:
 
713
            TestCaseInTempDir.TEST_ROOT = old_root
 
714
 
 
715
    def test_skipped_test(self):
 
716
        # run a test that is skipped, and check the suite as a whole still
 
717
        # succeeds.
 
718
        # skipping_test must be hidden in here so it's not run as a real test
 
719
        def skipping_test():
 
720
            raise TestSkipped('test intentionally skipped')
 
721
 
 
722
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
723
        test = unittest.FunctionTestCase(skipping_test)
 
724
        result = self.run_test_runner(runner, test)
 
725
        self.assertTrue(result.wasSuccessful())
 
726
 
 
727
    def test_skipped_from_setup(self):
 
728
        class SkippedSetupTest(TestCase):
 
729
 
 
730
            def setUp(self):
 
731
                self.counter = 1
 
732
                self.addCleanup(self.cleanup)
 
733
                raise TestSkipped('skipped setup')
 
734
 
 
735
            def test_skip(self):
 
736
                self.fail('test reached')
 
737
 
 
738
            def cleanup(self):
 
739
                self.counter -= 1
 
740
 
 
741
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
742
        test = SkippedSetupTest('test_skip')
 
743
        result = self.run_test_runner(runner, test)
 
744
        self.assertTrue(result.wasSuccessful())
 
745
        # Check if cleanup was called the right number of times.
 
746
        self.assertEqual(0, test.counter)
 
747
 
 
748
    def test_skipped_from_test(self):
 
749
        class SkippedTest(TestCase):
 
750
 
 
751
            def setUp(self):
 
752
                self.counter = 1
 
753
                self.addCleanup(self.cleanup)
 
754
 
 
755
            def test_skip(self):
 
756
                raise TestSkipped('skipped test')
 
757
 
 
758
            def cleanup(self):
 
759
                self.counter -= 1
 
760
 
 
761
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
762
        test = SkippedTest('test_skip')
 
763
        result = self.run_test_runner(runner, test)
 
764
        self.assertTrue(result.wasSuccessful())
 
765
        # Check if cleanup was called the right number of times.
 
766
        self.assertEqual(0, test.counter)
 
767
 
 
768
    def test_bench_history(self):
 
769
        # tests that the running the benchmark produces a history file
 
770
        # containing a timestamp and the revision id of the bzrlib source which
 
771
        # was tested.
 
772
        workingtree = _get_bzr_source_tree()
 
773
        test = TestRunner('dummy_test')
 
774
        output = StringIO()
 
775
        runner = TextTestRunner(stream=self._log_file, bench_history=output)
 
776
        result = self.run_test_runner(runner, test)
 
777
        output_string = output.getvalue()
 
778
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
779
        if workingtree is not None:
 
780
            revision_id = workingtree.get_parent_ids()[0]
 
781
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
782
 
 
783
    def test_success_log_deleted(self):
 
784
        """Successful tests have their log deleted"""
 
785
 
 
786
        class LogTester(TestCase):
 
787
 
 
788
            def test_success(self):
 
789
                self.log('this will be removed\n')
 
790
 
 
791
        sio = cStringIO.StringIO()
 
792
        runner = TextTestRunner(stream=sio)
 
793
        test = LogTester('test_success')
 
794
        result = self.run_test_runner(runner, test)
 
795
 
 
796
        log = test._get_log()
 
797
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
798
        self.assertEqual('', test._log_contents)
 
799
        self.assertIs(None, test._log_file_name)
 
800
 
 
801
    def test_fail_log_kept(self):
 
802
        """Failed tests have their log kept"""
 
803
 
 
804
        class LogTester(TestCase):
 
805
 
 
806
            def test_fail(self):
 
807
                self.log('this will be kept\n')
 
808
                self.fail('this test fails')
 
809
 
 
810
        sio = cStringIO.StringIO()
 
811
        runner = TextTestRunner(stream=sio)
 
812
        test = LogTester('test_fail')
 
813
        result = self.run_test_runner(runner, test)
 
814
 
 
815
        text = sio.getvalue()
 
816
        self.assertContainsRe(text, 'this will be kept')
 
817
        self.assertContainsRe(text, 'this test fails')
 
818
 
 
819
        log = test._get_log()
 
820
        self.assertContainsRe(log, 'this will be kept')
 
821
        self.assertEqual(log, test._log_contents)
 
822
 
 
823
    def test_error_log_kept(self):
 
824
        """Tests with errors have their log kept"""
 
825
 
 
826
        class LogTester(TestCase):
 
827
 
 
828
            def test_error(self):
 
829
                self.log('this will be kept\n')
 
830
                raise ValueError('random exception raised')
 
831
 
 
832
        sio = cStringIO.StringIO()
 
833
        runner = TextTestRunner(stream=sio)
 
834
        test = LogTester('test_error')
 
835
        result = self.run_test_runner(runner, test)
 
836
 
 
837
        text = sio.getvalue()
 
838
        self.assertContainsRe(text, 'this will be kept')
 
839
        self.assertContainsRe(text, 'random exception raised')
 
840
 
 
841
        log = test._get_log()
 
842
        self.assertContainsRe(log, 'this will be kept')
 
843
        self.assertEqual(log, test._log_contents)
 
844
 
 
845
 
 
846
class TestTestCase(TestCase):
 
847
    """Tests that test the core bzrlib TestCase."""
 
848
 
 
849
    def inner_test(self):
 
850
        # the inner child test
 
851
        note("inner_test")
 
852
 
 
853
    def outer_child(self):
 
854
        # the outer child test
 
855
        note("outer_start")
 
856
        self.inner_test = TestTestCase("inner_child")
 
857
        result = bzrlib.tests.TextTestResult(self._log_file,
 
858
                                        descriptions=0,
 
859
                                        verbosity=1)
 
860
        self.inner_test.run(result)
 
861
        note("outer finish")
 
862
 
 
863
    def test_trace_nesting(self):
 
864
        # this tests that each test case nests its trace facility correctly.
 
865
        # we do this by running a test case manually. That test case (A)
 
866
        # should setup a new log, log content to it, setup a child case (B),
 
867
        # which should log independently, then case (A) should log a trailer
 
868
        # and return.
 
869
        # we do two nested children so that we can verify the state of the 
 
870
        # logs after the outer child finishes is correct, which a bad clean
 
871
        # up routine in tearDown might trigger a fault in our test with only
 
872
        # one child, we should instead see the bad result inside our test with
 
873
        # the two children.
 
874
        # the outer child test
 
875
        original_trace = bzrlib.trace._trace_file
 
876
        outer_test = TestTestCase("outer_child")
 
877
        result = bzrlib.tests.TextTestResult(self._log_file,
 
878
                                        descriptions=0,
 
879
                                        verbosity=1)
 
880
        outer_test.run(result)
 
881
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
 
882
 
 
883
    def method_that_times_a_bit_twice(self):
 
884
        # call self.time twice to ensure it aggregates
 
885
        self.time(time.sleep, 0.007)
 
886
        self.time(time.sleep, 0.007)
 
887
 
 
888
    def test_time_creates_benchmark_in_result(self):
 
889
        """Test that the TestCase.time() method accumulates a benchmark time."""
 
890
        sample_test = TestTestCase("method_that_times_a_bit_twice")
 
891
        output_stream = StringIO()
 
892
        result = bzrlib.tests.VerboseTestResult(
 
893
            unittest._WritelnDecorator(output_stream),
 
894
            descriptions=0,
 
895
            verbosity=2,
 
896
            num_tests=sample_test.countTestCases())
 
897
        sample_test.run(result)
 
898
        self.assertContainsRe(
 
899
            output_stream.getvalue(),
 
900
            r"\d+ms/ +\d+ms\n$")
 
901
 
 
902
    def test_hooks_sanitised(self):
 
903
        """The bzrlib hooks should be sanitised by setUp."""
 
904
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
905
            bzrlib.branch.Branch.hooks)
 
906
 
 
907
    def test__gather_lsprof_in_benchmarks(self):
 
908
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
 
909
        
 
910
        Each self.time() call is individually and separately profiled.
 
911
        """
 
912
        try:
 
913
            import bzrlib.lsprof
 
914
        except ImportError:
 
915
            raise TestSkipped("lsprof not installed.")
 
916
        # overrides the class member with an instance member so no cleanup 
 
917
        # needed.
 
918
        self._gather_lsprof_in_benchmarks = True
 
919
        self.time(time.sleep, 0.000)
 
920
        self.time(time.sleep, 0.003)
 
921
        self.assertEqual(2, len(self._benchcalls))
 
922
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
 
923
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
 
924
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
 
925
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
 
926
 
 
927
 
 
928
@symbol_versioning.deprecated_function(zero_eleven)
 
929
def sample_deprecated_function():
 
930
    """A deprecated function to test applyDeprecated with."""
 
931
    return 2
 
932
 
 
933
 
 
934
def sample_undeprecated_function(a_param):
 
935
    """A undeprecated function to test applyDeprecated with."""
 
936
 
 
937
 
 
938
class ApplyDeprecatedHelper(object):
 
939
    """A helper class for ApplyDeprecated tests."""
 
940
 
 
941
    @symbol_versioning.deprecated_method(zero_eleven)
 
942
    def sample_deprecated_method(self, param_one):
 
943
        """A deprecated method for testing with."""
 
944
        return param_one
 
945
 
 
946
    def sample_normal_method(self):
 
947
        """A undeprecated method."""
 
948
 
 
949
    @symbol_versioning.deprecated_method(zero_ten)
 
950
    def sample_nested_deprecation(self):
 
951
        return sample_deprecated_function()
 
952
 
 
953
 
 
954
class TestExtraAssertions(TestCase):
 
955
    """Tests for new test assertions in bzrlib test suite"""
 
956
 
 
957
    def test_assert_isinstance(self):
 
958
        self.assertIsInstance(2, int)
 
959
        self.assertIsInstance(u'', basestring)
 
960
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
 
961
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
 
962
 
 
963
    def test_assertEndsWith(self):
 
964
        self.assertEndsWith('foo', 'oo')
 
965
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
 
966
 
 
967
    def test_applyDeprecated_not_deprecated(self):
 
968
        sample_object = ApplyDeprecatedHelper()
 
969
        # calling an undeprecated callable raises an assertion
 
970
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
971
            sample_object.sample_normal_method)
 
972
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
973
            sample_undeprecated_function, "a param value")
 
974
        # calling a deprecated callable (function or method) with the wrong
 
975
        # expected deprecation fails.
 
976
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
977
            sample_object.sample_deprecated_method, "a param value")
 
978
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
979
            sample_deprecated_function)
 
980
        # calling a deprecated callable (function or method) with the right
 
981
        # expected deprecation returns the functions result.
 
982
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
 
983
            sample_object.sample_deprecated_method, "a param value"))
 
984
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
 
985
            sample_deprecated_function))
 
986
        # calling a nested deprecation with the wrong deprecation version
 
987
        # fails even if a deeper nested function was deprecated with the 
 
988
        # supplied version.
 
989
        self.assertRaises(AssertionError, self.applyDeprecated,
 
990
            zero_eleven, sample_object.sample_nested_deprecation)
 
991
        # calling a nested deprecation with the right deprecation value
 
992
        # returns the calls result.
 
993
        self.assertEqual(2, self.applyDeprecated(zero_ten,
 
994
            sample_object.sample_nested_deprecation))
 
995
 
 
996
    def test_callDeprecated(self):
 
997
        def testfunc(be_deprecated, result=None):
 
998
            if be_deprecated is True:
 
999
                symbol_versioning.warn('i am deprecated', DeprecationWarning, 
 
1000
                                       stacklevel=1)
 
1001
            return result
 
1002
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
 
1003
        self.assertIs(None, result)
 
1004
        result = self.callDeprecated([], testfunc, False, 'result')
 
1005
        self.assertEqual('result', result)
 
1006
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
 
1007
        self.callDeprecated([], testfunc, be_deprecated=False)
 
1008
 
 
1009
 
 
1010
class TestConvenienceMakers(TestCaseWithTransport):
 
1011
    """Test for the make_* convenience functions."""
 
1012
 
 
1013
    def test_make_branch_and_tree_with_format(self):
 
1014
        # we should be able to supply a format to make_branch_and_tree
 
1015
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
 
1016
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
 
1017
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
 
1018
                              bzrlib.bzrdir.BzrDirMetaFormat1)
 
1019
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
 
1020
                              bzrlib.bzrdir.BzrDirFormat6)
 
1021
 
 
1022
    def test_make_branch_and_memory_tree(self):
 
1023
        # we should be able to get a new branch and a mutable tree from
 
1024
        # TestCaseWithTransport
 
1025
        tree = self.make_branch_and_memory_tree('a')
 
1026
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
 
1027
 
 
1028
 
 
1029
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
 
1030
 
 
1031
    def test_make_tree_for_sftp_branch(self):
 
1032
        """Transports backed by local directories create local trees."""
 
1033
 
 
1034
        tree = self.make_branch_and_tree('t1')
 
1035
        base = tree.bzrdir.root_transport.base
 
1036
        self.failIf(base.startswith('sftp'),
 
1037
                'base %r is on sftp but should be local' % base)
 
1038
        self.assertEquals(tree.bzrdir.root_transport,
 
1039
                tree.branch.bzrdir.root_transport)
 
1040
        self.assertEquals(tree.bzrdir.root_transport,
 
1041
                tree.branch.repository.bzrdir.root_transport)
 
1042
 
 
1043
 
 
1044
class TestSelftest(TestCase):
 
1045
    """Tests of bzrlib.tests.selftest."""
 
1046
 
 
1047
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
 
1048
        factory_called = []
 
1049
        def factory():
 
1050
            factory_called.append(True)
 
1051
            return TestSuite()
 
1052
        out = StringIO()
 
1053
        err = StringIO()
 
1054
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
 
1055
            test_suite_factory=factory)
 
1056
        self.assertEqual([True], factory_called)
 
1057
 
 
1058
 
 
1059
class TestSelftestCleanOutput(TestCaseInTempDir):
 
1060
 
 
1061
    def test_clean_output(self):
 
1062
        # test functionality of clean_selftest_output()
 
1063
        from bzrlib.tests import clean_selftest_output
 
1064
 
 
1065
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
 
1066
        files = ('bzr', 'setup.py', 'test9999.tmp')
 
1067
        for i in dirs:
 
1068
            os.mkdir(i)
 
1069
        for i in files:
 
1070
            f = file(i, 'wb')
 
1071
            f.write('content of ')
 
1072
            f.write(i)
 
1073
            f.close()
 
1074
 
 
1075
        root = os.getcwdu()
 
1076
        before = os.listdir(root)
 
1077
        before.sort()
 
1078
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1079
                           'test0000.tmp','test0001.tmp',
 
1080
                           'test9999.tmp','tests'],
 
1081
                           before)
 
1082
        clean_selftest_output(root, quiet=True)
 
1083
        after = os.listdir(root)
 
1084
        after.sort()
 
1085
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1086
                           'test9999.tmp','tests'],
 
1087
                           after)