~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-04 18:59:36 UTC
  • mto: This revision was merged to the branch mainline in revision 2643.
  • Revision ID: john@arbash-meinel.com-20070504185936-1mjdoqmtz74xe5mg
A C implementation of _fields_to_entry_0_parents drops the time from 400ms to 330ms for a 21k-entry tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    osutils,
33
33
    repository,
34
34
    symbol_versioning,
35
 
    tests,
36
35
    )
37
36
from bzrlib.progress import _BaseProgressBar
38
37
from bzrlib.repofmt import weaverepo
39
 
from bzrlib.symbol_versioning import (
40
 
    one_zero,
41
 
    zero_eleven,
42
 
    zero_ten,
43
 
    )
 
38
from bzrlib.symbol_versioning import zero_ten, zero_eleven
44
39
from bzrlib.tests import (
45
40
                          ChrootedTestCase,
46
41
                          ExtendedTestResult,
50
45
                          TestCaseInTempDir,
51
46
                          TestCaseWithMemoryTransport,
52
47
                          TestCaseWithTransport,
53
 
                          TestNotApplicable,
54
48
                          TestSkipped,
55
49
                          TestSuite,
56
50
                          TestUtil,
57
51
                          TextTestRunner,
58
52
                          UnavailableFeature,
59
 
                          condition_id_re,
60
 
                          condition_isinstance,
61
 
                          exclude_tests_by_condition,
62
 
                          exclude_tests_by_re,
63
 
                          filter_suite_by_condition,
 
53
                          clean_selftest_output,
 
54
                          iter_suite_tests,
64
55
                          filter_suite_by_re,
65
 
                          iter_suite_tests,
66
 
                          preserve_input,
67
 
                          randomize_suite,
68
56
                          sort_suite_by_re,
69
 
                          split_suite_by_re,
70
 
                          test_lsprof,
71
 
                          test_suite,
 
57
                          test_suite
72
58
                          )
73
59
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
74
60
from bzrlib.tests.TestUtil import _load_module_by_name
77
63
from bzrlib.version import _get_bzr_source_tree
78
64
 
79
65
 
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
66
class SelftestTests(TestCase):
86
67
 
87
68
    def test_import_tests(self):
132
113
            def get_test_permutations(self):
133
114
                return sample_permutation
134
115
        sample_permutation = [(1,2), (3,4)]
135
 
        from bzrlib.tests.test_transport_implementations \
136
 
            import TransportTestProviderAdapter
 
116
        from bzrlib.transport import TransportTestProviderAdapter
137
117
        adapter = TransportTestProviderAdapter()
138
118
        self.assertEqual(sample_permutation,
139
119
                         adapter.get_transport_test_permutations(MockModule()))
144
124
        # - we assume if this matches its probably doing the right thing
145
125
        # especially in combination with the tests for setting the right
146
126
        # classes below.
147
 
        from bzrlib.tests.test_transport_implementations \
148
 
            import TransportTestProviderAdapter
149
 
        from bzrlib.transport import _get_transport_modules
 
127
        from bzrlib.transport import (TransportTestProviderAdapter,
 
128
                                      _get_transport_modules
 
129
                                      )
150
130
        modules = _get_transport_modules()
151
131
        permutation_count = 0
152
132
        for module in modules:
169
149
        # This test used to know about all the possible transports and the
170
150
        # order they were returned but that seems overly brittle (mbp
171
151
        # 20060307)
172
 
        from bzrlib.tests.test_transport_implementations \
173
 
            import TransportTestProviderAdapter
174
 
        scenarios = TransportTestProviderAdapter().scenarios
 
152
        input_test = TestTransportProviderAdapter(
 
153
            "test_adapter_sets_transport_class")
 
154
        from bzrlib.transport import TransportTestProviderAdapter
 
155
        suite = TransportTestProviderAdapter().adapt(input_test)
 
156
        tests = list(iter(suite))
 
157
        self.assertTrue(len(tests) > 6)
175
158
        # 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"],
 
159
        one_test = tests[0]
 
160
        self.assertTrue(issubclass(one_test.transport_class, 
180
161
                                   bzrlib.transport.Transport))
181
 
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
 
162
        self.assertTrue(issubclass(one_test.transport_server, 
182
163
                                   bzrlib.transport.Server))
183
164
 
184
165
 
185
166
class TestBranchProviderAdapter(TestCase):
186
167
    """A group of tests that test the branch implementation test adapter."""
187
168
 
188
 
    def test_constructor(self):
 
169
    def test_adapted_tests(self):
189
170
        # check that constructor parameters are passed through to the adapted
190
171
        # test.
191
 
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
 
172
        from bzrlib.branch import BranchTestProviderAdapter
 
173
        input_test = TestBranchProviderAdapter(
 
174
            "test_adapted_tests")
192
175
        server1 = "a"
193
176
        server2 = "b"
194
177
        formats = [("c", "C"), ("d", "D")]
195
178
        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)
 
179
        suite = adapter.adapt(input_test)
 
180
        tests = list(iter(suite))
 
181
        self.assertEqual(2, len(tests))
 
182
        self.assertEqual(tests[0].branch_format, formats[0][0])
 
183
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
184
        self.assertEqual(tests[0].transport_server, server1)
 
185
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
186
        self.assertEqual(tests[1].branch_format, formats[1][0])
 
187
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
188
        self.assertEqual(tests[1].transport_server, server1)
 
189
        self.assertEqual(tests[1].transport_readonly_server, server2)
209
190
 
210
191
 
211
192
class TestBzrDirProviderAdapter(TestCase):
214
195
    def test_adapted_tests(self):
215
196
        # check that constructor parameters are passed through to the adapted
216
197
        # test.
217
 
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
 
198
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
 
199
        input_test = TestBzrDirProviderAdapter(
 
200
            "test_adapted_tests")
218
201
        vfs_factory = "v"
219
202
        server1 = "a"
220
203
        server2 = "b"
221
204
        formats = ["c", "d"]
222
205
        adapter = BzrDirTestProviderAdapter(vfs_factory,
223
206
            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)
 
207
        suite = adapter.adapt(input_test)
 
208
        tests = list(iter(suite))
 
209
        self.assertEqual(2, len(tests))
 
210
        self.assertEqual(tests[0].bzrdir_format, formats[0])
 
211
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
 
212
        self.assertEqual(tests[0].transport_server, server1)
 
213
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
214
        self.assertEqual(tests[1].bzrdir_format, formats[1])
 
215
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
 
216
        self.assertEqual(tests[1].transport_server, server1)
 
217
        self.assertEqual(tests[1].transport_readonly_server, server2)
236
218
 
237
219
 
238
220
class TestRepositoryProviderAdapter(TestCase):
239
221
    """A group of tests that test the repository implementation test adapter."""
240
222
 
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
 
223
    def test_adapted_tests(self):
 
224
        # check that constructor parameters are passed through to the adapted
 
225
        # test.
 
226
        from bzrlib.repository import RepositoryTestProviderAdapter
 
227
        input_test = TestRepositoryProviderAdapter(
 
228
            "test_adapted_tests")
245
229
        server1 = "a"
246
230
        server2 = "b"
247
231
        formats = [("c", "C"), ("d", "D")]
248
232
        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)
 
233
        suite = adapter.adapt(input_test)
 
234
        tests = list(iter(suite))
 
235
        self.assertEqual(2, len(tests))
 
236
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
237
        self.assertEqual(tests[0].repository_format, formats[0][0])
 
238
        self.assertEqual(tests[0].transport_server, server1)
 
239
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
240
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
241
        self.assertEqual(tests[1].repository_format, formats[1][0])
 
242
        self.assertEqual(tests[1].transport_server, server1)
 
243
        self.assertEqual(tests[1].transport_readonly_server, server2)
261
244
 
262
245
    def test_setting_vfs_transport(self):
263
246
        """The vfs_transport_factory can be set optionally."""
264
 
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
265
 
        formats = [("a", "b"), ("c", "d")]
 
247
        from bzrlib.repository import RepositoryTestProviderAdapter
 
248
        input_test = TestRepositoryProviderAdapter(
 
249
            "test_adapted_tests")
 
250
        formats = [("c", "C")]
266
251
        adapter = RepositoryTestProviderAdapter(None, None, formats,
267
252
            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())
 
253
        suite = adapter.adapt(input_test)
 
254
        tests = list(iter(suite))
 
255
        self.assertEqual(1, len(tests))
 
256
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
369
257
 
370
258
 
371
259
class TestInterRepositoryProviderAdapter(TestCase):
374
262
    def test_adapted_tests(self):
375
263
        # check that constructor parameters are passed through to the adapted
376
264
        # test.
377
 
        from bzrlib.tests.interrepository_implementations import \
378
 
            InterRepositoryTestProviderAdapter
 
265
        from bzrlib.repository import InterRepositoryTestProviderAdapter
 
266
        input_test = TestInterRepositoryProviderAdapter(
 
267
            "test_adapted_tests")
379
268
        server1 = "a"
380
269
        server2 = "b"
381
270
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
382
271
        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))
 
272
        suite = adapter.adapt(input_test)
 
273
        tests = list(iter(suite))
 
274
        self.assertEqual(2, len(tests))
 
275
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
 
276
        self.assertEqual(tests[0].repository_format, formats[0][1])
 
277
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
 
278
        self.assertEqual(tests[0].transport_server, server1)
 
279
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
280
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
 
281
        self.assertEqual(tests[1].repository_format, formats[1][1])
 
282
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
 
283
        self.assertEqual(tests[1].transport_server, server1)
 
284
        self.assertEqual(tests[1].transport_readonly_server, server2)
397
285
 
398
286
 
399
287
class TestInterVersionedFileProviderAdapter(TestCase):
400
288
    """A group of tests that test the InterVersionedFile test adapter."""
401
289
 
402
 
    def test_scenarios(self):
 
290
    def test_adapted_tests(self):
403
291
        # check that constructor parameters are passed through to the adapted
404
292
        # test.
405
 
        from bzrlib.tests.interversionedfile_implementations \
406
 
            import InterVersionedFileTestProviderAdapter
 
293
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
 
294
        input_test = TestInterRepositoryProviderAdapter(
 
295
            "test_adapted_tests")
407
296
        server1 = "a"
408
297
        server2 = "b"
409
298
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
410
299
        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)
 
300
        suite = adapter.adapt(input_test)
 
301
        tests = list(iter(suite))
 
302
        self.assertEqual(2, len(tests))
 
303
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
 
304
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
 
305
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
 
306
        self.assertEqual(tests[0].transport_server, server1)
 
307
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
308
        self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
 
309
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
 
310
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
 
311
        self.assertEqual(tests[1].transport_server, server1)
 
312
        self.assertEqual(tests[1].transport_readonly_server, server2)
425
313
 
426
314
 
427
315
class TestRevisionStoreProviderAdapter(TestCase):
428
316
    """A group of tests that test the RevisionStore test adapter."""
429
317
 
430
 
    def test_scenarios(self):
 
318
    def test_adapted_tests(self):
431
319
        # check that constructor parameters are passed through to the adapted
432
320
        # test.
433
 
        from bzrlib.tests.revisionstore_implementations \
434
 
            import RevisionStoreTestProviderAdapter
 
321
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
 
322
        input_test = TestRevisionStoreProviderAdapter(
 
323
            "test_adapted_tests")
435
324
        # revision stores need a store factory - i.e. RevisionKnit
436
325
        #, a readonly and rw transport 
437
326
        # transport servers:
439
328
        server2 = "b"
440
329
        store_factories = ["c", "d"]
441
330
        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)
 
331
        suite = adapter.adapt(input_test)
 
332
        tests = list(iter(suite))
 
333
        self.assertEqual(2, len(tests))
 
334
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
 
335
        self.assertEqual(tests[0].transport_server, server1)
 
336
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
337
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
 
338
        self.assertEqual(tests[1].transport_server, server1)
 
339
        self.assertEqual(tests[1].transport_readonly_server, server2)
452
340
 
453
341
 
454
342
class TestWorkingTreeProviderAdapter(TestCase):
455
343
    """A group of tests that test the workingtree implementation test adapter."""
456
344
 
457
 
    def test_scenarios(self):
 
345
    def test_adapted_tests(self):
458
346
        # check that constructor parameters are passed through to the adapted
459
347
        # test.
460
 
        from bzrlib.tests.workingtree_implementations \
461
 
            import WorkingTreeTestProviderAdapter
 
348
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
 
349
        input_test = TestWorkingTreeProviderAdapter(
 
350
            "test_adapted_tests")
462
351
        server1 = "a"
463
352
        server2 = "b"
464
353
        formats = [("c", "C"), ("d", "D")]
465
354
        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)
 
355
        suite = adapter.adapt(input_test)
 
356
        tests = list(iter(suite))
 
357
        self.assertEqual(2, len(tests))
 
358
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
359
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
360
        self.assertEqual(tests[0].transport_server, server1)
 
361
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
362
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
363
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
364
        self.assertEqual(tests[1].transport_server, server1)
 
365
        self.assertEqual(tests[1].transport_readonly_server, server2)
478
366
 
479
367
 
480
368
class TestTreeProviderAdapter(TestCase):
536
424
        # because each optimiser can be direction specific, we need to test
537
425
        # each optimiser in its chosen direction.
538
426
        # unlike the TestProviderAdapter we dont want to automatically add a
539
 
        # parameterized one for WorkingTree - the optimisers will tell us what
 
427
        # parameterised one for WorkingTree - the optimisers will tell us what
540
428
        # ones to add.
541
429
        from bzrlib.tests.tree_implementations import (
542
430
            return_parameter,
579
467
    def test_home_is_not_working(self):
580
468
        self.assertNotEqual(self.test_dir, self.test_home_dir)
581
469
        cwd = osutils.getcwd()
582
 
        self.assertIsSameRealPath(self.test_dir, cwd)
583
 
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
 
470
        self.assertEqual(self.test_dir, cwd)
 
471
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
584
472
 
585
473
 
586
474
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
594
482
        few tests should need to do that), and having a missing dir as home is
595
483
        an effective way to ensure that this is the case.
596
484
        """
597
 
        self.assertIsSameRealPath(
598
 
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
485
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
599
486
            self.test_home_dir)
600
 
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
 
487
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
601
488
        
602
489
    def test_cwd_is_TEST_ROOT(self):
603
 
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
 
490
        self.assertEqual(self.test_dir, self.TEST_ROOT)
604
491
        cwd = osutils.getcwd()
605
 
        self.assertIsSameRealPath(self.test_dir, cwd)
 
492
        self.assertEqual(self.test_dir, cwd)
606
493
 
607
494
    def test_make_branch_and_memory_tree(self):
608
495
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
628
515
        self.assertEqual(format.repository_format.__class__,
629
516
            tree.branch.repository._format.__class__)
630
517
 
631
 
    def test_safety_net(self):
632
 
        """No test should modify the safety .bzr directory.
633
 
 
634
 
        We just test that the _check_safety_net private method raises
635
 
        AssertionError, it's easier than building a test suite with the same
636
 
        test.
637
 
        """
638
 
        # Oops, a commit in the current directory (i.e. without local .bzr
639
 
        # directory) will crawl up the hierarchy to find a .bzr directory.
640
 
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
641
 
        # But we have a safety net in place.
642
 
        self.assertRaises(AssertionError, self._check_safety_net)
643
 
 
644
518
 
645
519
class TestTestCaseWithTransport(TestCaseWithTransport):
646
520
    """Tests for the convenience functions TestCaseWithTransport introduces."""
662
536
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
663
537
 
664
538
    def test_get_readonly_url_http(self):
665
 
        from bzrlib.tests.http_server import HttpServer
 
539
        from bzrlib.tests.HttpServer import HttpServer
666
540
        from bzrlib.transport import get_transport
667
541
        from bzrlib.transport.local import LocalURLServer
668
542
        from bzrlib.transport.http import HttpTransportBase
736
610
 
737
611
class TestTestResult(TestCase):
738
612
 
739
 
    def check_timing(self, test_case, expected_re):
 
613
    def test_elapsed_time_with_benchmarking(self):
740
614
        result = bzrlib.tests.TextTestResult(self._log_file,
741
 
                descriptions=0,
742
 
                verbosity=1,
743
 
                )
744
 
        test_case.run(result)
745
 
        timed_string = result._testTimeString(test_case)
746
 
        self.assertContainsRe(timed_string, expected_re)
747
 
 
748
 
    def test_test_reporting(self):
749
 
        class ShortDelayTestCase(TestCase):
750
 
            def test_short_delay(self):
751
 
                time.sleep(0.003)
752
 
            def test_short_benchmark(self):
753
 
                self.time(time.sleep, 0.003)
754
 
        self.check_timing(ShortDelayTestCase('test_short_delay'),
755
 
                          r"^ +[0-9]+ms$")
 
615
                                        descriptions=0,
 
616
                                        verbosity=1,
 
617
                                        )
 
618
        result._recordTestStartTime()
 
619
        time.sleep(0.003)
 
620
        result.extractBenchmarkTime(self)
 
621
        timed_string = result._testTimeString()
 
622
        # without explicit benchmarking, we should get a simple time.
 
623
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
756
624
        # if a benchmark time is given, we want a x of y style result.
757
 
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
758
 
                          r"^ +[0-9]+ms/ +[0-9]+ms$")
 
625
        self.time(time.sleep, 0.001)
 
626
        result.extractBenchmarkTime(self)
 
627
        timed_string = result._testTimeString()
 
628
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
 
629
        # extracting the time from a non-bzrlib testcase sets to None
 
630
        result._recordTestStartTime()
 
631
        result.extractBenchmarkTime(
 
632
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
 
633
        timed_string = result._testTimeString()
 
634
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
 
635
        # cheat. Yes, wash thy mouth out with soap.
 
636
        self._benchtime = None
759
637
 
760
 
    def test_unittest_reporting_unittest_class(self):
761
 
        # getting the time from a non-bzrlib test works ok
762
 
        class ShortDelayTestCase(unittest.TestCase):
763
 
            def test_short_delay(self):
764
 
                time.sleep(0.003)
765
 
        self.check_timing(ShortDelayTestCase('test_short_delay'),
766
 
                          r"^ +[0-9]+ms$")
767
 
        
768
638
    def test_assigned_benchmark_file_stores_date(self):
769
639
        output = StringIO()
770
640
        result = bzrlib.tests.TextTestResult(self._log_file,
773
643
                                        bench_history=output
774
644
                                        )
775
645
        output_string = output.getvalue()
 
646
        
776
647
        # if you are wondering about the regexp please read the comment in
777
648
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
778
649
        # XXX: what comment?  -- Andrew Bennetts
808
679
 
809
680
    def test_lsprofiling(self):
810
681
        """Verbose test result prints lsprof statistics from test cases."""
811
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
682
        try:
 
683
            import bzrlib.lsprof
 
684
        except ImportError:
 
685
            raise TestSkipped("lsprof not installed.")
812
686
        result_stream = StringIO()
813
687
        result = bzrlib.tests.VerboseTestResult(
814
688
            unittest._WritelnDecorator(result_stream),
877
751
            )
878
752
        test = self.get_passing_test()
879
753
        result.startTest(test)
 
754
        result.extractBenchmarkTime(test)
880
755
        prefix = len(result_stream.getvalue())
881
756
        # the err parameter has the shape:
882
757
        # (class, exception object, traceback)
902
777
        test = self.get_passing_test()
903
778
        # this seeds the state to handle reporting the test.
904
779
        result.startTest(test)
 
780
        result.extractBenchmarkTime(test)
905
781
        # the err parameter has the shape:
906
782
        # (class, exception object, traceback)
907
783
        # KnownFailures dont get their tracebacks shown though, so we
921
797
        test.run(result)
922
798
        self.assertEqual(
923
799
            [
924
 
            ('update', '[2 in 0s] passing_test', None, None),
 
800
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
925
801
            ],
926
802
            pb.calls[2:])
927
803
 
966
842
        test = self.get_passing_test()
967
843
        feature = Feature()
968
844
        result.startTest(test)
 
845
        result.extractBenchmarkTime(test)
969
846
        prefix = len(result_stream.getvalue())
970
847
        result.report_unsupported(test, feature)
971
848
        output = result_stream.getvalue()[prefix:]
985
862
        feature = Feature()
986
863
        # this seeds the state to handle reporting the test.
987
864
        result.startTest(test)
 
865
        result.extractBenchmarkTime(test)
988
866
        result.report_unsupported(test, feature)
989
867
        # no output on unsupported features
990
868
        self.assertEqual(
997
875
        test.run(result)
998
876
        self.assertEqual(
999
877
            [
1000
 
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
 
878
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
1001
879
            ],
1002
880
            pb.calls[1:])
1003
881
    
1021
899
        # and not count as an error
1022
900
        self.assertEqual(0, result.error_count)
1023
901
 
1024
 
    def test_strict_with_unsupported_feature(self):
1025
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1026
 
                                             verbosity=1)
1027
 
        test = self.get_passing_test()
1028
 
        feature = "Unsupported Feature"
1029
 
        result.addNotSupported(test, feature)
1030
 
        self.assertFalse(result.wasStrictlySuccessful())
1031
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1032
 
 
1033
 
    def test_strict_with_known_failure(self):
1034
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1035
 
                                             verbosity=1)
1036
 
        test = self.get_passing_test()
1037
 
        err = (KnownFailure, KnownFailure('foo'), None)
1038
 
        result._addKnownFailure(test, err)
1039
 
        self.assertFalse(result.wasStrictlySuccessful())
1040
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1041
 
 
1042
 
    def test_strict_with_success(self):
1043
 
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1044
 
                                             verbosity=1)
1045
 
        test = self.get_passing_test()
1046
 
        result.addSuccess(test)
1047
 
        self.assertTrue(result.wasStrictlySuccessful())
1048
 
        self.assertEqual(None, result._extractBenchmarkTime(test))
1049
 
 
1050
902
 
1051
903
class TestRunner(TestCase):
1052
904
 
1119
971
        def skipping_test():
1120
972
            raise TestSkipped('test intentionally skipped')
1121
973
 
1122
 
        runner = TextTestRunner(stream=self._log_file)
 
974
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
1123
975
        test = unittest.FunctionTestCase(skipping_test)
1124
976
        result = self.run_test_runner(runner, test)
1125
977
        self.assertTrue(result.wasSuccessful())
1126
978
 
1127
979
    def test_skipped_from_setup(self):
1128
 
        calls = []
1129
980
        class SkippedSetupTest(TestCase):
1130
981
 
1131
982
            def setUp(self):
1132
 
                calls.append('setUp')
 
983
                self.counter = 1
1133
984
                self.addCleanup(self.cleanup)
1134
985
                raise TestSkipped('skipped setup')
1135
986
 
1137
988
                self.fail('test reached')
1138
989
 
1139
990
            def cleanup(self):
1140
 
                calls.append('cleanup')
 
991
                self.counter -= 1
1141
992
 
1142
 
        runner = TextTestRunner(stream=self._log_file)
 
993
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
1143
994
        test = SkippedSetupTest('test_skip')
1144
995
        result = self.run_test_runner(runner, test)
1145
996
        self.assertTrue(result.wasSuccessful())
1146
997
        # Check if cleanup was called the right number of times.
1147
 
        self.assertEqual(['setUp', 'cleanup'], calls)
 
998
        self.assertEqual(0, test.counter)
1148
999
 
1149
1000
    def test_skipped_from_test(self):
1150
 
        calls = []
1151
1001
        class SkippedTest(TestCase):
1152
1002
 
1153
1003
            def setUp(self):
1154
 
                calls.append('setUp')
 
1004
                self.counter = 1
1155
1005
                self.addCleanup(self.cleanup)
1156
1006
 
1157
1007
            def test_skip(self):
1158
1008
                raise TestSkipped('skipped test')
1159
1009
 
1160
1010
            def cleanup(self):
1161
 
                calls.append('cleanup')
 
1011
                self.counter -= 1
1162
1012
 
1163
 
        runner = TextTestRunner(stream=self._log_file)
 
1013
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
1164
1014
        test = SkippedTest('test_skip')
1165
1015
        result = self.run_test_runner(runner, test)
1166
1016
        self.assertTrue(result.wasSuccessful())
1167
1017
        # Check if cleanup was called the right number of times.
1168
 
        self.assertEqual(['setUp', 'cleanup'], calls)
1169
 
 
1170
 
    def test_not_applicable(self):
1171
 
        # run a test that is skipped because it's not applicable
1172
 
        def not_applicable_test():
1173
 
            from bzrlib.tests import TestNotApplicable
1174
 
            raise TestNotApplicable('this test never runs')
1175
 
        out = StringIO()
1176
 
        runner = TextTestRunner(stream=out, verbosity=2)
1177
 
        test = unittest.FunctionTestCase(not_applicable_test)
1178
 
        result = self.run_test_runner(runner, test)
1179
 
        self._log_file.write(out.getvalue())
1180
 
        self.assertTrue(result.wasSuccessful())
1181
 
        self.assertTrue(result.wasStrictlySuccessful())
1182
 
        self.assertContainsRe(out.getvalue(),
1183
 
                r'(?m)not_applicable_test   * N/A')
1184
 
        self.assertContainsRe(out.getvalue(),
1185
 
                r'(?m)^    this test never runs')
1186
 
 
1187
 
    def test_not_applicable_demo(self):
1188
 
        # just so you can see it in the test output
1189
 
        raise TestNotApplicable('this test is just a demonstation')
 
1018
        self.assertEqual(0, test.counter)
1190
1019
 
1191
1020
    def test_unsupported_features_listed(self):
1192
1021
        """When unsupported features are encountered they are detailed."""
1228
1057
            revision_id = workingtree.get_parent_ids()[0]
1229
1058
            self.assertEndsWith(output_string.rstrip(), revision_id)
1230
1059
 
1231
 
    def assertLogDeleted(self, test):
1232
 
        log = test._get_log()
1233
 
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1234
 
        self.assertEqual('', test._log_contents)
1235
 
        self.assertIs(None, test._log_file_name)
1236
 
 
1237
1060
    def test_success_log_deleted(self):
1238
1061
        """Successful tests have their log deleted"""
1239
1062
 
1247
1070
        test = LogTester('test_success')
1248
1071
        result = self.run_test_runner(runner, test)
1249
1072
 
1250
 
        self.assertLogDeleted(test)
1251
 
 
1252
 
    def test_skipped_log_deleted(self):
1253
 
        """Skipped tests have their log deleted"""
1254
 
 
1255
 
        class LogTester(TestCase):
1256
 
 
1257
 
            def test_skipped(self):
1258
 
                self.log('this will be removed\n')
1259
 
                raise tests.TestSkipped()
1260
 
 
1261
 
        sio = cStringIO.StringIO()
1262
 
        runner = TextTestRunner(stream=sio)
1263
 
        test = LogTester('test_skipped')
1264
 
        result = self.run_test_runner(runner, test)
1265
 
 
1266
 
        self.assertLogDeleted(test)
1267
 
 
1268
 
    def test_not_aplicable_log_deleted(self):
1269
 
        """Not applicable tests have their log deleted"""
1270
 
 
1271
 
        class LogTester(TestCase):
1272
 
 
1273
 
            def test_not_applicable(self):
1274
 
                self.log('this will be removed\n')
1275
 
                raise tests.TestNotApplicable()
1276
 
 
1277
 
        sio = cStringIO.StringIO()
1278
 
        runner = TextTestRunner(stream=sio)
1279
 
        test = LogTester('test_not_applicable')
1280
 
        result = self.run_test_runner(runner, test)
1281
 
 
1282
 
        self.assertLogDeleted(test)
1283
 
 
1284
 
    def test_known_failure_log_deleted(self):
1285
 
        """Know failure tests have their log deleted"""
1286
 
 
1287
 
        class LogTester(TestCase):
1288
 
 
1289
 
            def test_known_failure(self):
1290
 
                self.log('this will be removed\n')
1291
 
                raise tests.KnownFailure()
1292
 
 
1293
 
        sio = cStringIO.StringIO()
1294
 
        runner = TextTestRunner(stream=sio)
1295
 
        test = LogTester('test_known_failure')
1296
 
        result = self.run_test_runner(runner, test)
1297
 
 
1298
 
        self.assertLogDeleted(test)
 
1073
        log = test._get_log()
 
1074
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
1075
        self.assertEqual('', test._log_contents)
 
1076
        self.assertIs(None, test._log_file_name)
1299
1077
 
1300
1078
    def test_fail_log_kept(self):
1301
1079
        """Failed tests have their log kept"""
1351
1129
class TestTestCase(TestCase):
1352
1130
    """Tests that test the core bzrlib TestCase."""
1353
1131
 
1354
 
    def test_debug_flags_sanitised(self):
1355
 
        """The bzrlib debug flags should be sanitised by setUp."""
1356
 
        # we could set something and run a test that will check
1357
 
        # it gets santised, but this is probably sufficient for now:
1358
 
        # if someone runs the test with -Dsomething it will error.
1359
 
        self.assertEqual(set(), bzrlib.debug.debug_flags)
1360
 
 
1361
1132
    def inner_test(self):
1362
1133
        # the inner child test
1363
1134
        note("inner_test")
1423
1194
        
1424
1195
        Each self.time() call is individually and separately profiled.
1425
1196
        """
1426
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
1197
        try:
 
1198
            import bzrlib.lsprof
 
1199
        except ImportError:
 
1200
            raise TestSkipped("lsprof not installed.")
1427
1201
        # overrides the class member with an instance member so no cleanup 
1428
1202
        # needed.
1429
1203
        self._gather_lsprof_in_benchmarks = True
1593
1367
        self.callDeprecated([], testfunc, be_deprecated=False)
1594
1368
 
1595
1369
 
1596
 
class TestWarningTests(TestCase):
1597
 
    """Tests for calling methods that raise warnings."""
1598
 
 
1599
 
    def test_callCatchWarnings(self):
1600
 
        def meth(a, b):
1601
 
            warnings.warn("this is your last warning")
1602
 
            return a + b
1603
 
        wlist, result = self.callCatchWarnings(meth, 1, 2)
1604
 
        self.assertEquals(3, result)
1605
 
        # would like just to compare them, but UserWarning doesn't implement
1606
 
        # eq well
1607
 
        w0, = wlist
1608
 
        self.assertIsInstance(w0, UserWarning)
1609
 
        self.assertEquals("this is your last warning", str(w0))
1610
 
 
1611
 
 
1612
1370
class TestConvenienceMakers(TestCaseWithTransport):
1613
1371
    """Test for the make_* convenience functions."""
1614
1372
 
1658
1416
        self.assertEqual([True], factory_called)
1659
1417
 
1660
1418
 
 
1419
class TestSelftestCleanOutput(TestCaseInTempDir):
 
1420
 
 
1421
    def test_clean_output(self):
 
1422
        # test functionality of clean_selftest_output()
 
1423
        self.build_tree(['test0000.tmp/', 'test0001.tmp/',
 
1424
                         'bzrlib/', 'tests/',
 
1425
                         'bzr', 'setup.py', 'test9999.tmp'])
 
1426
 
 
1427
        root = os.getcwdu()
 
1428
        before = os.listdir(root)
 
1429
        before.sort()
 
1430
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1431
                           'test0000.tmp','test0001.tmp',
 
1432
                           'test9999.tmp','tests'],
 
1433
                           before)
 
1434
        clean_selftest_output(root, quiet=True)
 
1435
        after = os.listdir(root)
 
1436
        after.sort()
 
1437
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1438
                           'test9999.tmp','tests'],
 
1439
                           after)
 
1440
 
 
1441
    def test_clean_readonly(self):
 
1442
        # test for delete read-only files
 
1443
        self.build_tree(['test0000.tmp/', 'test0000.tmp/foo'])
 
1444
        osutils.make_readonly('test0000.tmp/foo')
 
1445
        root = os.getcwdu()
 
1446
        before = os.listdir(root);  before.sort()
 
1447
        self.assertEquals(['test0000.tmp'], before)
 
1448
        clean_selftest_output(root, quiet=True)
 
1449
        after = os.listdir(root);   after.sort()
 
1450
        self.assertEquals([], after)
 
1451
 
 
1452
 
1661
1453
class TestKnownFailure(TestCase):
1662
1454
 
1663
1455
    def test_known_failure(self):
1728
1520
        self.loader = TestUtil.TestLoader()
1729
1521
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1730
1522
            'bzrlib.tests.test_selftest']))
1731
 
        self.all_names = _test_ids(self.suite)
1732
 
 
1733
 
    def test_condition_id_re(self):
1734
 
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1735
 
            'test_condition_id_re')
1736
 
        filtered_suite = filter_suite_by_condition(self.suite,
1737
 
            condition_id_re('test_condition_id_re'))
1738
 
        self.assertEqual([test_name], _test_ids(filtered_suite))
1739
 
 
1740
 
    def test_condition_id_in_list(self):
1741
 
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1742
 
                      'test_condition_id_in_list']
1743
 
        id_list = tests.TestIdList(test_names)
1744
 
        filtered_suite = filter_suite_by_condition(
1745
 
            self.suite, tests.condition_id_in_list(id_list))
1746
 
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1747
 
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
1748
 
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1749
 
 
1750
 
    def test_condition_isinstance(self):
1751
 
        filtered_suite = filter_suite_by_condition(self.suite,
1752
 
            condition_isinstance(self.__class__))
1753
 
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1754
 
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
1755
 
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1756
 
 
1757
 
    def test_exclude_tests_by_condition(self):
1758
 
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1759
 
            'test_exclude_tests_by_condition')
1760
 
        filtered_suite = exclude_tests_by_condition(self.suite,
1761
 
            lambda x:x.id() == excluded_name)
1762
 
        self.assertEqual(len(self.all_names) - 1,
1763
 
            filtered_suite.countTestCases())
1764
 
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
1765
 
        remaining_names = list(self.all_names)
1766
 
        remaining_names.remove(excluded_name)
1767
 
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
1768
 
 
1769
 
    def test_exclude_tests_by_re(self):
1770
 
        self.all_names = _test_ids(self.suite)
1771
 
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1772
 
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1773
 
            'test_exclude_tests_by_re')
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_filter_suite_by_condition(self):
1782
 
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1783
 
            'test_filter_suite_by_condition')
1784
 
        filtered_suite = filter_suite_by_condition(self.suite,
1785
 
            lambda x:x.id() == test_name)
1786
 
        self.assertEqual([test_name], _test_ids(filtered_suite))
 
1523
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
1787
1524
 
1788
1525
    def test_filter_suite_by_re(self):
1789
 
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
1790
 
        filtered_names = _test_ids(filtered_suite)
 
1526
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
 
1527
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
1791
1528
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1792
1529
            'TestSelftestFiltering.test_filter_suite_by_re'])
1793
 
 
1794
 
    def test_filter_suite_by_id_list(self):
1795
 
        test_list = ['bzrlib.tests.test_selftest.'
1796
 
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
1797
 
        filtered_suite = tests.filter_suite_by_id_list(
1798
 
            self.suite, tests.TestIdList(test_list))
1799
 
        filtered_names = _test_ids(filtered_suite)
1800
 
        self.assertEqual(
1801
 
            filtered_names,
1802
 
            ['bzrlib.tests.test_selftest.'
1803
 
             'TestSelftestFiltering.test_filter_suite_by_id_list'])
1804
 
 
1805
 
    def test_preserve_input(self):
1806
 
        # NB: Surely this is something in the stdlib to do this?
1807
 
        self.assertTrue(self.suite is preserve_input(self.suite))
1808
 
        self.assertTrue("@#$" is preserve_input("@#$"))
1809
 
 
1810
 
    def test_randomize_suite(self):
1811
 
        randomized_suite = randomize_suite(self.suite)
1812
 
        # randomizing should not add or remove test names.
1813
 
        self.assertEqual(set(_test_ids(self.suite)),
1814
 
                         set(_test_ids(randomized_suite)))
1815
 
        # Technically, this *can* fail, because random.shuffle(list) can be
1816
 
        # equal to list. Trying multiple times just pushes the frequency back.
1817
 
        # As its len(self.all_names)!:1, the failure frequency should be low
1818
 
        # enough to ignore. RBC 20071021.
1819
 
        # It should change the order.
1820
 
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
1821
 
        # But not the length. (Possibly redundant with the set test, but not
1822
 
        # necessarily.)
1823
 
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
1824
 
 
 
1530
            
1825
1531
    def test_sort_suite_by_re(self):
1826
 
        sorted_suite = self.applyDeprecated(one_zero,
1827
 
            sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
1828
 
        sorted_names = _test_ids(sorted_suite)
 
1532
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
 
1533
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
1829
1534
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1830
1535
            'TestSelftestFiltering.test_filter_suite_by_re')
1831
1536
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1832
1537
 
1833
 
    def test_split_suit_by_re(self):
1834
 
        self.all_names = _test_ids(self.suite)
1835
 
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
1836
 
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1837
 
            'test_filter_suite_by_re')
1838
 
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1839
 
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1840
 
        remaining_names = list(self.all_names)
1841
 
        remaining_names.remove(filtered_name)
1842
 
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1843
 
 
1844
 
 
1845
 
class TestCheckInventoryShape(TestCaseWithTransport):
1846
 
 
1847
 
    def test_check_inventory_shape(self):
1848
 
        files = ['a', 'b/', 'b/c']
1849
 
        tree = self.make_branch_and_tree('.')
1850
 
        self.build_tree(files)
1851
 
        tree.add(files)
1852
 
        tree.lock_read()
1853
 
        try:
1854
 
            self.check_inventory_shape(tree.inventory, files)
1855
 
        finally:
1856
 
            tree.unlock()
1857
 
 
1858
 
 
1859
 
class TestBlackboxSupport(TestCase):
1860
 
    """Tests for testsuite blackbox features."""
1861
 
 
1862
 
    def test_run_bzr_failure_not_caught(self):
1863
 
        # When we run bzr in blackbox mode, we want any unexpected errors to
1864
 
        # propagate up to the test suite so that it can show the error in the
1865
 
        # usual way, and we won't get a double traceback.
1866
 
        e = self.assertRaises(
1867
 
            AssertionError,
1868
 
            self.run_bzr, ['assert-fail'])
1869
 
        # make sure we got the real thing, not an error from somewhere else in
1870
 
        # the test framework
1871
 
        self.assertEquals('always fails', str(e))
1872
 
        # check that there's no traceback in the test log
1873
 
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
1874
 
            r'Traceback')
1875
 
 
1876
 
    def test_run_bzr_user_error_caught(self):
1877
 
        # Running bzr in blackbox mode, normal/expected/user errors should be
1878
 
        # caught in the regular way and turned into an error message plus exit
1879
 
        # code.
1880
 
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
1881
 
        self.assertEqual(out, '')
1882
 
        self.assertContainsRe(err,
1883
 
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
1884
 
 
1885
 
 
1886
 
class TestTestLoader(TestCase):
1887
 
    """Tests for the test loader."""
1888
 
 
1889
 
    def _get_loader_and_module(self):
1890
 
        """Gets a TestLoader and a module with one test in it."""
1891
 
        loader = TestUtil.TestLoader()
1892
 
        module = {}
1893
 
        class Stub(TestCase):
1894
 
            def test_foo(self):
1895
 
                pass
1896
 
        class MyModule(object):
1897
 
            pass
1898
 
        MyModule.a_class = Stub
1899
 
        module = MyModule()
1900
 
        return loader, module
1901
 
 
1902
 
    def test_module_no_load_tests_attribute_loads_classes(self):
1903
 
        loader, module = self._get_loader_and_module()
1904
 
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
1905
 
 
1906
 
    def test_module_load_tests_attribute_gets_called(self):
1907
 
        loader, module = self._get_loader_and_module()
1908
 
        # 'self' is here because we're faking the module with a class. Regular
1909
 
        # load_tests do not need that :)
1910
 
        def load_tests(self, standard_tests, module, loader):
1911
 
            result = loader.suiteClass()
1912
 
            for test in iter_suite_tests(standard_tests):
1913
 
                result.addTests([test, test])
1914
 
            return result
1915
 
        # add a load_tests() method which multiplies the tests from the module.
1916
 
        module.__class__.load_tests = load_tests
1917
 
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
1918
 
 
1919
 
    def test_load_tests_from_module_name_smoke_test(self):
1920
 
        loader = TestUtil.TestLoader()
1921
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
1922
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
1923
 
                          _test_ids(suite))
1924
 
 
1925
 
    def test_load_tests_from_module_name_with_bogus_module_name(self):
1926
 
        loader = TestUtil.TestLoader()
1927
 
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
1928
 
 
1929
 
 
1930
 
class TestTestIdList(tests.TestCase):
1931
 
 
1932
 
    def _create_id_list(self, test_list):
1933
 
        return tests.TestIdList(test_list)
1934
 
 
1935
 
    def _create_suite(self, test_id_list):
1936
 
 
1937
 
        class Stub(TestCase):
1938
 
            def test_foo(self):
1939
 
                pass
1940
 
 
1941
 
        def _create_test_id(id):
1942
 
            return lambda: id
1943
 
 
1944
 
        suite = TestUtil.TestSuite()
1945
 
        for id in test_id_list:
1946
 
            t  = Stub('test_foo')
1947
 
            t.id = _create_test_id(id)
1948
 
            suite.addTest(t)
1949
 
        return suite
1950
 
 
1951
 
    def _test_ids(self, test_suite):
1952
 
        """Get the ids for the tests in a test suite."""
1953
 
        return [t.id() for t in iter_suite_tests(test_suite)]
1954
 
 
1955
 
    def test_empty_list(self):
1956
 
        id_list = self._create_id_list([])
1957
 
        self.assertEquals({}, id_list.tests)
1958
 
        self.assertEquals({}, id_list.modules)
1959
 
 
1960
 
    def test_valid_list(self):
1961
 
        id_list = self._create_id_list(
1962
 
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
1963
 
             'mod1.func1', 'mod1.cl2.meth2',
1964
 
             'mod1.submod1',
1965
 
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
1966
 
             ])
1967
 
        self.assertTrue(id_list.refers_to('mod1'))
1968
 
        self.assertTrue(id_list.refers_to('mod1.submod1'))
1969
 
        self.assertTrue(id_list.refers_to('mod1.submod2'))
1970
 
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
1971
 
        self.assertTrue(id_list.includes('mod1.submod1'))
1972
 
        self.assertTrue(id_list.includes('mod1.func1'))
1973
 
 
1974
 
    def test_bad_chars_in_params(self):
1975
 
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
1976
 
        self.assertTrue(id_list.refers_to('mod1'))
1977
 
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
1978
 
 
1979
 
    def test_module_used(self):
1980
 
        id_list = self._create_id_list(['mod.class.meth'])
1981
 
        self.assertTrue(id_list.refers_to('mod'))
1982
 
        self.assertTrue(id_list.refers_to('mod.class'))
1983
 
        self.assertTrue(id_list.refers_to('mod.class.meth'))
1984
 
 
1985
 
    def test_test_suite(self):
1986
 
        # This test is slow, so we do a single test with one test in each
1987
 
        # category
1988
 
        test_list = [
1989
 
            # testmod_names
1990
 
            'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
1991
 
            # transport implementations
1992
 
            'bzrlib.tests.test_transport_implementations.TransportTests'
1993
 
            '.test_abspath(LocalURLServer)',
1994
 
            # packages_to_test()
1995
 
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
1996
 
            # MODULES_TO_DOCTEST
1997
 
            'bzrlib.timestamp.format_highres_date',
1998
 
            # plugins can't be tested that way since selftest may be run with
1999
 
            # --no-plugins
2000
 
            ]
2001
 
        suite = tests.test_suite(test_list)
2002
 
        self.assertEquals(test_list, _test_ids(suite))
2003
 
 
2004
 
    def test_test_suite_matches_id_list_with_unknown(self):
2005
 
        loader = TestUtil.TestLoader()
2006
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2007
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2008
 
                     'bogus']
2009
 
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2010
 
        self.assertEquals(['bogus'], not_found)
2011
 
        self.assertEquals([], duplicates)
2012
 
 
2013
 
    def test_suite_matches_id_list_with_duplicates(self):
2014
 
        loader = TestUtil.TestLoader()
2015
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2016
 
        dupes = loader.suiteClass()
2017
 
        for test in iter_suite_tests(suite):
2018
 
            dupes.addTest(test)
2019
 
            dupes.addTest(test) # Add it again
2020
 
 
2021
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2022
 
        not_found, duplicates = tests.suite_matches_id_list(
2023
 
            dupes, test_list)
2024
 
        self.assertEquals([], not_found)
2025
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2026
 
                          duplicates)
2027
 
 
2028
 
 
2029
 
class TestLoadTestIdList(tests.TestCaseInTempDir):
2030
 
 
2031
 
    def _create_test_list_file(self, file_name, content):
2032
 
        fl = open(file_name, 'wt')
2033
 
        fl.write(content)
2034
 
        fl.close()
2035
 
 
2036
 
    def test_load_unknown(self):
2037
 
        self.assertRaises(errors.NoSuchFile,
2038
 
                          tests.load_test_id_list, 'i_do_not_exist')
2039
 
 
2040
 
    def test_load_test_list(self):
2041
 
        test_list_fname = 'test.list'
2042
 
        self._create_test_list_file(test_list_fname,
2043
 
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
2044
 
        tlist = tests.load_test_id_list(test_list_fname)
2045
 
        self.assertEquals(2, len(tlist))
2046
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2047
 
        self.assertEquals('mod2.cl2.meth2', tlist[1])
2048
 
 
2049
 
    def test_load_dirty_file(self):
2050
 
        test_list_fname = 'test.list'
2051
 
        self._create_test_list_file(test_list_fname,
2052
 
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
2053
 
                                    'bar baz\n')
2054
 
        tlist = tests.load_test_id_list(test_list_fname)
2055
 
        self.assertEquals(4, len(tlist))
2056
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
2057
 
        self.assertEquals('', tlist[1])
2058
 
        self.assertEquals('mod2.cl2.meth2', tlist[2])
2059
 
        self.assertEquals('bar baz', tlist[3])
2060
 
 
2061
 
 
2062
 
class TestFilteredByModuleTestLoader(tests.TestCase):
2063
 
 
2064
 
    def _create_loader(self, test_list):
2065
 
        id_filter = tests.TestIdList(test_list)
2066
 
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
2067
 
        return loader
2068
 
 
2069
 
    def test_load_tests(self):
2070
 
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2071
 
        loader = self._create_loader(test_list)
2072
 
 
2073
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2074
 
        self.assertEquals(test_list, _test_ids(suite))
2075
 
 
2076
 
    def test_exclude_tests(self):
2077
 
        test_list = ['bogus']
2078
 
        loader = self._create_loader(test_list)
2079
 
 
2080
 
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2081
 
        self.assertEquals([], _test_ids(suite))