/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

  • Committer: Aaron Bentley
  • Date: 2006-11-17 04:06:03 UTC
  • mfrom: (2139 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2162.
  • Revision ID: aaron.bentley@utoronto.ca-20061117040603-pgebxndswvwk26tt
Merge from bzr.dev

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
15
16
 
16
17
"""Tests for the test framework."""
17
18
 
 
19
import cStringIO
18
20
import os
19
21
from StringIO import StringIO
20
22
import sys
22
24
import unittest
23
25
import warnings
24
26
 
25
 
from bzrlib import osutils
26
27
import bzrlib
 
28
from bzrlib import (
 
29
    bzrdir,
 
30
    memorytree,
 
31
    osutils,
 
32
    repository,
 
33
    )
27
34
from bzrlib.progress import _BaseProgressBar
28
35
from bzrlib.tests import (
29
36
                          ChrootedTestCase,
30
37
                          TestCase,
31
38
                          TestCaseInTempDir,
 
39
                          TestCaseWithMemoryTransport,
32
40
                          TestCaseWithTransport,
33
41
                          TestSkipped,
34
42
                          TestSuite,
55
63
                          _load_module_by_name,
56
64
                          'bzrlib.no-name-yet')
57
65
 
58
 
 
59
66
class MetaTestLog(TestCase):
60
67
 
61
68
    def test_logging(self):
62
69
        """Test logs are captured when a test fails."""
63
70
        self.log('a test message')
64
71
        self._log_file.flush()
65
 
        self.assertContainsRe(self._get_log(), 'a test message\n')
 
72
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
73
                              'a test message\n')
66
74
 
67
75
 
68
76
class TestTreeShape(TestCaseInTempDir):
432
440
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
433
441
 
434
442
 
 
443
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
 
444
 
 
445
    def test_home_is_non_existant_dir_under_root(self):
 
446
        """The test_home_dir for TestCaseWithMemoryTransport is missing.
 
447
 
 
448
        This is because TestCaseWithMemoryTransport is for tests that do not
 
449
        need any disk resources: they should be hooked into bzrlib in such a 
 
450
        way that no global settings are being changed by the test (only a 
 
451
        few tests should need to do that), and having a missing dir as home is
 
452
        an effective way to ensure that this is the case.
 
453
        """
 
454
        self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
 
455
            self.test_home_dir)
 
456
        self.assertEqual(self.test_home_dir, os.environ['HOME'])
 
457
        
 
458
    def test_cwd_is_TEST_ROOT(self):
 
459
        self.assertEqual(self.test_dir, self.TEST_ROOT)
 
460
        cwd = osutils.getcwd()
 
461
        self.assertEqual(self.test_dir, cwd)
 
462
 
 
463
    def test_make_branch_and_memory_tree(self):
 
464
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
 
465
 
 
466
        This is hard to comprehensively robustly test, so we settle for making
 
467
        a branch and checking no directory was created at its relpath.
 
468
        """
 
469
        tree = self.make_branch_and_memory_tree('dir')
 
470
        self.failIfExists('dir')
 
471
        self.assertIsInstance(tree, memorytree.MemoryTree)
 
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
 
 
483
 
435
484
class TestTestCaseWithTransport(TestCaseWithTransport):
436
485
    """Tests for the convenience functions TestCaseWithTransport introduces."""
437
486
 
453
502
 
454
503
    def test_get_readonly_url_http(self):
455
504
        from bzrlib.transport import get_transport
456
 
        from bzrlib.transport.local import LocalRelpathServer
 
505
        from bzrlib.transport.local import LocalURLServer
457
506
        from bzrlib.transport.http import HttpServer, HttpTransportBase
458
 
        self.transport_server = LocalRelpathServer
 
507
        self.transport_server = LocalURLServer
459
508
        self.transport_readonly_server = HttpServer
460
509
        # calling get_readonly_transport() gives us a HTTP server instance.
461
510
        url = self.get_readonly_url()
525
574
 
526
575
class TestTestResult(TestCase):
527
576
 
528
 
    def test_progress_bar_style_quiet(self):
529
 
        # test using a progress bar.
530
 
        dummy_test = TestTestResult('test_progress_bar_style_quiet')
531
 
        dummy_error = (Exception, None, [])
532
 
        mypb = MockProgress()
533
 
        mypb.update('Running tests', 0, 4)
534
 
        last_calls = mypb.calls[:]
535
 
 
536
 
        result = bzrlib.tests._MyResult(self._log_file,
537
 
                                        descriptions=0,
538
 
                                        verbosity=1,
539
 
                                        pb=mypb)
540
 
        self.assertEqual(last_calls, mypb.calls)
541
 
 
542
 
        def shorten(s):
543
 
            """Shorten a string based on the terminal width"""
544
 
            return result._ellipsise_unimportant_words(s,
545
 
                                 osutils.terminal_width())
546
 
 
547
 
        # an error 
548
 
        result.startTest(dummy_test)
549
 
        # starting a test prints the test name
550
 
        last_calls += [('update', '...tyle_quiet', 0, None)]
551
 
        self.assertEqual(last_calls, mypb.calls)
552
 
        result.addError(dummy_test, dummy_error)
553
 
        last_calls += [('update', 'ERROR        ', 1, None),
554
 
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
555
 
                      ]
556
 
        self.assertEqual(last_calls, mypb.calls)
557
 
 
558
 
        # a failure
559
 
        result.startTest(dummy_test)
560
 
        last_calls += [('update', '...tyle_quiet', 1, None)]
561
 
        self.assertEqual(last_calls, mypb.calls)
562
 
        last_calls += [('update', 'FAIL         ', 2, None),
563
 
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
564
 
                      ]
565
 
        result.addFailure(dummy_test, dummy_error)
566
 
        self.assertEqual(last_calls, mypb.calls)
567
 
 
568
 
        # a success
569
 
        result.startTest(dummy_test)
570
 
        last_calls += [('update', '...tyle_quiet', 2, None)]
571
 
        self.assertEqual(last_calls, mypb.calls)
572
 
        result.addSuccess(dummy_test)
573
 
        last_calls += [('update', 'OK           ', 3, None)]
574
 
        self.assertEqual(last_calls, mypb.calls)
575
 
 
576
 
        # a skip
577
 
        result.startTest(dummy_test)
578
 
        last_calls += [('update', '...tyle_quiet', 3, None)]
579
 
        self.assertEqual(last_calls, mypb.calls)
580
 
        result.addSkipped(dummy_test, dummy_error)
581
 
        last_calls += [('update', 'SKIP         ', 4, None)]
582
 
        self.assertEqual(last_calls, mypb.calls)
583
 
 
584
577
    def test_elapsed_time_with_benchmarking(self):
585
 
        result = bzrlib.tests._MyResult(self._log_file,
 
578
        result = bzrlib.tests.TextTestResult(self._log_file,
586
579
                                        descriptions=0,
587
580
                                        verbosity=1,
588
581
                                        )
608
601
 
609
602
    def test_assigned_benchmark_file_stores_date(self):
610
603
        output = StringIO()
611
 
        result = bzrlib.tests._MyResult(self._log_file,
 
604
        result = bzrlib.tests.TextTestResult(self._log_file,
612
605
                                        descriptions=0,
613
606
                                        verbosity=1,
614
607
                                        bench_history=output
615
608
                                        )
616
609
        output_string = output.getvalue()
 
610
        
617
611
        # if you are wondering about the regexp please read the comment in
618
612
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
619
613
        # XXX: what comment?  -- Andrew Bennetts
621
615
 
622
616
    def test_benchhistory_records_test_times(self):
623
617
        result_stream = StringIO()
624
 
        result = bzrlib.tests._MyResult(
 
618
        result = bzrlib.tests.TextTestResult(
625
619
            self._log_file,
626
620
            descriptions=0,
627
621
            verbosity=1,
654
648
        except ImportError:
655
649
            raise TestSkipped("lsprof not installed.")
656
650
        result_stream = StringIO()
657
 
        result = bzrlib.tests._MyResult(
 
651
        result = bzrlib.tests.VerboseTestResult(
658
652
            unittest._WritelnDecorator(result_stream),
659
653
            descriptions=0,
660
654
            verbosity=2,
709
703
        finally:
710
704
            TestCaseInTempDir.TEST_ROOT = old_root
711
705
 
712
 
    def test_accepts_and_uses_pb_parameter(self):
713
 
        test = TestRunner('dummy_test')
714
 
        mypb = MockProgress()
715
 
        self.assertEqual([], mypb.calls)
716
 
        runner = TextTestRunner(stream=self._log_file, pb=mypb)
717
 
        result = self.run_test_runner(runner, test)
718
 
        self.assertEqual(1, result.testsRun)
719
 
        self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
720
 
        self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
721
 
        self.assertEqual(('update', 'OK           ', 1, None), mypb.calls[2])
722
 
        self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
723
 
        self.assertEqual(('clear',), mypb.calls[4])
724
 
        self.assertEqual(5, len(mypb.calls))
725
 
 
726
706
    def test_skipped_test(self):
727
707
        # run a test that is skipped, and check the suite as a whole still
728
708
        # succeeds.
749
729
            revision_id = workingtree.get_parent_ids()[0]
750
730
            self.assertEndsWith(output_string.rstrip(), revision_id)
751
731
 
 
732
    def test_success_log_deleted(self):
 
733
        """Successful tests have their log deleted"""
 
734
 
 
735
        class LogTester(TestCase):
 
736
 
 
737
            def test_success(self):
 
738
                self.log('this will be removed\n')
 
739
 
 
740
        sio = cStringIO.StringIO()
 
741
        runner = TextTestRunner(stream=sio)
 
742
        test = LogTester('test_success')
 
743
        result = self.run_test_runner(runner, test)
 
744
 
 
745
        log = test._get_log()
 
746
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
747
        self.assertEqual('', test._log_contents)
 
748
        self.assertIs(None, test._log_file_name)
 
749
 
 
750
    def test_fail_log_kept(self):
 
751
        """Failed tests have their log kept"""
 
752
 
 
753
        class LogTester(TestCase):
 
754
 
 
755
            def test_fail(self):
 
756
                self.log('this will be kept\n')
 
757
                self.fail('this test fails')
 
758
 
 
759
        sio = cStringIO.StringIO()
 
760
        runner = TextTestRunner(stream=sio)
 
761
        test = LogTester('test_fail')
 
762
        result = self.run_test_runner(runner, test)
 
763
 
 
764
        text = sio.getvalue()
 
765
        self.assertContainsRe(text, 'this will be kept')
 
766
        self.assertContainsRe(text, 'this test fails')
 
767
 
 
768
        log = test._get_log()
 
769
        self.assertContainsRe(log, 'this will be kept')
 
770
        self.assertEqual(log, test._log_contents)
 
771
 
 
772
    def test_error_log_kept(self):
 
773
        """Tests with errors have their log kept"""
 
774
 
 
775
        class LogTester(TestCase):
 
776
 
 
777
            def test_error(self):
 
778
                self.log('this will be kept\n')
 
779
                raise ValueError('random exception raised')
 
780
 
 
781
        sio = cStringIO.StringIO()
 
782
        runner = TextTestRunner(stream=sio)
 
783
        test = LogTester('test_error')
 
784
        result = self.run_test_runner(runner, test)
 
785
 
 
786
        text = sio.getvalue()
 
787
        self.assertContainsRe(text, 'this will be kept')
 
788
        self.assertContainsRe(text, 'random exception raised')
 
789
 
 
790
        log = test._get_log()
 
791
        self.assertContainsRe(log, 'this will be kept')
 
792
        self.assertEqual(log, test._log_contents)
 
793
 
752
794
 
753
795
class TestTestCase(TestCase):
754
796
    """Tests that test the core bzrlib TestCase."""
761
803
        # the outer child test
762
804
        note("outer_start")
763
805
        self.inner_test = TestTestCase("inner_child")
764
 
        result = bzrlib.tests._MyResult(self._log_file,
 
806
        result = bzrlib.tests.TextTestResult(self._log_file,
765
807
                                        descriptions=0,
766
808
                                        verbosity=1)
767
809
        self.inner_test.run(result)
781
823
        # the outer child test
782
824
        original_trace = bzrlib.trace._trace_file
783
825
        outer_test = TestTestCase("outer_child")
784
 
        result = bzrlib.tests._MyResult(self._log_file,
 
826
        result = bzrlib.tests.TextTestResult(self._log_file,
785
827
                                        descriptions=0,
786
828
                                        verbosity=1)
787
829
        outer_test.run(result)
796
838
        """Test that the TestCase.time() method accumulates a benchmark time."""
797
839
        sample_test = TestTestCase("method_that_times_a_bit_twice")
798
840
        output_stream = StringIO()
799
 
        result = bzrlib.tests._MyResult(
 
841
        result = bzrlib.tests.VerboseTestResult(
800
842
            unittest._WritelnDecorator(output_stream),
801
843
            descriptions=0,
802
 
            verbosity=2)
 
844
            verbosity=2,
 
845
            num_tests=sample_test.countTestCases())
803
846
        sample_test.run(result)
804
847
        self.assertContainsRe(
805
848
            output_stream.getvalue(),
806
 
            "[1-9][0-9]ms/   [1-9][0-9]ms\n$")
 
849
            r"\d+ms/ +\d+ms\n$")
807
850
        
808
851
    def test__gather_lsprof_in_benchmarks(self):
809
852
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
920
963
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
921
964
                              bzrlib.bzrdir.BzrDirFormat6)
922
965
 
923
 
    def test_make_branch_and_mutable_tree(self):
 
966
    def test_make_branch_and_memory_tree(self):
924
967
        # we should be able to get a new branch and a mutable tree from
925
968
        # TestCaseWithTransport
926
969
        tree = self.make_branch_and_memory_tree('a')