~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Joe Julian
  • Date: 2010-01-10 02:25:31 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: joe@julianfamily.org-20100110022531-wqk61rsagz8xsiga
Added MANIFEST.in to allow bdist_rpm to have all the required include files and tools. bdist_rpm will still fail to build correctly on some distributions due to a disttools bug http://bugs.python.org/issue644744

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
21
import signal
23
22
import sys
25
24
import unittest
26
25
import warnings
27
26
 
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
27
import bzrlib
37
28
from bzrlib import (
38
29
    branchbuilder,
47
38
    repository,
48
39
    symbol_versioning,
49
40
    tests,
50
 
    transport,
51
41
    workingtree,
52
42
    )
53
43
from bzrlib.repofmt import (
61
51
    deprecated_method,
62
52
    )
63
53
from bzrlib.tests import (
64
 
    features,
65
 
    stub_sftp,
 
54
    SubUnitFeature,
66
55
    test_lsprof,
67
 
    test_server,
68
56
    test_sftp_transport,
69
57
    TestUtil,
70
58
    )
71
59
from bzrlib.trace import note
72
 
from bzrlib.transport import memory
 
60
from bzrlib.transport.memory import MemoryServer, MemoryTransport
73
61
from bzrlib.version import _get_bzr_source_tree
74
62
 
75
63
 
89
77
                          TestUtil._load_module_by_name,
90
78
                          'bzrlib.no-name-yet')
91
79
 
92
 
 
93
80
class MetaTestLog(tests.TestCase):
94
81
 
95
82
    def test_logging(self):
96
83
        """Test logs are captured when a test fails."""
97
84
        self.log('a test message')
98
 
        details = self.getDetails()
99
 
        log = details['log']
100
 
        self.assertThat(log.content_type, Equals(ContentType(
101
 
            "text", "plain", {"charset": "utf8"})))
102
 
        self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
103
 
        self.assertThat(self.get_log(),
104
 
            DocTestMatches(u"...a test message\n", ELLIPSIS))
 
85
        self._log_file.flush()
 
86
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
87
                              'a test message\n')
105
88
 
106
89
 
107
90
class TestUnicodeFilename(tests.TestCase):
506
489
        self.assertEqualStat(real, fake)
507
490
 
508
491
    def test_assertEqualStat_notequal(self):
509
 
        self.build_tree(["foo", "longname"])
 
492
        self.build_tree(["foo", "bar"])
510
493
        self.assertRaises(AssertionError, self.assertEqualStat,
511
 
            os.lstat("foo"), os.lstat("longname"))
 
494
            os.lstat("foo"), os.lstat("bar"))
512
495
 
513
496
 
514
497
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
532
515
        cwd = osutils.getcwd()
533
516
        self.assertIsSameRealPath(self.test_dir, cwd)
534
517
 
535
 
    def test_BZR_HOME_and_HOME_are_bytestrings(self):
536
 
        """The $BZR_HOME and $HOME environment variables should not be unicode.
537
 
 
538
 
        See https://bugs.launchpad.net/bzr/+bug/464174
539
 
        """
540
 
        self.assertIsInstance(os.environ['BZR_HOME'], str)
541
 
        self.assertIsInstance(os.environ['HOME'], str)
542
 
 
543
518
    def test_make_branch_and_memory_tree(self):
544
519
        """In TestCaseWithMemoryTransport we should not make the branch on disk.
545
520
 
601
576
                         self.get_transport().get_bytes(
602
577
                            'dir/.bzr/repository/format'))
603
578
 
 
579
    def test_safety_net(self):
 
580
        """No test should modify the safety .bzr directory.
 
581
 
 
582
        We just test that the _check_safety_net private method raises
 
583
        AssertionError, it's easier than building a test suite with the same
 
584
        test.
 
585
        """
 
586
        # Oops, a commit in the current directory (i.e. without local .bzr
 
587
        # directory) will crawl up the hierarchy to find a .bzr directory.
 
588
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
 
589
        # But we have a safety net in place.
 
590
        self.assertRaises(AssertionError, self._check_safety_net)
 
591
 
604
592
    def test_dangling_locks_cause_failures(self):
605
593
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
606
594
            def test_function(self):
623
611
 
624
612
    def test_get_readonly_url_none(self):
625
613
        from bzrlib.transport import get_transport
 
614
        from bzrlib.transport.memory import MemoryServer
626
615
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
627
 
        self.vfs_transport_factory = memory.MemoryServer
 
616
        self.vfs_transport_factory = MemoryServer
628
617
        self.transport_readonly_server = None
629
618
        # calling get_readonly_transport() constructs a decorator on the url
630
619
        # for the server
639
628
    def test_get_readonly_url_http(self):
640
629
        from bzrlib.tests.http_server import HttpServer
641
630
        from bzrlib.transport import get_transport
 
631
        from bzrlib.transport.local import LocalURLServer
642
632
        from bzrlib.transport.http import HttpTransportBase
643
 
        self.transport_server = test_server.LocalURLServer
 
633
        self.transport_server = LocalURLServer
644
634
        self.transport_readonly_server = HttpServer
645
635
        # calling get_readonly_transport() gives us a HTTP server instance.
646
636
        url = self.get_readonly_url()
677
667
 
678
668
    def setUp(self):
679
669
        super(TestTestCaseTransports, self).setUp()
680
 
        self.vfs_transport_factory = memory.MemoryServer
 
670
        self.vfs_transport_factory = MemoryServer
681
671
 
682
672
    def test_make_bzrdir_preserves_transport(self):
683
673
        t = self.get_transport()
684
674
        result_bzrdir = self.make_bzrdir('subdir')
685
675
        self.assertIsInstance(result_bzrdir.transport,
686
 
                              memory.MemoryTransport)
 
676
                              MemoryTransport)
687
677
        # should not be on disk, should only be in memory
688
678
        self.failIfExists('subdir')
689
679
 
697
687
        self.assertEqual(url, t.clone('..').base)
698
688
 
699
689
 
700
 
class TestProfileResult(tests.TestCase):
701
 
 
702
 
    def test_profiles_tests(self):
703
 
        self.requireFeature(test_lsprof.LSProfFeature)
704
 
        terminal = testtools.tests.helpers.ExtendedTestResult()
705
 
        result = tests.ProfileResult(terminal)
706
 
        class Sample(tests.TestCase):
707
 
            def a(self):
708
 
                self.sample_function()
709
 
            def sample_function(self):
710
 
                pass
711
 
        test = Sample("a")
712
 
        test.run(result)
713
 
        case = terminal._events[0][1]
714
 
        self.assertLength(1, case._benchcalls)
715
 
        # We must be able to unpack it as the test reporting code wants
716
 
        (_, _, _), stats = case._benchcalls[0]
717
 
        self.assertTrue(callable(stats.pprint))
718
 
 
719
 
 
720
690
class TestTestResult(tests.TestCase):
721
691
 
722
692
    def check_timing(self, test_case, expected_re):
724
694
                descriptions=0,
725
695
                verbosity=1,
726
696
                )
727
 
        capture = testtools.tests.helpers.ExtendedTestResult()
728
 
        test_case.run(MultiTestResult(result, capture))
729
 
        run_case = capture._events[0][1]
730
 
        timed_string = result._testTimeString(run_case)
 
697
        test_case.run(result)
 
698
        timed_string = result._testTimeString(test_case)
731
699
        self.assertContainsRe(timed_string, expected_re)
732
700
 
733
701
    def test_test_reporting(self):
751
719
        self.check_timing(ShortDelayTestCase('test_short_delay'),
752
720
                          r"^ +[0-9]+ms$")
753
721
 
754
 
    def _patch_get_bzr_source_tree(self):
755
 
        # Reading from the actual source tree breaks isolation, but we don't
756
 
        # want to assume that thats *all* that would happen.
757
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
758
 
 
759
722
    def test_assigned_benchmark_file_stores_date(self):
760
 
        self._patch_get_bzr_source_tree()
761
723
        output = StringIO()
762
724
        result = bzrlib.tests.TextTestResult(self._log_file,
763
725
                                        descriptions=0,
771
733
        self.assertContainsRe(output_string, "--date [0-9.]+")
772
734
 
773
735
    def test_benchhistory_records_test_times(self):
774
 
        self._patch_get_bzr_source_tree()
775
736
        result_stream = StringIO()
776
737
        result = bzrlib.tests.TextTestResult(
777
738
            self._log_file,
839
800
    def test_known_failure(self):
840
801
        """A KnownFailure being raised should trigger several result actions."""
841
802
        class InstrumentedTestResult(tests.ExtendedTestResult):
842
 
            def stopTestRun(self): pass
 
803
            def done(self): pass
843
804
            def startTests(self): pass
844
805
            def report_test_start(self, test): pass
845
 
            def report_known_failure(self, test, err=None, details=None):
846
 
                self._call = test, 'known failure'
 
806
            def report_known_failure(self, test, err):
 
807
                self._call = test, err
847
808
        result = InstrumentedTestResult(None, None, None, None)
848
 
        class Test(tests.TestCase):
849
 
            def test_function(self):
850
 
                raise tests.KnownFailure('failed!')
851
 
        test = Test("test_function")
 
809
        def test_function():
 
810
            raise tests.KnownFailure('failed!')
 
811
        test = unittest.FunctionTestCase(test_function)
852
812
        test.run(result)
853
813
        # it should invoke 'report_known_failure'.
854
814
        self.assertEqual(2, len(result._call))
855
 
        self.assertEqual(test.id(), result._call[0].id())
856
 
        self.assertEqual('known failure', result._call[1])
 
815
        self.assertEqual(test, result._call[0])
 
816
        self.assertEqual(tests.KnownFailure, result._call[1][0])
 
817
        self.assertIsInstance(result._call[1][1], tests.KnownFailure)
857
818
        # we dont introspec the traceback, if the rest is ok, it would be
858
819
        # exceptional for it not to be.
859
820
        # it should update the known_failure_count on the object.
893
854
    def test_add_not_supported(self):
894
855
        """Test the behaviour of invoking addNotSupported."""
895
856
        class InstrumentedTestResult(tests.ExtendedTestResult):
896
 
            def stopTestRun(self): pass
 
857
            def done(self): pass
897
858
            def startTests(self): pass
898
859
            def report_test_start(self, test): pass
899
860
            def report_unsupported(self, test, feature):
931
892
        result.report_unsupported(test, feature)
932
893
        output = result_stream.getvalue()[prefix:]
933
894
        lines = output.splitlines()
934
 
        # We don't check for the final '0ms' since it may fail on slow hosts
935
 
        self.assertStartsWith(lines[0], 'NODEP')
936
 
        self.assertEqual(lines[1],
937
 
                         "    The feature 'Feature' is not available.")
 
895
        # XXX: This is a timing dependent test. I've had it fail because it
 
896
        #      took 6ms to evaluate... :(
 
897
        self.assertEqual(lines, ['NODEP        0ms',
 
898
                                 "    The feature 'Feature' is not available."])
938
899
 
939
900
    def test_unavailable_exception(self):
940
901
        """An UnavailableFeature being raised should invoke addNotSupported."""
941
902
        class InstrumentedTestResult(tests.ExtendedTestResult):
942
 
            def stopTestRun(self): pass
 
903
            def done(self): pass
943
904
            def startTests(self): pass
944
905
            def report_test_start(self, test): pass
945
906
            def addNotSupported(self, test, feature):
946
907
                self._call = test, feature
947
908
        result = InstrumentedTestResult(None, None, None, None)
948
909
        feature = tests.Feature()
949
 
        class Test(tests.TestCase):
950
 
            def test_function(self):
951
 
                raise tests.UnavailableFeature(feature)
952
 
        test = Test("test_function")
 
910
        def test_function():
 
911
            raise tests.UnavailableFeature(feature)
 
912
        test = unittest.FunctionTestCase(test_function)
953
913
        test.run(result)
954
914
        # it should invoke 'addNotSupported'.
955
915
        self.assertEqual(2, len(result._call))
956
 
        self.assertEqual(test.id(), result._call[0].id())
 
916
        self.assertEqual(test, result._call[0])
957
917
        self.assertEqual(feature, result._call[1])
958
918
        # and not count as an error
959
919
        self.assertEqual(0, result.error_count)
972
932
                                             verbosity=1)
973
933
        test = self.get_passing_test()
974
934
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
975
 
        result.addExpectedFailure(test, err)
 
935
        result._addKnownFailure(test, err)
976
936
        self.assertFalse(result.wasStrictlySuccessful())
977
937
        self.assertEqual(None, result._extractBenchmarkTime(test))
978
938
 
1023
983
        because of our use of global state.
1024
984
        """
1025
985
        old_root = tests.TestCaseInTempDir.TEST_ROOT
1026
 
        old_leak = tests.TestCase._first_thread_leaker_id
1027
986
        try:
1028
987
            tests.TestCaseInTempDir.TEST_ROOT = None
1029
 
            tests.TestCase._first_thread_leaker_id = None
1030
988
            return testrunner.run(test)
1031
989
        finally:
1032
990
            tests.TestCaseInTempDir.TEST_ROOT = old_root
1033
 
            tests.TestCase._first_thread_leaker_id = old_leak
1034
991
 
1035
992
    def test_known_failure_failed_run(self):
1036
993
        # run a test that generates a known failure which should be printed in
1037
994
        # the final output when real failures occur.
1038
 
        class Test(tests.TestCase):
1039
 
            def known_failure_test(self):
1040
 
                self.expectFailure('failed', self.assertTrue, False)
 
995
        def known_failure_test():
 
996
            raise tests.KnownFailure('failed')
1041
997
        test = unittest.TestSuite()
1042
 
        test.addTest(Test("known_failure_test"))
 
998
        test.addTest(unittest.FunctionTestCase(known_failure_test))
1043
999
        def failing_test():
1044
 
            self.fail('foo')
 
1000
            raise AssertionError('foo')
1045
1001
        test.addTest(unittest.FunctionTestCase(failing_test))
1046
1002
        stream = StringIO()
1047
1003
        runner = tests.TextTestRunner(stream=stream)
1048
1004
        result = self.run_test_runner(runner, test)
1049
1005
        lines = stream.getvalue().splitlines()
1050
1006
        self.assertContainsRe(stream.getvalue(),
1051
 
            '(?sm)^bzr selftest.*$'
 
1007
            '(?sm)^testing.*$'
1052
1008
            '.*'
1053
1009
            '^======================================================================\n'
1054
 
            '^FAIL: failing_test\n'
 
1010
            '^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1055
1011
            '^----------------------------------------------------------------------\n'
1056
1012
            'Traceback \\(most recent call last\\):\n'
1057
1013
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1058
 
            '    self.fail\\(\'foo\'\\)\n'
 
1014
            '    raise AssertionError\\(\'foo\'\\)\n'
1059
1015
            '.*'
1060
1016
            '^----------------------------------------------------------------------\n'
1061
1017
            '.*'
1063
1019
            )
1064
1020
 
1065
1021
    def test_known_failure_ok_run(self):
1066
 
        # run a test that generates a known failure which should be printed in
1067
 
        # the final output.
1068
 
        class Test(tests.TestCase):
1069
 
            def known_failure_test(self):
1070
 
                self.expectFailure('failed', self.assertTrue, False)
1071
 
        test = Test("known_failure_test")
 
1022
        # run a test that generates a known failure which should be printed in the final output.
 
1023
        def known_failure_test():
 
1024
            raise tests.KnownFailure('failed')
 
1025
        test = unittest.FunctionTestCase(known_failure_test)
1072
1026
        stream = StringIO()
1073
1027
        runner = tests.TextTestRunner(stream=stream)
1074
1028
        result = self.run_test_runner(runner, test)
1079
1033
            '\n'
1080
1034
            'OK \\(known_failures=1\\)\n')
1081
1035
 
1082
 
    def test_result_decorator(self):
1083
 
        # decorate results
1084
 
        calls = []
1085
 
        class LoggingDecorator(tests.ForwardingResult):
1086
 
            def startTest(self, test):
1087
 
                tests.ForwardingResult.startTest(self, test)
1088
 
                calls.append('start')
1089
 
        test = unittest.FunctionTestCase(lambda:None)
1090
 
        stream = StringIO()
1091
 
        runner = tests.TextTestRunner(stream=stream,
1092
 
            result_decorators=[LoggingDecorator])
1093
 
        result = self.run_test_runner(runner, test)
1094
 
        self.assertLength(1, calls)
1095
 
 
1096
1036
    def test_skipped_test(self):
1097
1037
        # run a test that is skipped, and check the suite as a whole still
1098
1038
        # succeeds.
1151
1091
 
1152
1092
    def test_not_applicable(self):
1153
1093
        # run a test that is skipped because it's not applicable
1154
 
        class Test(tests.TestCase):
1155
 
            def not_applicable_test(self):
1156
 
                raise tests.TestNotApplicable('this test never runs')
 
1094
        def not_applicable_test():
 
1095
            raise tests.TestNotApplicable('this test never runs')
1157
1096
        out = StringIO()
1158
1097
        runner = tests.TextTestRunner(stream=out, verbosity=2)
1159
 
        test = Test("not_applicable_test")
 
1098
        test = unittest.FunctionTestCase(not_applicable_test)
1160
1099
        result = self.run_test_runner(runner, test)
1161
1100
        self._log_file.write(out.getvalue())
1162
1101
        self.assertTrue(result.wasSuccessful())
1166
1105
        self.assertContainsRe(out.getvalue(),
1167
1106
                r'(?m)^    this test never runs')
1168
1107
 
 
1108
    def test_not_applicable_demo(self):
 
1109
        # just so you can see it in the test output
 
1110
        raise tests.TestNotApplicable('this test is just a demonstation')
 
1111
 
1169
1112
    def test_unsupported_features_listed(self):
1170
1113
        """When unsupported features are encountered they are detailed."""
1171
1114
        class Feature1(tests.Feature):
1191
1134
            ],
1192
1135
            lines[-3:])
1193
1136
 
1194
 
    def _patch_get_bzr_source_tree(self):
1195
 
        # Reading from the actual source tree breaks isolation, but we don't
1196
 
        # want to assume that thats *all* that would happen.
1197
 
        self._get_source_tree_calls = []
1198
 
        def new_get():
1199
 
            self._get_source_tree_calls.append("called")
1200
 
            return None
1201
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
1202
 
 
1203
1137
    def test_bench_history(self):
1204
 
        # tests that the running the benchmark passes bench_history into
1205
 
        # the test result object. We can tell that happens if
1206
 
        # _get_bzr_source_tree is called.
1207
 
        self._patch_get_bzr_source_tree()
 
1138
        # tests that the running the benchmark produces a history file
 
1139
        # containing a timestamp and the revision id of the bzrlib source which
 
1140
        # was tested.
 
1141
        workingtree = _get_bzr_source_tree()
1208
1142
        test = TestRunner('dummy_test')
1209
1143
        output = StringIO()
1210
1144
        runner = tests.TextTestRunner(stream=self._log_file,
1212
1146
        result = self.run_test_runner(runner, test)
1213
1147
        output_string = output.getvalue()
1214
1148
        self.assertContainsRe(output_string, "--date [0-9.]+")
1215
 
        self.assertLength(1, self._get_source_tree_calls)
1216
 
 
1217
 
    def test_startTestRun(self):
1218
 
        """run should call result.startTestRun()"""
1219
 
        calls = []
1220
 
        class LoggingDecorator(tests.ForwardingResult):
1221
 
            def startTestRun(self):
1222
 
                tests.ForwardingResult.startTestRun(self)
1223
 
                calls.append('startTestRun')
1224
 
        test = unittest.FunctionTestCase(lambda:None)
1225
 
        stream = StringIO()
1226
 
        runner = tests.TextTestRunner(stream=stream,
1227
 
            result_decorators=[LoggingDecorator])
1228
 
        result = self.run_test_runner(runner, test)
1229
 
        self.assertLength(1, calls)
1230
 
 
1231
 
    def test_stopTestRun(self):
1232
 
        """run should call result.stopTestRun()"""
1233
 
        calls = []
1234
 
        class LoggingDecorator(tests.ForwardingResult):
1235
 
            def stopTestRun(self):
1236
 
                tests.ForwardingResult.stopTestRun(self)
1237
 
                calls.append('stopTestRun')
1238
 
        test = unittest.FunctionTestCase(lambda:None)
1239
 
        stream = StringIO()
1240
 
        runner = tests.TextTestRunner(stream=stream,
1241
 
            result_decorators=[LoggingDecorator])
1242
 
        result = self.run_test_runner(runner, test)
1243
 
        self.assertLength(1, calls)
 
1149
        if workingtree is not None:
 
1150
            revision_id = workingtree.get_parent_ids()[0]
 
1151
            self.assertEndsWith(output_string.rstrip(), revision_id)
 
1152
 
 
1153
    def assertLogDeleted(self, test):
 
1154
        log = test._get_log()
 
1155
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
1156
        self.assertEqual('', test._log_contents)
 
1157
        self.assertIs(None, test._log_file_name)
 
1158
 
 
1159
    def test_success_log_deleted(self):
 
1160
        """Successful tests have their log deleted"""
 
1161
 
 
1162
        class LogTester(tests.TestCase):
 
1163
 
 
1164
            def test_success(self):
 
1165
                self.log('this will be removed\n')
 
1166
 
 
1167
        sio = StringIO()
 
1168
        runner = tests.TextTestRunner(stream=sio)
 
1169
        test = LogTester('test_success')
 
1170
        result = self.run_test_runner(runner, test)
 
1171
 
 
1172
        self.assertLogDeleted(test)
 
1173
 
 
1174
    def test_skipped_log_deleted(self):
 
1175
        """Skipped tests have their log deleted"""
 
1176
 
 
1177
        class LogTester(tests.TestCase):
 
1178
 
 
1179
            def test_skipped(self):
 
1180
                self.log('this will be removed\n')
 
1181
                raise tests.TestSkipped()
 
1182
 
 
1183
        sio = StringIO()
 
1184
        runner = tests.TextTestRunner(stream=sio)
 
1185
        test = LogTester('test_skipped')
 
1186
        result = self.run_test_runner(runner, test)
 
1187
 
 
1188
        self.assertLogDeleted(test)
 
1189
 
 
1190
    def test_not_aplicable_log_deleted(self):
 
1191
        """Not applicable tests have their log deleted"""
 
1192
 
 
1193
        class LogTester(tests.TestCase):
 
1194
 
 
1195
            def test_not_applicable(self):
 
1196
                self.log('this will be removed\n')
 
1197
                raise tests.TestNotApplicable()
 
1198
 
 
1199
        sio = StringIO()
 
1200
        runner = tests.TextTestRunner(stream=sio)
 
1201
        test = LogTester('test_not_applicable')
 
1202
        result = self.run_test_runner(runner, test)
 
1203
 
 
1204
        self.assertLogDeleted(test)
 
1205
 
 
1206
    def test_known_failure_log_deleted(self):
 
1207
        """Know failure tests have their log deleted"""
 
1208
 
 
1209
        class LogTester(tests.TestCase):
 
1210
 
 
1211
            def test_known_failure(self):
 
1212
                self.log('this will be removed\n')
 
1213
                raise tests.KnownFailure()
 
1214
 
 
1215
        sio = StringIO()
 
1216
        runner = tests.TextTestRunner(stream=sio)
 
1217
        test = LogTester('test_known_failure')
 
1218
        result = self.run_test_runner(runner, test)
 
1219
 
 
1220
        self.assertLogDeleted(test)
 
1221
 
 
1222
    def test_fail_log_kept(self):
 
1223
        """Failed tests have their log kept"""
 
1224
 
 
1225
        class LogTester(tests.TestCase):
 
1226
 
 
1227
            def test_fail(self):
 
1228
                self.log('this will be kept\n')
 
1229
                self.fail('this test fails')
 
1230
 
 
1231
        sio = StringIO()
 
1232
        runner = tests.TextTestRunner(stream=sio)
 
1233
        test = LogTester('test_fail')
 
1234
        result = self.run_test_runner(runner, test)
 
1235
 
 
1236
        text = sio.getvalue()
 
1237
        self.assertContainsRe(text, 'this will be kept')
 
1238
        self.assertContainsRe(text, 'this test fails')
 
1239
 
 
1240
        log = test._get_log()
 
1241
        self.assertContainsRe(log, 'this will be kept')
 
1242
        self.assertEqual(log, test._log_contents)
 
1243
 
 
1244
    def test_error_log_kept(self):
 
1245
        """Tests with errors have their log kept"""
 
1246
 
 
1247
        class LogTester(tests.TestCase):
 
1248
 
 
1249
            def test_error(self):
 
1250
                self.log('this will be kept\n')
 
1251
                raise ValueError('random exception raised')
 
1252
 
 
1253
        sio = StringIO()
 
1254
        runner = tests.TextTestRunner(stream=sio)
 
1255
        test = LogTester('test_error')
 
1256
        result = self.run_test_runner(runner, test)
 
1257
 
 
1258
        text = sio.getvalue()
 
1259
        self.assertContainsRe(text, 'this will be kept')
 
1260
        self.assertContainsRe(text, 'random exception raised')
 
1261
 
 
1262
        log = test._get_log()
 
1263
        self.assertContainsRe(log, 'this will be kept')
 
1264
        self.assertEqual(log, test._log_contents)
1244
1265
 
1245
1266
 
1246
1267
class SampleTestCase(tests.TestCase):
1312
1333
        self.assertEqual(flags, bzrlib.debug.debug_flags)
1313
1334
 
1314
1335
    def change_selftest_debug_flags(self, new_flags):
1315
 
        self.overrideAttr(tests, 'selftest_debug_flags', set(new_flags))
 
1336
        orig_selftest_flags = tests.selftest_debug_flags
 
1337
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
 
1338
        tests.selftest_debug_flags = set(new_flags)
 
1339
 
 
1340
    def _restore_selftest_debug_flags(self, flags):
 
1341
        tests.selftest_debug_flags = flags
1316
1342
 
1317
1343
    def test_allow_debug_flag(self):
1318
1344
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1378
1404
        self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)
1379
1405
 
1380
1406
    def make_test_result(self):
1381
 
        """Get a test result that writes to the test log file."""
1382
1407
        return tests.TextTestResult(self._log_file, descriptions=0, verbosity=1)
1383
1408
 
1384
1409
    def inner_test(self):
1392
1417
        result = self.make_test_result()
1393
1418
        self.inner_test.run(result)
1394
1419
        note("outer finish")
1395
 
        self.addCleanup(osutils.delete_any, self._log_file_name)
1396
1420
 
1397
1421
    def test_trace_nesting(self):
1398
1422
        # this tests that each test case nests its trace facility correctly.
1458
1482
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1459
1483
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1460
1484
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1461
 
        del self._benchcalls[:]
1462
1485
 
1463
1486
    def test_knownFailure(self):
1464
1487
        """Self.knownFailure() should raise a KnownFailure exception."""
1465
1488
        self.assertRaises(tests.KnownFailure, self.knownFailure, "A Failure")
1466
1489
 
1467
 
    def test_open_bzrdir_safe_roots(self):
1468
 
        # even a memory transport should fail to open when its url isn't 
1469
 
        # permitted.
1470
 
        # Manually set one up (TestCase doesn't and shouldn't provide magic
1471
 
        # machinery)
1472
 
        transport_server = memory.MemoryServer()
1473
 
        transport_server.start_server()
1474
 
        self.addCleanup(transport_server.stop_server)
1475
 
        t = transport.get_transport(transport_server.get_url())
1476
 
        bzrdir.BzrDir.create(t.base)
1477
 
        self.assertRaises(errors.BzrError,
1478
 
            bzrdir.BzrDir.open_from_transport, t)
1479
 
        # But if we declare this as safe, we can open the bzrdir.
1480
 
        self.permit_url(t.base)
1481
 
        self._bzr_selftest_roots.append(t.base)
1482
 
        bzrdir.BzrDir.open_from_transport(t)
1483
 
 
1484
1490
    def test_requireFeature_available(self):
1485
1491
        """self.requireFeature(available) is a no-op."""
1486
1492
        class Available(tests.Feature):
1530
1536
        """Test disabled tests behaviour with support aware results."""
1531
1537
        test = SampleTestCase('_test_pass')
1532
1538
        class DisabledFeature(object):
1533
 
            def __eq__(self, other):
1534
 
                return isinstance(other, DisabledFeature)
1535
1539
            def available(self):
1536
1540
                return False
1537
1541
        the_feature = DisabledFeature()
1548
1552
                self.calls.append(('addNotSupported', test, feature))
1549
1553
        result = InstrumentedTestResult()
1550
1554
        test.run(result)
1551
 
        case = result.calls[0][1]
1552
1555
        self.assertEqual([
1553
 
            ('startTest', case),
1554
 
            ('addNotSupported', case, the_feature),
1555
 
            ('stopTest', case),
 
1556
            ('startTest', test),
 
1557
            ('addNotSupported', test, the_feature),
 
1558
            ('stopTest', test),
1556
1559
            ],
1557
1560
            result.calls)
1558
1561
 
1559
 
    def test_start_server_registers_url(self):
1560
 
        transport_server = memory.MemoryServer()
1561
 
        # A little strict, but unlikely to be changed soon.
1562
 
        self.assertEqual([], self._bzr_selftest_roots)
1563
 
        self.start_server(transport_server)
1564
 
        self.assertSubset([transport_server.get_url()],
1565
 
            self._bzr_selftest_roots)
1566
 
 
1567
1562
    def test_assert_list_raises_on_generator(self):
1568
1563
        def generator_which_will_raise():
1569
1564
            # This will not raise until after the first yield
1619
1614
        self.assertRaises(AssertionError,
1620
1615
            self.assertListRaises, _TestException, success_generator)
1621
1616
 
1622
 
    def test_overrideAttr_without_value(self):
1623
 
        self.test_attr = 'original' # Define a test attribute
1624
 
        obj = self # Make 'obj' visible to the embedded test
1625
 
        class Test(tests.TestCase):
1626
 
 
1627
 
            def setUp(self):
1628
 
                tests.TestCase.setUp(self)
1629
 
                self.orig = self.overrideAttr(obj, 'test_attr')
1630
 
 
1631
 
            def test_value(self):
1632
 
                self.assertEqual('original', self.orig)
1633
 
                self.assertEqual('original', obj.test_attr)
1634
 
                obj.test_attr = 'modified'
1635
 
                self.assertEqual('modified', obj.test_attr)
1636
 
 
1637
 
        test = Test('test_value')
1638
 
        test.run(unittest.TestResult())
1639
 
        self.assertEqual('original', obj.test_attr)
1640
 
 
1641
 
    def test_overrideAttr_with_value(self):
1642
 
        self.test_attr = 'original' # Define a test attribute
1643
 
        obj = self # Make 'obj' visible to the embedded test
1644
 
        class Test(tests.TestCase):
1645
 
 
1646
 
            def setUp(self):
1647
 
                tests.TestCase.setUp(self)
1648
 
                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
1649
 
 
1650
 
            def test_value(self):
1651
 
                self.assertEqual('original', self.orig)
1652
 
                self.assertEqual('modified', obj.test_attr)
1653
 
 
1654
 
        test = Test('test_value')
1655
 
        test.run(unittest.TestResult())
1656
 
        self.assertEqual('original', obj.test_attr)
1657
 
 
1658
1617
 
1659
1618
# NB: Don't delete this; it's not actually from 0.11!
1660
1619
@deprecated_function(deprecated_in((0, 11, 0)))
1703
1662
        self.assertEndsWith('foo', 'oo')
1704
1663
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1705
1664
 
1706
 
    def test_assertEqualDiff(self):
1707
 
        e = self.assertRaises(AssertionError,
1708
 
                              self.assertEqualDiff, '', '\n')
1709
 
        self.assertEquals(str(e),
1710
 
                          # Don't blink ! The '+' applies to the second string
1711
 
                          'first string is missing a final newline.\n+ \n')
1712
 
        e = self.assertRaises(AssertionError,
1713
 
                              self.assertEqualDiff, '\n', '')
1714
 
        self.assertEquals(str(e),
1715
 
                          # Don't blink ! The '-' applies to the second string
1716
 
                          'second string is missing a final newline.\n- \n')
1717
 
 
1718
 
 
1719
 
class TestDeprecations(tests.TestCase):
1720
 
 
1721
1665
    def test_applyDeprecated_not_deprecated(self):
1722
1666
        sample_object = ApplyDeprecatedHelper()
1723
1667
        # calling an undeprecated callable raises an assertion
1800
1744
        tree = self.make_branch_and_memory_tree('a')
1801
1745
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1802
1746
 
1803
 
    def test_make_tree_for_local_vfs_backed_transport(self):
1804
 
        # make_branch_and_tree has to use local branch and repositories
1805
 
        # when the vfs transport and local disk are colocated, even if
1806
 
        # a different transport is in use for url generation.
1807
 
        self.transport_server = test_server.FakeVFATServer
1808
 
        self.assertFalse(self.get_url('t1').startswith('file://'))
 
1747
 
 
1748
class TestSFTPMakeBranchAndTree(test_sftp_transport.TestCaseWithSFTPServer):
 
1749
 
 
1750
    def test_make_tree_for_sftp_branch(self):
 
1751
        """Transports backed by local directories create local trees."""
 
1752
        # NB: This is arguably a bug in the definition of make_branch_and_tree.
1809
1753
        tree = self.make_branch_and_tree('t1')
1810
1754
        base = tree.bzrdir.root_transport.base
1811
 
        self.assertStartsWith(base, 'file://')
 
1755
        self.failIf(base.startswith('sftp'),
 
1756
                'base %r is on sftp but should be local' % base)
1812
1757
        self.assertEquals(tree.bzrdir.root_transport,
1813
1758
                tree.branch.bzrdir.root_transport)
1814
1759
        self.assertEquals(tree.bzrdir.root_transport,
1874
1819
        self.assertNotContainsRe("Test.b", output.getvalue())
1875
1820
        self.assertLength(2, output.readlines())
1876
1821
 
1877
 
    def test_lsprof_tests(self):
1878
 
        self.requireFeature(test_lsprof.LSProfFeature)
1879
 
        calls = []
1880
 
        class Test(object):
1881
 
            def __call__(test, result):
1882
 
                test.run(result)
1883
 
            def run(test, result):
1884
 
                self.assertIsInstance(result, tests.ForwardingResult)
1885
 
                calls.append("called")
1886
 
            def countTestCases(self):
1887
 
                return 1
1888
 
        self.run_selftest(test_suite_factory=Test, lsprof_tests=True)
1889
 
        self.assertLength(1, calls)
1890
 
 
1891
1822
    def test_random(self):
1892
1823
        # test randomising by listing a number of tests.
1893
1824
        output_123 = self.run_selftest(test_suite_factory=self.factory,
1908
1839
        self.assertEqual(expected.getvalue(), repeated.getvalue())
1909
1840
 
1910
1841
    def test_runner_class(self):
1911
 
        self.requireFeature(features.subunit)
 
1842
        self.requireFeature(SubUnitFeature)
1912
1843
        from subunit import ProtocolTestCase
1913
1844
        stream = self.run_selftest(runner_class=tests.SubUnitBzrRunner,
1914
1845
            test_suite_factory=self.factory)
1946
1877
        self.assertEqual(transport_server, captured_transport[0])
1947
1878
 
1948
1879
    def test_transport_sftp(self):
1949
 
        self.requireFeature(features.paramiko)
1950
 
        self.check_transport_set(stub_sftp.SFTPAbsoluteServer)
 
1880
        try:
 
1881
            import bzrlib.transport.sftp
 
1882
        except errors.ParamikoNotPresent:
 
1883
            raise tests.TestSkipped("Paramiko not present")
 
1884
        self.check_transport_set(bzrlib.transport.sftp.SFTPAbsoluteServer)
1951
1885
 
1952
1886
    def test_transport_memory(self):
1953
 
        self.check_transport_set(memory.MemoryServer)
 
1887
        self.check_transport_set(bzrlib.transport.memory.MemoryServer)
1954
1888
 
1955
1889
 
1956
1890
class TestSelftestWithIdList(tests.TestCaseInTempDir, SelfTestHelper):
1982
1916
 
1983
1917
        Attempts to run bzr from inside this class don't actually run it.
1984
1918
 
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.
 
1919
        We test how run_bzr actually invokes bzr in another location.
 
1920
        Here we only need to test that it is run_bzr passes the right
 
1921
        parameters to run_bzr.
1987
1922
        """
1988
1923
        self.argv = list(argv)
1989
1924
        self.retcode = retcode
1990
1925
        self.encoding = encoding
1991
1926
        self.stdin = stdin
1992
1927
        self.working_dir = working_dir
1993
 
        return self.retcode, self.out, self.err
 
1928
        return self.out, self.err
1994
1929
 
1995
1930
    def test_run_bzr_error(self):
1996
1931
        self.out = "It sure does!\n"
1997
1932
        out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=34)
1998
1933
        self.assertEqual(['rocks'], self.argv)
1999
1934
        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)
 
1935
        self.assertEqual(out, 'It sure does!\n')
2004
1936
 
2005
1937
    def test_run_bzr_error_regexes(self):
2006
1938
        self.out = ''
2007
1939
        self.err = "bzr: ERROR: foobarbaz is not versioned"
2008
1940
        out, err = self.run_bzr_error(
2009
 
            ["bzr: ERROR: foobarbaz is not versioned"],
2010
 
            ['file-id', 'foobarbaz'])
 
1941
                ["bzr: ERROR: foobarbaz is not versioned"],
 
1942
                ['file-id', 'foobarbaz'])
2011
1943
 
2012
1944
    def test_encoding(self):
2013
1945
        """Test that run_bzr passes encoding to _run_bzr_core"""
2142
2074
        return self.out, self.err
2143
2075
 
2144
2076
 
2145
 
class TestWithFakedStartBzrSubprocess(tests.TestCaseWithTransport):
2146
 
    """Base class for tests testing how we might run bzr."""
 
2077
class TestRunBzrSubprocess(tests.TestCaseWithTransport):
2147
2078
 
2148
2079
    def setUp(self):
2149
2080
        tests.TestCaseWithTransport.setUp(self)
2160
2091
            'working_dir':working_dir, 'allow_plugins':allow_plugins})
2161
2092
        return self.next_subprocess
2162
2093
 
2163
 
 
2164
 
class TestRunBzrSubprocess(TestWithFakedStartBzrSubprocess):
2165
 
 
2166
2094
    def assertRunBzrSubprocess(self, expected_args, process, *args, **kwargs):
2167
2095
        """Run run_bzr_subprocess with args and kwargs using a stubbed process.
2168
2096
 
2231
2159
            StubProcess(), '', allow_plugins=True)
2232
2160
 
2233
2161
 
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
2162
class _DontSpawnProcess(Exception):
2261
2163
    """A simple exception which just allows us to skip unnecessary steps"""
2262
2164
 
2340
2242
        self.assertEqual(['foo', 'current'], chdirs)
2341
2243
 
2342
2244
 
2343
 
class TestActuallyStartBzrSubprocess(tests.TestCaseWithTransport):
2344
 
    """Tests that really need to do things with an external bzr."""
 
2245
class TestBzrSubprocess(tests.TestCaseWithTransport):
 
2246
 
 
2247
    def test_start_and_stop_bzr_subprocess(self):
 
2248
        """We can start and perform other test actions while that process is
 
2249
        still alive.
 
2250
        """
 
2251
        process = self.start_bzr_subprocess(['--version'])
 
2252
        result = self.finish_bzr_subprocess(process)
 
2253
        self.assertContainsRe(result[0], 'is free software')
 
2254
        self.assertEqual('', result[1])
 
2255
 
 
2256
    def test_start_and_stop_bzr_subprocess_with_error(self):
 
2257
        """finish_bzr_subprocess allows specification of the desired exit code.
 
2258
        """
 
2259
        process = self.start_bzr_subprocess(['--versionn'])
 
2260
        result = self.finish_bzr_subprocess(process, retcode=3)
 
2261
        self.assertEqual('', result[0])
 
2262
        self.assertContainsRe(result[1], 'unknown command')
 
2263
 
 
2264
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
 
2265
        """finish_bzr_subprocess allows the exit code to be ignored."""
 
2266
        process = self.start_bzr_subprocess(['--versionn'])
 
2267
        result = self.finish_bzr_subprocess(process, retcode=None)
 
2268
        self.assertEqual('', result[0])
 
2269
        self.assertContainsRe(result[1], 'unknown command')
 
2270
 
 
2271
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
 
2272
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
2273
        not the expected one.
 
2274
        """
 
2275
        process = self.start_bzr_subprocess(['--versionn'])
 
2276
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
 
2277
                          process)
2345
2278
 
2346
2279
    def test_start_and_stop_bzr_subprocess_send_signal(self):
2347
2280
        """finish_bzr_subprocess raises self.failureException if the retcode is
2348
2281
        not the expected one.
2349
2282
        """
2350
 
        self.disable_missing_extensions_warning()
2351
2283
        process = self.start_bzr_subprocess(['wait-until-signalled'],
2352
2284
                                            skip_if_plan_to_signal=True)
2353
2285
        self.assertEqual('running\n', process.stdout.readline())
2356
2288
        self.assertEqual('', result[0])
2357
2289
        self.assertEqual('bzr: interrupted\n', result[1])
2358
2290
 
 
2291
    def test_start_and_stop_working_dir(self):
 
2292
        cwd = osutils.getcwd()
 
2293
        self.make_branch_and_tree('one')
 
2294
        process = self.start_bzr_subprocess(['root'], working_dir='one')
 
2295
        result = self.finish_bzr_subprocess(process, universal_newlines=True)
 
2296
        self.assertEndsWith(result[0], 'one\n')
 
2297
        self.assertEqual('', result[1])
 
2298
 
 
2299
 
 
2300
class TestKnownFailure(tests.TestCase):
 
2301
 
 
2302
    def test_known_failure(self):
 
2303
        """Check that KnownFailure is defined appropriately."""
 
2304
        # a KnownFailure is an assertion error for compatability with unaware
 
2305
        # runners.
 
2306
        self.assertIsInstance(tests.KnownFailure(""), AssertionError)
 
2307
 
 
2308
    def test_expect_failure(self):
 
2309
        try:
 
2310
            self.expectFailure("Doomed to failure", self.assertTrue, False)
 
2311
        except tests.KnownFailure, e:
 
2312
            self.assertEqual('Doomed to failure', e.args[0])
 
2313
        try:
 
2314
            self.expectFailure("Doomed to failure", self.assertTrue, True)
 
2315
        except AssertionError, e:
 
2316
            self.assertEqual('Unexpected success.  Should have failed:'
 
2317
                             ' Doomed to failure', e.args[0])
 
2318
        else:
 
2319
            self.fail('Assertion not raised')
 
2320
 
2359
2321
 
2360
2322
class TestFeature(tests.TestCase):
2361
2323
 
2398
2360
        self.assertIs(feature, exception.args[0])
2399
2361
 
2400
2362
 
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
2363
class TestSelftestFiltering(tests.TestCase):
2435
2364
 
2436
2365
    def setUp(self):
2615
2544
        # the test framework
2616
2545
        self.assertEquals('always fails', str(e))
2617
2546
        # check that there's no traceback in the test log
2618
 
        self.assertNotContainsRe(self.get_log(), r'Traceback')
 
2547
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
 
2548
            r'Traceback')
2619
2549
 
2620
2550
    def test_run_bzr_user_error_caught(self):
2621
2551
        # Running bzr in blackbox mode, normal/expected/user errors should be
2622
2552
        # caught in the regular way and turned into an error message plus exit
2623
2553
        # 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)
 
2554
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
2630
2555
        self.assertEqual(out, '')
2631
2556
        self.assertContainsRe(err,
2632
2557
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
2758
2683
 
2759
2684
class TestTestSuite(tests.TestCase):
2760
2685
 
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
 
        self.assertSubset([
2777
 
            'bzrlib.timestamp',
2778
 
            ],
2779
 
            test_list)
2780
 
 
2781
2686
    def test_test_suite(self):
2782
 
        # test_suite() loads the entire test suite to operate. To avoid this
2783
 
        # overhead, and yet still be confident that things are happening,
2784
 
        # we temporarily replace two functions used by test_suite with 
2785
 
        # test doubles that supply a few sample tests to load, and check they
2786
 
        # are loaded.
2787
 
        calls = []
2788
 
        def testmod_names():
2789
 
            calls.append("testmod_names")
2790
 
            return [
2791
 
                'bzrlib.tests.blackbox.test_branch',
2792
 
                'bzrlib.tests.per_transport',
2793
 
                'bzrlib.tests.test_selftest',
2794
 
                ]
2795
 
        self.overrideAttr(tests, '_test_suite_testmod_names', testmod_names)
2796
 
        def doctests():
2797
 
            calls.append("modules_to_doctest")
2798
 
            return ['bzrlib.timestamp']
2799
 
        self.overrideAttr(tests, '_test_suite_modules_to_doctest', doctests)
2800
 
        expected_test_list = [
 
2687
        # This test is slow - it loads the entire test suite to operate, so we
 
2688
        # do a single test with one test in each category
 
2689
        test_list = [
2801
2690
            # testmod_names
2802
2691
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2803
2692
            ('bzrlib.tests.per_transport.TransportTests'
2804
 
             '.test_abspath(LocalTransport,LocalURLServer)'),
 
2693
             '.test_abspath(LocalURLServer)'),
2805
2694
            'bzrlib.tests.test_selftest.TestTestSuite.test_test_suite',
2806
2695
            # modules_to_doctest
2807
2696
            'bzrlib.timestamp.format_highres_date',
2808
2697
            # plugins can't be tested that way since selftest may be run with
2809
2698
            # --no-plugins
2810
2699
            ]
2811
 
        suite = tests.test_suite()
2812
 
        self.assertEqual(set(["testmod_names", "modules_to_doctest"]),
2813
 
            set(calls))
2814
 
        self.assertSubset(expected_test_list, _test_ids(suite))
 
2700
        suite = tests.test_suite(test_list)
 
2701
        self.assertEquals(test_list, _test_ids(suite))
2815
2702
 
2816
2703
    def test_test_suite_list_and_start(self):
2817
2704
        # We cannot test this at the same time as the main load, because we want
2818
 
        # to know that starting_with == None works. So a second load is
2819
 
        # incurred - note that the starting_with parameter causes a partial load
2820
 
        # rather than a full load so this test should be pretty quick.
 
2705
        # to know that starting_with == None works. So a second full load is
 
2706
        # incurred.
2821
2707
        test_list = ['bzrlib.tests.test_selftest.TestTestSuite.test_test_suite']
2822
2708
        suite = tests.test_suite(test_list,
2823
2709
                                 ['bzrlib.tests.test_selftest.TestTestSuite'])
2868
2754
    def test_load_tests(self):
2869
2755
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2870
2756
        loader = self._create_loader(test_list)
 
2757
 
2871
2758
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2872
2759
        self.assertEquals(test_list, _test_ids(suite))
2873
2760
 
2874
2761
    def test_exclude_tests(self):
2875
2762
        test_list = ['bogus']
2876
2763
        loader = self._create_loader(test_list)
 
2764
 
2877
2765
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2878
2766
        self.assertEquals([], _test_ids(suite))
2879
2767
 
2924
2812
        tpr.register('bar', 'bbb.aaa.rrr')
2925
2813
        tpr.register('bar', 'bBB.aAA.rRR')
2926
2814
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
2927
 
        self.assertThat(self.get_log(),
2928
 
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR", ELLIPSIS))
 
2815
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
2816
                              r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')
2929
2817
 
2930
2818
    def test_get_unknown_prefix(self):
2931
2819
        tpr = self._get_registry()
2967
2855
                                                self.verbosity)
2968
2856
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
2969
2857
        self.assertLength(1, calls)
 
2858
 
 
2859
    def test_done(self):
 
2860
        """run_suite should call result.done()"""
 
2861
        self.calls = 0
 
2862
        def one_more_call(): self.calls += 1
 
2863
        def test_function():
 
2864
            pass
 
2865
        test = unittest.FunctionTestCase(test_function)
 
2866
        class InstrumentedTestResult(tests.ExtendedTestResult):
 
2867
            def done(self): one_more_call()
 
2868
        class MyRunner(tests.TextTestRunner):
 
2869
            def run(self, test):
 
2870
                return InstrumentedTestResult(self.stream, self.descriptions,
 
2871
                                              self.verbosity)
 
2872
        tests.run_suite(test, runner_class=MyRunner, stream=StringIO())
 
2873
        self.assertEquals(1, self.calls)