/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2009-04-20 04:19:45 UTC
  • mto: This revision was merged to the branch mainline in revision 4304.
  • Revision ID: robertc@robertcollins.net-20090420041945-qvim67wg99c3euki
Move directory checking for bzr push options into Branch.create_clone_on_transport.

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