~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import unittest
23
23
import warnings
24
24
 
 
25
from bzrlib import osutils
25
26
import bzrlib
26
27
from bzrlib.progress import _BaseProgressBar
27
28
from bzrlib.tests import (
323
324
        self.assertEqual(tests[1].transport_readonly_server, server2)
324
325
 
325
326
 
 
327
class TestTreeProviderAdapter(TestCase):
 
328
    """Test the setup of tree_implementation tests."""
 
329
 
 
330
    def test_adapted_tests(self):
 
331
        # the tree implementation adapter is meant to setup one instance for
 
332
        # each working tree format, and one additional instance that will
 
333
        # use the default wt format, but create a revision tree for the tests.
 
334
        # this means that the wt ones should have the workingtree_to_test_tree
 
335
        # attribute set to 'return_parameter' and the revision one set to
 
336
        # revision_tree_from_workingtree.
 
337
 
 
338
        from bzrlib.tests.tree_implementations import (
 
339
            TreeTestProviderAdapter,
 
340
            return_parameter,
 
341
            revision_tree_from_workingtree
 
342
            )
 
343
        from bzrlib.workingtree import WorkingTreeFormat
 
344
        input_test = TestTreeProviderAdapter(
 
345
            "test_adapted_tests")
 
346
        server1 = "a"
 
347
        server2 = "b"
 
348
        formats = [("c", "C"), ("d", "D")]
 
349
        adapter = TreeTestProviderAdapter(server1, server2, formats)
 
350
        suite = adapter.adapt(input_test)
 
351
        tests = list(iter(suite))
 
352
        self.assertEqual(3, len(tests))
 
353
        default_format = WorkingTreeFormat.get_default_format()
 
354
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
 
355
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
 
356
        self.assertEqual(tests[0].transport_server, server1)
 
357
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
358
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
 
359
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
 
360
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
 
361
        self.assertEqual(tests[1].transport_server, server1)
 
362
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
363
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
 
364
        self.assertEqual(tests[2].workingtree_format, default_format)
 
365
        self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
 
366
        self.assertEqual(tests[2].transport_server, server1)
 
367
        self.assertEqual(tests[2].transport_readonly_server, server2)
 
368
        self.assertEqual(tests[2].workingtree_to_test_tree,
 
369
            revision_tree_from_workingtree)
 
370
 
 
371
 
 
372
class TestInterTreeProviderAdapter(TestCase):
 
373
    """A group of tests that test the InterTreeTestAdapter."""
 
374
 
 
375
    def test_adapted_tests(self):
 
376
        # check that constructor parameters are passed through to the adapted
 
377
        # test.
 
378
        # for InterTree tests we want the machinery to bring up two trees in
 
379
        # each instance: the base one, and the one we are interacting with.
 
380
        # because each optimiser can be direction specific, we need to test
 
381
        # each optimiser in its chosen direction.
 
382
        # unlike the TestProviderAdapter we dont want to automatically add a
 
383
        # parameterised one for WorkingTree - the optimisers will tell us what
 
384
        # ones to add.
 
385
        from bzrlib.tests.tree_implementations import (
 
386
            return_parameter,
 
387
            revision_tree_from_workingtree
 
388
            )
 
389
        from bzrlib.tests.intertree_implementations import (
 
390
            InterTreeTestProviderAdapter,
 
391
            )
 
392
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
 
393
        input_test = TestInterTreeProviderAdapter(
 
394
            "test_adapted_tests")
 
395
        server1 = "a"
 
396
        server2 = "b"
 
397
        format1 = WorkingTreeFormat2()
 
398
        format2 = WorkingTreeFormat3()
 
399
        formats = [(str, format1, format2, False, True),
 
400
            (int, format2, format1, False, True)]
 
401
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
 
402
        suite = adapter.adapt(input_test)
 
403
        tests = list(iter(suite))
 
404
        self.assertEqual(2, len(tests))
 
405
        self.assertEqual(tests[0].intertree_class, formats[0][0])
 
406
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
 
407
        self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
 
408
        self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
 
409
        self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
 
410
        self.assertEqual(tests[0].transport_server, server1)
 
411
        self.assertEqual(tests[0].transport_readonly_server, server2)
 
412
        self.assertEqual(tests[1].intertree_class, formats[1][0])
 
413
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
 
414
        self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
 
415
        self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
 
416
        self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
 
417
        self.assertEqual(tests[1].transport_server, server1)
 
418
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
419
 
326
420
class TestTestCaseWithTransport(TestCaseWithTransport):
327
421
    """Tests for the convenience functions TestCaseWithTransport introduces."""
328
422
 
395
489
    def clear(self):
396
490
        self.calls.append(('clear',))
397
491
 
 
492
    def note(self, msg, *args):
 
493
        self.calls.append(('note', msg, args))
 
494
 
398
495
 
399
496
class TestTestResult(TestCase):
400
497
 
405
502
        mypb = MockProgress()
406
503
        mypb.update('Running tests', 0, 4)
407
504
        last_calls = mypb.calls[:]
 
505
 
408
506
        result = bzrlib.tests._MyResult(self._log_file,
409
507
                                        descriptions=0,
410
508
                                        verbosity=1,
411
509
                                        pb=mypb)
412
510
        self.assertEqual(last_calls, mypb.calls)
413
511
 
 
512
        def shorten(s):
 
513
            """Shorten a string based on the terminal width"""
 
514
            return result._ellipsise_unimportant_words(s,
 
515
                                 osutils.terminal_width())
 
516
 
414
517
        # an error 
415
518
        result.startTest(dummy_test)
416
519
        # starting a test prints the test name
417
 
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 0, None)], mypb.calls)
418
 
        last_calls = mypb.calls[:]
 
520
        last_calls += [('update', '...tyle_quiet', 0, None)]
 
521
        self.assertEqual(last_calls, mypb.calls)
419
522
        result.addError(dummy_test, dummy_error)
420
 
        self.assertEqual(last_calls + [('update', 'ERROR        ', 1, None)], mypb.calls)
421
 
        last_calls = mypb.calls[:]
 
523
        last_calls += [('update', 'ERROR        ', 1, None),
 
524
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
 
525
                      ]
 
526
        self.assertEqual(last_calls, mypb.calls)
422
527
 
423
528
        # a failure
424
529
        result.startTest(dummy_test)
425
 
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 1, None)], mypb.calls)
426
 
        last_calls = mypb.calls[:]
 
530
        last_calls += [('update', '...tyle_quiet', 1, None)]
 
531
        self.assertEqual(last_calls, mypb.calls)
 
532
        last_calls += [('update', 'FAIL         ', 2, None),
 
533
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
 
534
                      ]
427
535
        result.addFailure(dummy_test, dummy_error)
428
 
        self.assertEqual(last_calls + [('update', 'FAIL         ', 2, None)], mypb.calls)
429
 
        last_calls = mypb.calls[:]
 
536
        self.assertEqual(last_calls, mypb.calls)
430
537
 
431
538
        # a success
432
539
        result.startTest(dummy_test)
433
 
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 2, None)], mypb.calls)
434
 
        last_calls = mypb.calls[:]
 
540
        last_calls += [('update', '...tyle_quiet', 2, None)]
 
541
        self.assertEqual(last_calls, mypb.calls)
435
542
        result.addSuccess(dummy_test)
436
 
        self.assertEqual(last_calls + [('update', 'OK           ', 3, None)], mypb.calls)
437
 
        last_calls = mypb.calls[:]
 
543
        last_calls += [('update', 'OK           ', 3, None)]
 
544
        self.assertEqual(last_calls, mypb.calls)
438
545
 
439
546
        # a skip
440
547
        result.startTest(dummy_test)
441
 
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 3, None)], mypb.calls)
442
 
        last_calls = mypb.calls[:]
 
548
        last_calls += [('update', '...tyle_quiet', 3, None)]
 
549
        self.assertEqual(last_calls, mypb.calls)
443
550
        result.addSkipped(dummy_test, dummy_error)
444
 
        self.assertEqual(last_calls + [('update', 'SKIP         ', 4, None)], mypb.calls)
445
 
        last_calls = mypb.calls[:]
 
551
        last_calls += [('update', 'SKIP         ', 4, None)]
 
552
        self.assertEqual(last_calls, mypb.calls)
446
553
 
447
554
    def test_elapsed_time_with_benchmarking(self):
448
555
        result = bzrlib.tests._MyResult(self._log_file,
681
788
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
682
789
            test_suite_factory=factory)
683
790
        self.assertEqual([True], factory_called)
684
 
 
685
 
    def test_run_bzr_subprocess(self):
686
 
        """The run_bzr_helper_external comand behaves nicely."""
687
 
        result = self.run_bzr_subprocess('--version')
688
 
        result = self.run_bzr_subprocess('--version', retcode=None)
689
 
        self.assertContainsRe(result[0], 'is free software')
690
 
        self.assertRaises(AssertionError, self.run_bzr_subprocess, 
691
 
                          '--versionn')
692
 
        result = self.run_bzr_subprocess('--versionn', retcode=3)
693
 
        result = self.run_bzr_subprocess('--versionn', retcode=None)
694
 
        self.assertContainsRe(result[1], 'unknown command')
695
 
        err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge', 
696
 
                                      retcode=3)[1]
697
 
        self.assertContainsRe(err, 'No known merge type magic merge')
698
 
 
699
 
    def test_run_bzr_error(self):
700
 
        out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
701
 
        self.assertEqual(out, 'it sure does!\n')
702
 
 
703
 
        out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
704
 
                                      'file-id', 'foobarbaz')