/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

[merge] bzr.dev 2255, resolve conflicts, update copyrights

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
62
63
                          _load_module_by_name,
63
64
                          'bzrlib.no-name-yet')
64
65
 
65
 
 
66
66
class MetaTestLog(TestCase):
67
67
 
68
68
    def test_logging(self):
467
467
        a branch and checking no directory was created at its relpath.
468
468
        """
469
469
        tree = self.make_branch_and_memory_tree('dir')
470
 
        self.failIfExists('dir')
 
470
        # Guard against regression into MemoryTransport leaking
 
471
        # files to disk instead of keeping them in memory.
 
472
        self.failIf(osutils.lexists('dir'))
471
473
        self.assertIsInstance(tree, memorytree.MemoryTree)
472
474
 
473
475
    def test_make_branch_and_memory_tree_with_format(self):
475
477
        format = bzrdir.BzrDirMetaFormat1()
476
478
        format.repository_format = repository.RepositoryFormat7()
477
479
        tree = self.make_branch_and_memory_tree('dir', format=format)
478
 
        self.failIfExists('dir')
 
480
        # Guard against regression into MemoryTransport leaking
 
481
        # files to disk instead of keeping them in memory.
 
482
        self.failIf(osutils.lexists('dir'))
479
483
        self.assertIsInstance(tree, memorytree.MemoryTree)
480
484
        self.assertEqual(format.repository_format.__class__,
481
485
            tree.branch.repository._format.__class__)
501
505
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
502
506
 
503
507
    def test_get_readonly_url_http(self):
 
508
        from bzrlib.tests.HttpServer import HttpServer
504
509
        from bzrlib.transport import get_transport
505
 
        from bzrlib.transport.local import LocalRelpathServer
506
 
        from bzrlib.transport.http import HttpServer, HttpTransportBase
507
 
        self.transport_server = LocalRelpathServer
 
510
        from bzrlib.transport.local import LocalURLServer
 
511
        from bzrlib.transport.http import HttpTransportBase
 
512
        self.transport_server = LocalURLServer
508
513
        self.transport_readonly_server = HttpServer
509
514
        # calling get_readonly_transport() gives us a HTTP server instance.
510
515
        url = self.get_readonly_url()
574
579
 
575
580
class TestTestResult(TestCase):
576
581
 
577
 
    def test_progress_bar_style_quiet(self):
578
 
        # test using a progress bar.
579
 
        dummy_test = TestTestResult('test_progress_bar_style_quiet')
580
 
        dummy_error = (Exception, None, [])
581
 
        mypb = MockProgress()
582
 
        mypb.update('Running tests', 0, 4)
583
 
        last_calls = mypb.calls[:]
584
 
 
585
 
        result = bzrlib.tests._MyResult(self._log_file,
586
 
                                        descriptions=0,
587
 
                                        verbosity=1,
588
 
                                        pb=mypb)
589
 
        self.assertEqual(last_calls, mypb.calls)
590
 
 
591
 
        def shorten(s):
592
 
            """Shorten a string based on the terminal width"""
593
 
            return result._ellipsise_unimportant_words(s,
594
 
                                 osutils.terminal_width())
595
 
 
596
 
        # an error 
597
 
        result.startTest(dummy_test)
598
 
        # starting a test prints the test name
599
 
        last_calls += [('update', '...tyle_quiet', 0, None)]
600
 
        self.assertEqual(last_calls, mypb.calls)
601
 
        result.addError(dummy_test, dummy_error)
602
 
        last_calls += [('update', 'ERROR        ', 1, None),
603
 
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
604
 
                      ]
605
 
        self.assertEqual(last_calls, mypb.calls)
606
 
 
607
 
        # a failure
608
 
        result.startTest(dummy_test)
609
 
        last_calls += [('update', '...tyle_quiet', 1, None)]
610
 
        self.assertEqual(last_calls, mypb.calls)
611
 
        last_calls += [('update', 'FAIL         ', 2, None),
612
 
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
613
 
                      ]
614
 
        result.addFailure(dummy_test, dummy_error)
615
 
        self.assertEqual(last_calls, mypb.calls)
616
 
 
617
 
        # a success
618
 
        result.startTest(dummy_test)
619
 
        last_calls += [('update', '...tyle_quiet', 2, None)]
620
 
        self.assertEqual(last_calls, mypb.calls)
621
 
        result.addSuccess(dummy_test)
622
 
        last_calls += [('update', 'OK           ', 3, None)]
623
 
        self.assertEqual(last_calls, mypb.calls)
624
 
 
625
 
        # a skip
626
 
        result.startTest(dummy_test)
627
 
        last_calls += [('update', '...tyle_quiet', 3, None)]
628
 
        self.assertEqual(last_calls, mypb.calls)
629
 
        result.addSkipped(dummy_test, dummy_error)
630
 
        last_calls += [('update', 'SKIP         ', 4, None)]
631
 
        self.assertEqual(last_calls, mypb.calls)
632
 
 
633
582
    def test_elapsed_time_with_benchmarking(self):
634
 
        result = bzrlib.tests._MyResult(self._log_file,
 
583
        result = bzrlib.tests.TextTestResult(self._log_file,
635
584
                                        descriptions=0,
636
585
                                        verbosity=1,
637
586
                                        )
640
589
        result.extractBenchmarkTime(self)
641
590
        timed_string = result._testTimeString()
642
591
        # without explicit benchmarking, we should get a simple time.
643
 
        self.assertContainsRe(timed_string, "^         [ 1-9][0-9]ms$")
 
592
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
644
593
        # if a benchmark time is given, we want a x of y style result.
645
594
        self.time(time.sleep, 0.001)
646
595
        result.extractBenchmarkTime(self)
647
596
        timed_string = result._testTimeString()
648
 
        self.assertContainsRe(timed_string, "^   [ 1-9][0-9]ms/   [ 1-9][0-9]ms$")
 
597
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
649
598
        # extracting the time from a non-bzrlib testcase sets to None
650
599
        result._recordTestStartTime()
651
600
        result.extractBenchmarkTime(
652
601
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
653
602
        timed_string = result._testTimeString()
654
 
        self.assertContainsRe(timed_string, "^         [ 1-9][0-9]ms$")
 
603
        self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
655
604
        # cheat. Yes, wash thy mouth out with soap.
656
605
        self._benchtime = None
657
606
 
658
607
    def test_assigned_benchmark_file_stores_date(self):
659
608
        output = StringIO()
660
 
        result = bzrlib.tests._MyResult(self._log_file,
 
609
        result = bzrlib.tests.TextTestResult(self._log_file,
661
610
                                        descriptions=0,
662
611
                                        verbosity=1,
663
612
                                        bench_history=output
664
613
                                        )
665
614
        output_string = output.getvalue()
 
615
        
666
616
        # if you are wondering about the regexp please read the comment in
667
617
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
668
618
        # XXX: what comment?  -- Andrew Bennetts
670
620
 
671
621
    def test_benchhistory_records_test_times(self):
672
622
        result_stream = StringIO()
673
 
        result = bzrlib.tests._MyResult(
 
623
        result = bzrlib.tests.TextTestResult(
674
624
            self._log_file,
675
625
            descriptions=0,
676
626
            verbosity=1,
703
653
        except ImportError:
704
654
            raise TestSkipped("lsprof not installed.")
705
655
        result_stream = StringIO()
706
 
        result = bzrlib.tests._MyResult(
 
656
        result = bzrlib.tests.VerboseTestResult(
707
657
            unittest._WritelnDecorator(result_stream),
708
658
            descriptions=0,
709
659
            verbosity=2,
758
708
        finally:
759
709
            TestCaseInTempDir.TEST_ROOT = old_root
760
710
 
761
 
    def test_accepts_and_uses_pb_parameter(self):
762
 
        test = TestRunner('dummy_test')
763
 
        mypb = MockProgress()
764
 
        self.assertEqual([], mypb.calls)
765
 
        runner = TextTestRunner(stream=self._log_file, pb=mypb)
766
 
        result = self.run_test_runner(runner, test)
767
 
        self.assertEqual(1, result.testsRun)
768
 
        self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
769
 
        self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
770
 
        self.assertEqual(('update', 'OK           ', 1, None), mypb.calls[2])
771
 
        self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
772
 
        self.assertEqual(('clear',), mypb.calls[4])
773
 
        self.assertEqual(5, len(mypb.calls))
774
 
 
775
711
    def test_skipped_test(self):
776
712
        # run a test that is skipped, and check the suite as a whole still
777
713
        # succeeds.
872
808
        # the outer child test
873
809
        note("outer_start")
874
810
        self.inner_test = TestTestCase("inner_child")
875
 
        result = bzrlib.tests._MyResult(self._log_file,
 
811
        result = bzrlib.tests.TextTestResult(self._log_file,
876
812
                                        descriptions=0,
877
813
                                        verbosity=1)
878
814
        self.inner_test.run(result)
892
828
        # the outer child test
893
829
        original_trace = bzrlib.trace._trace_file
894
830
        outer_test = TestTestCase("outer_child")
895
 
        result = bzrlib.tests._MyResult(self._log_file,
 
831
        result = bzrlib.tests.TextTestResult(self._log_file,
896
832
                                        descriptions=0,
897
833
                                        verbosity=1)
898
834
        outer_test.run(result)
907
843
        """Test that the TestCase.time() method accumulates a benchmark time."""
908
844
        sample_test = TestTestCase("method_that_times_a_bit_twice")
909
845
        output_stream = StringIO()
910
 
        result = bzrlib.tests._MyResult(
 
846
        result = bzrlib.tests.VerboseTestResult(
911
847
            unittest._WritelnDecorator(output_stream),
912
848
            descriptions=0,
913
 
            verbosity=2)
 
849
            verbosity=2,
 
850
            num_tests=sample_test.countTestCases())
914
851
        sample_test.run(result)
915
852
        self.assertContainsRe(
916
853
            output_stream.getvalue(),
917
 
            "[1-9][0-9]ms/   [1-9][0-9]ms\n$")
918
 
        
 
854
            r"\d+ms/ +\d+ms\n$")
 
855
 
 
856
    def test_hooks_sanitised(self):
 
857
        """The bzrlib hooks should be sanitised by setUp."""
 
858
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
859
            bzrlib.branch.Branch.hooks)
 
860
 
919
861
    def test__gather_lsprof_in_benchmarks(self):
920
862
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
921
863
        
1066
1008
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
1067
1009
            test_suite_factory=factory)
1068
1010
        self.assertEqual([True], factory_called)
 
1011
 
 
1012
 
 
1013
class TestSelftestCleanOutput(TestCaseInTempDir):
 
1014
 
 
1015
    def test_clean_output(self):
 
1016
        # test functionality of clean_selftest_output()
 
1017
        from bzrlib.tests import clean_selftest_output
 
1018
 
 
1019
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
 
1020
        files = ('bzr', 'setup.py', 'test9999.tmp')
 
1021
        for i in dirs:
 
1022
            os.mkdir(i)
 
1023
        for i in files:
 
1024
            f = file(i, 'wb')
 
1025
            f.write('content of ')
 
1026
            f.write(i)
 
1027
            f.close()
 
1028
 
 
1029
        root = os.getcwdu()
 
1030
        before = os.listdir(root)
 
1031
        before.sort()
 
1032
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1033
                           'test0000.tmp','test0001.tmp',
 
1034
                           'test9999.tmp','tests'],
 
1035
                           before)
 
1036
        clean_selftest_output(root, quiet=True)
 
1037
        after = os.listdir(root)
 
1038
        after.sort()
 
1039
        self.assertEquals(['bzr','bzrlib','setup.py',
 
1040
                           'test9999.tmp','tests'],
 
1041
                           after)