~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Martin Pool
  • Date: 2006-11-02 10:20:19 UTC
  • mfrom: (2114 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2119.
  • Revision ID: mbp@sourcefrog.net-20061102102019-9a5a02f485dff6f6
merge bzr.dev and reconcile several changes, also some test fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License version 2 as published by
5
 
# the Free Software Foundation.
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
6
7
#
7
8
# This program is distributed in the hope that it will be useful,
8
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
24
import unittest
24
25
import warnings
25
26
 
26
 
from bzrlib import osutils, memorytree
27
27
import bzrlib
 
28
from bzrlib import (
 
29
    bzrdir,
 
30
    memorytree,
 
31
    osutils,
 
32
    repository,
 
33
    )
28
34
from bzrlib.progress import _BaseProgressBar
29
35
from bzrlib.tests import (
30
36
                          ChrootedTestCase,
57
63
                          _load_module_by_name,
58
64
                          'bzrlib.no-name-yet')
59
65
 
60
 
 
61
66
class MetaTestLog(TestCase):
62
67
 
63
68
    def test_logging(self):
465
470
        self.failIfExists('dir')
466
471
        self.assertIsInstance(tree, memorytree.MemoryTree)
467
472
 
 
473
    def test_make_branch_and_memory_tree_with_format(self):
 
474
        """make_branch_and_memory_tree should accept a format option."""
 
475
        format = bzrdir.BzrDirMetaFormat1()
 
476
        format.repository_format = repository.RepositoryFormat7()
 
477
        tree = self.make_branch_and_memory_tree('dir', format=format)
 
478
        self.failIfExists('dir')
 
479
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
480
        self.assertEqual(format.repository_format.__class__,
 
481
            tree.branch.repository._format.__class__)
 
482
 
468
483
 
469
484
class TestTestCaseWithTransport(TestCaseWithTransport):
470
485
    """Tests for the convenience functions TestCaseWithTransport introduces."""
487
502
 
488
503
    def test_get_readonly_url_http(self):
489
504
        from bzrlib.transport import get_transport
490
 
        from bzrlib.transport.local import LocalRelpathServer
 
505
        from bzrlib.transport.local import LocalURLServer
491
506
        from bzrlib.transport.http import HttpServer, HttpTransportBase
492
 
        self.transport_server = LocalRelpathServer
 
507
        self.transport_server = LocalURLServer
493
508
        self.transport_readonly_server = HttpServer
494
509
        # calling get_readonly_transport() gives us a HTTP server instance.
495
510
        url = self.get_readonly_url()
559
574
 
560
575
class TestTestResult(TestCase):
561
576
 
562
 
    def test_progress_bar_style_quiet(self):
563
 
        # test using a progress bar.
564
 
        dummy_test = TestTestResult('test_progress_bar_style_quiet')
565
 
        dummy_error = (Exception, None, [])
566
 
        mypb = MockProgress()
567
 
        mypb.update('Running tests', 0, 4)
568
 
        last_calls = mypb.calls[:]
569
 
 
570
 
        result = bzrlib.tests._MyResult(self._log_file,
571
 
                                        descriptions=0,
572
 
                                        verbosity=1,
573
 
                                        pb=mypb)
574
 
        self.assertEqual(last_calls, mypb.calls)
575
 
 
576
 
        def shorten(s):
577
 
            """Shorten a string based on the terminal width"""
578
 
            return result._ellipsise_unimportant_words(s,
579
 
                                 osutils.terminal_width())
580
 
 
581
 
        # an error 
582
 
        result.startTest(dummy_test)
583
 
        # starting a test prints the test name
584
 
        last_calls += [('update', '...tyle_quiet', 0, None)]
585
 
        self.assertEqual(last_calls, mypb.calls)
586
 
        result.addError(dummy_test, dummy_error)
587
 
        last_calls += [('update', 'ERROR        ', 1, None),
588
 
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
589
 
                      ]
590
 
        self.assertEqual(last_calls, mypb.calls)
591
 
 
592
 
        # a failure
593
 
        result.startTest(dummy_test)
594
 
        last_calls += [('update', '...tyle_quiet', 1, None)]
595
 
        self.assertEqual(last_calls, mypb.calls)
596
 
        last_calls += [('update', 'FAIL         ', 2, None),
597
 
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
598
 
                      ]
599
 
        result.addFailure(dummy_test, dummy_error)
600
 
        self.assertEqual(last_calls, mypb.calls)
601
 
 
602
 
        # a success
603
 
        result.startTest(dummy_test)
604
 
        last_calls += [('update', '...tyle_quiet', 2, None)]
605
 
        self.assertEqual(last_calls, mypb.calls)
606
 
        result.addSuccess(dummy_test)
607
 
        last_calls += [('update', 'OK           ', 3, None)]
608
 
        self.assertEqual(last_calls, mypb.calls)
609
 
 
610
 
        # a skip
611
 
        result.startTest(dummy_test)
612
 
        last_calls += [('update', '...tyle_quiet', 3, None)]
613
 
        self.assertEqual(last_calls, mypb.calls)
614
 
        result.addSkipped(dummy_test, dummy_error)
615
 
        last_calls += [('update', 'SKIP         ', 4, None)]
616
 
        self.assertEqual(last_calls, mypb.calls)
617
 
 
618
577
    def test_elapsed_time_with_benchmarking(self):
619
 
        result = bzrlib.tests._MyResult(self._log_file,
 
578
        result = bzrlib.tests.TextTestResult(self._log_file,
620
579
                                        descriptions=0,
621
580
                                        verbosity=1,
622
581
                                        )
642
601
 
643
602
    def test_assigned_benchmark_file_stores_date(self):
644
603
        output = StringIO()
645
 
        result = bzrlib.tests._MyResult(self._log_file,
 
604
        result = bzrlib.tests.TextTestResult(self._log_file,
646
605
                                        descriptions=0,
647
606
                                        verbosity=1,
648
607
                                        bench_history=output
649
608
                                        )
650
609
        output_string = output.getvalue()
 
610
        
651
611
        # if you are wondering about the regexp please read the comment in
652
612
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
653
613
        # XXX: what comment?  -- Andrew Bennetts
655
615
 
656
616
    def test_benchhistory_records_test_times(self):
657
617
        result_stream = StringIO()
658
 
        result = bzrlib.tests._MyResult(
 
618
        result = bzrlib.tests.TextTestResult(
659
619
            self._log_file,
660
620
            descriptions=0,
661
621
            verbosity=1,
688
648
        except ImportError:
689
649
            raise TestSkipped("lsprof not installed.")
690
650
        result_stream = StringIO()
691
 
        result = bzrlib.tests._MyResult(
 
651
        result = bzrlib.tests.VerboseTestResult(
692
652
            unittest._WritelnDecorator(result_stream),
693
653
            descriptions=0,
694
654
            verbosity=2,
743
703
        finally:
744
704
            TestCaseInTempDir.TEST_ROOT = old_root
745
705
 
746
 
    def test_accepts_and_uses_pb_parameter(self):
747
 
        test = TestRunner('dummy_test')
748
 
        mypb = MockProgress()
749
 
        self.assertEqual([], mypb.calls)
750
 
        runner = TextTestRunner(stream=self._log_file, pb=mypb)
751
 
        result = self.run_test_runner(runner, test)
752
 
        self.assertEqual(1, result.testsRun)
753
 
        self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
754
 
        self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
755
 
        self.assertEqual(('update', 'OK           ', 1, None), mypb.calls[2])
756
 
        self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
757
 
        self.assertEqual(('clear',), mypb.calls[4])
758
 
        self.assertEqual(5, len(mypb.calls))
759
 
 
760
706
    def test_skipped_test(self):
761
707
        # run a test that is skipped, and check the suite as a whole still
762
708
        # succeeds.
857
803
        # the outer child test
858
804
        note("outer_start")
859
805
        self.inner_test = TestTestCase("inner_child")
860
 
        result = bzrlib.tests._MyResult(self._log_file,
 
806
        result = bzrlib.tests.TextTestResult(self._log_file,
861
807
                                        descriptions=0,
862
808
                                        verbosity=1)
863
809
        self.inner_test.run(result)
877
823
        # the outer child test
878
824
        original_trace = bzrlib.trace._trace_file
879
825
        outer_test = TestTestCase("outer_child")
880
 
        result = bzrlib.tests._MyResult(self._log_file,
 
826
        result = bzrlib.tests.TextTestResult(self._log_file,
881
827
                                        descriptions=0,
882
828
                                        verbosity=1)
883
829
        outer_test.run(result)
892
838
        """Test that the TestCase.time() method accumulates a benchmark time."""
893
839
        sample_test = TestTestCase("method_that_times_a_bit_twice")
894
840
        output_stream = StringIO()
895
 
        result = bzrlib.tests._MyResult(
 
841
        result = bzrlib.tests.VerboseTestResult(
896
842
            unittest._WritelnDecorator(output_stream),
897
843
            descriptions=0,
898
 
            verbosity=2)
 
844
            verbosity=2,
 
845
            num_tests=sample_test.countTestCases())
899
846
        sample_test.run(result)
900
847
        self.assertContainsRe(
901
848
            output_stream.getvalue(),