/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: Canonical.com Patch Queue Manager
  • Date: 2009-08-19 18:04:49 UTC
  • mfrom: (4593.5.43 1.19-known-graph-sorted)
  • Revision ID: pqm@pqm.ubuntu.com-20090819180449-p5dibldf9pcp24n4
(jam) Add VersionedFiles.get_known_graph_ancestry and
        KnownGraph.merge_sort()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Tests for the test framework."""
18
18
 
19
19
from cStringIO import StringIO
20
 
from doctest import ELLIPSIS
21
20
import os
22
 
import signal
23
21
import sys
24
22
import time
25
23
import unittest
26
24
import warnings
27
25
 
28
 
from testtools import MultiTestResult
29
 
from testtools.content_type import ContentType
30
 
from testtools.matchers import (
31
 
    DocTestMatches,
32
 
    Equals,
33
 
    )
34
 
import testtools.tests.helpers
35
 
 
36
26
import bzrlib
37
27
from bzrlib import (
38
28
    branchbuilder,
47
37
    repository,
48
38
    symbol_versioning,
49
39
    tests,
50
 
    transport,
51
40
    workingtree,
52
41
    )
53
42
from bzrlib.repofmt import (
61
50
    deprecated_method,
62
51
    )
63
52
from bzrlib.tests import (
64
 
    features,
65
53
    test_lsprof,
66
 
    test_server,
67
54
    test_sftp_transport,
68
55
    TestUtil,
69
56
    )
70
57
from bzrlib.trace import note
71
 
from bzrlib.transport import memory
 
58
from bzrlib.transport.memory import MemoryServer, MemoryTransport
72
59
from bzrlib.version import _get_bzr_source_tree
73
60
 
74
61
 
88
75
                          TestUtil._load_module_by_name,
89
76
                          'bzrlib.no-name-yet')
90
77
 
91
 
 
92
78
class MetaTestLog(tests.TestCase):
93
79
 
94
80
    def test_logging(self):
95
81
        """Test logs are captured when a test fails."""
96
82
        self.log('a test message')
97
 
        details = self.getDetails()
98
 
        log = details['log']
99
 
        self.assertThat(log.content_type, Equals(ContentType(
100
 
            "text", "plain", {"charset": "utf8"})))
101
 
        self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
102
 
        self.assertThat(self.get_log(),
103
 
            DocTestMatches(u"...a test message\n", ELLIPSIS))
 
83
        self._log_file.flush()
 
84
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
85
                              'a test message\n')
104
86
 
105
87
 
106
88
class TestUnicodeFilename(tests.TestCase):
505
487
        self.assertEqualStat(real, fake)
506
488
 
507
489
    def test_assertEqualStat_notequal(self):
508
 
        self.build_tree(["foo", "longname"])
 
490
        self.build_tree(["foo", "bar"])
509
491
        self.assertRaises(AssertionError, self.assertEqualStat,
510
 
            os.lstat("foo"), os.lstat("longname"))
 
492
            os.lstat("foo"), os.lstat("bar"))
511
493
 
512
494
 
513
495
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
531
513
        cwd = osutils.getcwd()
532
514
        self.assertIsSameRealPath(self.test_dir, cwd)
533
515
 
534
 
    def test_BZR_HOME_and_HOME_are_bytestrings(self):
535
 
        """The $BZR_HOME and $HOME environment variables should not be unicode.
536
 
 
537
 
        See https://bugs.launchpad.net/bzr/+bug/464174
538
 
        """
539
 
        self.assertIsInstance(os.environ['BZR_HOME'], str)
540
 
        self.assertIsInstance(os.environ['HOME'], str)
541
 
 
542
516
    def test_make_branch_and_memory_tree(self):
543
517
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
544
518
 
600
574
                         self.get_transport().get_bytes(
601
575
                            'dir/.bzr/repository/format'))
602
576
 
 
577
    def test_safety_net(self):
 
578
        """No test should modify the safety .bzr directory.
 
579
 
 
580
        We just test that the _check_safety_net private method raises
 
581
        AssertionError, it's easier than building a test suite with the same
 
582
        test.
 
583
        """
 
584
        # Oops, a commit in the current directory (i.e. without local .bzr
 
585
        # directory) will crawl up the hierarchy to find a .bzr directory.
 
586
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
 
587
        # But we have a safety net in place.
 
588
        self.assertRaises(AssertionError, self._check_safety_net)
 
589
 
603
590
    def test_dangling_locks_cause_failures(self):
604
591
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
605
592
            def test_function(self):
622
609
 
623
610
    def test_get_readonly_url_none(self):
624
611
        from bzrlib.transport import get_transport
 
612
        from bzrlib.transport.memory import MemoryServer
625
613
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
626
 
        self.vfs_transport_factory = memory.MemoryServer
 
614
        self.vfs_transport_factory = MemoryServer
627
615
        self.transport_readonly_server = None
628
616
        # calling get_readonly_transport() constructs a decorator on the url
629
617
        # for the server
638
626
    def test_get_readonly_url_http(self):
639
627
        from bzrlib.tests.http_server import HttpServer
640
628
        from bzrlib.transport import get_transport
 
629
        from bzrlib.transport.local import LocalURLServer
641
630
        from bzrlib.transport.http import HttpTransportBase
642
 
        self.transport_server = test_server.LocalURLServer
 
631
        self.transport_server = LocalURLServer
643
632
        self.transport_readonly_server = HttpServer
644
633
        # calling get_readonly_transport() gives us a HTTP server instance.
645
634
        url = self.get_readonly_url()
676
665
 
677
666
    def setUp(self):
678
667
        super(TestTestCaseTransports, self).setUp()
679
 
        self.vfs_transport_factory = memory.MemoryServer
 
668
        self.vfs_transport_factory = MemoryServer
680
669
 
681
670
    def test_make_bzrdir_preserves_transport(self):
682
671
        t = self.get_transport()
683
672
        result_bzrdir = self.make_bzrdir('subdir')
684
673
        self.assertIsInstance(result_bzrdir.transport,
685
 
                              memory.MemoryTransport)
 
674
                              MemoryTransport)
686
675
        # should not be on disk, should only be in memory
687
676
        self.failIfExists('subdir')
688
677
 
696
685
        self.assertEqual(url, t.clone('..').base)
697
686
 
698
687
 
699
 
class TestProfileResult(tests.TestCase):
700
 
 
701
 
    def test_profiles_tests(self):
702
 
        self.requireFeature(test_lsprof.LSProfFeature)
703
 
        terminal = testtools.tests.helpers.ExtendedTestResult()
704
 
        result = tests.ProfileResult(terminal)
705
 
        class Sample(tests.TestCase):
706
 
            def a(self):
707
 
                self.sample_function()
708
 
            def sample_function(self):
709
 
                pass
710
 
        test = Sample("a")
711
 
        test.run(result)
712
 
        case = terminal._events[0][1]
713
 
        self.assertLength(1, case._benchcalls)
714
 
        # We must be able to unpack it as the test reporting code wants
715
 
        (_, _, _), stats = case._benchcalls[0]
716
 
        self.assertTrue(callable(stats.pprint))
717
 
 
718
 
 
719
688
class TestTestResult(tests.TestCase):
720
689
 
721
690
    def check_timing(self, test_case, expected_re):
723
692
                descriptions=0,
724
693
                verbosity=1,
725
694
                )
726
 
        capture = testtools.tests.helpers.ExtendedTestResult()
727
 
        test_case.run(MultiTestResult(result, capture))
728
 
        run_case = capture._events[0][1]
729
 
        timed_string = result._testTimeString(run_case)
 
695
        test_case.run(result)
 
696
        timed_string = result._testTimeString(test_case)
730
697
        self.assertContainsRe(timed_string, expected_re)
731
698
 
732
699
    def test_test_reporting(self):
750
717
        self.check_timing(ShortDelayTestCase('test_short_delay'),
751
718
                          r"^ +[0-9]+ms$")
752
719
 
753
 
    def _patch_get_bzr_source_tree(self):
754
 
        # Reading from the actual source tree breaks isolation, but we don't
755
 
        # want to assume that thats *all* that would happen.
756
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
757
 
 
758
720
    def test_assigned_benchmark_file_stores_date(self):
759
 
        self._patch_get_bzr_source_tree()
760
721
        output = StringIO()
761
722
        result = bzrlib.tests.TextTestResult(self._log_file,
762
723
                                        descriptions=0,
770
731
        self.assertContainsRe(output_string, "--date [0-9.]+")
771
732
 
772
733
    def test_benchhistory_records_test_times(self):
773
 
        self._patch_get_bzr_source_tree()
774
734
        result_stream = StringIO()
775
735
        result = bzrlib.tests.TextTestResult(
776
736
            self._log_file,
838
798
    def test_known_failure(self):
839
799
        """A KnownFailure being raised should trigger several result actions."""
840
800
        class InstrumentedTestResult(tests.ExtendedTestResult):
841
 
            def stopTestRun(self): pass
 
801
            def done(self): pass
842
802
            def startTests(self): pass
843
803
            def report_test_start(self, test): pass
844
 
            def report_known_failure(self, test, err=None, details=None):
845
 
                self._call = test, 'known failure'
 
804
            def report_known_failure(self, test, err):
 
805
                self._call = test, err
846
806
        result = InstrumentedTestResult(None, None, None, None)
847
 
        class Test(tests.TestCase):
848
 
            def test_function(self):
849
 
                raise tests.KnownFailure('failed!')
850
 
        test = Test("test_function")
 
807
        def test_function():
 
808
            raise tests.KnownFailure('failed!')
 
809
        test = unittest.FunctionTestCase(test_function)
851
810
        test.run(result)
852
811
        # it should invoke 'report_known_failure'.
853
812
        self.assertEqual(2, len(result._call))
854
 
        self.assertEqual(test.id(), result._call[0].id())
855
 
        self.assertEqual('known failure', result._call[1])
 
813
        self.assertEqual(test, result._call[0])
 
814
        self.assertEqual(tests.KnownFailure, result._call[1][0])
 
815
        self.assertIsInstance(result._call[1][1], tests.KnownFailure)
856
816
        # we dont introspec the traceback, if the rest is ok, it would be
857
817
        # exceptional for it not to be.
858
818
        # it should update the known_failure_count on the object.
892
852
    def test_add_not_supported(self):
893
853
        """Test the behaviour of invoking addNotSupported."""
894
854
        class InstrumentedTestResult(tests.ExtendedTestResult):
895
 
            def stopTestRun(self): pass
 
855
            def done(self): pass
896
856
            def startTests(self): pass
897
857
            def report_test_start(self, test): pass
898
858
            def report_unsupported(self, test, feature):
930
890
        result.report_unsupported(test, feature)
931
891
        output = result_stream.getvalue()[prefix:]
932
892
        lines = output.splitlines()
933
 
        # We don't check for the final '0ms' since it may fail on slow hosts
934
 
        self.assertStartsWith(lines[0], 'NODEP')
935
 
        self.assertEqual(lines[1],
936
 
                         "    The feature 'Feature' is not available.")
 
893
        self.assertEqual(lines, ['NODEP        0ms',
 
894
                                 "    The feature 'Feature' is not available."])
937
895
 
938
896
    def test_unavailable_exception(self):
939
897
        """An UnavailableFeature being raised should invoke addNotSupported."""
940
898
        class InstrumentedTestResult(tests.ExtendedTestResult):
941
 
            def stopTestRun(self): pass
 
899
            def done(self): pass
942
900
            def startTests(self): pass
943
901
            def report_test_start(self, test): pass
944
902
            def addNotSupported(self, test, feature):
945
903
                self._call = test, feature
946
904
        result = InstrumentedTestResult(None, None, None, None)
947
905
        feature = tests.Feature()
948
 
        class Test(tests.TestCase):
949
 
            def test_function(self):
950
 
                raise tests.UnavailableFeature(feature)
951
 
        test = Test("test_function")
 
906
        def test_function():
 
907
            raise tests.UnavailableFeature(feature)
 
908
        test = unittest.FunctionTestCase(test_function)
952
909
        test.run(result)
953
910
        # it should invoke 'addNotSupported'.
954
911
        self.assertEqual(2, len(result._call))
955
 
        self.assertEqual(test.id(), result._call[0].id())
 
912
        self.assertEqual(test, result._call[0])
956
913
        self.assertEqual(feature, result._call[1])
957
914
        # and not count as an error
958
915
        self.assertEqual(0, result.error_count)
971
928
                                             verbosity=1)
972
929
        test = self.get_passing_test()
973
930
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
974
 
        result.addExpectedFailure(test, err)
 
931
        result._addKnownFailure(test, err)
975
932
        self.assertFalse(result.wasStrictlySuccessful())
976
933
        self.assertEqual(None, result._extractBenchmarkTime(test))
977
934
 
1022
979
        because of our use of global state.
1023
980
        """
1024
981
        old_root = tests.TestCaseInTempDir.TEST_ROOT
1025
 
        old_leak = tests.TestCase._first_thread_leaker_id
1026
982
        try:
1027
983
            tests.TestCaseInTempDir.TEST_ROOT = None
1028
 
            tests.TestCase._first_thread_leaker_id = None
1029
984
            return testrunner.run(test)
1030
985
        finally:
1031
986
            tests.TestCaseInTempDir.TEST_ROOT = old_root
1032
 
            tests.TestCase._first_thread_leaker_id = old_leak
1033
987
 
1034
988
    def test_known_failure_failed_run(self):
1035
989
        # run a test that generates a known failure which should be printed in
1036
990
        # the final output when real failures occur.
1037
 
        class Test(tests.TestCase):
1038
 
            def known_failure_test(self):
1039
 
                self.expectFailure('failed', self.assertTrue, False)
 
991
        def known_failure_test():
 
992
            raise tests.KnownFailure('failed')
1040
993
        test = unittest.TestSuite()
1041
 
        test.addTest(Test("known_failure_test"))
 
994
        test.addTest(unittest.FunctionTestCase(known_failure_test))
1042
995
        def failing_test():
1043
 
            self.fail('foo')
 
996
            raise AssertionError('foo')
1044
997
        test.addTest(unittest.FunctionTestCase(failing_test))
1045
998
        stream = StringIO()
1046
999
        runner = tests.TextTestRunner(stream=stream)
1047
1000
        result = self.run_test_runner(runner, test)
1048
1001
        lines = stream.getvalue().splitlines()
1049
1002
        self.assertContainsRe(stream.getvalue(),
1050
 
            '(?sm)^bzr selftest.*$'
 
1003
            '(?sm)^testing.*$'
1051
1004
            '.*'
1052
1005
            '^======================================================================\n'
1053
 
            '^FAIL: failing_test\n'
 
1006
            '^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1054
1007
            '^----------------------------------------------------------------------\n'
1055
1008
            'Traceback \\(most recent call last\\):\n'
1056
1009
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1057
 
            '    self.fail\\(\'foo\'\\)\n'
 
1010
            '    raise AssertionError\\(\'foo\'\\)\n'
1058
1011
            '.*'
1059
1012
            '^----------------------------------------------------------------------\n'
1060
1013
            '.*'
1062
1015
            )
1063
1016
 
1064
1017
    def test_known_failure_ok_run(self):
1065
 
        # run a test that generates a known failure which should be printed in
1066
 
        # the final output.
1067
 
        class Test(tests.TestCase):
1068
 
            def known_failure_test(self):
1069
 
                self.expectFailure('failed', self.assertTrue, False)
1070
 
        test = Test("known_failure_test")
 
1018
        # run a test that generates a known failure which should be printed in the final output.
 
1019
        def known_failure_test():
 
1020
            raise tests.KnownFailure('failed')
 
1021
        test = unittest.FunctionTestCase(known_failure_test)
1071
1022
        stream = StringIO()
1072
1023
        runner = tests.TextTestRunner(stream=stream)
1073
1024
        result = self.run_test_runner(runner, test)
1078
1029
            '\n'
1079
1030
            'OK \\(known_failures=1\\)\n')
1080
1031
 
1081
 
    def test_result_decorator(self):
1082
 
        # decorate results
1083
 
        calls = []
1084
 
        class LoggingDecorator(tests.ForwardingResult):
1085
 
            def startTest(self, test):
1086
 
                tests.ForwardingResult.startTest(self, test)
1087
 
                calls.append('start')
1088
 
        test = unittest.FunctionTestCase(lambda:None)
1089
 
        stream = StringIO()
1090
 
        runner = tests.TextTestRunner(stream=stream,
1091
 
            result_decorators=[LoggingDecorator])
1092
 
        result = self.run_test_runner(runner, test)
1093
 
        self.assertLength(1, calls)
1094
 
 
1095
1032
    def test_skipped_test(self):
1096
1033
        # run a test that is skipped, and check the suite as a whole still
1097
1034
        # succeeds.
1150
1087
 
1151
1088
    def test_not_applicable(self):
1152
1089
        # run a test that is skipped because it's not applicable
1153
 
        class Test(tests.TestCase):
1154
 
            def not_applicable_test(self):
1155
 
                raise tests.TestNotApplicable('this test never runs')
 
1090
        def not_applicable_test():
 
1091
            raise tests.TestNotApplicable('this test never runs')
1156
1092
        out = StringIO()
1157
1093
        runner = tests.TextTestRunner(stream=out, verbosity=2)
1158
 
        test = Test("not_applicable_test")
 
1094
        test = unittest.FunctionTestCase(not_applicable_test)
1159
1095
        result = self.run_test_runner(runner, test)
1160
1096
        self._log_file.write(out.getvalue())
1161
1097
        self.assertTrue(result.wasSuccessful())
1165
1101
        self.assertContainsRe(out.getvalue(),
1166
1102
                r'(?m)^    this test never runs')
1167
1103
 
 
1104
    def test_not_applicable_demo(self):
 
1105
        # just so you can see it in the test output
 
1106
        raise tests.TestNotApplicable('this test is just a demonstation')
 
1107
 
1168
1108
    def test_unsupported_features_listed(self):
1169
1109
        """When unsupported features are encountered they are detailed."""
1170
1110
        class Feature1(tests.Feature):
1190
1130
            ],
1191
1131
            lines[-3:])
1192
1132
 
1193
 
    def _patch_get_bzr_source_tree(self):
1194
 
        # Reading from the actual source tree breaks isolation, but we don't
1195
 
        # want to assume that thats *all* that would happen.
1196
 
        self._get_source_tree_calls = []
1197
 
        def new_get():
1198
 
            self._get_source_tree_calls.append("called")
1199
 
            return None
1200
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
1201
 
 
1202
1133
    def test_bench_history(self):
1203
 
        # tests that the running the benchmark passes bench_history into
1204
 
        # the test result object. We can tell that happens if
1205
 
        # _get_bzr_source_tree is called.
1206
 
        self._patch_get_bzr_source_tree()
 
1134
        # tests that the running the benchmark produces a history file
 
1135
        # containing a timestamp and the revision id of the bzrlib source which
 
1136
        # was tested.
 
1137
        workingtree = _get_bzr_source_tree()
1207
1138
        test = TestRunner('dummy_test')
1208
1139
        output = StringIO()
1209
1140
        runner = tests.TextTestRunner(stream=self._log_file,
1211
1142
        result = self.run_test_runner(runner, test)
1212
1143
        output_string = output.getvalue()
1213
1144
        self.assertContainsRe(output_string, "--date [0-9.]+")
1214
 
        self.assertLength(1, self._get_source_tree_calls)
1215
 
 
1216
 
    def test_startTestRun(self):
1217
 
        """run should call result.startTestRun()"""
1218
 
        calls = []
1219
 
        class LoggingDecorator(tests.ForwardingResult):
1220
 
            def startTestRun(self):
1221
 
                tests.ForwardingResult.startTestRun(self)
1222
 
                calls.append('startTestRun')
1223
 
        test = unittest.FunctionTestCase(lambda:None)
1224
 
        stream = StringIO()
1225
 
        runner = tests.TextTestRunner(stream=stream,
1226
 
            result_decorators=[LoggingDecorator])
1227
 
        result = self.run_test_runner(runner, test)
1228
 
        self.assertLength(1, calls)
1229
 
 
1230
 
    def test_stopTestRun(self):
1231
 
        """run should call result.stopTestRun()"""
1232
 
        calls = []
1233
 
        class LoggingDecorator(tests.ForwardingResult):
1234
 
            def stopTestRun(self):
1235
 
                tests.ForwardingResult.stopTestRun(self)
1236
 
                calls.append('stopTestRun')
1237
 
        test = unittest.FunctionTestCase(lambda:None)
1238
 
        stream = StringIO()
1239
 
        runner = tests.TextTestRunner(stream=stream,
1240
 
            result_decorators=[LoggingDecorator])
1241
 
        result = self.run_test_runner(runner, test)
1242
 
        self.assertLength(1, calls)
 
1145
        if workingtree is not None:
 
1146
            revision_id = workingtree.get_parent_ids()[0]
 
1147
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
1148
 
 
1149
    def assertLogDeleted(self, test):
 
1150
        log = test._get_log()
 
1151
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
1152
        self.assertEqual('', test._log_contents)
 
1153
        self.assertIs(None, test._log_file_name)
 
1154
 
 
1155
    def test_success_log_deleted(self):
 
1156
        """Successful tests have their log deleted"""
 
1157
 
 
1158
        class LogTester(tests.TestCase):
 
1159
 
 
1160
            def test_success(self):
 
1161
                self.log('this will be removed\n')
 
1162
 
 
1163
        sio = StringIO()
 
1164
        runner = tests.TextTestRunner(stream=sio)
 
1165
        test = LogTester('test_success')
 
1166
        result = self.run_test_runner(runner, test)
 
1167
 
 
1168
        self.assertLogDeleted(test)
 
1169
 
 
1170
    def test_skipped_log_deleted(self):
 
1171
        """Skipped tests have their log deleted"""
 
1172
 
 
1173
        class LogTester(tests.TestCase):
 
1174
 
 
1175
            def test_skipped(self):
 
1176
                self.log('this will be removed\n')
 
1177
                raise tests.TestSkipped()
 
1178
 
 
1179
        sio = StringIO()
 
1180
        runner = tests.TextTestRunner(stream=sio)
 
1181
        test = LogTester('test_skipped')
 
1182
        result = self.run_test_runner(runner, test)
 
1183
 
 
1184
        self.assertLogDeleted(test)
 
1185
 
 
1186
    def test_not_aplicable_log_deleted(self):
 
1187
        """Not applicable tests have their log deleted"""
 
1188
 
 
1189
        class LogTester(tests.TestCase):
 
1190
 
 
1191
            def test_not_applicable(self):
 
1192
                self.log('this will be removed\n')
 
1193
                raise tests.TestNotApplicable()
 
1194
 
 
1195
        sio = StringIO()
 
1196
        runner = tests.TextTestRunner(stream=sio)
 
1197
        test = LogTester('test_not_applicable')
 
1198
        result = self.run_test_runner(runner, test)
 
1199
 
 
1200
        self.assertLogDeleted(test)
 
1201
 
 
1202
    def test_known_failure_log_deleted(self):
 
1203
        """Know failure tests have their log deleted"""
 
1204
 
 
1205
        class LogTester(tests.TestCase):
 
1206
 
 
1207
            def test_known_failure(self):
 
1208
                self.log('this will be removed\n')
 
1209
                raise tests.KnownFailure()
 
1210
 
 
1211
        sio = StringIO()
 
1212
        runner = tests.TextTestRunner(stream=sio)
 
1213
        test = LogTester('test_known_failure')
 
1214
        result = self.run_test_runner(runner, test)
 
1215
 
 
1216
        self.assertLogDeleted(test)
 
1217
 
 
1218
    def test_fail_log_kept(self):
 
1219
        """Failed tests have their log kept"""
 
1220
 
 
1221
        class LogTester(tests.TestCase):
 
1222
 
 
1223
            def test_fail(self):
 
1224
                self.log('this will be kept\n')
 
1225
                self.fail('this test fails')
 
1226
 
 
1227
        sio = StringIO()
 
1228
        runner = tests.TextTestRunner(stream=sio)
 
1229
        test = LogTester('test_fail')
 
1230
        result = self.run_test_runner(runner, test)
 
1231
 
 
1232
        text = sio.getvalue()
 
1233
        self.assertContainsRe(text, 'this will be kept')
 
1234
        self.assertContainsRe(text, 'this test fails')
 
1235
 
 
1236
        log = test._get_log()
 
1237
        self.assertContainsRe(log, 'this will be kept')
 
1238
        self.assertEqual(log, test._log_contents)
 
1239
 
 
1240
    def test_error_log_kept(self):
 
1241
        """Tests with errors have their log kept"""
 
1242
 
 
1243
        class LogTester(tests.TestCase):
 
1244
 
 
1245
            def test_error(self):
 
1246
                self.log('this will be kept\n')
 
1247
                raise ValueError('random exception raised')
 
1248
 
 
1249
        sio = StringIO()
 
1250
        runner = tests.TextTestRunner(stream=sio)
 
1251
        test = LogTester('test_error')
 
1252
        result = self.run_test_runner(runner, test)
 
1253
 
 
1254
        text = sio.getvalue()
 
1255
        self.assertContainsRe(text, 'this will be kept')
 
1256
        self.assertContainsRe(text, 'random exception raised')
 
1257
 
 
1258
        log = test._get_log()
 
1259
        self.assertContainsRe(log, 'this will be kept')
 
1260
        self.assertEqual(log, test._log_contents)
1243
1261
 
1244
1262
 
1245
1263
class SampleTestCase(tests.TestCase):
1311
1329
        self.assertEqual(flags, bzrlib.debug.debug_flags)
1312
1330
 
1313
1331
    def change_selftest_debug_flags(self, new_flags):
1314
 
        self.overrideAttr(tests, 'selftest_debug_flags', set(new_flags))
 
1332
        orig_selftest_flags = tests.selftest_debug_flags
 
1333
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
 
1334
        tests.selftest_debug_flags = set(new_flags)
 
1335
 
 
1336
    def _restore_selftest_debug_flags(self, flags):
 
1337
        tests.selftest_debug_flags = flags
1315
1338
 
1316
1339
    def test_allow_debug_flag(self):
1317
1340
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1377
1400
        self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)
1378
1401
 
1379
1402
    def make_test_result(self):
1380
 
        """Get a test result that writes to the test log file."""
1381
1403
        return tests.TextTestResult(self._log_file, descriptions=0, verbosity=1)
1382
1404
 
1383
1405
    def inner_test(self):
1391
1413
        result = self.make_test_result()
1392
1414
        self.inner_test.run(result)
1393
1415
        note("outer finish")
1394
 
        self.addCleanup(osutils.delete_any, self._log_file_name)
1395
1416
 
1396
1417
    def test_trace_nesting(self):
1397
1418
        # this tests that each test case nests its trace facility correctly.
1457
1478
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1458
1479
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1459
1480
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1460
 
        del self._benchcalls[:]
1461
1481
 
1462
1482
    def test_knownFailure(self):
1463
1483
        """Self.knownFailure() should raise a KnownFailure exception."""
1464
1484
        self.assertRaises(tests.KnownFailure, self.knownFailure, "A Failure")
1465
1485
 
1466
 
    def test_open_bzrdir_safe_roots(self):
1467
 
        # even a memory transport should fail to open when its url isn't 
1468
 
        # permitted.
1469
 
        # Manually set one up (TestCase doesn't and shouldn't provide magic
1470
 
        # machinery)
1471
 
        transport_server = memory.MemoryServer()
1472
 
        transport_server.start_server()
1473
 
        self.addCleanup(transport_server.stop_server)
1474
 
        t = transport.get_transport(transport_server.get_url())
1475
 
        bzrdir.BzrDir.create(t.base)
1476
 
        self.assertRaises(errors.BzrError,
1477
 
            bzrdir.BzrDir.open_from_transport, t)
1478
 
        # But if we declare this as safe, we can open the bzrdir.
1479
 
        self.permit_url(t.base)
1480
 
        self._bzr_selftest_roots.append(t.base)
1481
 
        bzrdir.BzrDir.open_from_transport(t)
1482
 
 
1483
1486
    def test_requireFeature_available(self):
1484
1487
        """self.requireFeature(available) is a no-op."""
1485
1488
        class Available(tests.Feature):
1529
1532
        """Test disabled tests behaviour with support aware results."""
1530
1533
        test = SampleTestCase('_test_pass')
1531
1534
        class DisabledFeature(object):
1532
 
            def __eq__(self, other):
1533
 
                return isinstance(other, DisabledFeature)
1534
1535
            def available(self):
1535
1536
                return False
1536
1537
        the_feature = DisabledFeature()
1547
1548
                self.calls.append(('addNotSupported', test, feature))
1548
1549
        result = InstrumentedTestResult()
1549
1550
        test.run(result)
1550
 
        case = result.calls[0][1]
1551
1551
        self.assertEqual([
1552
 
            ('startTest', case),
1553
 
            ('addNotSupported', case, the_feature),
1554
 
            ('stopTest', case),
 
1552
            ('startTest', test),
 
1553
            ('addNotSupported', test, the_feature),
 
1554
            ('stopTest', test),
1555
1555
            ],
1556
1556
            result.calls)
1557
1557
 
1558
 
    def test_start_server_registers_url(self):
1559
 
        transport_server = memory.MemoryServer()
1560
 
        # A little strict, but unlikely to be changed soon.
1561
 
        self.assertEqual([], self._bzr_selftest_roots)
1562
 
        self.start_server(transport_server)
1563
 
        self.assertSubset([transport_server.get_url()],
1564
 
            self._bzr_selftest_roots)
1565
 
 
1566
1558
    def test_assert_list_raises_on_generator(self):
1567
1559
        def generator_which_will_raise():
1568
1560
            # This will not raise until after the first yield
1618
1610
        self.assertRaises(AssertionError,
1619
1611
            self.assertListRaises, _TestException, success_generator)
1620
1612
 
1621
 
    def test_overrideAttr_without_value(self):
1622
 
        self.test_attr = 'original' # Define a test attribute
1623
 
        obj = self # Make 'obj' visible to the embedded test
1624
 
        class Test(tests.TestCase):
1625
 
 
1626
 
            def setUp(self):
1627
 
                tests.TestCase.setUp(self)
1628
 
                self.orig = self.overrideAttr(obj, 'test_attr')
1629
 
 
1630
 
            def test_value(self):
1631
 
                self.assertEqual('original', self.orig)
1632
 
                self.assertEqual('original', obj.test_attr)
1633
 
                obj.test_attr = 'modified'
1634
 
                self.assertEqual('modified', obj.test_attr)
1635
 
 
1636
 
        test = Test('test_value')
1637
 
        test.run(unittest.TestResult())
1638
 
        self.assertEqual('original', obj.test_attr)
1639
 
 
1640
 
    def test_overrideAttr_with_value(self):
1641
 
        self.test_attr = 'original' # Define a test attribute
1642
 
        obj = self # Make 'obj' visible to the embedded test
1643
 
        class Test(tests.TestCase):
1644
 
 
1645
 
            def setUp(self):
1646
 
                tests.TestCase.setUp(self)
1647
 
                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
1648
 
 
1649
 
            def test_value(self):
1650
 
                self.assertEqual('original', self.orig)
1651
 
                self.assertEqual('modified', obj.test_attr)
1652
 
 
1653
 
        test = Test('test_value')
1654
 
        test.run(unittest.TestResult())
1655
 
        self.assertEqual('original', obj.test_attr)
1656
 
 
1657
1613
 
1658
1614
# NB: Don't delete this; it's not actually from 0.11!
1659
1615
@deprecated_function(deprecated_in((0, 11, 0)))
1702
1658
        self.assertEndsWith('foo', 'oo')
1703
1659
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1704
1660
 
1705
 
    def test_assertEqualDiff(self):
1706
 
        e = self.assertRaises(AssertionError,
1707
 
                              self.assertEqualDiff, '', '\n')
1708
 
        self.assertEquals(str(e),
1709
 
                          # Don't blink ! The '+' applies to the second string
1710
 
                          'first string is missing a final newline.\n+ \n')
1711
 
        e = self.assertRaises(AssertionError,
1712
 
                              self.assertEqualDiff, '\n', '')
1713
 
        self.assertEquals(str(e),
1714
 
                          # Don't blink ! The '-' applies to the second string
1715
 
                          'second string is missing a final newline.\n- \n')
1716
 
 
1717
 
 
1718
 
class TestDeprecations(tests.TestCase):
1719
 
 
1720
1661
    def test_applyDeprecated_not_deprecated(self):
1721
1662
        sample_object = ApplyDeprecatedHelper()
1722
1663
        # calling an undeprecated callable raises an assertion
1799
1740
        tree = self.make_branch_and_memory_tree('a')
1800
1741
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1801
1742
 
1802
 
    def test_make_tree_for_local_vfs_backed_transport(self):
1803
 
        # make_branch_and_tree has to use local branch and repositories
1804
 
        # when the vfs transport and local disk are colocated, even if
1805
 
        # a different transport is in use for url generation.
1806
 
        self.transport_server = test_server.FakeVFATServer
1807
 
        self.assertFalse(self.get_url('t1').startswith('file://'))
 
1743
 
 
1744
class TestSFTPMakeBranchAndTree(test_sftp_transport.TestCaseWithSFTPServer):
 
1745
 
 
1746
    def test_make_tree_for_sftp_branch(self):
 
1747
        """Transports backed by local directories create local trees."""
 
1748
 
1808
1749
        tree = self.make_branch_and_tree('t1')
1809
1750
        base = tree.bzrdir.root_transport.base
1810
 
        self.assertStartsWith(base, 'file://')
 
1751
        self.failIf(base.startswith('sftp'),
 
1752
                'base %r is on sftp but should be local' % base)
1811
1753
        self.assertEquals(tree.bzrdir.root_transport,
1812
1754
                tree.branch.bzrdir.root_transport)
1813
1755
        self.assertEquals(tree.bzrdir.root_transport,
1814
1756
                tree.branch.repository.bzrdir.root_transport)
1815
1757
 
1816
1758
 
1817
 
class SelfTestHelper:
1818
 
 
1819
 
    def run_selftest(self, **kwargs):
1820
 
        """Run selftest returning its output."""
1821
 
        output = StringIO()
1822
 
        old_transport = bzrlib.tests.default_transport
1823
 
        old_root = tests.TestCaseWithMemoryTransport.TEST_ROOT
1824
 
        tests.TestCaseWithMemoryTransport.TEST_ROOT = None
1825
 
        try:
1826
 
            self.assertEqual(True, tests.selftest(stream=output, **kwargs))
1827
 
        finally:
1828
 
            bzrlib.tests.default_transport = old_transport
1829
 
            tests.TestCaseWithMemoryTransport.TEST_ROOT = old_root
1830
 
        output.seek(0)
1831
 
        return output
1832
 
 
1833
 
 
1834
 
class TestSelftest(tests.TestCase, SelfTestHelper):
 
1759
class TestSelftest(tests.TestCase):
1835
1760
    """Tests of bzrlib.tests.selftest."""
1836
1761
 
1837
1762
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1845
1770
            test_suite_factory=factory)
1846
1771
        self.assertEqual([True], factory_called)
1847
1772
 
1848
 
    def factory(self):
1849
 
        """A test suite factory."""
1850
 
        class Test(tests.TestCase):
1851
 
            def a(self):
1852
 
                pass
1853
 
            def b(self):
1854
 
                pass
1855
 
            def c(self):
1856
 
                pass
1857
 
        return TestUtil.TestSuite([Test("a"), Test("b"), Test("c")])
1858
 
 
1859
 
    def test_list_only(self):
1860
 
        output = self.run_selftest(test_suite_factory=self.factory,
1861
 
            list_only=True)
1862
 
        self.assertEqual(3, len(output.readlines()))
1863
 
 
1864
 
    def test_list_only_filtered(self):
1865
 
        output = self.run_selftest(test_suite_factory=self.factory,
1866
 
            list_only=True, pattern="Test.b")
1867
 
        self.assertEndsWith(output.getvalue(), "Test.b\n")
1868
 
        self.assertLength(1, output.readlines())
1869
 
 
1870
 
    def test_list_only_excludes(self):
1871
 
        output = self.run_selftest(test_suite_factory=self.factory,
1872
 
            list_only=True, exclude_pattern="Test.b")
1873
 
        self.assertNotContainsRe("Test.b", output.getvalue())
1874
 
        self.assertLength(2, output.readlines())
1875
 
 
1876
 
    def test_lsprof_tests(self):
1877
 
        self.requireFeature(test_lsprof.LSProfFeature)
1878
 
        calls = []
1879
 
        class Test(object):
1880
 
            def __call__(test, result):
1881
 
                test.run(result)
1882
 
            def run(test, result):
1883
 
                self.assertIsInstance(result, tests.ForwardingResult)
1884
 
                calls.append("called")
1885
 
            def countTestCases(self):
1886
 
                return 1
1887
 
        self.run_selftest(test_suite_factory=Test, lsprof_tests=True)
1888
 
        self.assertLength(1, calls)
1889
 
 
1890
 
    def test_random(self):
1891
 
        # test randomising by listing a number of tests.
1892
 
        output_123 = self.run_selftest(test_suite_factory=self.factory,
1893
 
            list_only=True, random_seed="123")
1894
 
        output_234 = self.run_selftest(test_suite_factory=self.factory,
1895
 
            list_only=True, random_seed="234")
1896
 
        self.assertNotEqual(output_123, output_234)
1897
 
        # "Randominzing test order..\n\n
1898
 
        self.assertLength(5, output_123.readlines())
1899
 
        self.assertLength(5, output_234.readlines())
1900
 
 
1901
 
    def test_random_reuse_is_same_order(self):
1902
 
        # test randomising by listing a number of tests.
1903
 
        expected = self.run_selftest(test_suite_factory=self.factory,
1904
 
            list_only=True, random_seed="123")
1905
 
        repeated = self.run_selftest(test_suite_factory=self.factory,
1906
 
            list_only=True, random_seed="123")
1907
 
        self.assertEqual(expected.getvalue(), repeated.getvalue())
1908
 
 
1909
 
    def test_runner_class(self):
1910
 
        self.requireFeature(features.subunit)
1911
 
        from subunit import ProtocolTestCase
1912
 
        stream = self.run_selftest(runner_class=tests.SubUnitBzrRunner,
1913
 
            test_suite_factory=self.factory)
1914
 
        test = ProtocolTestCase(stream)
1915
 
        result = unittest.TestResult()
1916
 
        test.run(result)
1917
 
        self.assertEqual(3, result.testsRun)
1918
 
 
1919
 
    def test_starting_with_single_argument(self):
1920
 
        output = self.run_selftest(test_suite_factory=self.factory,
1921
 
            starting_with=['bzrlib.tests.test_selftest.Test.a'],
1922
 
            list_only=True)
1923
 
        self.assertEqual('bzrlib.tests.test_selftest.Test.a\n',
1924
 
            output.getvalue())
1925
 
 
1926
 
    def test_starting_with_multiple_argument(self):
1927
 
        output = self.run_selftest(test_suite_factory=self.factory,
1928
 
            starting_with=['bzrlib.tests.test_selftest.Test.a',
1929
 
                'bzrlib.tests.test_selftest.Test.b'],
1930
 
            list_only=True)
1931
 
        self.assertEqual('bzrlib.tests.test_selftest.Test.a\n'
1932
 
            'bzrlib.tests.test_selftest.Test.b\n',
1933
 
            output.getvalue())
1934
 
 
1935
 
    def check_transport_set(self, transport_server):
1936
 
        captured_transport = []
1937
 
        def seen_transport(a_transport):
1938
 
            captured_transport.append(a_transport)
1939
 
        class Capture(tests.TestCase):
1940
 
            def a(self):
1941
 
                seen_transport(bzrlib.tests.default_transport)
1942
 
        def factory():
1943
 
            return TestUtil.TestSuite([Capture("a")])
1944
 
        self.run_selftest(transport=transport_server, test_suite_factory=factory)
1945
 
        self.assertEqual(transport_server, captured_transport[0])
1946
 
 
1947
 
    def test_transport_sftp(self):
1948
 
        self.requireFeature(features.paramiko)
1949
 
        from bzrlib.tests import stub_sftp
1950
 
        self.check_transport_set(stub_sftp.SFTPAbsoluteServer)
1951
 
 
1952
 
    def test_transport_memory(self):
1953
 
        self.check_transport_set(memory.MemoryServer)
1954
 
 
1955
 
 
1956
 
class TestSelftestWithIdList(tests.TestCaseInTempDir, SelfTestHelper):
1957
 
    # Does IO: reads test.list
1958
 
 
1959
 
    def test_load_list(self):
1960
 
        # Provide a list with one test - this test.
1961
 
        test_id_line = '%s\n' % self.id()
1962
 
        self.build_tree_contents([('test.list', test_id_line)])
1963
 
        # And generate a list of the tests in  the suite.
1964
 
        stream = self.run_selftest(load_list='test.list', list_only=True)
1965
 
        self.assertEqual(test_id_line, stream.getvalue())
1966
 
 
1967
 
    def test_load_unknown(self):
1968
 
        # Provide a list with one test - this test.
1969
 
        # And generate a list of the tests in  the suite.
1970
 
        err = self.assertRaises(errors.NoSuchFile, self.run_selftest,
1971
 
            load_list='missing file name', list_only=True)
1972
 
 
1973
 
 
1974
 
class TestRunBzr(tests.TestCase):
1975
 
 
1976
 
    out = ''
1977
 
    err = ''
1978
 
 
1979
 
    def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
1980
 
                         working_dir=None):
1981
 
        """Override _run_bzr_core to test how it is invoked by run_bzr.
1982
 
 
1983
 
        Attempts to run bzr from inside this class don't actually run it.
1984
 
 
1985
 
        We test how run_bzr actually invokes bzr in another location.  Here we
1986
 
        only need to test that it passes the right parameters to run_bzr.
1987
 
        """
1988
 
        self.argv = list(argv)
1989
 
        self.retcode = retcode
1990
 
        self.encoding = encoding
1991
 
        self.stdin = stdin
1992
 
        self.working_dir = working_dir
1993
 
        return self.retcode, self.out, self.err
1994
 
 
1995
 
    def test_run_bzr_error(self):
1996
 
        self.out = "It sure does!\n"
1997
 
        out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=34)
1998
 
        self.assertEqual(['rocks'], self.argv)
1999
 
        self.assertEqual(34, self.retcode)
2000
 
        self.assertEqual('It sure does!\n', out)
2001
 
        self.assertEquals(out, self.out)
2002
 
        self.assertEqual('', err)
2003
 
        self.assertEquals(err, self.err)
2004
 
 
2005
 
    def test_run_bzr_error_regexes(self):
2006
 
        self.out = ''
2007
 
        self.err = "bzr: ERROR: foobarbaz is not versioned"
2008
 
        out, err = self.run_bzr_error(
2009
 
            ["bzr: ERROR: foobarbaz is not versioned"],
2010
 
            ['file-id', 'foobarbaz'])
2011
 
 
2012
 
    def test_encoding(self):
2013
 
        """Test that run_bzr passes encoding to _run_bzr_core"""
2014
 
        self.run_bzr('foo bar')
2015
 
        self.assertEqual(None, self.encoding)
2016
 
        self.assertEqual(['foo', 'bar'], self.argv)
2017
 
 
2018
 
        self.run_bzr('foo bar', encoding='baz')
2019
 
        self.assertEqual('baz', self.encoding)
2020
 
        self.assertEqual(['foo', 'bar'], self.argv)
2021
 
 
2022
 
    def test_retcode(self):
2023
 
        """Test that run_bzr passes retcode to _run_bzr_core"""
2024
 
        # Default is retcode == 0
2025
 
        self.run_bzr('foo bar')
2026
 
        self.assertEqual(0, self.retcode)
2027
 
        self.assertEqual(['foo', 'bar'], self.argv)
2028
 
 
2029
 
        self.run_bzr('foo bar', retcode=1)
2030
 
        self.assertEqual(1, self.retcode)
2031
 
        self.assertEqual(['foo', 'bar'], self.argv)
2032
 
 
2033
 
        self.run_bzr('foo bar', retcode=None)
2034
 
        self.assertEqual(None, self.retcode)
2035
 
        self.assertEqual(['foo', 'bar'], self.argv)
2036
 
 
2037
 
        self.run_bzr(['foo', 'bar'], retcode=3)
2038
 
        self.assertEqual(3, self.retcode)
2039
 
        self.assertEqual(['foo', 'bar'], self.argv)
2040
 
 
2041
 
    def test_stdin(self):
2042
 
        # test that the stdin keyword to run_bzr is passed through to
2043
 
        # _run_bzr_core as-is. We do this by overriding
2044
 
        # _run_bzr_core in this class, and then calling run_bzr,
2045
 
        # which is a convenience function for _run_bzr_core, so
2046
 
        # should invoke it.
2047
 
        self.run_bzr('foo bar', stdin='gam')
2048
 
        self.assertEqual('gam', self.stdin)
2049
 
        self.assertEqual(['foo', 'bar'], self.argv)
2050
 
 
2051
 
        self.run_bzr('foo bar', stdin='zippy')
2052
 
        self.assertEqual('zippy', self.stdin)
2053
 
        self.assertEqual(['foo', 'bar'], self.argv)
2054
 
 
2055
 
    def test_working_dir(self):
2056
 
        """Test that run_bzr passes working_dir to _run_bzr_core"""
2057
 
        self.run_bzr('foo bar')
2058
 
        self.assertEqual(None, self.working_dir)
2059
 
        self.assertEqual(['foo', 'bar'], self.argv)
2060
 
 
2061
 
        self.run_bzr('foo bar', working_dir='baz')
2062
 
        self.assertEqual('baz', self.working_dir)
2063
 
        self.assertEqual(['foo', 'bar'], self.argv)
2064
 
 
2065
 
    def test_reject_extra_keyword_arguments(self):
2066
 
        self.assertRaises(TypeError, self.run_bzr, "foo bar",
2067
 
                          error_regex=['error message'])
2068
 
 
2069
 
 
2070
 
class TestRunBzrCaptured(tests.TestCaseWithTransport):
2071
 
    # Does IO when testing the working_dir parameter.
2072
 
 
2073
 
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
2074
 
                         a_callable=None, *args, **kwargs):
2075
 
        self.stdin = stdin
2076
 
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
2077
 
        self.factory = bzrlib.ui.ui_factory
2078
 
        self.working_dir = osutils.getcwd()
2079
 
        stdout.write('foo\n')
2080
 
        stderr.write('bar\n')
2081
 
        return 0
2082
 
 
2083
 
    def test_stdin(self):
2084
 
        # test that the stdin keyword to _run_bzr_core is passed through to
2085
 
        # apply_redirected as a StringIO. We do this by overriding
2086
 
        # apply_redirected in this class, and then calling _run_bzr_core,
2087
 
        # which calls apply_redirected.
2088
 
        self.run_bzr(['foo', 'bar'], stdin='gam')
2089
 
        self.assertEqual('gam', self.stdin.read())
2090
 
        self.assertTrue(self.stdin is self.factory_stdin)
2091
 
        self.run_bzr(['foo', 'bar'], stdin='zippy')
2092
 
        self.assertEqual('zippy', self.stdin.read())
2093
 
        self.assertTrue(self.stdin is self.factory_stdin)
2094
 
 
2095
 
    def test_ui_factory(self):
2096
 
        # each invocation of self.run_bzr should get its
2097
 
        # own UI factory, which is an instance of TestUIFactory,
2098
 
        # with stdin, stdout and stderr attached to the stdin,
2099
 
        # stdout and stderr of the invoked run_bzr
2100
 
        current_factory = bzrlib.ui.ui_factory
2101
 
        self.run_bzr(['foo'])
2102
 
        self.failIf(current_factory is self.factory)
2103
 
        self.assertNotEqual(sys.stdout, self.factory.stdout)
2104
 
        self.assertNotEqual(sys.stderr, self.factory.stderr)
2105
 
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
2106
 
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
2107
 
        self.assertIsInstance(self.factory, tests.TestUIFactory)
2108
 
 
2109
 
    def test_working_dir(self):
2110
 
        self.build_tree(['one/', 'two/'])
2111
 
        cwd = osutils.getcwd()
2112
 
 
2113
 
        # Default is to work in the current directory
2114
 
        self.run_bzr(['foo', 'bar'])
2115
 
        self.assertEqual(cwd, self.working_dir)
2116
 
 
2117
 
        self.run_bzr(['foo', 'bar'], working_dir=None)
2118
 
        self.assertEqual(cwd, self.working_dir)
2119
 
 
2120
 
        # The function should be run in the alternative directory
2121
 
        # but afterwards the current working dir shouldn't be changed
2122
 
        self.run_bzr(['foo', 'bar'], working_dir='one')
2123
 
        self.assertNotEqual(cwd, self.working_dir)
2124
 
        self.assertEndsWith(self.working_dir, 'one')
2125
 
        self.assertEqual(cwd, osutils.getcwd())
2126
 
 
2127
 
        self.run_bzr(['foo', 'bar'], working_dir='two')
2128
 
        self.assertNotEqual(cwd, self.working_dir)
2129
 
        self.assertEndsWith(self.working_dir, 'two')
2130
 
        self.assertEqual(cwd, osutils.getcwd())
2131
 
 
2132
 
 
2133
 
class StubProcess(object):
2134
 
    """A stub process for testing run_bzr_subprocess."""
2135
 
    
2136
 
    def __init__(self, out="", err="", retcode=0):
2137
 
        self.out = out
2138
 
        self.err = err
2139
 
        self.returncode = retcode
2140
 
 
2141
 
    def communicate(self):
2142
 
        return self.out, self.err
2143
 
 
2144
 
 
2145
 
class TestWithFakedStartBzrSubprocess(tests.TestCaseWithTransport):
2146
 
    """Base class for tests testing how we might run bzr."""
2147
 
 
2148
 
    def setUp(self):
2149
 
        tests.TestCaseWithTransport.setUp(self)
2150
 
        self.subprocess_calls = []
2151
 
 
2152
 
    def start_bzr_subprocess(self, process_args, env_changes=None,
2153
 
                             skip_if_plan_to_signal=False,
2154
 
                             working_dir=None,
2155
 
                             allow_plugins=False):
2156
 
        """capture what run_bzr_subprocess tries to do."""
2157
 
        self.subprocess_calls.append({'process_args':process_args,
2158
 
            'env_changes':env_changes,
2159
 
            'skip_if_plan_to_signal':skip_if_plan_to_signal,
2160
 
            'working_dir':working_dir, 'allow_plugins':allow_plugins})
2161
 
        return self.next_subprocess
2162
 
 
2163
 
 
2164
 
class TestRunBzrSubprocess(TestWithFakedStartBzrSubprocess):
2165
 
 
2166
 
    def assertRunBzrSubprocess(self, expected_args, process, *args, **kwargs):
2167
 
        """Run run_bzr_subprocess with args and kwargs using a stubbed process.
2168
 
 
2169
 
        Inside TestRunBzrSubprocessCommands we use a stub start_bzr_subprocess
2170
 
        that will return static results. This assertion method populates those
2171
 
        results and also checks the arguments run_bzr_subprocess generates.
2172
 
        """
2173
 
        self.next_subprocess = process
2174
 
        try:
2175
 
            result = self.run_bzr_subprocess(*args, **kwargs)
2176
 
        except:
2177
 
            self.next_subprocess = None
2178
 
            for key, expected in expected_args.iteritems():
2179
 
                self.assertEqual(expected, self.subprocess_calls[-1][key])
2180
 
            raise
 
1773
 
 
1774
class TestKnownFailure(tests.TestCase):
 
1775
 
 
1776
    def test_known_failure(self):
 
1777
        """Check that KnownFailure is defined appropriately."""
 
1778
        # a KnownFailure is an assertion error for compatability with unaware
 
1779
        # runners.
 
1780
        self.assertIsInstance(tests.KnownFailure(""), AssertionError)
 
1781
 
 
1782
    def test_expect_failure(self):
 
1783
        try:
 
1784
            self.expectFailure("Doomed to failure", self.assertTrue, False)
 
1785
        except tests.KnownFailure, e:
 
1786
            self.assertEqual('Doomed to failure', e.args[0])
 
1787
        try:
 
1788
            self.expectFailure("Doomed to failure", self.assertTrue, True)
 
1789
        except AssertionError, e:
 
1790
            self.assertEqual('Unexpected success.  Should have failed:'
 
1791
                             ' Doomed to failure', e.args[0])
2181
1792
        else:
2182
 
            self.next_subprocess = None
2183
 
            for key, expected in expected_args.iteritems():
2184
 
                self.assertEqual(expected, self.subprocess_calls[-1][key])
2185
 
            return result
2186
 
 
2187
 
    def test_run_bzr_subprocess(self):
2188
 
        """The run_bzr_helper_external command behaves nicely."""
2189
 
        self.assertRunBzrSubprocess({'process_args':['--version']},
2190
 
            StubProcess(), '--version')
2191
 
        self.assertRunBzrSubprocess({'process_args':['--version']},
2192
 
            StubProcess(), ['--version'])
2193
 
        # retcode=None disables retcode checking
2194
 
        result = self.assertRunBzrSubprocess({},
2195
 
            StubProcess(retcode=3), '--version', retcode=None)
2196
 
        result = self.assertRunBzrSubprocess({},
2197
 
            StubProcess(out="is free software"), '--version')
2198
 
        self.assertContainsRe(result[0], 'is free software')
2199
 
        # Running a subcommand that is missing errors
2200
 
        self.assertRaises(AssertionError, self.assertRunBzrSubprocess,
2201
 
            {'process_args':['--versionn']}, StubProcess(retcode=3),
2202
 
            '--versionn')
2203
 
        # Unless it is told to expect the error from the subprocess
2204
 
        result = self.assertRunBzrSubprocess({},
2205
 
            StubProcess(retcode=3), '--versionn', retcode=3)
2206
 
        # Or to ignore retcode checking
2207
 
        result = self.assertRunBzrSubprocess({},
2208
 
            StubProcess(err="unknown command", retcode=3), '--versionn',
2209
 
            retcode=None)
2210
 
        self.assertContainsRe(result[1], 'unknown command')
2211
 
 
2212
 
    def test_env_change_passes_through(self):
2213
 
        self.assertRunBzrSubprocess(
2214
 
            {'env_changes':{'new':'value', 'changed':'newvalue', 'deleted':None}},
2215
 
            StubProcess(), '',
2216
 
            env_changes={'new':'value', 'changed':'newvalue', 'deleted':None})
2217
 
 
2218
 
    def test_no_working_dir_passed_as_None(self):
2219
 
        self.assertRunBzrSubprocess({'working_dir': None}, StubProcess(), '')
2220
 
 
2221
 
    def test_no_working_dir_passed_through(self):
2222
 
        self.assertRunBzrSubprocess({'working_dir': 'dir'}, StubProcess(), '',
2223
 
            working_dir='dir')
2224
 
 
2225
 
    def test_run_bzr_subprocess_no_plugins(self):
2226
 
        self.assertRunBzrSubprocess({'allow_plugins': False},
2227
 
            StubProcess(), '')
2228
 
 
2229
 
    def test_allow_plugins(self):
2230
 
        self.assertRunBzrSubprocess({'allow_plugins': True},
2231
 
            StubProcess(), '', allow_plugins=True)
2232
 
 
2233
 
 
2234
 
class TestFinishBzrSubprocess(TestWithFakedStartBzrSubprocess):
2235
 
 
2236
 
    def test_finish_bzr_subprocess_with_error(self):
2237
 
        """finish_bzr_subprocess allows specification of the desired exit code.
2238
 
        """
2239
 
        process = StubProcess(err="unknown command", retcode=3)
2240
 
        result = self.finish_bzr_subprocess(process, retcode=3)
2241
 
        self.assertEqual('', result[0])
2242
 
        self.assertContainsRe(result[1], 'unknown command')
2243
 
 
2244
 
    def test_finish_bzr_subprocess_ignoring_retcode(self):
2245
 
        """finish_bzr_subprocess allows the exit code to be ignored."""
2246
 
        process = StubProcess(err="unknown command", retcode=3)
2247
 
        result = self.finish_bzr_subprocess(process, retcode=None)
2248
 
        self.assertEqual('', result[0])
2249
 
        self.assertContainsRe(result[1], 'unknown command')
2250
 
 
2251
 
    def test_finish_subprocess_with_unexpected_retcode(self):
2252
 
        """finish_bzr_subprocess raises self.failureException if the retcode is
2253
 
        not the expected one.
2254
 
        """
2255
 
        process = StubProcess(err="unknown command", retcode=3)
2256
 
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
2257
 
                          process)
2258
 
 
2259
 
 
2260
 
class _DontSpawnProcess(Exception):
2261
 
    """A simple exception which just allows us to skip unnecessary steps"""
2262
 
 
2263
 
 
2264
 
class TestStartBzrSubProcess(tests.TestCase):
2265
 
 
2266
 
    def check_popen_state(self):
2267
 
        """Replace to make assertions when popen is called."""
2268
 
 
2269
 
    def _popen(self, *args, **kwargs):
2270
 
        """Record the command that is run, so that we can ensure it is correct"""
2271
 
        self.check_popen_state()
2272
 
        self._popen_args = args
2273
 
        self._popen_kwargs = kwargs
2274
 
        raise _DontSpawnProcess()
2275
 
 
2276
 
    def test_run_bzr_subprocess_no_plugins(self):
2277
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [])
2278
 
        command = self._popen_args[0]
2279
 
        self.assertEqual(sys.executable, command[0])
2280
 
        self.assertEqual(self.get_bzr_path(), command[1])
2281
 
        self.assertEqual(['--no-plugins'], command[2:])
2282
 
 
2283
 
    def test_allow_plugins(self):
2284
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2285
 
            allow_plugins=True)
2286
 
        command = self._popen_args[0]
2287
 
        self.assertEqual([], command[2:])
2288
 
 
2289
 
    def test_set_env(self):
2290
 
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2291
 
        # set in the child
2292
 
        def check_environment():
2293
 
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2294
 
        self.check_popen_state = check_environment
2295
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2296
 
            env_changes={'EXISTANT_ENV_VAR':'set variable'})
2297
 
        # not set in theparent
2298
 
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2299
 
 
2300
 
    def test_run_bzr_subprocess_env_del(self):
2301
 
        """run_bzr_subprocess can remove environment variables too."""
2302
 
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2303
 
        def check_environment():
2304
 
            self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2305
 
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2306
 
        self.check_popen_state = check_environment
2307
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2308
 
            env_changes={'EXISTANT_ENV_VAR':None})
2309
 
        # Still set in parent
2310
 
        self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2311
 
        del os.environ['EXISTANT_ENV_VAR']
2312
 
 
2313
 
    def test_env_del_missing(self):
2314
 
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
2315
 
        def check_environment():
2316
 
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2317
 
        self.check_popen_state = check_environment
2318
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2319
 
            env_changes={'NON_EXISTANT_ENV_VAR':None})
2320
 
 
2321
 
    def test_working_dir(self):
2322
 
        """Test that we can specify the working dir for the child"""
2323
 
        orig_getcwd = osutils.getcwd
2324
 
        orig_chdir = os.chdir
2325
 
        chdirs = []
2326
 
        def chdir(path):
2327
 
            chdirs.append(path)
2328
 
        os.chdir = chdir
2329
 
        try:
2330
 
            def getcwd():
2331
 
                return 'current'
2332
 
            osutils.getcwd = getcwd
2333
 
            try:
2334
 
                self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2335
 
                    working_dir='foo')
2336
 
            finally:
2337
 
                osutils.getcwd = orig_getcwd
2338
 
        finally:
2339
 
            os.chdir = orig_chdir
2340
 
        self.assertEqual(['foo', 'current'], chdirs)
2341
 
 
2342
 
 
2343
 
class TestActuallyStartBzrSubprocess(tests.TestCaseWithTransport):
2344
 
    """Tests that really need to do things with an external bzr."""
2345
 
 
2346
 
    def test_start_and_stop_bzr_subprocess_send_signal(self):
2347
 
        """finish_bzr_subprocess raises self.failureException if the retcode is
2348
 
        not the expected one.
2349
 
        """
2350
 
        self.disable_missing_extensions_warning()
2351
 
        process = self.start_bzr_subprocess(['wait-until-signalled'],
2352
 
                                            skip_if_plan_to_signal=True)
2353
 
        self.assertEqual('running\n', process.stdout.readline())
2354
 
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
2355
 
                                            retcode=3)
2356
 
        self.assertEqual('', result[0])
2357
 
        self.assertEqual('bzr: interrupted\n', result[1])
 
1793
            self.fail('Assertion not raised')
2358
1794
 
2359
1795
 
2360
1796
class TestFeature(tests.TestCase):
2398
1834
        self.assertIs(feature, exception.args[0])
2399
1835
 
2400
1836
 
2401
 
simple_thunk_feature = tests._CompatabilityThunkFeature(
2402
 
    deprecated_in((2, 1, 0)),
2403
 
    'bzrlib.tests.test_selftest',
2404
 
    'simple_thunk_feature','UnicodeFilename',
2405
 
    replacement_module='bzrlib.tests'
2406
 
    )
2407
 
 
2408
 
class Test_CompatibilityFeature(tests.TestCase):
2409
 
 
2410
 
    def test_does_thunk(self):
2411
 
        res = self.callDeprecated(
2412
 
            ['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2413
 
             ' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2414
 
            simple_thunk_feature.available)
2415
 
        self.assertEqual(tests.UnicodeFilename.available(), res)
2416
 
 
2417
 
 
2418
 
class TestModuleAvailableFeature(tests.TestCase):
2419
 
 
2420
 
    def test_available_module(self):
2421
 
        feature = tests.ModuleAvailableFeature('bzrlib.tests')
2422
 
        self.assertEqual('bzrlib.tests', feature.module_name)
2423
 
        self.assertEqual('bzrlib.tests', str(feature))
2424
 
        self.assertTrue(feature.available())
2425
 
        self.assertIs(tests, feature.module)
2426
 
 
2427
 
    def test_unavailable_module(self):
2428
 
        feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2429
 
        self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2430
 
        self.assertFalse(feature.available())
2431
 
        self.assertIs(None, feature.module)
2432
 
 
2433
 
 
2434
1837
class TestSelftestFiltering(tests.TestCase):
2435
1838
 
2436
1839
    def setUp(self):
2437
1840
        tests.TestCase.setUp(self)
2438
1841
        self.suite = TestUtil.TestSuite()
2439
1842
        self.loader = TestUtil.TestLoader()
2440
 
        self.suite.addTest(self.loader.loadTestsFromModule(
2441
 
            sys.modules['bzrlib.tests.test_selftest']))
 
1843
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
 
1844
            'bzrlib.tests.test_selftest']))
2442
1845
        self.all_names = _test_ids(self.suite)
2443
1846
 
2444
1847
    def test_condition_id_re(self):
2615
2018
        # the test framework
2616
2019
        self.assertEquals('always fails', str(e))
2617
2020
        # check that there's no traceback in the test log
2618
 
        self.assertNotContainsRe(self.get_log(), r'Traceback')
 
2021
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
 
2022
            r'Traceback')
2619
2023
 
2620
2024
    def test_run_bzr_user_error_caught(self):
2621
2025
        # Running bzr in blackbox mode, normal/expected/user errors should be
2622
2026
        # caught in the regular way and turned into an error message plus exit
2623
2027
        # code.
2624
 
        transport_server = memory.MemoryServer()
2625
 
        transport_server.start_server()
2626
 
        self.addCleanup(transport_server.stop_server)
2627
 
        url = transport_server.get_url()
2628
 
        self.permit_url(url)
2629
 
        out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)
 
2028
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
2630
2029
        self.assertEqual(out, '')
2631
2030
        self.assertContainsRe(err,
2632
2031
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
2758
2157
 
2759
2158
class TestTestSuite(tests.TestCase):
2760
2159
 
2761
 
    def test__test_suite_testmod_names(self):
2762
 
        # Test that a plausible list of test module names are returned
2763
 
        # by _test_suite_testmod_names.
2764
 
        test_list = tests._test_suite_testmod_names()
2765
 
        self.assertSubset([
2766
 
            'bzrlib.tests.blackbox',
2767
 
            'bzrlib.tests.per_transport',
2768
 
            'bzrlib.tests.test_selftest',
2769
 
            ],
2770
 
            test_list)
2771
 
 
2772
 
    def test__test_suite_modules_to_doctest(self):
2773
 
        # Test that a plausible list of modules to doctest is returned
2774
 
        # by _test_suite_modules_to_doctest.
2775
 
        test_list = tests._test_suite_modules_to_doctest()
2776
 
        if __doc__ is None:
2777
 
            # When docstrings are stripped, there are no modules to doctest
2778
 
            self.assertEqual([], test_list)
2779
 
            return
2780
 
        self.assertSubset([
2781
 
            'bzrlib.timestamp',
2782
 
            ],
2783
 
            test_list)
2784
 
 
2785
2160
    def test_test_suite(self):
2786
 
        # test_suite() loads the entire test suite to operate. To avoid this
2787
 
        # overhead, and yet still be confident that things are happening,
2788
 
        # we temporarily replace two functions used by test_suite with 
2789
 
        # test doubles that supply a few sample tests to load, and check they
2790
 
        # are loaded.
2791
 
        calls = []
2792
 
        def testmod_names():
2793
 
            calls.append("testmod_names")
2794
 
            return [
2795
 
                'bzrlib.tests.blackbox.test_branch',
2796
 
                'bzrlib.tests.per_transport',
2797
 
                'bzrlib.tests.test_selftest',
2798
 
                ]
2799
 
        self.overrideAttr(tests, '_test_suite_testmod_names', testmod_names)
2800
 
        def doctests():
2801
 
            calls.append("modules_to_doctest")
2802
 
            if __doc__ is None:
2803
 
                return []
2804
 
            return ['bzrlib.timestamp']
2805
 
        self.overrideAttr(tests, '_test_suite_modules_to_doctest', doctests)
2806
 
        expected_test_list = [
 
2161
        # This test is slow, so we do a single test with one test in each
 
2162
        # category
 
2163
        test_list = [
2807
2164
            # testmod_names
2808
2165
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2809
2166
            ('bzrlib.tests.per_transport.TransportTests'
2810
 
             '.test_abspath(LocalTransport,LocalURLServer)'),
 
2167
             '.test_abspath(LocalURLServer)'),
2811
2168
            'bzrlib.tests.test_selftest.TestTestSuite.test_test_suite',
 
2169
            # modules_to_doctest
 
2170
            'bzrlib.timestamp.format_highres_date',
2812
2171
            # plugins can't be tested that way since selftest may be run with
2813
2172
            # --no-plugins
2814
2173
            ]
2815
 
        if __doc__ is not None:
2816
 
            expected_test_list.extend([
2817
 
                # modules_to_doctest
2818
 
                'bzrlib.timestamp.format_highres_date',
2819
 
                ])
2820
 
        suite = tests.test_suite()
2821
 
        self.assertEqual(set(["testmod_names", "modules_to_doctest"]),
2822
 
            set(calls))
2823
 
        self.assertSubset(expected_test_list, _test_ids(suite))
 
2174
        suite = tests.test_suite(test_list)
 
2175
        self.assertEquals(test_list, _test_ids(suite))
2824
2176
 
2825
2177
    def test_test_suite_list_and_start(self):
2826
 
        # We cannot test this at the same time as the main load, because we want
2827
 
        # to know that starting_with == None works. So a second load is
2828
 
        # incurred - note that the starting_with parameter causes a partial load
2829
 
        # rather than a full load so this test should be pretty quick.
2830
2178
        test_list = ['bzrlib.tests.test_selftest.TestTestSuite.test_test_suite']
2831
2179
        suite = tests.test_suite(test_list,
2832
2180
                                 ['bzrlib.tests.test_selftest.TestTestSuite'])
2877
2225
    def test_load_tests(self):
2878
2226
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2879
2227
        loader = self._create_loader(test_list)
 
2228
 
2880
2229
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2881
2230
        self.assertEquals(test_list, _test_ids(suite))
2882
2231
 
2883
2232
    def test_exclude_tests(self):
2884
2233
        test_list = ['bogus']
2885
2234
        loader = self._create_loader(test_list)
 
2235
 
2886
2236
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2887
2237
        self.assertEquals([], _test_ids(suite))
2888
2238
 
2933
2283
        tpr.register('bar', 'bbb.aaa.rrr')
2934
2284
        tpr.register('bar', 'bBB.aAA.rRR')
2935
2285
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
2936
 
        self.assertThat(self.get_log(),
2937
 
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR", ELLIPSIS))
 
2286
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
2287
                              r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')
2938
2288
 
2939
2289
    def test_get_unknown_prefix(self):
2940
2290
        tpr = self._get_registry()
2976
2326
                                                self.verbosity)
2977
2327
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
2978
2328
        self.assertLength(1, calls)
 
2329
 
 
2330
    def test_done(self):
 
2331
        """run_suite should call result.done()"""
 
2332
        self.calls = 0
 
2333
        def one_more_call(): self.calls += 1
 
2334
        def test_function():
 
2335
            pass
 
2336
        test = unittest.FunctionTestCase(test_function)
 
2337
        class InstrumentedTestResult(tests.ExtendedTestResult):
 
2338
            def done(self): one_more_call()
 
2339
        class MyRunner(tests.TextTestRunner):
 
2340
            def run(self, test):
 
2341
                return InstrumentedTestResult(self.stream, self.descriptions,
 
2342
                                              self.verbosity)
 
2343
        tests.run_suite(test, runner_class=MyRunner, stream=StringIO())
 
2344
        self.assertEquals(1, self.calls)