~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-07 07:52:50 UTC
  • mfrom: (3340.1.1 208418-1.4)
  • Revision ID: pqm@pqm.ubuntu.com-20080407075250-phs53xnslo8boaeo
Return the correct knit serialisation method in _StreamAccess.
        (Andrew Bennetts, Martin Pool, Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    osutils,
33
33
    repository,
34
34
    symbol_versioning,
 
35
    tests,
35
36
    )
36
37
from bzrlib.progress import _BaseProgressBar
37
38
from bzrlib.repofmt import weaverepo
38
 
from bzrlib.symbol_versioning import zero_ten, zero_eleven
 
39
from bzrlib.symbol_versioning import (
 
40
    one_zero,
 
41
    zero_eleven,
 
42
    zero_ten,
 
43
    )
39
44
from bzrlib.tests import (
40
45
                          ChrootedTestCase,
41
46
                          ExtendedTestResult,
45
50
                          TestCaseInTempDir,
46
51
                          TestCaseWithMemoryTransport,
47
52
                          TestCaseWithTransport,
 
53
                          TestNotApplicable,
48
54
                          TestSkipped,
49
55
                          TestSuite,
50
56
                          TestUtil,
51
57
                          TextTestRunner,
52
58
                          UnavailableFeature,
53
 
                          clean_selftest_output,
 
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,
54
65
                          iter_suite_tests,
55
 
                          filter_suite_by_re,
 
66
                          preserve_input,
 
67
                          randomize_suite,
56
68
                          sort_suite_by_re,
57
 
                          test_suite
 
69
                          split_suite_by_re,
 
70
                          test_lsprof,
 
71
                          test_suite,
58
72
                          )
59
73
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
60
74
from bzrlib.tests.TestUtil import _load_module_by_name
63
77
from bzrlib.version import _get_bzr_source_tree
64
78
 
65
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
 
66
85
class SelftestTests(TestCase):
67
86
 
68
87
    def test_import_tests(self):
113
132
            def get_test_permutations(self):
114
133
                return sample_permutation
115
134
        sample_permutation = [(1,2), (3,4)]
116
 
        from bzrlib.transport import TransportTestProviderAdapter
 
135
        from bzrlib.tests.test_transport_implementations \
 
136
            import TransportTestProviderAdapter
117
137
        adapter = TransportTestProviderAdapter()
118
138
        self.assertEqual(sample_permutation,
119
139
                         adapter.get_transport_test_permutations(MockModule()))
124
144
        # - we assume if this matches its probably doing the right thing
125
145
        # especially in combination with the tests for setting the right
126
146
        # classes below.
127
 
        from bzrlib.transport import (TransportTestProviderAdapter,
128
 
                                      _get_transport_modules
129
 
                                      )
 
147
        from bzrlib.tests.test_transport_implementations \
 
148
            import TransportTestProviderAdapter
 
149
        from bzrlib.transport import _get_transport_modules
130
150
        modules = _get_transport_modules()
131
151
        permutation_count = 0
132
152
        for module in modules:
149
169
        # This test used to know about all the possible transports and the
150
170
        # order they were returned but that seems overly brittle (mbp
151
171
        # 20060307)
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)
 
172
        from bzrlib.tests.test_transport_implementations \
 
173
            import TransportTestProviderAdapter
 
174
        scenarios = TransportTestProviderAdapter().scenarios
158
175
        # there are at least that many builtin transports
159
 
        one_test = tests[0]
160
 
        self.assertTrue(issubclass(one_test.transport_class, 
 
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"],
161
180
                                   bzrlib.transport.Transport))
162
 
        self.assertTrue(issubclass(one_test.transport_server, 
 
181
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
163
182
                                   bzrlib.transport.Server))
164
183
 
165
184
 
166
185
class TestBranchProviderAdapter(TestCase):
167
186
    """A group of tests that test the branch implementation test adapter."""
168
187
 
169
 
    def test_adapted_tests(self):
 
188
    def test_constructor(self):
170
189
        # check that constructor parameters are passed through to the adapted
171
190
        # test.
172
 
        from bzrlib.branch import BranchTestProviderAdapter
173
 
        input_test = TestBranchProviderAdapter(
174
 
            "test_adapted_tests")
 
191
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
175
192
        server1 = "a"
176
193
        server2 = "b"
177
194
        formats = [("c", "C"), ("d", "D")]
178
195
        adapter = BranchTestProviderAdapter(server1, server2, formats)
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)
 
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)
190
209
 
191
210
 
192
211
class TestBzrDirProviderAdapter(TestCase):
195
214
    def test_adapted_tests(self):
196
215
        # check that constructor parameters are passed through to the adapted
197
216
        # test.
198
 
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
199
 
        input_test = TestBzrDirProviderAdapter(
200
 
            "test_adapted_tests")
 
217
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
201
218
        vfs_factory = "v"
202
219
        server1 = "a"
203
220
        server2 = "b"
204
221
        formats = ["c", "d"]
205
222
        adapter = BzrDirTestProviderAdapter(vfs_factory,
206
223
            server1, server2, formats)
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)
 
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)
218
236
 
219
237
 
220
238
class TestRepositoryProviderAdapter(TestCase):
221
239
    """A group of tests that test the repository implementation test adapter."""
222
240
 
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")
 
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
229
245
        server1 = "a"
230
246
        server2 = "b"
231
247
        formats = [("c", "C"), ("d", "D")]
232
248
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
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)
 
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)
244
261
 
245
262
    def test_setting_vfs_transport(self):
246
263
        """The vfs_transport_factory can be set optionally."""
247
 
        from bzrlib.repository import RepositoryTestProviderAdapter
248
 
        input_test = TestRepositoryProviderAdapter(
249
 
            "test_adapted_tests")
250
 
        formats = [("c", "C")]
 
264
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
265
        formats = [("a", "b"), ("c", "d")]
251
266
        adapter = RepositoryTestProviderAdapter(None, None, formats,
252
267
            vfs_transport_factory="vfs")
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")
 
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())
257
369
 
258
370
 
259
371
class TestInterRepositoryProviderAdapter(TestCase):
262
374
    def test_adapted_tests(self):
263
375
        # check that constructor parameters are passed through to the adapted
264
376
        # test.
265
 
        from bzrlib.repository import InterRepositoryTestProviderAdapter
266
 
        input_test = TestInterRepositoryProviderAdapter(
267
 
            "test_adapted_tests")
 
377
        from bzrlib.tests.interrepository_implementations import \
 
378
            InterRepositoryTestProviderAdapter
268
379
        server1 = "a"
269
380
        server2 = "b"
270
381
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
271
382
        adapter = InterRepositoryTestProviderAdapter(server1, server2, 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)
 
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))
285
397
 
286
398
 
287
399
class TestInterVersionedFileProviderAdapter(TestCase):
288
400
    """A group of tests that test the InterVersionedFile test adapter."""
289
401
 
290
 
    def test_adapted_tests(self):
 
402
    def test_scenarios(self):
291
403
        # check that constructor parameters are passed through to the adapted
292
404
        # test.
293
 
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
294
 
        input_test = TestInterRepositoryProviderAdapter(
295
 
            "test_adapted_tests")
 
405
        from bzrlib.tests.interversionedfile_implementations \
 
406
            import InterVersionedFileTestProviderAdapter
296
407
        server1 = "a"
297
408
        server2 = "b"
298
409
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
299
410
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
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)
 
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)
313
425
 
314
426
 
315
427
class TestRevisionStoreProviderAdapter(TestCase):
316
428
    """A group of tests that test the RevisionStore test adapter."""
317
429
 
318
 
    def test_adapted_tests(self):
 
430
    def test_scenarios(self):
319
431
        # check that constructor parameters are passed through to the adapted
320
432
        # test.
321
 
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
322
 
        input_test = TestRevisionStoreProviderAdapter(
323
 
            "test_adapted_tests")
 
433
        from bzrlib.tests.revisionstore_implementations \
 
434
            import RevisionStoreTestProviderAdapter
324
435
        # revision stores need a store factory - i.e. RevisionKnit
325
436
        #, a readonly and rw transport 
326
437
        # transport servers:
328
439
        server2 = "b"
329
440
        store_factories = ["c", "d"]
330
441
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
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)
 
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)
340
452
 
341
453
 
342
454
class TestWorkingTreeProviderAdapter(TestCase):
343
455
    """A group of tests that test the workingtree implementation test adapter."""
344
456
 
345
 
    def test_adapted_tests(self):
 
457
    def test_scenarios(self):
346
458
        # check that constructor parameters are passed through to the adapted
347
459
        # test.
348
 
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
349
 
        input_test = TestWorkingTreeProviderAdapter(
350
 
            "test_adapted_tests")
 
460
        from bzrlib.tests.workingtree_implementations \
 
461
            import WorkingTreeTestProviderAdapter
351
462
        server1 = "a"
352
463
        server2 = "b"
353
464
        formats = [("c", "C"), ("d", "D")]
354
465
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
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)
 
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)
366
478
 
367
479
 
368
480
class TestTreeProviderAdapter(TestCase):
424
536
        # because each optimiser can be direction specific, we need to test
425
537
        # each optimiser in its chosen direction.
426
538
        # unlike the TestProviderAdapter we dont want to automatically add a
427
 
        # parameterised one for WorkingTree - the optimisers will tell us what
 
539
        # parameterized one for WorkingTree - the optimisers will tell us what
428
540
        # ones to add.
429
541
        from bzrlib.tests.tree_implementations import (
430
542
            return_parameter,
467
579
    def test_home_is_not_working(self):
468
580
        self.assertNotEqual(self.test_dir, self.test_home_dir)
469
581
        cwd = osutils.getcwd()
470
 
        self.assertEqual(self.test_dir, cwd)
471
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
582
        self.assertIsSameRealPath(self.test_dir, cwd)
 
583
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
472
584
 
473
585
 
474
586
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
482
594
        few tests should need to do that), and having a missing dir as home is
483
595
        an effective way to ensure that this is the case.
484
596
        """
485
 
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
597
        self.assertIsSameRealPath(
 
598
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
486
599
            self.test_home_dir)
487
 
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
600
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
488
601
        
489
602
    def test_cwd_is_TEST_ROOT(self):
490
 
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
603
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
491
604
        cwd = osutils.getcwd()
492
 
        self.assertEqual(self.test_dir, cwd)
 
605
        self.assertIsSameRealPath(self.test_dir, cwd)
493
606
 
494
607
    def test_make_branch_and_memory_tree(self):
495
608
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
515
628
        self.assertEqual(format.repository_format.__class__,
516
629
            tree.branch.repository._format.__class__)
517
630
 
 
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
 
518
644
 
519
645
class TestTestCaseWithTransport(TestCaseWithTransport):
520
646
    """Tests for the convenience functions TestCaseWithTransport introduces."""
536
662
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
537
663
 
538
664
    def test_get_readonly_url_http(self):
539
 
        from bzrlib.tests.HttpServer import HttpServer
 
665
        from bzrlib.tests.http_server import HttpServer
540
666
        from bzrlib.transport import get_transport
541
667
        from bzrlib.transport.local import LocalURLServer
542
668
        from bzrlib.transport.http import HttpTransportBase
610
736
 
611
737
class TestTestResult(TestCase):
612
738
 
613
 
    def test_elapsed_time_with_benchmarking(self):
 
739
    def check_timing(self, test_case, expected_re):
614
740
        result = bzrlib.tests.TextTestResult(self._log_file,
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$")
 
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$")
624
756
        # if a benchmark time is given, we want a x of y style result.
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
 
757
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
 
758
                          r"^ +[0-9]+ms/ +[0-9]+ms$")
637
759
 
 
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
        
638
768
    def test_assigned_benchmark_file_stores_date(self):
639
769
        output = StringIO()
640
770
        result = bzrlib.tests.TextTestResult(self._log_file,
643
773
                                        bench_history=output
644
774
                                        )
645
775
        output_string = output.getvalue()
646
 
        
647
776
        # if you are wondering about the regexp please read the comment in
648
777
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
649
778
        # XXX: what comment?  -- Andrew Bennetts
679
808
 
680
809
    def test_lsprofiling(self):
681
810
        """Verbose test result prints lsprof statistics from test cases."""
682
 
        try:
683
 
            import bzrlib.lsprof
684
 
        except ImportError:
685
 
            raise TestSkipped("lsprof not installed.")
 
811
        self.requireFeature(test_lsprof.LSProfFeature)
686
812
        result_stream = StringIO()
687
813
        result = bzrlib.tests.VerboseTestResult(
688
814
            unittest._WritelnDecorator(result_stream),
751
877
            )
752
878
        test = self.get_passing_test()
753
879
        result.startTest(test)
754
 
        result.extractBenchmarkTime(test)
755
880
        prefix = len(result_stream.getvalue())
756
881
        # the err parameter has the shape:
757
882
        # (class, exception object, traceback)
777
902
        test = self.get_passing_test()
778
903
        # this seeds the state to handle reporting the test.
779
904
        result.startTest(test)
780
 
        result.extractBenchmarkTime(test)
781
905
        # the err parameter has the shape:
782
906
        # (class, exception object, traceback)
783
907
        # KnownFailures dont get their tracebacks shown though, so we
797
921
        test.run(result)
798
922
        self.assertEqual(
799
923
            [
800
 
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
 
924
            ('update', '[2 in 0s] passing_test', None, None),
801
925
            ],
802
926
            pb.calls[2:])
803
927
 
842
966
        test = self.get_passing_test()
843
967
        feature = Feature()
844
968
        result.startTest(test)
845
 
        result.extractBenchmarkTime(test)
846
969
        prefix = len(result_stream.getvalue())
847
970
        result.report_unsupported(test, feature)
848
971
        output = result_stream.getvalue()[prefix:]
862
985
        feature = Feature()
863
986
        # this seeds the state to handle reporting the test.
864
987
        result.startTest(test)
865
 
        result.extractBenchmarkTime(test)
866
988
        result.report_unsupported(test, feature)
867
989
        # no output on unsupported features
868
990
        self.assertEqual(
875
997
        test.run(result)
876
998
        self.assertEqual(
877
999
            [
878
 
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
 
1000
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
879
1001
            ],
880
1002
            pb.calls[1:])
881
1003
    
899
1021
        # and not count as an error
900
1022
        self.assertEqual(0, result.error_count)
901
1023
 
 
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
 
902
1050
 
903
1051
class TestRunner(TestCase):
904
1052
 
971
1119
        def skipping_test():
972
1120
            raise TestSkipped('test intentionally skipped')
973
1121
 
974
 
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
1122
        runner = TextTestRunner(stream=self._log_file)
975
1123
        test = unittest.FunctionTestCase(skipping_test)
976
1124
        result = self.run_test_runner(runner, test)
977
1125
        self.assertTrue(result.wasSuccessful())
978
1126
 
979
1127
    def test_skipped_from_setup(self):
 
1128
        calls = []
980
1129
        class SkippedSetupTest(TestCase):
981
1130
 
982
1131
            def setUp(self):
983
 
                self.counter = 1
 
1132
                calls.append('setUp')
984
1133
                self.addCleanup(self.cleanup)
985
1134
                raise TestSkipped('skipped setup')
986
1135
 
988
1137
                self.fail('test reached')
989
1138
 
990
1139
            def cleanup(self):
991
 
                self.counter -= 1
 
1140
                calls.append('cleanup')
992
1141
 
993
 
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
1142
        runner = TextTestRunner(stream=self._log_file)
994
1143
        test = SkippedSetupTest('test_skip')
995
1144
        result = self.run_test_runner(runner, test)
996
1145
        self.assertTrue(result.wasSuccessful())
997
1146
        # Check if cleanup was called the right number of times.
998
 
        self.assertEqual(0, test.counter)
 
1147
        self.assertEqual(['setUp', 'cleanup'], calls)
999
1148
 
1000
1149
    def test_skipped_from_test(self):
 
1150
        calls = []
1001
1151
        class SkippedTest(TestCase):
1002
1152
 
1003
1153
            def setUp(self):
1004
 
                self.counter = 1
 
1154
                calls.append('setUp')
1005
1155
                self.addCleanup(self.cleanup)
1006
1156
 
1007
1157
            def test_skip(self):
1008
1158
                raise TestSkipped('skipped test')
1009
1159
 
1010
1160
            def cleanup(self):
1011
 
                self.counter -= 1
 
1161
                calls.append('cleanup')
1012
1162
 
1013
 
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
1163
        runner = TextTestRunner(stream=self._log_file)
1014
1164
        test = SkippedTest('test_skip')
1015
1165
        result = self.run_test_runner(runner, test)
1016
1166
        self.assertTrue(result.wasSuccessful())
1017
1167
        # Check if cleanup was called the right number of times.
1018
 
        self.assertEqual(0, test.counter)
 
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')
1019
1190
 
1020
1191
    def test_unsupported_features_listed(self):
1021
1192
        """When unsupported features are encountered they are detailed."""
1057
1228
            revision_id = workingtree.get_parent_ids()[0]
1058
1229
            self.assertEndsWith(output_string.rstrip(), revision_id)
1059
1230
 
 
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
 
1060
1237
    def test_success_log_deleted(self):
1061
1238
        """Successful tests have their log deleted"""
1062
1239
 
1070
1247
        test = LogTester('test_success')
1071
1248
        result = self.run_test_runner(runner, test)
1072
1249
 
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)
 
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)
1077
1299
 
1078
1300
    def test_fail_log_kept(self):
1079
1301
        """Failed tests have their log kept"""
1129
1351
class TestTestCase(TestCase):
1130
1352
    """Tests that test the core bzrlib TestCase."""
1131
1353
 
 
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
 
1132
1361
    def inner_test(self):
1133
1362
        # the inner child test
1134
1363
        note("inner_test")
1194
1423
        
1195
1424
        Each self.time() call is individually and separately profiled.
1196
1425
        """
1197
 
        try:
1198
 
            import bzrlib.lsprof
1199
 
        except ImportError:
1200
 
            raise TestSkipped("lsprof not installed.")
 
1426
        self.requireFeature(test_lsprof.LSProfFeature)
1201
1427
        # overrides the class member with an instance member so no cleanup 
1202
1428
        # needed.
1203
1429
        self._gather_lsprof_in_benchmarks = True
1367
1593
        self.callDeprecated([], testfunc, be_deprecated=False)
1368
1594
 
1369
1595
 
 
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
 
1370
1612
class TestConvenienceMakers(TestCaseWithTransport):
1371
1613
    """Test for the make_* convenience functions."""
1372
1614
 
1416
1658
        self.assertEqual([True], factory_called)
1417
1659
 
1418
1660
 
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
 
 
1453
1661
class TestKnownFailure(TestCase):
1454
1662
 
1455
1663
    def test_known_failure(self):
1520
1728
        self.loader = TestUtil.TestLoader()
1521
1729
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1522
1730
            'bzrlib.tests.test_selftest']))
1523
 
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
 
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))
1524
1787
 
1525
1788
    def test_filter_suite_by_re(self):
1526
 
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1527
 
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
 
1789
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1790
        filtered_names = _test_ids(filtered_suite)
1528
1791
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1529
1792
            'TestSelftestFiltering.test_filter_suite_by_re'])
1530
 
            
 
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
 
1531
1825
    def test_sort_suite_by_re(self):
1532
 
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1533
 
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
 
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)
1534
1829
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1535
1830
            'TestSelftestFiltering.test_filter_suite_by_re')
1536
1831
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1537
1832
 
 
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))