~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2005-08-23 06:52:09 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050823065209-81cd5962c401751b
move io redirection into each test case from the global runner

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for the test framework."""
18
 
 
19
 
from cStringIO import StringIO
20
 
import os
21
 
import sys
22
 
import time
23
 
import unittest
24
 
import warnings
25
 
 
26
 
import bzrlib
27
 
from bzrlib import (
28
 
    branchbuilder,
29
 
    bzrdir,
30
 
    debug,
31
 
    errors,
32
 
    lockdir,
33
 
    memorytree,
34
 
    osutils,
35
 
    progress,
36
 
    remote,
37
 
    repository,
38
 
    symbol_versioning,
39
 
    tests,
40
 
    workingtree,
41
 
    )
42
 
from bzrlib.repofmt import (
43
 
    pack_repo,
44
 
    weaverepo,
45
 
    )
46
 
from bzrlib.symbol_versioning import (
47
 
    deprecated_function,
48
 
    deprecated_in,
49
 
    deprecated_method,
50
 
    )
51
 
from bzrlib.tests import (
52
 
    test_lsprof,
53
 
    test_sftp_transport,
54
 
    TestUtil,
55
 
    )
56
 
from bzrlib.trace import note
57
 
from bzrlib.transport.memory import MemoryServer, MemoryTransport
58
 
from bzrlib.version import _get_bzr_source_tree
59
 
 
60
 
 
61
 
def _test_ids(test_suite):
62
 
    """Get the ids for the tests in a test suite."""
63
 
    return [t.id() for t in tests.iter_suite_tests(test_suite)]
64
 
 
65
 
 
66
 
class SelftestTests(tests.TestCase):
67
 
 
68
 
    def test_import_tests(self):
69
 
        mod = TestUtil._load_module_by_name('bzrlib.tests.test_selftest')
70
 
        self.assertEqual(mod.SelftestTests, SelftestTests)
71
 
 
72
 
    def test_import_test_failure(self):
73
 
        self.assertRaises(ImportError,
74
 
                          TestUtil._load_module_by_name,
75
 
                          'bzrlib.no-name-yet')
76
 
 
77
 
class MetaTestLog(tests.TestCase):
78
 
 
79
 
    def test_logging(self):
80
 
        """Test logs are captured when a test fails."""
81
 
        self.log('a test message')
82
 
        self._log_file.flush()
83
 
        self.assertContainsRe(self._get_log(keep_log_file=True),
84
 
                              'a test message\n')
85
 
 
86
 
 
87
 
class TestUnicodeFilename(tests.TestCase):
88
 
 
89
 
    def test_probe_passes(self):
90
 
        """UnicodeFilename._probe passes."""
91
 
        # We can't test much more than that because the behaviour depends
92
 
        # on the platform.
93
 
        tests.UnicodeFilename._probe()
94
 
 
95
 
 
96
 
class TestTreeShape(tests.TestCaseInTempDir):
97
 
 
98
 
    def test_unicode_paths(self):
99
 
        self.requireFeature(tests.UnicodeFilename)
100
 
 
101
 
        filename = u'hell\u00d8'
102
 
        self.build_tree_contents([(filename, 'contents of hello')])
103
 
        self.failUnlessExists(filename)
104
 
 
105
 
 
106
 
class TestTransportScenarios(tests.TestCase):
107
 
    """A group of tests that test the transport implementation adaption core.
108
 
 
109
 
    This is a meta test that the tests are applied to all available
110
 
    transports.
111
 
 
112
 
    This will be generalised in the future which is why it is in this
113
 
    test file even though it is specific to transport tests at the moment.
114
 
    """
115
 
 
116
 
    def test_get_transport_permutations(self):
117
 
        # this checks that get_test_permutations defined by the module is
118
 
        # called by the get_transport_test_permutations function.
119
 
        class MockModule(object):
120
 
            def get_test_permutations(self):
121
 
                return sample_permutation
122
 
        sample_permutation = [(1,2), (3,4)]
123
 
        from bzrlib.tests.test_transport_implementations \
124
 
            import get_transport_test_permutations
125
 
        self.assertEqual(sample_permutation,
126
 
                         get_transport_test_permutations(MockModule()))
127
 
 
128
 
    def test_scenarios_invlude_all_modules(self):
129
 
        # this checks that the scenario generator returns as many permutations
130
 
        # as there are in all the registered transport modules - we assume if
131
 
        # this matches its probably doing the right thing especially in
132
 
        # combination with the tests for setting the right classes below.
133
 
        from bzrlib.tests.test_transport_implementations \
134
 
            import transport_test_permutations
135
 
        from bzrlib.transport import _get_transport_modules
136
 
        modules = _get_transport_modules()
137
 
        permutation_count = 0
138
 
        for module in modules:
139
 
            try:
140
 
                permutation_count += len(reduce(getattr,
141
 
                    (module + ".get_test_permutations").split('.')[1:],
142
 
                     __import__(module))())
143
 
            except errors.DependencyNotPresent:
144
 
                pass
145
 
        scenarios = transport_test_permutations()
146
 
        self.assertEqual(permutation_count, len(scenarios))
147
 
 
148
 
    def test_scenarios_include_transport_class(self):
149
 
        # This test used to know about all the possible transports and the
150
 
        # order they were returned but that seems overly brittle (mbp
151
 
        # 20060307)
152
 
        from bzrlib.tests.test_transport_implementations \
153
 
            import transport_test_permutations
154
 
        scenarios = transport_test_permutations()
155
 
        # there are at least that many builtin transports
156
 
        self.assertTrue(len(scenarios) > 6)
157
 
        one_scenario = scenarios[0]
158
 
        self.assertIsInstance(one_scenario[0], str)
159
 
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
160
 
                                   bzrlib.transport.Transport))
161
 
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
162
 
                                   bzrlib.transport.Server))
163
 
 
164
 
 
165
 
class TestBranchScenarios(tests.TestCase):
166
 
 
167
 
    def test_scenarios(self):
168
 
        # check that constructor parameters are passed through to the adapted
169
 
        # test.
170
 
        from bzrlib.tests.branch_implementations import make_scenarios
171
 
        server1 = "a"
172
 
        server2 = "b"
173
 
        formats = [("c", "C"), ("d", "D")]
174
 
        scenarios = make_scenarios(server1, server2, formats)
175
 
        self.assertEqual(2, len(scenarios))
176
 
        self.assertEqual([
177
 
            ('str',
178
 
             {'branch_format': 'c',
179
 
              'bzrdir_format': 'C',
180
 
              'transport_readonly_server': 'b',
181
 
              'transport_server': 'a'}),
182
 
            ('str',
183
 
             {'branch_format': 'd',
184
 
              'bzrdir_format': 'D',
185
 
              'transport_readonly_server': 'b',
186
 
              'transport_server': 'a'})],
187
 
            scenarios)
188
 
 
189
 
 
190
 
class TestBzrDirScenarios(tests.TestCase):
191
 
 
192
 
    def test_scenarios(self):
193
 
        # check that constructor parameters are passed through to the adapted
194
 
        # test.
195
 
        from bzrlib.tests.bzrdir_implementations import make_scenarios
196
 
        vfs_factory = "v"
197
 
        server1 = "a"
198
 
        server2 = "b"
199
 
        formats = ["c", "d"]
200
 
        scenarios = make_scenarios(vfs_factory, server1, server2, formats)
201
 
        self.assertEqual([
202
 
            ('str',
203
 
             {'bzrdir_format': 'c',
204
 
              'transport_readonly_server': 'b',
205
 
              'transport_server': 'a',
206
 
              'vfs_transport_factory': 'v'}),
207
 
            ('str',
208
 
             {'bzrdir_format': 'd',
209
 
              'transport_readonly_server': 'b',
210
 
              'transport_server': 'a',
211
 
              'vfs_transport_factory': 'v'})],
212
 
            scenarios)
213
 
 
214
 
 
215
 
class TestRepositoryScenarios(tests.TestCase):
216
 
 
217
 
    def test_formats_to_scenarios(self):
218
 
        from bzrlib.tests.per_repository import formats_to_scenarios
219
 
        formats = [("(c)", remote.RemoteRepositoryFormat()),
220
 
                   ("(d)", repository.format_registry.get(
221
 
                        'Bazaar pack repository format 1 (needs bzr 0.92)\n'))]
222
 
        no_vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
223
 
            None)
224
 
        vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
225
 
            vfs_transport_factory="vfs")
226
 
        # no_vfs generate scenarios without vfs_transport_factory
227
 
        self.assertEqual([
228
 
            ('RemoteRepositoryFormat(c)',
229
 
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
230
 
              'repository_format': remote.RemoteRepositoryFormat(),
231
 
              'transport_readonly_server': 'readonly',
232
 
              'transport_server': 'server'}),
233
 
            ('RepositoryFormatKnitPack1(d)',
234
 
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
235
 
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
236
 
              'transport_readonly_server': 'readonly',
237
 
              'transport_server': 'server'})],
238
 
            no_vfs_scenarios)
239
 
        self.assertEqual([
240
 
            ('RemoteRepositoryFormat(c)',
241
 
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
242
 
              'repository_format': remote.RemoteRepositoryFormat(),
243
 
              'transport_readonly_server': 'readonly',
244
 
              'transport_server': 'server',
245
 
              'vfs_transport_factory': 'vfs'}),
246
 
            ('RepositoryFormatKnitPack1(d)',
247
 
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
248
 
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
249
 
              'transport_readonly_server': 'readonly',
250
 
              'transport_server': 'server',
251
 
              'vfs_transport_factory': 'vfs'})],
252
 
            vfs_scenarios)
253
 
 
254
 
 
255
 
class TestTestScenarioApplication(tests.TestCase):
256
 
    """Tests for the test adaption facilities."""
257
 
 
258
 
    def test_apply_scenario(self):
259
 
        from bzrlib.tests import apply_scenario
260
 
        input_test = TestTestScenarioApplication("test_apply_scenario")
261
 
        # setup two adapted tests
262
 
        adapted_test1 = apply_scenario(input_test,
263
 
            ("new id",
264
 
            {"bzrdir_format":"bzr_format",
265
 
             "repository_format":"repo_fmt",
266
 
             "transport_server":"transport_server",
267
 
             "transport_readonly_server":"readonly-server"}))
268
 
        adapted_test2 = apply_scenario(input_test,
269
 
            ("new id 2", {"bzrdir_format":None}))
270
 
        # input_test should have been altered.
271
 
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
272
 
        # the new tests are mutually incompatible, ensuring it has
273
 
        # made new ones, and unspecified elements in the scenario
274
 
        # should not have been altered.
275
 
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
276
 
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
277
 
        self.assertEqual("transport_server", adapted_test1.transport_server)
278
 
        self.assertEqual("readonly-server",
279
 
            adapted_test1.transport_readonly_server)
280
 
        self.assertEqual(
281
 
            "bzrlib.tests.test_selftest.TestTestScenarioApplication."
282
 
            "test_apply_scenario(new id)",
283
 
            adapted_test1.id())
284
 
        self.assertEqual(None, adapted_test2.bzrdir_format)
285
 
        self.assertEqual(
286
 
            "bzrlib.tests.test_selftest.TestTestScenarioApplication."
287
 
            "test_apply_scenario(new id 2)",
288
 
            adapted_test2.id())
289
 
 
290
 
 
291
 
class TestInterRepositoryScenarios(tests.TestCase):
292
 
 
293
 
    def test_scenarios(self):
294
 
        # check that constructor parameters are passed through to the adapted
295
 
        # test.
296
 
        from bzrlib.tests.interrepository_implementations import \
297
 
            make_scenarios
298
 
        server1 = "a"
299
 
        server2 = "b"
300
 
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
301
 
        scenarios = make_scenarios(server1, server2, formats)
302
 
        self.assertEqual([
303
 
            ('str,str,str',
304
 
             {'interrepo_class': str,
305
 
              'repository_format': 'C1',
306
 
              'repository_format_to': 'C2',
307
 
              'transport_readonly_server': 'b',
308
 
              'transport_server': 'a'}),
309
 
            ('int,str,str',
310
 
             {'interrepo_class': int,
311
 
              'repository_format': 'D1',
312
 
              'repository_format_to': 'D2',
313
 
              'transport_readonly_server': 'b',
314
 
              'transport_server': 'a'})],
315
 
            scenarios)
316
 
 
317
 
 
318
 
class TestWorkingTreeScenarios(tests.TestCase):
319
 
 
320
 
    def test_scenarios(self):
321
 
        # check that constructor parameters are passed through to the adapted
322
 
        # test.
323
 
        from bzrlib.tests.workingtree_implementations \
324
 
            import make_scenarios
325
 
        server1 = "a"
326
 
        server2 = "b"
327
 
        formats = [workingtree.WorkingTreeFormat2(),
328
 
                   workingtree.WorkingTreeFormat3(),]
329
 
        scenarios = make_scenarios(server1, server2, formats)
330
 
        self.assertEqual([
331
 
            ('WorkingTreeFormat2',
332
 
             {'bzrdir_format': formats[0]._matchingbzrdir,
333
 
              'transport_readonly_server': 'b',
334
 
              'transport_server': 'a',
335
 
              'workingtree_format': formats[0]}),
336
 
            ('WorkingTreeFormat3',
337
 
             {'bzrdir_format': formats[1]._matchingbzrdir,
338
 
              'transport_readonly_server': 'b',
339
 
              'transport_server': 'a',
340
 
              'workingtree_format': formats[1]})],
341
 
            scenarios)
342
 
 
343
 
 
344
 
class TestTreeScenarios(tests.TestCase):
345
 
 
346
 
    def test_scenarios(self):
347
 
        # the tree implementation scenario generator is meant to setup one
348
 
        # instance for each working tree format, and one additional instance
349
 
        # that will use the default wt format, but create a revision tree for
350
 
        # the tests.  this means that the wt ones should have the
351
 
        # workingtree_to_test_tree attribute set to 'return_parameter' and the
352
 
        # revision one set to revision_tree_from_workingtree.
353
 
 
354
 
        from bzrlib.tests.tree_implementations import (
355
 
            _dirstate_tree_from_workingtree,
356
 
            make_scenarios,
357
 
            preview_tree_pre,
358
 
            preview_tree_post,
359
 
            return_parameter,
360
 
            revision_tree_from_workingtree
361
 
            )
362
 
        server1 = "a"
363
 
        server2 = "b"
364
 
        formats = [workingtree.WorkingTreeFormat2(),
365
 
                   workingtree.WorkingTreeFormat3(),]
366
 
        scenarios = make_scenarios(server1, server2, formats)
367
 
        self.assertEqual(7, len(scenarios))
368
 
        default_wt_format = workingtree.WorkingTreeFormat4._default_format
369
 
        wt4_format = workingtree.WorkingTreeFormat4()
370
 
        wt5_format = workingtree.WorkingTreeFormat5()
371
 
        expected_scenarios = [
372
 
            ('WorkingTreeFormat2',
373
 
             {'bzrdir_format': formats[0]._matchingbzrdir,
374
 
              'transport_readonly_server': 'b',
375
 
              'transport_server': 'a',
376
 
              'workingtree_format': formats[0],
377
 
              '_workingtree_to_test_tree': return_parameter,
378
 
              }),
379
 
            ('WorkingTreeFormat3',
380
 
             {'bzrdir_format': formats[1]._matchingbzrdir,
381
 
              'transport_readonly_server': 'b',
382
 
              'transport_server': 'a',
383
 
              'workingtree_format': formats[1],
384
 
              '_workingtree_to_test_tree': return_parameter,
385
 
             }),
386
 
            ('RevisionTree',
387
 
             {'_workingtree_to_test_tree': revision_tree_from_workingtree,
388
 
              'bzrdir_format': default_wt_format._matchingbzrdir,
389
 
              'transport_readonly_server': 'b',
390
 
              'transport_server': 'a',
391
 
              'workingtree_format': default_wt_format,
392
 
             }),
393
 
            ('DirStateRevisionTree,WT4',
394
 
             {'_workingtree_to_test_tree': _dirstate_tree_from_workingtree,
395
 
              'bzrdir_format': wt4_format._matchingbzrdir,
396
 
              'transport_readonly_server': 'b',
397
 
              'transport_server': 'a',
398
 
              'workingtree_format': wt4_format,
399
 
             }),
400
 
            ('DirStateRevisionTree,WT5',
401
 
             {'_workingtree_to_test_tree': _dirstate_tree_from_workingtree,
402
 
              'bzrdir_format': wt5_format._matchingbzrdir,
403
 
              'transport_readonly_server': 'b',
404
 
              'transport_server': 'a',
405
 
              'workingtree_format': wt5_format,
406
 
             }),
407
 
            ('PreviewTree',
408
 
             {'_workingtree_to_test_tree': preview_tree_pre,
409
 
              'bzrdir_format': default_wt_format._matchingbzrdir,
410
 
              'transport_readonly_server': 'b',
411
 
              'transport_server': 'a',
412
 
              'workingtree_format': default_wt_format}),
413
 
            ('PreviewTreePost',
414
 
             {'_workingtree_to_test_tree': preview_tree_post,
415
 
              'bzrdir_format': default_wt_format._matchingbzrdir,
416
 
              'transport_readonly_server': 'b',
417
 
              'transport_server': 'a',
418
 
              'workingtree_format': default_wt_format}),
419
 
             ]
420
 
        self.assertEqual(expected_scenarios, scenarios)
421
 
 
422
 
 
423
 
class TestInterTreeScenarios(tests.TestCase):
424
 
    """A group of tests that test the InterTreeTestAdapter."""
425
 
 
426
 
    def test_scenarios(self):
427
 
        # check that constructor parameters are passed through to the adapted
428
 
        # test.
429
 
        # for InterTree tests we want the machinery to bring up two trees in
430
 
        # each instance: the base one, and the one we are interacting with.
431
 
        # because each optimiser can be direction specific, we need to test
432
 
        # each optimiser in its chosen direction.
433
 
        # unlike the TestProviderAdapter we dont want to automatically add a
434
 
        # parameterized one for WorkingTree - the optimisers will tell us what
435
 
        # ones to add.
436
 
        from bzrlib.tests.tree_implementations import (
437
 
            return_parameter,
438
 
            revision_tree_from_workingtree
439
 
            )
440
 
        from bzrlib.tests.intertree_implementations import (
441
 
            make_scenarios,
442
 
            )
443
 
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
444
 
        input_test = TestInterTreeScenarios(
445
 
            "test_scenarios")
446
 
        server1 = "a"
447
 
        server2 = "b"
448
 
        format1 = WorkingTreeFormat2()
449
 
        format2 = WorkingTreeFormat3()
450
 
        formats = [("1", str, format1, format2, "converter1"),
451
 
            ("2", int, format2, format1, "converter2")]
452
 
        scenarios = make_scenarios(server1, server2, formats)
453
 
        self.assertEqual(2, len(scenarios))
454
 
        expected_scenarios = [
455
 
            ("1", {
456
 
                "bzrdir_format": format1._matchingbzrdir,
457
 
                "intertree_class": formats[0][1],
458
 
                "workingtree_format": formats[0][2],
459
 
                "workingtree_format_to": formats[0][3],
460
 
                "mutable_trees_to_test_trees": formats[0][4],
461
 
                "_workingtree_to_test_tree": return_parameter,
462
 
                "transport_server": server1,
463
 
                "transport_readonly_server": server2,
464
 
                }),
465
 
            ("2", {
466
 
                "bzrdir_format": format2._matchingbzrdir,
467
 
                "intertree_class": formats[1][1],
468
 
                "workingtree_format": formats[1][2],
469
 
                "workingtree_format_to": formats[1][3],
470
 
                "mutable_trees_to_test_trees": formats[1][4],
471
 
                "_workingtree_to_test_tree": return_parameter,
472
 
                "transport_server": server1,
473
 
                "transport_readonly_server": server2,
474
 
                }),
475
 
            ]
476
 
        self.assertEqual(scenarios, expected_scenarios)
477
 
 
478
 
 
479
 
class TestTestCaseInTempDir(tests.TestCaseInTempDir):
480
 
 
481
 
    def test_home_is_not_working(self):
482
 
        self.assertNotEqual(self.test_dir, self.test_home_dir)
483
 
        cwd = osutils.getcwd()
484
 
        self.assertIsSameRealPath(self.test_dir, cwd)
485
 
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
486
 
 
487
 
    def test_assertEqualStat_equal(self):
488
 
        from bzrlib.tests.test_dirstate import _FakeStat
489
 
        self.build_tree(["foo"])
490
 
        real = os.lstat("foo")
491
 
        fake = _FakeStat(real.st_size, real.st_mtime, real.st_ctime,
492
 
            real.st_dev, real.st_ino, real.st_mode)
493
 
        self.assertEqualStat(real, fake)
494
 
 
495
 
    def test_assertEqualStat_notequal(self):
496
 
        self.build_tree(["foo", "bar"])
497
 
        self.assertRaises(AssertionError, self.assertEqualStat,
498
 
            os.lstat("foo"), os.lstat("bar"))
499
 
 
500
 
 
501
 
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
502
 
 
503
 
    def test_home_is_non_existant_dir_under_root(self):
504
 
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
505
 
 
506
 
        This is because TestCaseWithMemoryTransport is for tests that do not
507
 
        need any disk resources: they should be hooked into bzrlib in such a
508
 
        way that no global settings are being changed by the test (only a
509
 
        few tests should need to do that), and having a missing dir as home is
510
 
        an effective way to ensure that this is the case.
511
 
        """
512
 
        self.assertIsSameRealPath(
513
 
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
514
 
            self.test_home_dir)
515
 
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
516
 
 
517
 
    def test_cwd_is_TEST_ROOT(self):
518
 
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
519
 
        cwd = osutils.getcwd()
520
 
        self.assertIsSameRealPath(self.test_dir, cwd)
521
 
 
522
 
    def test_make_branch_and_memory_tree(self):
523
 
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
524
 
 
525
 
        This is hard to comprehensively robustly test, so we settle for making
526
 
        a branch and checking no directory was created at its relpath.
527
 
        """
528
 
        tree = self.make_branch_and_memory_tree('dir')
529
 
        # Guard against regression into MemoryTransport leaking
530
 
        # files to disk instead of keeping them in memory.
531
 
        self.failIf(osutils.lexists('dir'))
532
 
        self.assertIsInstance(tree, memorytree.MemoryTree)
533
 
 
534
 
    def test_make_branch_and_memory_tree_with_format(self):
535
 
        """make_branch_and_memory_tree should accept a format option."""
536
 
        format = bzrdir.BzrDirMetaFormat1()
537
 
        format.repository_format = weaverepo.RepositoryFormat7()
538
 
        tree = self.make_branch_and_memory_tree('dir', format=format)
539
 
        # Guard against regression into MemoryTransport leaking
540
 
        # files to disk instead of keeping them in memory.
541
 
        self.failIf(osutils.lexists('dir'))
542
 
        self.assertIsInstance(tree, memorytree.MemoryTree)
543
 
        self.assertEqual(format.repository_format.__class__,
544
 
            tree.branch.repository._format.__class__)
545
 
 
546
 
    def test_make_branch_builder(self):
547
 
        builder = self.make_branch_builder('dir')
548
 
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
549
 
        # Guard against regression into MemoryTransport leaking
550
 
        # files to disk instead of keeping them in memory.
551
 
        self.failIf(osutils.lexists('dir'))
552
 
 
553
 
    def test_make_branch_builder_with_format(self):
554
 
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
555
 
        # that the format objects are used.
556
 
        format = bzrdir.BzrDirMetaFormat1()
557
 
        repo_format = weaverepo.RepositoryFormat7()
558
 
        format.repository_format = repo_format
559
 
        builder = self.make_branch_builder('dir', format=format)
560
 
        the_branch = builder.get_branch()
561
 
        # Guard against regression into MemoryTransport leaking
562
 
        # files to disk instead of keeping them in memory.
563
 
        self.failIf(osutils.lexists('dir'))
564
 
        self.assertEqual(format.repository_format.__class__,
565
 
                         the_branch.repository._format.__class__)
566
 
        self.assertEqual(repo_format.get_format_string(),
567
 
                         self.get_transport().get_bytes(
568
 
                            'dir/.bzr/repository/format'))
569
 
 
570
 
    def test_make_branch_builder_with_format_name(self):
571
 
        builder = self.make_branch_builder('dir', format='knit')
572
 
        the_branch = builder.get_branch()
573
 
        # Guard against regression into MemoryTransport leaking
574
 
        # files to disk instead of keeping them in memory.
575
 
        self.failIf(osutils.lexists('dir'))
576
 
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
577
 
        self.assertEqual(dir_format.repository_format.__class__,
578
 
                         the_branch.repository._format.__class__)
579
 
        self.assertEqual('Bazaar-NG Knit Repository Format 1',
580
 
                         self.get_transport().get_bytes(
581
 
                            'dir/.bzr/repository/format'))
582
 
 
583
 
    def test_safety_net(self):
584
 
        """No test should modify the safety .bzr directory.
585
 
 
586
 
        We just test that the _check_safety_net private method raises
587
 
        AssertionError, it's easier than building a test suite with the same
588
 
        test.
589
 
        """
590
 
        # Oops, a commit in the current directory (i.e. without local .bzr
591
 
        # directory) will crawl up the hierarchy to find a .bzr directory.
592
 
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
593
 
        # But we have a safety net in place.
594
 
        self.assertRaises(AssertionError, self._check_safety_net)
595
 
 
596
 
    def test_dangling_locks_cause_failures(self):
597
 
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
598
 
            def test_function(self):
599
 
                t = self.get_transport('.')
600
 
                l = lockdir.LockDir(t, 'lock')
601
 
                l.create()
602
 
                l.attempt_lock()
603
 
        test = TestDanglingLock('test_function')
604
 
        result = test.run()
605
 
        self.assertEqual(1, len(result.errors))
606
 
 
607
 
 
608
 
class TestTestCaseWithTransport(tests.TestCaseWithTransport):
609
 
    """Tests for the convenience functions TestCaseWithTransport introduces."""
610
 
 
611
 
    def test_get_readonly_url_none(self):
612
 
        from bzrlib.transport import get_transport
613
 
        from bzrlib.transport.memory import MemoryServer
614
 
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
615
 
        self.vfs_transport_factory = MemoryServer
616
 
        self.transport_readonly_server = None
617
 
        # calling get_readonly_transport() constructs a decorator on the url
618
 
        # for the server
619
 
        url = self.get_readonly_url()
620
 
        url2 = self.get_readonly_url('foo/bar')
621
 
        t = get_transport(url)
622
 
        t2 = get_transport(url2)
623
 
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
624
 
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
625
 
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
626
 
 
627
 
    def test_get_readonly_url_http(self):
628
 
        from bzrlib.tests.http_server import HttpServer
629
 
        from bzrlib.transport import get_transport
630
 
        from bzrlib.transport.local import LocalURLServer
631
 
        from bzrlib.transport.http import HttpTransportBase
632
 
        self.transport_server = LocalURLServer
633
 
        self.transport_readonly_server = HttpServer
634
 
        # calling get_readonly_transport() gives us a HTTP server instance.
635
 
        url = self.get_readonly_url()
636
 
        url2 = self.get_readonly_url('foo/bar')
637
 
        # the transport returned may be any HttpTransportBase subclass
638
 
        t = get_transport(url)
639
 
        t2 = get_transport(url2)
640
 
        self.failUnless(isinstance(t, HttpTransportBase))
641
 
        self.failUnless(isinstance(t2, HttpTransportBase))
642
 
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
643
 
 
644
 
    def test_is_directory(self):
645
 
        """Test assertIsDirectory assertion"""
646
 
        t = self.get_transport()
647
 
        self.build_tree(['a_dir/', 'a_file'], transport=t)
648
 
        self.assertIsDirectory('a_dir', t)
649
 
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
650
 
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
651
 
 
652
 
    def test_make_branch_builder(self):
653
 
        builder = self.make_branch_builder('dir')
654
 
        rev_id = builder.build_commit()
655
 
        self.failUnlessExists('dir')
656
 
        a_dir = bzrdir.BzrDir.open('dir')
657
 
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
658
 
        a_branch = a_dir.open_branch()
659
 
        builder_branch = builder.get_branch()
660
 
        self.assertEqual(a_branch.base, builder_branch.base)
661
 
        self.assertEqual((1, rev_id), builder_branch.last_revision_info())
662
 
        self.assertEqual((1, rev_id), a_branch.last_revision_info())
663
 
 
664
 
 
665
 
class TestTestCaseTransports(tests.TestCaseWithTransport):
666
 
 
667
 
    def setUp(self):
668
 
        super(TestTestCaseTransports, self).setUp()
669
 
        self.vfs_transport_factory = MemoryServer
670
 
 
671
 
    def test_make_bzrdir_preserves_transport(self):
672
 
        t = self.get_transport()
673
 
        result_bzrdir = self.make_bzrdir('subdir')
674
 
        self.assertIsInstance(result_bzrdir.transport,
675
 
                              MemoryTransport)
676
 
        # should not be on disk, should only be in memory
677
 
        self.failIfExists('subdir')
678
 
 
679
 
 
680
 
class TestChrootedTest(tests.ChrootedTestCase):
681
 
 
682
 
    def test_root_is_root(self):
683
 
        from bzrlib.transport import get_transport
684
 
        t = get_transport(self.get_readonly_url())
685
 
        url = t.base
686
 
        self.assertEqual(url, t.clone('..').base)
687
 
 
688
 
 
689
 
class MockProgress(progress._BaseProgressBar):
690
 
    """Progress-bar standin that records calls.
691
 
 
692
 
    Useful for testing pb using code.
693
 
    """
694
 
 
695
 
    def __init__(self):
696
 
        progress._BaseProgressBar.__init__(self)
697
 
        self.calls = []
698
 
 
699
 
    def tick(self):
700
 
        self.calls.append(('tick',))
701
 
 
702
 
    def update(self, msg=None, current=None, total=None):
703
 
        self.calls.append(('update', msg, current, total))
704
 
 
705
 
    def clear(self):
706
 
        self.calls.append(('clear',))
707
 
 
708
 
    def note(self, msg, *args):
709
 
        self.calls.append(('note', msg, args))
710
 
 
711
 
 
712
 
class TestTestResult(tests.TestCase):
713
 
 
714
 
    def check_timing(self, test_case, expected_re):
715
 
        result = bzrlib.tests.TextTestResult(self._log_file,
716
 
                descriptions=0,
717
 
                verbosity=1,
718
 
                )
719
 
        test_case.run(result)
720
 
        timed_string = result._testTimeString(test_case)
721
 
        self.assertContainsRe(timed_string, expected_re)
722
 
 
723
 
    def test_test_reporting(self):
724
 
        class ShortDelayTestCase(tests.TestCase):
725
 
            def test_short_delay(self):
726
 
                time.sleep(0.003)
727
 
            def test_short_benchmark(self):
728
 
                self.time(time.sleep, 0.003)
729
 
        self.check_timing(ShortDelayTestCase('test_short_delay'),
730
 
                          r"^ +[0-9]+ms$")
731
 
        # if a benchmark time is given, we want a x of y style result.
732
 
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
733
 
                          r"^ +[0-9]+ms/ +[0-9]+ms$")
734
 
 
735
 
    def test_unittest_reporting_unittest_class(self):
736
 
        # getting the time from a non-bzrlib test works ok
737
 
        class ShortDelayTestCase(unittest.TestCase):
738
 
            def test_short_delay(self):
739
 
                time.sleep(0.003)
740
 
        self.check_timing(ShortDelayTestCase('test_short_delay'),
741
 
                          r"^ +[0-9]+ms$")
742
 
 
743
 
    def test_assigned_benchmark_file_stores_date(self):
744
 
        output = StringIO()
745
 
        result = bzrlib.tests.TextTestResult(self._log_file,
746
 
                                        descriptions=0,
747
 
                                        verbosity=1,
748
 
                                        bench_history=output
749
 
                                        )
750
 
        output_string = output.getvalue()
751
 
        # if you are wondering about the regexp please read the comment in
752
 
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
753
 
        # XXX: what comment?  -- Andrew Bennetts
754
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
755
 
 
756
 
    def test_benchhistory_records_test_times(self):
757
 
        result_stream = StringIO()
758
 
        result = bzrlib.tests.TextTestResult(
759
 
            self._log_file,
760
 
            descriptions=0,
761
 
            verbosity=1,
762
 
            bench_history=result_stream
763
 
            )
764
 
 
765
 
        # we want profile a call and check that its test duration is recorded
766
 
        # make a new test instance that when run will generate a benchmark
767
 
        example_test_case = TestTestResult("_time_hello_world_encoding")
768
 
        # execute the test, which should succeed and record times
769
 
        example_test_case.run(result)
770
 
        lines = result_stream.getvalue().splitlines()
771
 
        self.assertEqual(2, len(lines))
772
 
        self.assertContainsRe(lines[1],
773
 
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
774
 
            "._time_hello_world_encoding")
775
 
 
776
 
    def _time_hello_world_encoding(self):
777
 
        """Profile two sleep calls
778
 
 
779
 
        This is used to exercise the test framework.
780
 
        """
781
 
        self.time(unicode, 'hello', errors='replace')
782
 
        self.time(unicode, 'world', errors='replace')
783
 
 
784
 
    def test_lsprofiling(self):
785
 
        """Verbose test result prints lsprof statistics from test cases."""
786
 
        self.requireFeature(test_lsprof.LSProfFeature)
787
 
        result_stream = StringIO()
788
 
        result = bzrlib.tests.VerboseTestResult(
789
 
            unittest._WritelnDecorator(result_stream),
790
 
            descriptions=0,
791
 
            verbosity=2,
792
 
            )
793
 
        # we want profile a call of some sort and check it is output by
794
 
        # addSuccess. We dont care about addError or addFailure as they
795
 
        # are not that interesting for performance tuning.
796
 
        # make a new test instance that when run will generate a profile
797
 
        example_test_case = TestTestResult("_time_hello_world_encoding")
798
 
        example_test_case._gather_lsprof_in_benchmarks = True
799
 
        # execute the test, which should succeed and record profiles
800
 
        example_test_case.run(result)
801
 
        # lsprofile_something()
802
 
        # if this worked we want
803
 
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
804
 
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
805
 
        # (the lsprof header)
806
 
        # ... an arbitrary number of lines
807
 
        # and the function call which is time.sleep.
808
 
        #           1        0            ???         ???       ???(sleep)
809
 
        # and then repeated but with 'world', rather than 'hello'.
810
 
        # this should appear in the output stream of our test result.
811
 
        output = result_stream.getvalue()
812
 
        self.assertContainsRe(output,
813
 
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
814
 
        self.assertContainsRe(output,
815
 
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
816
 
        self.assertContainsRe(output,
817
 
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
818
 
        self.assertContainsRe(output,
819
 
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
820
 
 
821
 
    def test_known_failure(self):
822
 
        """A KnownFailure being raised should trigger several result actions."""
823
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
824
 
            def done(self): pass
825
 
            def startTests(self): pass
826
 
            def report_test_start(self, test): pass
827
 
            def report_known_failure(self, test, err):
828
 
                self._call = test, err
829
 
        result = InstrumentedTestResult(None, None, None, None)
830
 
        def test_function():
831
 
            raise tests.KnownFailure('failed!')
832
 
        test = unittest.FunctionTestCase(test_function)
833
 
        test.run(result)
834
 
        # it should invoke 'report_known_failure'.
835
 
        self.assertEqual(2, len(result._call))
836
 
        self.assertEqual(test, result._call[0])
837
 
        self.assertEqual(tests.KnownFailure, result._call[1][0])
838
 
        self.assertIsInstance(result._call[1][1], tests.KnownFailure)
839
 
        # we dont introspec the traceback, if the rest is ok, it would be
840
 
        # exceptional for it not to be.
841
 
        # it should update the known_failure_count on the object.
842
 
        self.assertEqual(1, result.known_failure_count)
843
 
        # the result should be successful.
844
 
        self.assertTrue(result.wasSuccessful())
845
 
 
846
 
    def test_verbose_report_known_failure(self):
847
 
        # verbose test output formatting
848
 
        result_stream = StringIO()
849
 
        result = bzrlib.tests.VerboseTestResult(
850
 
            unittest._WritelnDecorator(result_stream),
851
 
            descriptions=0,
852
 
            verbosity=2,
853
 
            )
854
 
        test = self.get_passing_test()
855
 
        result.startTest(test)
856
 
        prefix = len(result_stream.getvalue())
857
 
        # the err parameter has the shape:
858
 
        # (class, exception object, traceback)
859
 
        # KnownFailures dont get their tracebacks shown though, so we
860
 
        # can skip that.
861
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
862
 
        result.report_known_failure(test, err)
863
 
        output = result_stream.getvalue()[prefix:]
864
 
        lines = output.splitlines()
865
 
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
866
 
        self.assertEqual(lines[1], '    foo')
867
 
        self.assertEqual(2, len(lines))
868
 
 
869
 
    def test_text_report_known_failure(self):
870
 
        # text test output formatting
871
 
        pb = MockProgress()
872
 
        result = bzrlib.tests.TextTestResult(
873
 
            StringIO(),
874
 
            descriptions=0,
875
 
            verbosity=1,
876
 
            pb=pb,
877
 
            )
878
 
        test = self.get_passing_test()
879
 
        # this seeds the state to handle reporting the test.
880
 
        result.startTest(test)
881
 
        # the err parameter has the shape:
882
 
        # (class, exception object, traceback)
883
 
        # KnownFailures dont get their tracebacks shown though, so we
884
 
        # can skip that.
885
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
886
 
        result.report_known_failure(test, err)
887
 
        self.assertEqual(
888
 
            [
889
 
            ('update', '[1 in 0s] passing_test', None, None),
890
 
            ('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
891
 
            ],
892
 
            pb.calls)
893
 
        # known_failures should be printed in the summary, so if we run a test
894
 
        # after there are some known failures, the update prefix should match
895
 
        # this.
896
 
        result.known_failure_count = 3
897
 
        test.run(result)
898
 
        self.assertEqual(
899
 
            [
900
 
            ('update', '[2 in 0s] passing_test', None, None),
901
 
            ],
902
 
            pb.calls[2:])
903
 
 
904
 
    def get_passing_test(self):
905
 
        """Return a test object that can't be run usefully."""
906
 
        def passing_test():
907
 
            pass
908
 
        return unittest.FunctionTestCase(passing_test)
909
 
 
910
 
    def test_add_not_supported(self):
911
 
        """Test the behaviour of invoking addNotSupported."""
912
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
913
 
            def done(self): pass
914
 
            def startTests(self): pass
915
 
            def report_test_start(self, test): pass
916
 
            def report_unsupported(self, test, feature):
917
 
                self._call = test, feature
918
 
        result = InstrumentedTestResult(None, None, None, None)
919
 
        test = SampleTestCase('_test_pass')
920
 
        feature = tests.Feature()
921
 
        result.startTest(test)
922
 
        result.addNotSupported(test, feature)
923
 
        # it should invoke 'report_unsupported'.
924
 
        self.assertEqual(2, len(result._call))
925
 
        self.assertEqual(test, result._call[0])
926
 
        self.assertEqual(feature, result._call[1])
927
 
        # the result should be successful.
928
 
        self.assertTrue(result.wasSuccessful())
929
 
        # it should record the test against a count of tests not run due to
930
 
        # this feature.
931
 
        self.assertEqual(1, result.unsupported['Feature'])
932
 
        # and invoking it again should increment that counter
933
 
        result.addNotSupported(test, feature)
934
 
        self.assertEqual(2, result.unsupported['Feature'])
935
 
 
936
 
    def test_verbose_report_unsupported(self):
937
 
        # verbose test output formatting
938
 
        result_stream = StringIO()
939
 
        result = bzrlib.tests.VerboseTestResult(
940
 
            unittest._WritelnDecorator(result_stream),
941
 
            descriptions=0,
942
 
            verbosity=2,
943
 
            )
944
 
        test = self.get_passing_test()
945
 
        feature = tests.Feature()
946
 
        result.startTest(test)
947
 
        prefix = len(result_stream.getvalue())
948
 
        result.report_unsupported(test, feature)
949
 
        output = result_stream.getvalue()[prefix:]
950
 
        lines = output.splitlines()
951
 
        self.assertEqual(lines, ['NODEP                   0ms',
952
 
                                 "    The feature 'Feature' is not available."])
953
 
 
954
 
    def test_text_report_unsupported(self):
955
 
        # text test output formatting
956
 
        pb = MockProgress()
957
 
        result = bzrlib.tests.TextTestResult(
958
 
            StringIO(),
959
 
            descriptions=0,
960
 
            verbosity=1,
961
 
            pb=pb,
962
 
            )
963
 
        test = self.get_passing_test()
964
 
        feature = tests.Feature()
965
 
        # this seeds the state to handle reporting the test.
966
 
        result.startTest(test)
967
 
        result.report_unsupported(test, feature)
968
 
        # no output on unsupported features
969
 
        self.assertEqual(
970
 
            [('update', '[1 in 0s] passing_test', None, None)
971
 
            ],
972
 
            pb.calls)
973
 
        # the number of missing features should be printed in the progress
974
 
        # summary, so check for that.
975
 
        result.unsupported = {'foo':0, 'bar':0}
976
 
        test.run(result)
977
 
        self.assertEqual(
978
 
            [
979
 
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
980
 
            ],
981
 
            pb.calls[1:])
982
 
 
983
 
    def test_unavailable_exception(self):
984
 
        """An UnavailableFeature being raised should invoke addNotSupported."""
985
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
986
 
            def done(self): pass
987
 
            def startTests(self): pass
988
 
            def report_test_start(self, test): pass
989
 
            def addNotSupported(self, test, feature):
990
 
                self._call = test, feature
991
 
        result = InstrumentedTestResult(None, None, None, None)
992
 
        feature = tests.Feature()
993
 
        def test_function():
994
 
            raise tests.UnavailableFeature(feature)
995
 
        test = unittest.FunctionTestCase(test_function)
996
 
        test.run(result)
997
 
        # it should invoke 'addNotSupported'.
998
 
        self.assertEqual(2, len(result._call))
999
 
        self.assertEqual(test, result._call[0])
1000
 
        self.assertEqual(feature, result._call[1])
1001
 
        # and not count as an error
1002
 
        self.assertEqual(0, result.error_count)
1003
 
 
1004
 
    def test_strict_with_unsupported_feature(self):
1005
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1006
 
                                             verbosity=1)
1007
 
        test = self.get_passing_test()
1008
 
        feature = "Unsupported Feature"
1009
 
        result.addNotSupported(test, feature)
1010
 
        self.assertFalse(result.wasStrictlySuccessful())
1011
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1012
 
 
1013
 
    def test_strict_with_known_failure(self):
1014
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1015
 
                                             verbosity=1)
1016
 
        test = self.get_passing_test()
1017
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
1018
 
        result._addKnownFailure(test, err)
1019
 
        self.assertFalse(result.wasStrictlySuccessful())
1020
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1021
 
 
1022
 
    def test_strict_with_success(self):
1023
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1024
 
                                             verbosity=1)
1025
 
        test = self.get_passing_test()
1026
 
        result.addSuccess(test)
1027
 
        self.assertTrue(result.wasStrictlySuccessful())
1028
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1029
 
 
1030
 
    def test_startTests(self):
1031
 
        """Starting the first test should trigger startTests."""
1032
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
1033
 
            calls = 0
1034
 
            def startTests(self): self.calls += 1
1035
 
            def report_test_start(self, test): pass
1036
 
        result = InstrumentedTestResult(None, None, None, None)
1037
 
        def test_function():
1038
 
            pass
1039
 
        test = unittest.FunctionTestCase(test_function)
1040
 
        test.run(result)
1041
 
        self.assertEquals(1, result.calls)
1042
 
 
1043
 
 
1044
 
class TestUnicodeFilenameFeature(tests.TestCase):
1045
 
 
1046
 
    def test_probe_passes(self):
1047
 
        """UnicodeFilenameFeature._probe passes."""
1048
 
        # We can't test much more than that because the behaviour depends
1049
 
        # on the platform.
1050
 
        tests.UnicodeFilenameFeature._probe()
1051
 
 
1052
 
 
1053
 
class TestRunner(tests.TestCase):
1054
 
 
1055
 
    def dummy_test(self):
1056
 
        pass
1057
 
 
1058
 
    def run_test_runner(self, testrunner, test):
1059
 
        """Run suite in testrunner, saving global state and restoring it.
1060
 
 
1061
 
        This current saves and restores:
1062
 
        TestCaseInTempDir.TEST_ROOT
1063
 
 
1064
 
        There should be no tests in this file that use
1065
 
        bzrlib.tests.TextTestRunner without using this convenience method,
1066
 
        because of our use of global state.
1067
 
        """
1068
 
        old_root = tests.TestCaseInTempDir.TEST_ROOT
1069
 
        try:
1070
 
            tests.TestCaseInTempDir.TEST_ROOT = None
1071
 
            return testrunner.run(test)
1072
 
        finally:
1073
 
            tests.TestCaseInTempDir.TEST_ROOT = old_root
1074
 
 
1075
 
    def test_known_failure_failed_run(self):
1076
 
        # run a test that generates a known failure which should be printed in
1077
 
        # the final output when real failures occur.
1078
 
        def known_failure_test():
1079
 
            raise tests.KnownFailure('failed')
1080
 
        test = unittest.TestSuite()
1081
 
        test.addTest(unittest.FunctionTestCase(known_failure_test))
1082
 
        def failing_test():
1083
 
            raise AssertionError('foo')
1084
 
        test.addTest(unittest.FunctionTestCase(failing_test))
1085
 
        stream = StringIO()
1086
 
        runner = tests.TextTestRunner(stream=stream)
1087
 
        result = self.run_test_runner(runner, test)
1088
 
        lines = stream.getvalue().splitlines()
1089
 
        self.assertEqual([
1090
 
            '',
1091
 
            '======================================================================',
1092
 
            'FAIL: unittest.FunctionTestCase (failing_test)',
1093
 
            '----------------------------------------------------------------------',
1094
 
            'Traceback (most recent call last):',
1095
 
            '    raise AssertionError(\'foo\')',
1096
 
            'AssertionError: foo',
1097
 
            '',
1098
 
            '----------------------------------------------------------------------',
1099
 
            '',
1100
 
            'FAILED (failures=1, known_failure_count=1)'],
1101
 
            lines[3:8] + lines[9:13] + lines[14:])
1102
 
 
1103
 
    def test_known_failure_ok_run(self):
1104
 
        # run a test that generates a known failure which should be printed in the final output.
1105
 
        def known_failure_test():
1106
 
            raise tests.KnownFailure('failed')
1107
 
        test = unittest.FunctionTestCase(known_failure_test)
1108
 
        stream = StringIO()
1109
 
        runner = tests.TextTestRunner(stream=stream)
1110
 
        result = self.run_test_runner(runner, test)
1111
 
        self.assertContainsRe(stream.getvalue(),
1112
 
            '\n'
1113
 
            '-*\n'
1114
 
            'Ran 1 test in .*\n'
1115
 
            '\n'
1116
 
            'OK \\(known_failures=1\\)\n')
1117
 
 
1118
 
    def test_skipped_test(self):
1119
 
        # run a test that is skipped, and check the suite as a whole still
1120
 
        # succeeds.
1121
 
        # skipping_test must be hidden in here so it's not run as a real test
1122
 
        class SkippingTest(tests.TestCase):
1123
 
            def skipping_test(self):
1124
 
                raise tests.TestSkipped('test intentionally skipped')
1125
 
        runner = tests.TextTestRunner(stream=self._log_file)
1126
 
        test = SkippingTest("skipping_test")
1127
 
        result = self.run_test_runner(runner, test)
1128
 
        self.assertTrue(result.wasSuccessful())
1129
 
 
1130
 
    def test_skipped_from_setup(self):
1131
 
        calls = []
1132
 
        class SkippedSetupTest(tests.TestCase):
1133
 
 
1134
 
            def setUp(self):
1135
 
                calls.append('setUp')
1136
 
                self.addCleanup(self.cleanup)
1137
 
                raise tests.TestSkipped('skipped setup')
1138
 
 
1139
 
            def test_skip(self):
1140
 
                self.fail('test reached')
1141
 
 
1142
 
            def cleanup(self):
1143
 
                calls.append('cleanup')
1144
 
 
1145
 
        runner = tests.TextTestRunner(stream=self._log_file)
1146
 
        test = SkippedSetupTest('test_skip')
1147
 
        result = self.run_test_runner(runner, test)
1148
 
        self.assertTrue(result.wasSuccessful())
1149
 
        # Check if cleanup was called the right number of times.
1150
 
        self.assertEqual(['setUp', 'cleanup'], calls)
1151
 
 
1152
 
    def test_skipped_from_test(self):
1153
 
        calls = []
1154
 
        class SkippedTest(tests.TestCase):
1155
 
 
1156
 
            def setUp(self):
1157
 
                tests.TestCase.setUp(self)
1158
 
                calls.append('setUp')
1159
 
                self.addCleanup(self.cleanup)
1160
 
 
1161
 
            def test_skip(self):
1162
 
                raise tests.TestSkipped('skipped test')
1163
 
 
1164
 
            def cleanup(self):
1165
 
                calls.append('cleanup')
1166
 
 
1167
 
        runner = tests.TextTestRunner(stream=self._log_file)
1168
 
        test = SkippedTest('test_skip')
1169
 
        result = self.run_test_runner(runner, test)
1170
 
        self.assertTrue(result.wasSuccessful())
1171
 
        # Check if cleanup was called the right number of times.
1172
 
        self.assertEqual(['setUp', 'cleanup'], calls)
1173
 
 
1174
 
    def test_not_applicable(self):
1175
 
        # run a test that is skipped because it's not applicable
1176
 
        def not_applicable_test():
1177
 
            raise tests.TestNotApplicable('this test never runs')
1178
 
        out = StringIO()
1179
 
        runner = tests.TextTestRunner(stream=out, verbosity=2)
1180
 
        test = unittest.FunctionTestCase(not_applicable_test)
1181
 
        result = self.run_test_runner(runner, test)
1182
 
        self._log_file.write(out.getvalue())
1183
 
        self.assertTrue(result.wasSuccessful())
1184
 
        self.assertTrue(result.wasStrictlySuccessful())
1185
 
        self.assertContainsRe(out.getvalue(),
1186
 
                r'(?m)not_applicable_test   * N/A')
1187
 
        self.assertContainsRe(out.getvalue(),
1188
 
                r'(?m)^    this test never runs')
1189
 
 
1190
 
    def test_not_applicable_demo(self):
1191
 
        # just so you can see it in the test output
1192
 
        raise tests.TestNotApplicable('this test is just a demonstation')
1193
 
 
1194
 
    def test_unsupported_features_listed(self):
1195
 
        """When unsupported features are encountered they are detailed."""
1196
 
        class Feature1(tests.Feature):
1197
 
            def _probe(self): return False
1198
 
        class Feature2(tests.Feature):
1199
 
            def _probe(self): return False
1200
 
        # create sample tests
1201
 
        test1 = SampleTestCase('_test_pass')
1202
 
        test1._test_needs_features = [Feature1()]
1203
 
        test2 = SampleTestCase('_test_pass')
1204
 
        test2._test_needs_features = [Feature2()]
1205
 
        test = unittest.TestSuite()
1206
 
        test.addTest(test1)
1207
 
        test.addTest(test2)
1208
 
        stream = StringIO()
1209
 
        runner = tests.TextTestRunner(stream=stream)
1210
 
        result = self.run_test_runner(runner, test)
1211
 
        lines = stream.getvalue().splitlines()
1212
 
        self.assertEqual([
1213
 
            'OK',
1214
 
            "Missing feature 'Feature1' skipped 1 tests.",
1215
 
            "Missing feature 'Feature2' skipped 1 tests.",
1216
 
            ],
1217
 
            lines[-3:])
1218
 
 
1219
 
    def test_bench_history(self):
1220
 
        # tests that the running the benchmark produces a history file
1221
 
        # containing a timestamp and the revision id of the bzrlib source which
1222
 
        # was tested.
1223
 
        workingtree = _get_bzr_source_tree()
1224
 
        test = TestRunner('dummy_test')
1225
 
        output = StringIO()
1226
 
        runner = tests.TextTestRunner(stream=self._log_file,
1227
 
                                      bench_history=output)
1228
 
        result = self.run_test_runner(runner, test)
1229
 
        output_string = output.getvalue()
1230
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
1231
 
        if workingtree is not None:
1232
 
            revision_id = workingtree.get_parent_ids()[0]
1233
 
            self.assertEndsWith(output_string.rstrip(), revision_id)
1234
 
 
1235
 
    def assertLogDeleted(self, test):
1236
 
        log = test._get_log()
1237
 
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1238
 
        self.assertEqual('', test._log_contents)
1239
 
        self.assertIs(None, test._log_file_name)
1240
 
 
1241
 
    def test_success_log_deleted(self):
1242
 
        """Successful tests have their log deleted"""
1243
 
 
1244
 
        class LogTester(tests.TestCase):
1245
 
 
1246
 
            def test_success(self):
1247
 
                self.log('this will be removed\n')
1248
 
 
1249
 
        sio = StringIO()
1250
 
        runner = tests.TextTestRunner(stream=sio)
1251
 
        test = LogTester('test_success')
1252
 
        result = self.run_test_runner(runner, test)
1253
 
 
1254
 
        self.assertLogDeleted(test)
1255
 
 
1256
 
    def test_skipped_log_deleted(self):
1257
 
        """Skipped tests have their log deleted"""
1258
 
 
1259
 
        class LogTester(tests.TestCase):
1260
 
 
1261
 
            def test_skipped(self):
1262
 
                self.log('this will be removed\n')
1263
 
                raise tests.TestSkipped()
1264
 
 
1265
 
        sio = StringIO()
1266
 
        runner = tests.TextTestRunner(stream=sio)
1267
 
        test = LogTester('test_skipped')
1268
 
        result = self.run_test_runner(runner, test)
1269
 
 
1270
 
        self.assertLogDeleted(test)
1271
 
 
1272
 
    def test_not_aplicable_log_deleted(self):
1273
 
        """Not applicable tests have their log deleted"""
1274
 
 
1275
 
        class LogTester(tests.TestCase):
1276
 
 
1277
 
            def test_not_applicable(self):
1278
 
                self.log('this will be removed\n')
1279
 
                raise tests.TestNotApplicable()
1280
 
 
1281
 
        sio = StringIO()
1282
 
        runner = tests.TextTestRunner(stream=sio)
1283
 
        test = LogTester('test_not_applicable')
1284
 
        result = self.run_test_runner(runner, test)
1285
 
 
1286
 
        self.assertLogDeleted(test)
1287
 
 
1288
 
    def test_known_failure_log_deleted(self):
1289
 
        """Know failure tests have their log deleted"""
1290
 
 
1291
 
        class LogTester(tests.TestCase):
1292
 
 
1293
 
            def test_known_failure(self):
1294
 
                self.log('this will be removed\n')
1295
 
                raise tests.KnownFailure()
1296
 
 
1297
 
        sio = StringIO()
1298
 
        runner = tests.TextTestRunner(stream=sio)
1299
 
        test = LogTester('test_known_failure')
1300
 
        result = self.run_test_runner(runner, test)
1301
 
 
1302
 
        self.assertLogDeleted(test)
1303
 
 
1304
 
    def test_fail_log_kept(self):
1305
 
        """Failed tests have their log kept"""
1306
 
 
1307
 
        class LogTester(tests.TestCase):
1308
 
 
1309
 
            def test_fail(self):
1310
 
                self.log('this will be kept\n')
1311
 
                self.fail('this test fails')
1312
 
 
1313
 
        sio = StringIO()
1314
 
        runner = tests.TextTestRunner(stream=sio)
1315
 
        test = LogTester('test_fail')
1316
 
        result = self.run_test_runner(runner, test)
1317
 
 
1318
 
        text = sio.getvalue()
1319
 
        self.assertContainsRe(text, 'this will be kept')
1320
 
        self.assertContainsRe(text, 'this test fails')
1321
 
 
1322
 
        log = test._get_log()
1323
 
        self.assertContainsRe(log, 'this will be kept')
1324
 
        self.assertEqual(log, test._log_contents)
1325
 
 
1326
 
    def test_error_log_kept(self):
1327
 
        """Tests with errors have their log kept"""
1328
 
 
1329
 
        class LogTester(tests.TestCase):
1330
 
 
1331
 
            def test_error(self):
1332
 
                self.log('this will be kept\n')
1333
 
                raise ValueError('random exception raised')
1334
 
 
1335
 
        sio = StringIO()
1336
 
        runner = tests.TextTestRunner(stream=sio)
1337
 
        test = LogTester('test_error')
1338
 
        result = self.run_test_runner(runner, test)
1339
 
 
1340
 
        text = sio.getvalue()
1341
 
        self.assertContainsRe(text, 'this will be kept')
1342
 
        self.assertContainsRe(text, 'random exception raised')
1343
 
 
1344
 
        log = test._get_log()
1345
 
        self.assertContainsRe(log, 'this will be kept')
1346
 
        self.assertEqual(log, test._log_contents)
1347
 
 
1348
 
 
1349
 
class SampleTestCase(tests.TestCase):
1350
 
 
1351
 
    def _test_pass(self):
1352
 
        pass
1353
 
 
1354
 
class _TestException(Exception):
1355
 
    pass
1356
 
 
1357
 
class TestTestCase(tests.TestCase):
1358
 
    """Tests that test the core bzrlib TestCase."""
1359
 
 
1360
 
    def test_assertLength_matches_empty(self):
1361
 
        a_list = []
1362
 
        self.assertLength(0, a_list)
1363
 
 
1364
 
    def test_assertLength_matches_nonempty(self):
1365
 
        a_list = [1, 2, 3]
1366
 
        self.assertLength(3, a_list)
1367
 
 
1368
 
    def test_assertLength_fails_different(self):
1369
 
        a_list = []
1370
 
        self.assertRaises(AssertionError, self.assertLength, 1, a_list)
1371
 
 
1372
 
    def test_assertLength_shows_sequence_in_failure(self):
1373
 
        a_list = [1, 2, 3]
1374
 
        exception = self.assertRaises(AssertionError, self.assertLength, 2,
1375
 
            a_list)
1376
 
        self.assertEqual('Incorrect length: wanted 2, got 3 for [1, 2, 3]',
1377
 
            exception.args[0])
1378
 
 
1379
 
    def test_base_setUp_not_called_causes_failure(self):
1380
 
        class TestCaseWithBrokenSetUp(tests.TestCase):
1381
 
            def setUp(self):
1382
 
                pass # does not call TestCase.setUp
1383
 
            def test_foo(self):
1384
 
                pass
1385
 
        test = TestCaseWithBrokenSetUp('test_foo')
1386
 
        result = unittest.TestResult()
1387
 
        test.run(result)
1388
 
        self.assertFalse(result.wasSuccessful())
1389
 
        self.assertEqual(1, result.testsRun)
1390
 
 
1391
 
    def test_base_tearDown_not_called_causes_failure(self):
1392
 
        class TestCaseWithBrokenTearDown(tests.TestCase):
1393
 
            def tearDown(self):
1394
 
                pass # does not call TestCase.tearDown
1395
 
            def test_foo(self):
1396
 
                pass
1397
 
        test = TestCaseWithBrokenTearDown('test_foo')
1398
 
        result = unittest.TestResult()
1399
 
        test.run(result)
1400
 
        self.assertFalse(result.wasSuccessful())
1401
 
        self.assertEqual(1, result.testsRun)
1402
 
 
1403
 
    def test_debug_flags_sanitised(self):
1404
 
        """The bzrlib debug flags should be sanitised by setUp."""
1405
 
        if 'allow_debug' in tests.selftest_debug_flags:
1406
 
            raise tests.TestNotApplicable(
1407
 
                '-Eallow_debug option prevents debug flag sanitisation')
1408
 
        # we could set something and run a test that will check
1409
 
        # it gets santised, but this is probably sufficient for now:
1410
 
        # if someone runs the test with -Dsomething it will error.
1411
 
        self.assertEqual(set(), bzrlib.debug.debug_flags)
1412
 
 
1413
 
    def change_selftest_debug_flags(self, new_flags):
1414
 
        orig_selftest_flags = tests.selftest_debug_flags
1415
 
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
1416
 
        tests.selftest_debug_flags = set(new_flags)
1417
 
 
1418
 
    def _restore_selftest_debug_flags(self, flags):
1419
 
        tests.selftest_debug_flags = flags
1420
 
 
1421
 
    def test_allow_debug_flag(self):
1422
 
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1423
 
        sanitised (i.e. cleared) before running a test.
1424
 
        """
1425
 
        self.change_selftest_debug_flags(set(['allow_debug']))
1426
 
        bzrlib.debug.debug_flags = set(['a-flag'])
1427
 
        class TestThatRecordsFlags(tests.TestCase):
1428
 
            def test_foo(nested_self):
1429
 
                self.flags = set(bzrlib.debug.debug_flags)
1430
 
        test = TestThatRecordsFlags('test_foo')
1431
 
        test.run(self.make_test_result())
1432
 
        self.assertEqual(set(['a-flag']), self.flags)
1433
 
 
1434
 
    def test_debug_flags_restored(self):
1435
 
        """The bzrlib debug flags should be restored to their original state
1436
 
        after the test was run, even if allow_debug is set.
1437
 
        """
1438
 
        self.change_selftest_debug_flags(set(['allow_debug']))
1439
 
        # Now run a test that modifies debug.debug_flags.
1440
 
        bzrlib.debug.debug_flags = set(['original-state'])
1441
 
        class TestThatModifiesFlags(tests.TestCase):
1442
 
            def test_foo(self):
1443
 
                bzrlib.debug.debug_flags = set(['modified'])
1444
 
        test = TestThatModifiesFlags('test_foo')
1445
 
        test.run(self.make_test_result())
1446
 
        self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)
1447
 
 
1448
 
    def make_test_result(self):
1449
 
        return tests.TextTestResult(self._log_file, descriptions=0, verbosity=1)
1450
 
 
1451
 
    def inner_test(self):
1452
 
        # the inner child test
1453
 
        note("inner_test")
1454
 
 
1455
 
    def outer_child(self):
1456
 
        # the outer child test
1457
 
        note("outer_start")
1458
 
        self.inner_test = TestTestCase("inner_child")
1459
 
        result = self.make_test_result()
1460
 
        self.inner_test.run(result)
1461
 
        note("outer finish")
1462
 
 
1463
 
    def test_trace_nesting(self):
1464
 
        # this tests that each test case nests its trace facility correctly.
1465
 
        # we do this by running a test case manually. That test case (A)
1466
 
        # should setup a new log, log content to it, setup a child case (B),
1467
 
        # which should log independently, then case (A) should log a trailer
1468
 
        # and return.
1469
 
        # we do two nested children so that we can verify the state of the
1470
 
        # logs after the outer child finishes is correct, which a bad clean
1471
 
        # up routine in tearDown might trigger a fault in our test with only
1472
 
        # one child, we should instead see the bad result inside our test with
1473
 
        # the two children.
1474
 
        # the outer child test
1475
 
        original_trace = bzrlib.trace._trace_file
1476
 
        outer_test = TestTestCase("outer_child")
1477
 
        result = self.make_test_result()
1478
 
        outer_test.run(result)
1479
 
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
1480
 
 
1481
 
    def method_that_times_a_bit_twice(self):
1482
 
        # call self.time twice to ensure it aggregates
1483
 
        self.time(time.sleep, 0.007)
1484
 
        self.time(time.sleep, 0.007)
1485
 
 
1486
 
    def test_time_creates_benchmark_in_result(self):
1487
 
        """Test that the TestCase.time() method accumulates a benchmark time."""
1488
 
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1489
 
        output_stream = StringIO()
1490
 
        result = bzrlib.tests.VerboseTestResult(
1491
 
            unittest._WritelnDecorator(output_stream),
1492
 
            descriptions=0,
1493
 
            verbosity=2,
1494
 
            num_tests=sample_test.countTestCases())
1495
 
        sample_test.run(result)
1496
 
        self.assertContainsRe(
1497
 
            output_stream.getvalue(),
1498
 
            r"\d+ms/ +\d+ms\n$")
1499
 
 
1500
 
    def test_hooks_sanitised(self):
1501
 
        """The bzrlib hooks should be sanitised by setUp."""
1502
 
        # Note this test won't fail with hooks that the core library doesn't
1503
 
        # use - but it trigger with a plugin that adds hooks, so its still a
1504
 
        # useful warning in that case.
1505
 
        self.assertEqual(bzrlib.branch.BranchHooks(),
1506
 
            bzrlib.branch.Branch.hooks)
1507
 
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1508
 
            bzrlib.smart.server.SmartTCPServer.hooks)
1509
 
        self.assertEqual(bzrlib.commands.CommandHooks(),
1510
 
            bzrlib.commands.Command.hooks)
1511
 
 
1512
 
    def test__gather_lsprof_in_benchmarks(self):
1513
 
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1514
 
 
1515
 
        Each self.time() call is individually and separately profiled.
1516
 
        """
1517
 
        self.requireFeature(test_lsprof.LSProfFeature)
1518
 
        # overrides the class member with an instance member so no cleanup
1519
 
        # needed.
1520
 
        self._gather_lsprof_in_benchmarks = True
1521
 
        self.time(time.sleep, 0.000)
1522
 
        self.time(time.sleep, 0.003)
1523
 
        self.assertEqual(2, len(self._benchcalls))
1524
 
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1525
 
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1526
 
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1527
 
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1528
 
 
1529
 
    def test_knownFailure(self):
1530
 
        """Self.knownFailure() should raise a KnownFailure exception."""
1531
 
        self.assertRaises(tests.KnownFailure, self.knownFailure, "A Failure")
1532
 
 
1533
 
    def test_requireFeature_available(self):
1534
 
        """self.requireFeature(available) is a no-op."""
1535
 
        class Available(tests.Feature):
1536
 
            def _probe(self):return True
1537
 
        feature = Available()
1538
 
        self.requireFeature(feature)
1539
 
 
1540
 
    def test_requireFeature_unavailable(self):
1541
 
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1542
 
        class Unavailable(tests.Feature):
1543
 
            def _probe(self):return False
1544
 
        feature = Unavailable()
1545
 
        self.assertRaises(tests.UnavailableFeature,
1546
 
                          self.requireFeature, feature)
1547
 
 
1548
 
    def test_run_no_parameters(self):
1549
 
        test = SampleTestCase('_test_pass')
1550
 
        test.run()
1551
 
 
1552
 
    def test_run_enabled_unittest_result(self):
1553
 
        """Test we revert to regular behaviour when the test is enabled."""
1554
 
        test = SampleTestCase('_test_pass')
1555
 
        class EnabledFeature(object):
1556
 
            def available(self):
1557
 
                return True
1558
 
        test._test_needs_features = [EnabledFeature()]
1559
 
        result = unittest.TestResult()
1560
 
        test.run(result)
1561
 
        self.assertEqual(1, result.testsRun)
1562
 
        self.assertEqual([], result.errors)
1563
 
        self.assertEqual([], result.failures)
1564
 
 
1565
 
    def test_run_disabled_unittest_result(self):
1566
 
        """Test our compatability for disabled tests with unittest results."""
1567
 
        test = SampleTestCase('_test_pass')
1568
 
        class DisabledFeature(object):
1569
 
            def available(self):
1570
 
                return False
1571
 
        test._test_needs_features = [DisabledFeature()]
1572
 
        result = unittest.TestResult()
1573
 
        test.run(result)
1574
 
        self.assertEqual(1, result.testsRun)
1575
 
        self.assertEqual([], result.errors)
1576
 
        self.assertEqual([], result.failures)
1577
 
 
1578
 
    def test_run_disabled_supporting_result(self):
1579
 
        """Test disabled tests behaviour with support aware results."""
1580
 
        test = SampleTestCase('_test_pass')
1581
 
        class DisabledFeature(object):
1582
 
            def available(self):
1583
 
                return False
1584
 
        the_feature = DisabledFeature()
1585
 
        test._test_needs_features = [the_feature]
1586
 
        class InstrumentedTestResult(unittest.TestResult):
1587
 
            def __init__(self):
1588
 
                unittest.TestResult.__init__(self)
1589
 
                self.calls = []
1590
 
            def startTest(self, test):
1591
 
                self.calls.append(('startTest', test))
1592
 
            def stopTest(self, test):
1593
 
                self.calls.append(('stopTest', test))
1594
 
            def addNotSupported(self, test, feature):
1595
 
                self.calls.append(('addNotSupported', test, feature))
1596
 
        result = InstrumentedTestResult()
1597
 
        test.run(result)
1598
 
        self.assertEqual([
1599
 
            ('startTest', test),
1600
 
            ('addNotSupported', test, the_feature),
1601
 
            ('stopTest', test),
1602
 
            ],
1603
 
            result.calls)
1604
 
 
1605
 
    def test_assert_list_raises_on_generator(self):
1606
 
        def generator_which_will_raise():
1607
 
            # This will not raise until after the first yield
1608
 
            yield 1
1609
 
            raise _TestException()
1610
 
 
1611
 
        e = self.assertListRaises(_TestException, generator_which_will_raise)
1612
 
        self.assertIsInstance(e, _TestException)
1613
 
 
1614
 
        e = self.assertListRaises(Exception, generator_which_will_raise)
1615
 
        self.assertIsInstance(e, _TestException)
1616
 
 
1617
 
    def test_assert_list_raises_on_plain(self):
1618
 
        def plain_exception():
1619
 
            raise _TestException()
1620
 
            return []
1621
 
 
1622
 
        e = self.assertListRaises(_TestException, plain_exception)
1623
 
        self.assertIsInstance(e, _TestException)
1624
 
 
1625
 
        e = self.assertListRaises(Exception, plain_exception)
1626
 
        self.assertIsInstance(e, _TestException)
1627
 
 
1628
 
    def test_assert_list_raises_assert_wrong_exception(self):
1629
 
        class _NotTestException(Exception):
1630
 
            pass
1631
 
 
1632
 
        def wrong_exception():
1633
 
            raise _NotTestException()
1634
 
 
1635
 
        def wrong_exception_generator():
1636
 
            yield 1
1637
 
            yield 2
1638
 
            raise _NotTestException()
1639
 
 
1640
 
        # Wrong exceptions are not intercepted
1641
 
        self.assertRaises(_NotTestException,
1642
 
            self.assertListRaises, _TestException, wrong_exception)
1643
 
        self.assertRaises(_NotTestException,
1644
 
            self.assertListRaises, _TestException, wrong_exception_generator)
1645
 
 
1646
 
    def test_assert_list_raises_no_exception(self):
1647
 
        def success():
1648
 
            return []
1649
 
 
1650
 
        def success_generator():
1651
 
            yield 1
1652
 
            yield 2
1653
 
 
1654
 
        self.assertRaises(AssertionError,
1655
 
            self.assertListRaises, _TestException, success)
1656
 
 
1657
 
        self.assertRaises(AssertionError,
1658
 
            self.assertListRaises, _TestException, success_generator)
1659
 
 
1660
 
 
1661
 
# NB: Don't delete this; it's not actually from 0.11!
1662
 
@deprecated_function(deprecated_in((0, 11, 0)))
1663
 
def sample_deprecated_function():
1664
 
    """A deprecated function to test applyDeprecated with."""
1665
 
    return 2
1666
 
 
1667
 
 
1668
 
def sample_undeprecated_function(a_param):
1669
 
    """A undeprecated function to test applyDeprecated with."""
1670
 
 
1671
 
 
1672
 
class ApplyDeprecatedHelper(object):
1673
 
    """A helper class for ApplyDeprecated tests."""
1674
 
 
1675
 
    @deprecated_method(deprecated_in((0, 11, 0)))
1676
 
    def sample_deprecated_method(self, param_one):
1677
 
        """A deprecated method for testing with."""
1678
 
        return param_one
1679
 
 
1680
 
    def sample_normal_method(self):
1681
 
        """A undeprecated method."""
1682
 
 
1683
 
    @deprecated_method(deprecated_in((0, 10, 0)))
1684
 
    def sample_nested_deprecation(self):
1685
 
        return sample_deprecated_function()
1686
 
 
1687
 
 
1688
 
class TestExtraAssertions(tests.TestCase):
1689
 
    """Tests for new test assertions in bzrlib test suite"""
1690
 
 
1691
 
    def test_assert_isinstance(self):
1692
 
        self.assertIsInstance(2, int)
1693
 
        self.assertIsInstance(u'', basestring)
1694
 
        self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1695
 
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1696
 
 
1697
 
    def test_assertEndsWith(self):
1698
 
        self.assertEndsWith('foo', 'oo')
1699
 
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1700
 
 
1701
 
    def test_applyDeprecated_not_deprecated(self):
1702
 
        sample_object = ApplyDeprecatedHelper()
1703
 
        # calling an undeprecated callable raises an assertion
1704
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1705
 
            deprecated_in((0, 11, 0)),
1706
 
            sample_object.sample_normal_method)
1707
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1708
 
            deprecated_in((0, 11, 0)),
1709
 
            sample_undeprecated_function, "a param value")
1710
 
        # calling a deprecated callable (function or method) with the wrong
1711
 
        # expected deprecation fails.
1712
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1713
 
            deprecated_in((0, 10, 0)),
1714
 
            sample_object.sample_deprecated_method, "a param value")
1715
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1716
 
            deprecated_in((0, 10, 0)),
1717
 
            sample_deprecated_function)
1718
 
        # calling a deprecated callable (function or method) with the right
1719
 
        # expected deprecation returns the functions result.
1720
 
        self.assertEqual("a param value",
1721
 
            self.applyDeprecated(deprecated_in((0, 11, 0)),
1722
 
            sample_object.sample_deprecated_method, "a param value"))
1723
 
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 11, 0)),
1724
 
            sample_deprecated_function))
1725
 
        # calling a nested deprecation with the wrong deprecation version
1726
 
        # fails even if a deeper nested function was deprecated with the
1727
 
        # supplied version.
1728
 
        self.assertRaises(AssertionError, self.applyDeprecated,
1729
 
            deprecated_in((0, 11, 0)), sample_object.sample_nested_deprecation)
1730
 
        # calling a nested deprecation with the right deprecation value
1731
 
        # returns the calls result.
1732
 
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 10, 0)),
1733
 
            sample_object.sample_nested_deprecation))
1734
 
 
1735
 
    def test_callDeprecated(self):
1736
 
        def testfunc(be_deprecated, result=None):
1737
 
            if be_deprecated is True:
1738
 
                symbol_versioning.warn('i am deprecated', DeprecationWarning,
1739
 
                                       stacklevel=1)
1740
 
            return result
1741
 
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
1742
 
        self.assertIs(None, result)
1743
 
        result = self.callDeprecated([], testfunc, False, 'result')
1744
 
        self.assertEqual('result', result)
1745
 
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1746
 
        self.callDeprecated([], testfunc, be_deprecated=False)
1747
 
 
1748
 
 
1749
 
class TestWarningTests(tests.TestCase):
1750
 
    """Tests for calling methods that raise warnings."""
1751
 
 
1752
 
    def test_callCatchWarnings(self):
1753
 
        def meth(a, b):
1754
 
            warnings.warn("this is your last warning")
1755
 
            return a + b
1756
 
        wlist, result = self.callCatchWarnings(meth, 1, 2)
1757
 
        self.assertEquals(3, result)
1758
 
        # would like just to compare them, but UserWarning doesn't implement
1759
 
        # eq well
1760
 
        w0, = wlist
1761
 
        self.assertIsInstance(w0, UserWarning)
1762
 
        self.assertEquals("this is your last warning", str(w0))
1763
 
 
1764
 
 
1765
 
class TestConvenienceMakers(tests.TestCaseWithTransport):
1766
 
    """Test for the make_* convenience functions."""
1767
 
 
1768
 
    def test_make_branch_and_tree_with_format(self):
1769
 
        # we should be able to supply a format to make_branch_and_tree
1770
 
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1771
 
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1772
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1773
 
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1774
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1775
 
                              bzrlib.bzrdir.BzrDirFormat6)
1776
 
 
1777
 
    def test_make_branch_and_memory_tree(self):
1778
 
        # we should be able to get a new branch and a mutable tree from
1779
 
        # TestCaseWithTransport
1780
 
        tree = self.make_branch_and_memory_tree('a')
1781
 
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1782
 
 
1783
 
 
1784
 
class TestSFTPMakeBranchAndTree(test_sftp_transport.TestCaseWithSFTPServer):
1785
 
 
1786
 
    def test_make_tree_for_sftp_branch(self):
1787
 
        """Transports backed by local directories create local trees."""
1788
 
 
1789
 
        tree = self.make_branch_and_tree('t1')
1790
 
        base = tree.bzrdir.root_transport.base
1791
 
        self.failIf(base.startswith('sftp'),
1792
 
                'base %r is on sftp but should be local' % base)
1793
 
        self.assertEquals(tree.bzrdir.root_transport,
1794
 
                tree.branch.bzrdir.root_transport)
1795
 
        self.assertEquals(tree.bzrdir.root_transport,
1796
 
                tree.branch.repository.bzrdir.root_transport)
1797
 
 
1798
 
 
1799
 
class TestSelftest(tests.TestCase):
1800
 
    """Tests of bzrlib.tests.selftest."""
1801
 
 
1802
 
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1803
 
        factory_called = []
1804
 
        def factory():
1805
 
            factory_called.append(True)
1806
 
            return TestUtil.TestSuite()
1807
 
        out = StringIO()
1808
 
        err = StringIO()
1809
 
        self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1810
 
            test_suite_factory=factory)
1811
 
        self.assertEqual([True], factory_called)
1812
 
 
1813
 
 
1814
 
class TestKnownFailure(tests.TestCase):
1815
 
 
1816
 
    def test_known_failure(self):
1817
 
        """Check that KnownFailure is defined appropriately."""
1818
 
        # a KnownFailure is an assertion error for compatability with unaware
1819
 
        # runners.
1820
 
        self.assertIsInstance(tests.KnownFailure(""), AssertionError)
1821
 
 
1822
 
    def test_expect_failure(self):
1823
 
        try:
1824
 
            self.expectFailure("Doomed to failure", self.assertTrue, False)
1825
 
        except tests.KnownFailure, e:
1826
 
            self.assertEqual('Doomed to failure', e.args[0])
1827
 
        try:
1828
 
            self.expectFailure("Doomed to failure", self.assertTrue, True)
1829
 
        except AssertionError, e:
1830
 
            self.assertEqual('Unexpected success.  Should have failed:'
1831
 
                             ' Doomed to failure', e.args[0])
1832
 
        else:
1833
 
            self.fail('Assertion not raised')
1834
 
 
1835
 
 
1836
 
class TestFeature(tests.TestCase):
1837
 
 
1838
 
    def test_caching(self):
1839
 
        """Feature._probe is called by the feature at most once."""
1840
 
        class InstrumentedFeature(tests.Feature):
1841
 
            def __init__(self):
1842
 
                super(InstrumentedFeature, self).__init__()
1843
 
                self.calls = []
1844
 
            def _probe(self):
1845
 
                self.calls.append('_probe')
1846
 
                return False
1847
 
        feature = InstrumentedFeature()
1848
 
        feature.available()
1849
 
        self.assertEqual(['_probe'], feature.calls)
1850
 
        feature.available()
1851
 
        self.assertEqual(['_probe'], feature.calls)
1852
 
 
1853
 
    def test_named_str(self):
1854
 
        """Feature.__str__ should thunk to feature_name()."""
1855
 
        class NamedFeature(tests.Feature):
1856
 
            def feature_name(self):
1857
 
                return 'symlinks'
1858
 
        feature = NamedFeature()
1859
 
        self.assertEqual('symlinks', str(feature))
1860
 
 
1861
 
    def test_default_str(self):
1862
 
        """Feature.__str__ should default to __class__.__name__."""
1863
 
        class NamedFeature(tests.Feature):
1864
 
            pass
1865
 
        feature = NamedFeature()
1866
 
        self.assertEqual('NamedFeature', str(feature))
1867
 
 
1868
 
 
1869
 
class TestUnavailableFeature(tests.TestCase):
1870
 
 
1871
 
    def test_access_feature(self):
1872
 
        feature = tests.Feature()
1873
 
        exception = tests.UnavailableFeature(feature)
1874
 
        self.assertIs(feature, exception.args[0])
1875
 
 
1876
 
 
1877
 
class TestSelftestFiltering(tests.TestCase):
1878
 
 
1879
 
    def setUp(self):
1880
 
        tests.TestCase.setUp(self)
1881
 
        self.suite = TestUtil.TestSuite()
1882
 
        self.loader = TestUtil.TestLoader()
1883
 
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1884
 
            'bzrlib.tests.test_selftest']))
1885
 
        self.all_names = _test_ids(self.suite)
1886
 
 
1887
 
    def test_condition_id_re(self):
1888
 
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1889
 
            'test_condition_id_re')
1890
 
        filtered_suite = tests.filter_suite_by_condition(
1891
 
            self.suite, tests.condition_id_re('test_condition_id_re'))
1892
 
        self.assertEqual([test_name], _test_ids(filtered_suite))
1893
 
 
1894
 
    def test_condition_id_in_list(self):
1895
 
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1896
 
                      'test_condition_id_in_list']
1897
 
        id_list = tests.TestIdList(test_names)
1898
 
        filtered_suite = tests.filter_suite_by_condition(
1899
 
            self.suite, tests.condition_id_in_list(id_list))
1900
 
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1901
 
        re_filtered = tests.filter_suite_by_re(self.suite, my_pattern)
1902
 
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1903
 
 
1904
 
    def test_condition_id_startswith(self):
1905
 
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1906
 
        start1 = klass + 'test_condition_id_starts'
1907
 
        start2 = klass + 'test_condition_id_in'
1908
 
        test_names = [ klass + 'test_condition_id_in_list',
1909
 
                      klass + 'test_condition_id_startswith',
1910
 
                     ]
1911
 
        filtered_suite = tests.filter_suite_by_condition(
1912
 
            self.suite, tests.condition_id_startswith([start1, start2]))
1913
 
        self.assertEqual(test_names, _test_ids(filtered_suite))
1914
 
 
1915
 
    def test_condition_isinstance(self):
1916
 
        filtered_suite = tests.filter_suite_by_condition(
1917
 
            self.suite, tests.condition_isinstance(self.__class__))
1918
 
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1919
 
        re_filtered = tests.filter_suite_by_re(self.suite, class_pattern)
1920
 
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1921
 
 
1922
 
    def test_exclude_tests_by_condition(self):
1923
 
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1924
 
            'test_exclude_tests_by_condition')
1925
 
        filtered_suite = tests.exclude_tests_by_condition(self.suite,
1926
 
            lambda x:x.id() == excluded_name)
1927
 
        self.assertEqual(len(self.all_names) - 1,
1928
 
            filtered_suite.countTestCases())
1929
 
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
1930
 
        remaining_names = list(self.all_names)
1931
 
        remaining_names.remove(excluded_name)
1932
 
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
1933
 
 
1934
 
    def test_exclude_tests_by_re(self):
1935
 
        self.all_names = _test_ids(self.suite)
1936
 
        filtered_suite = tests.exclude_tests_by_re(self.suite,
1937
 
                                                   'exclude_tests_by_re')
1938
 
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1939
 
            'test_exclude_tests_by_re')
1940
 
        self.assertEqual(len(self.all_names) - 1,
1941
 
            filtered_suite.countTestCases())
1942
 
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
1943
 
        remaining_names = list(self.all_names)
1944
 
        remaining_names.remove(excluded_name)
1945
 
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
1946
 
 
1947
 
    def test_filter_suite_by_condition(self):
1948
 
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1949
 
            'test_filter_suite_by_condition')
1950
 
        filtered_suite = tests.filter_suite_by_condition(self.suite,
1951
 
            lambda x:x.id() == test_name)
1952
 
        self.assertEqual([test_name], _test_ids(filtered_suite))
1953
 
 
1954
 
    def test_filter_suite_by_re(self):
1955
 
        filtered_suite = tests.filter_suite_by_re(self.suite,
1956
 
                                                  'test_filter_suite_by_r')
1957
 
        filtered_names = _test_ids(filtered_suite)
1958
 
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1959
 
            'TestSelftestFiltering.test_filter_suite_by_re'])
1960
 
 
1961
 
    def test_filter_suite_by_id_list(self):
1962
 
        test_list = ['bzrlib.tests.test_selftest.'
1963
 
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
1964
 
        filtered_suite = tests.filter_suite_by_id_list(
1965
 
            self.suite, tests.TestIdList(test_list))
1966
 
        filtered_names = _test_ids(filtered_suite)
1967
 
        self.assertEqual(
1968
 
            filtered_names,
1969
 
            ['bzrlib.tests.test_selftest.'
1970
 
             'TestSelftestFiltering.test_filter_suite_by_id_list'])
1971
 
 
1972
 
    def test_filter_suite_by_id_startswith(self):
1973
 
        # By design this test may fail if another test is added whose name also
1974
 
        # begins with one of the start value used.
1975
 
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1976
 
        start1 = klass + 'test_filter_suite_by_id_starts'
1977
 
        start2 = klass + 'test_filter_suite_by_id_li'
1978
 
        test_list = [klass + 'test_filter_suite_by_id_list',
1979
 
                     klass + 'test_filter_suite_by_id_startswith',
1980
 
                     ]
1981
 
        filtered_suite = tests.filter_suite_by_id_startswith(
1982
 
            self.suite, [start1, start2])
1983
 
        self.assertEqual(
1984
 
            test_list,
1985
 
            _test_ids(filtered_suite),
1986
 
            )
1987
 
 
1988
 
    def test_preserve_input(self):
1989
 
        # NB: Surely this is something in the stdlib to do this?
1990
 
        self.assertTrue(self.suite is tests.preserve_input(self.suite))
1991
 
        self.assertTrue("@#$" is tests.preserve_input("@#$"))
1992
 
 
1993
 
    def test_randomize_suite(self):
1994
 
        randomized_suite = tests.randomize_suite(self.suite)
1995
 
        # randomizing should not add or remove test names.
1996
 
        self.assertEqual(set(_test_ids(self.suite)),
1997
 
                         set(_test_ids(randomized_suite)))
1998
 
        # Technically, this *can* fail, because random.shuffle(list) can be
1999
 
        # equal to list. Trying multiple times just pushes the frequency back.
2000
 
        # As its len(self.all_names)!:1, the failure frequency should be low
2001
 
        # enough to ignore. RBC 20071021.
2002
 
        # It should change the order.
2003
 
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
2004
 
        # But not the length. (Possibly redundant with the set test, but not
2005
 
        # necessarily.)
2006
 
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
2007
 
 
2008
 
    def test_split_suit_by_condition(self):
2009
 
        self.all_names = _test_ids(self.suite)
2010
 
        condition = tests.condition_id_re('test_filter_suite_by_r')
2011
 
        split_suite = tests.split_suite_by_condition(self.suite, condition)
2012
 
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
2013
 
            'test_filter_suite_by_re')
2014
 
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
2015
 
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
2016
 
        remaining_names = list(self.all_names)
2017
 
        remaining_names.remove(filtered_name)
2018
 
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2019
 
 
2020
 
    def test_split_suit_by_re(self):
2021
 
        self.all_names = _test_ids(self.suite)
2022
 
        split_suite = tests.split_suite_by_re(self.suite,
2023
 
                                              'test_filter_suite_by_r')
2024
 
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
2025
 
            'test_filter_suite_by_re')
2026
 
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
2027
 
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
2028
 
        remaining_names = list(self.all_names)
2029
 
        remaining_names.remove(filtered_name)
2030
 
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2031
 
 
2032
 
 
2033
 
class TestCheckInventoryShape(tests.TestCaseWithTransport):
2034
 
 
2035
 
    def test_check_inventory_shape(self):
2036
 
        files = ['a', 'b/', 'b/c']
2037
 
        tree = self.make_branch_and_tree('.')
2038
 
        self.build_tree(files)
2039
 
        tree.add(files)
2040
 
        tree.lock_read()
2041
 
        try:
2042
 
            self.check_inventory_shape(tree.inventory, files)
2043
 
        finally:
2044
 
            tree.unlock()
2045
 
 
2046
 
 
2047
 
class TestBlackboxSupport(tests.TestCase):
2048
 
    """Tests for testsuite blackbox features."""
2049
 
 
2050
 
    def test_run_bzr_failure_not_caught(self):
2051
 
        # When we run bzr in blackbox mode, we want any unexpected errors to
2052
 
        # propagate up to the test suite so that it can show the error in the
2053
 
        # usual way, and we won't get a double traceback.
2054
 
        e = self.assertRaises(
2055
 
            AssertionError,
2056
 
            self.run_bzr, ['assert-fail'])
2057
 
        # make sure we got the real thing, not an error from somewhere else in
2058
 
        # the test framework
2059
 
        self.assertEquals('always fails', str(e))
2060
 
        # check that there's no traceback in the test log
2061
 
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
2062
 
            r'Traceback')
2063
 
 
2064
 
    def test_run_bzr_user_error_caught(self):
2065
 
        # Running bzr in blackbox mode, normal/expected/user errors should be
2066
 
        # caught in the regular way and turned into an error message plus exit
2067
 
        # code.
2068
 
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
2069
 
        self.assertEqual(out, '')
2070
 
        self.assertContainsRe(err,
2071
 
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
2072
 
 
2073
 
 
2074
 
class TestTestLoader(tests.TestCase):
2075
 
    """Tests for the test loader."""
2076
 
 
2077
 
    def _get_loader_and_module(self):
2078
 
        """Gets a TestLoader and a module with one test in it."""
2079
 
        loader = TestUtil.TestLoader()
2080
 
        module = {}
2081
 
        class Stub(tests.TestCase):
2082
 
            def test_foo(self):
2083
 
                pass
2084
 
        class MyModule(object):
2085
 
            pass
2086
 
        MyModule.a_class = Stub
2087
 
        module = MyModule()
2088
 
        return loader, module
2089
 
 
2090
 
    def test_module_no_load_tests_attribute_loads_classes(self):
2091
 
        loader, module = self._get_loader_and_module()
2092
 
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
2093
 
 
2094
 
    def test_module_load_tests_attribute_gets_called(self):
2095
 
        loader, module = self._get_loader_and_module()
2096
 
        # 'self' is here because we're faking the module with a class. Regular
2097
 
        # load_tests do not need that :)
2098
 
        def load_tests(self, standard_tests, module, loader):
2099
 
            result = loader.suiteClass()
2100
 
            for test in tests.iter_suite_tests(standard_tests):
2101
 
                result.addTests([test, test])
2102
 
            return result
2103
 
        # add a load_tests() method which multiplies the tests from the module.
2104
 
        module.__class__.load_tests = load_tests
2105
 
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
2106
 
 
2107
 
    def test_load_tests_from_module_name_smoke_test(self):
2108
 
        loader = TestUtil.TestLoader()
2109
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2110
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2111
 
                          _test_ids(suite))
2112
 
 
2113
 
    def test_load_tests_from_module_name_with_bogus_module_name(self):
2114
 
        loader = TestUtil.TestLoader()
2115
 
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
2116
 
 
2117
 
 
2118
 
class TestTestIdList(tests.TestCase):
2119
 
 
2120
 
    def _create_id_list(self, test_list):
2121
 
        return tests.TestIdList(test_list)
2122
 
 
2123
 
    def _create_suite(self, test_id_list):
2124
 
 
2125
 
        class Stub(tests.TestCase):
2126
 
            def test_foo(self):
2127
 
                pass
2128
 
 
2129
 
        def _create_test_id(id):
2130
 
            return lambda: id
2131
 
 
2132
 
        suite = TestUtil.TestSuite()
2133
 
        for id in test_id_list:
2134
 
            t  = Stub('test_foo')
2135
 
            t.id = _create_test_id(id)
2136
 
            suite.addTest(t)
2137
 
        return suite
2138
 
 
2139
 
    def _test_ids(self, test_suite):
2140
 
        """Get the ids for the tests in a test suite."""
2141
 
        return [t.id() for t in tests.iter_suite_tests(test_suite)]
2142
 
 
2143
 
    def test_empty_list(self):
2144
 
        id_list = self._create_id_list([])
2145
 
        self.assertEquals({}, id_list.tests)
2146
 
        self.assertEquals({}, id_list.modules)
2147
 
 
2148
 
    def test_valid_list(self):
2149
 
        id_list = self._create_id_list(
2150
 
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
2151
 
             'mod1.func1', 'mod1.cl2.meth2',
2152
 
             'mod1.submod1',
2153
 
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
2154
 
             ])
2155
 
        self.assertTrue(id_list.refers_to('mod1'))
2156
 
        self.assertTrue(id_list.refers_to('mod1.submod1'))
2157
 
        self.assertTrue(id_list.refers_to('mod1.submod2'))
2158
 
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
2159
 
        self.assertTrue(id_list.includes('mod1.submod1'))
2160
 
        self.assertTrue(id_list.includes('mod1.func1'))
2161
 
 
2162
 
    def test_bad_chars_in_params(self):
2163
 
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
2164
 
        self.assertTrue(id_list.refers_to('mod1'))
2165
 
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
2166
 
 
2167
 
    def test_module_used(self):
2168
 
        id_list = self._create_id_list(['mod.class.meth'])
2169
 
        self.assertTrue(id_list.refers_to('mod'))
2170
 
        self.assertTrue(id_list.refers_to('mod.class'))
2171
 
        self.assertTrue(id_list.refers_to('mod.class.meth'))
2172
 
 
2173
 
    def test_test_suite_matches_id_list_with_unknown(self):
2174
 
        loader = TestUtil.TestLoader()
2175
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2176
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2177
 
                     'bogus']
2178
 
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2179
 
        self.assertEquals(['bogus'], not_found)
2180
 
        self.assertEquals([], duplicates)
2181
 
 
2182
 
    def test_suite_matches_id_list_with_duplicates(self):
2183
 
        loader = TestUtil.TestLoader()
2184
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2185
 
        dupes = loader.suiteClass()
2186
 
        for test in tests.iter_suite_tests(suite):
2187
 
            dupes.addTest(test)
2188
 
            dupes.addTest(test) # Add it again
2189
 
 
2190
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2191
 
        not_found, duplicates = tests.suite_matches_id_list(
2192
 
            dupes, test_list)
2193
 
        self.assertEquals([], not_found)
2194
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2195
 
                          duplicates)
2196
 
 
2197
 
 
2198
 
class TestTestSuite(tests.TestCase):
2199
 
 
2200
 
    def test_test_suite(self):
2201
 
        # This test is slow, so we do a single test with one test in each
2202
 
        # category
2203
 
        test_list = [
2204
 
            # testmod_names
2205
 
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2206
 
            'bzrlib.tests.test_selftest.TestTestSuite.test_test_suite',
2207
 
            # transport implementations
2208
 
            'bzrlib.tests.test_transport_implementations.TransportTests'
2209
 
            '.test_abspath(LocalURLServer)',
2210
 
            # modules_to_doctest
2211
 
            'bzrlib.timestamp.format_highres_date',
2212
 
            # plugins can't be tested that way since selftest may be run with
2213
 
            # --no-plugins
2214
 
            ]
2215
 
        suite = tests.test_suite(test_list)
2216
 
        self.assertEquals(test_list, _test_ids(suite))
2217
 
 
2218
 
    def test_test_suite_list_and_start(self):
2219
 
        test_list = ['bzrlib.tests.test_selftest.TestTestSuite.test_test_suite']
2220
 
        suite = tests.test_suite(test_list,
2221
 
                                 ['bzrlib.tests.test_selftest.TestTestSuite'])
2222
 
        # test_test_suite_list_and_start is not included 
2223
 
        self.assertEquals(test_list, _test_ids(suite))
2224
 
 
2225
 
 
2226
 
class TestLoadTestIdList(tests.TestCaseInTempDir):
2227
 
 
2228
 
    def _create_test_list_file(self, file_name, content):
2229
 
        fl = open(file_name, 'wt')
2230
 
        fl.write(content)
2231
 
        fl.close()
2232
 
 
2233
 
    def test_load_unknown(self):
2234
 
        self.assertRaises(errors.NoSuchFile,
2235
 
                          tests.load_test_id_list, 'i_do_not_exist')
2236
 
 
2237
 
    def test_load_test_list(self):
2238
 
        test_list_fname = 'test.list'
2239
 
        self._create_test_list_file(test_list_fname,
2240
 
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
2241
 
        tlist = tests.load_test_id_list(test_list_fname)
2242
 
        self.assertEquals(2, len(tlist))
2243
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2244
 
        self.assertEquals('mod2.cl2.meth2', tlist[1])
2245
 
 
2246
 
    def test_load_dirty_file(self):
2247
 
        test_list_fname = 'test.list'
2248
 
        self._create_test_list_file(test_list_fname,
2249
 
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
2250
 
                                    'bar baz\n')
2251
 
        tlist = tests.load_test_id_list(test_list_fname)
2252
 
        self.assertEquals(4, len(tlist))
2253
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2254
 
        self.assertEquals('', tlist[1])
2255
 
        self.assertEquals('mod2.cl2.meth2', tlist[2])
2256
 
        self.assertEquals('bar baz', tlist[3])
2257
 
 
2258
 
 
2259
 
class TestFilteredByModuleTestLoader(tests.TestCase):
2260
 
 
2261
 
    def _create_loader(self, test_list):
2262
 
        id_filter = tests.TestIdList(test_list)
2263
 
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
2264
 
        return loader
2265
 
 
2266
 
    def test_load_tests(self):
2267
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2268
 
        loader = self._create_loader(test_list)
2269
 
 
2270
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2271
 
        self.assertEquals(test_list, _test_ids(suite))
2272
 
 
2273
 
    def test_exclude_tests(self):
2274
 
        test_list = ['bogus']
2275
 
        loader = self._create_loader(test_list)
2276
 
 
2277
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2278
 
        self.assertEquals([], _test_ids(suite))
2279
 
 
2280
 
 
2281
 
class TestFilteredByNameStartTestLoader(tests.TestCase):
2282
 
 
2283
 
    def _create_loader(self, name_start):
2284
 
        def needs_module(name):
2285
 
            return name.startswith(name_start) or name_start.startswith(name)
2286
 
        loader = TestUtil.FilteredByModuleTestLoader(needs_module)
2287
 
        return loader
2288
 
 
2289
 
    def test_load_tests(self):
2290
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2291
 
        loader = self._create_loader('bzrlib.tests.test_samp')
2292
 
 
2293
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2294
 
        self.assertEquals(test_list, _test_ids(suite))
2295
 
 
2296
 
    def test_load_tests_inside_module(self):
2297
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2298
 
        loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
2299
 
 
2300
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2301
 
        self.assertEquals(test_list, _test_ids(suite))
2302
 
 
2303
 
    def test_exclude_tests(self):
2304
 
        test_list = ['bogus']
2305
 
        loader = self._create_loader('bogus')
2306
 
 
2307
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2308
 
        self.assertEquals([], _test_ids(suite))
2309
 
 
2310
 
 
2311
 
class TestTestPrefixRegistry(tests.TestCase):
2312
 
 
2313
 
    def _get_registry(self):
2314
 
        tp_registry = tests.TestPrefixAliasRegistry()
2315
 
        return tp_registry
2316
 
 
2317
 
    def test_register_new_prefix(self):
2318
 
        tpr = self._get_registry()
2319
 
        tpr.register('foo', 'fff.ooo.ooo')
2320
 
        self.assertEquals('fff.ooo.ooo', tpr.get('foo'))
2321
 
 
2322
 
    def test_register_existing_prefix(self):
2323
 
        tpr = self._get_registry()
2324
 
        tpr.register('bar', 'bbb.aaa.rrr')
2325
 
        tpr.register('bar', 'bBB.aAA.rRR')
2326
 
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
2327
 
        self.assertContainsRe(self._get_log(keep_log_file=True),
2328
 
                              r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')
2329
 
 
2330
 
    def test_get_unknown_prefix(self):
2331
 
        tpr = self._get_registry()
2332
 
        self.assertRaises(KeyError, tpr.get, 'I am not a prefix')
2333
 
 
2334
 
    def test_resolve_prefix(self):
2335
 
        tpr = self._get_registry()
2336
 
        tpr.register('bar', 'bb.aa.rr')
2337
 
        self.assertEquals('bb.aa.rr', tpr.resolve_alias('bar'))
2338
 
 
2339
 
    def test_resolve_unknown_alias(self):
2340
 
        tpr = self._get_registry()
2341
 
        self.assertRaises(errors.BzrCommandError,
2342
 
                          tpr.resolve_alias, 'I am not a prefix')
2343
 
 
2344
 
    def test_predefined_prefixes(self):
2345
 
        tpr = tests.test_prefix_alias_registry
2346
 
        self.assertEquals('bzrlib', tpr.resolve_alias('bzrlib'))
2347
 
        self.assertEquals('bzrlib.doc', tpr.resolve_alias('bd'))
2348
 
        self.assertEquals('bzrlib.utils', tpr.resolve_alias('bu'))
2349
 
        self.assertEquals('bzrlib.tests', tpr.resolve_alias('bt'))
2350
 
        self.assertEquals('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
2351
 
        self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))
2352
 
 
2353
 
 
2354
 
class TestRunSuite(tests.TestCase):
2355
 
 
2356
 
    def test_runner_class(self):
2357
 
        """run_suite accepts and uses a runner_class keyword argument."""
2358
 
        class Stub(tests.TestCase):
2359
 
            def test_foo(self):
2360
 
                pass
2361
 
        suite = Stub("test_foo")
2362
 
        calls = []
2363
 
        class MyRunner(tests.TextTestRunner):
2364
 
            def run(self, test):
2365
 
                calls.append(test)
2366
 
                return tests.ExtendedTestResult(self.stream, self.descriptions,
2367
 
                                                self.verbosity)
2368
 
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
2369
 
        self.assertEqual(calls, [suite])
2370
 
 
2371
 
    def test_done(self):
2372
 
        """run_suite should call result.done()"""
2373
 
        self.calls = 0
2374
 
        def one_more_call(): self.calls += 1
2375
 
        def test_function():
2376
 
            pass
2377
 
        test = unittest.FunctionTestCase(test_function)
2378
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
2379
 
            def done(self): one_more_call()
2380
 
        class MyRunner(tests.TextTestRunner):
2381
 
            def run(self, test):
2382
 
                return InstrumentedTestResult(self.stream, self.descriptions,
2383
 
                                              self.verbosity)
2384
 
        tests.run_suite(test, runner_class=MyRunner, stream=StringIO())
2385
 
        self.assertEquals(1, self.calls)