~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-02-18 13:27:08 UTC
  • mfrom: (4011.4.3 ssh-hints)
  • Revision ID: pqm@pqm.ubuntu.com-20090218132708-okubrahz9exvae9r
(Jelmer) Point out bzr+ssh:// to the user when they use ssh://.

Show diffs side-by-side

added added

removed removed

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