/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Lukáš Lalinský
  • Date: 2008-05-23 10:39:44 UTC
  • mto: This revision was merged to the branch mainline in revision 3477.
  • Revision ID: lalinsky@gmail.com-20080523103944-00kkkromakot9y9p
Sanitize branch nick before using it as an attachment filename in ``bzr send``

Show diffs side-by-side

added added

removed removed

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