/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: Robert Collins
  • Date: 2009-05-23 20:57:12 UTC
  • mfrom: (4371 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090523205712-lcwbfqk6vwavinuv
MergeĀ .dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the test framework."""
18
18
 
28
28
from bzrlib import (
29
29
    branchbuilder,
30
30
    bzrdir,
 
31
    debug,
31
32
    errors,
 
33
    lockdir,
32
34
    memorytree,
33
35
    osutils,
34
36
    remote,
43
45
    weaverepo,
44
46
    )
45
47
from bzrlib.symbol_versioning import (
46
 
    one_zero,
47
 
    zero_eleven,
48
 
    zero_ten,
 
48
    deprecated_function,
 
49
    deprecated_in,
 
50
    deprecated_method,
49
51
    )
50
52
from bzrlib.tests import (
51
53
                          ChrootedTestCase,
619
621
        # But we have a safety net in place.
620
622
        self.assertRaises(AssertionError, self._check_safety_net)
621
623
 
 
624
    def test_dangling_locks_cause_failures(self):
 
625
        class TestDanglingLock(TestCaseWithMemoryTransport):
 
626
            def test_function(self):
 
627
                t = self.get_transport('.')
 
628
                l = lockdir.LockDir(t, 'lock')
 
629
                l.create()
 
630
                l.attempt_lock()
 
631
        test = TestDanglingLock('test_function')
 
632
        result = test.run()
 
633
        self.assertEqual(1, len(result.errors))
 
634
 
622
635
 
623
636
class TestTestCaseWithTransport(TestCaseWithTransport):
624
637
    """Tests for the convenience functions TestCaseWithTransport introduces."""
836
849
    def test_known_failure(self):
837
850
        """A KnownFailure being raised should trigger several result actions."""
838
851
        class InstrumentedTestResult(ExtendedTestResult):
839
 
 
 
852
            def done(self): pass
 
853
            def startTests(self): pass
840
854
            def report_test_start(self, test): pass
841
855
            def report_known_failure(self, test, err):
842
856
                self._call = test, err
884
898
        # text test output formatting
885
899
        pb = MockProgress()
886
900
        result = bzrlib.tests.TextTestResult(
887
 
            None,
 
901
            StringIO(),
888
902
            descriptions=0,
889
903
            verbosity=1,
890
904
            pb=pb,
924
938
    def test_add_not_supported(self):
925
939
        """Test the behaviour of invoking addNotSupported."""
926
940
        class InstrumentedTestResult(ExtendedTestResult):
 
941
            def done(self): pass
 
942
            def startTests(self): pass
927
943
            def report_test_start(self, test): pass
928
944
            def report_unsupported(self, test, feature):
929
945
                self._call = test, feature
966
982
        # text test output formatting
967
983
        pb = MockProgress()
968
984
        result = bzrlib.tests.TextTestResult(
969
 
            None,
 
985
            StringIO(),
970
986
            descriptions=0,
971
987
            verbosity=1,
972
988
            pb=pb,
994
1010
    def test_unavailable_exception(self):
995
1011
        """An UnavailableFeature being raised should invoke addNotSupported."""
996
1012
        class InstrumentedTestResult(ExtendedTestResult):
997
 
 
 
1013
            def done(self): pass
 
1014
            def startTests(self): pass
998
1015
            def report_test_start(self, test): pass
999
1016
            def addNotSupported(self, test, feature):
1000
1017
                self._call = test, feature
1037
1054
        self.assertTrue(result.wasStrictlySuccessful())
1038
1055
        self.assertEqual(None, result._extractBenchmarkTime(test))
1039
1056
 
 
1057
    def test_startTests(self):
 
1058
        """Starting the first test should trigger startTests."""
 
1059
        class InstrumentedTestResult(ExtendedTestResult):
 
1060
            calls = 0
 
1061
            def startTests(self): self.calls += 1
 
1062
            def report_test_start(self, test): pass
 
1063
        result = InstrumentedTestResult(None, None, None, None)
 
1064
        def test_function():
 
1065
            pass
 
1066
        test = unittest.FunctionTestCase(test_function)
 
1067
        test.run(result)
 
1068
        self.assertEquals(1, result.calls)
 
1069
 
1040
1070
 
1041
1071
class TestUnicodeFilenameFeature(TestCase):
1042
1072
 
1094
1124
            '----------------------------------------------------------------------',
1095
1125
            '',
1096
1126
            'FAILED (failures=1, known_failure_count=1)'],
1097
 
            lines[0:5] + lines[6:10] + lines[11:])
 
1127
            lines[3:8] + lines[9:13] + lines[14:])
1098
1128
 
1099
1129
    def test_known_failure_ok_run(self):
1100
1130
        # run a test that generates a known failure which should be printed in the final output.
1150
1180
        class SkippedTest(TestCase):
1151
1181
 
1152
1182
            def setUp(self):
 
1183
                TestCase.setUp(self)
1153
1184
                calls.append('setUp')
1154
1185
                self.addCleanup(self.cleanup)
1155
1186
 
1352
1383
class TestTestCase(TestCase):
1353
1384
    """Tests that test the core bzrlib TestCase."""
1354
1385
 
 
1386
    def test_assertLength_matches_empty(self):
 
1387
        a_list = []
 
1388
        self.assertLength(0, a_list)
 
1389
 
 
1390
    def test_assertLength_matches_nonempty(self):
 
1391
        a_list = [1, 2, 3]
 
1392
        self.assertLength(3, a_list)
 
1393
 
 
1394
    def test_assertLength_fails_different(self):
 
1395
        a_list = []
 
1396
        self.assertRaises(AssertionError, self.assertLength, 1, a_list)
 
1397
 
 
1398
    def test_assertLength_shows_sequence_in_failure(self):
 
1399
        a_list = [1, 2, 3]
 
1400
        exception = self.assertRaises(AssertionError, self.assertLength, 2,
 
1401
            a_list)
 
1402
        self.assertEqual('Incorrect length: wanted 2, got 3 for [1, 2, 3]',
 
1403
            exception.args[0])
 
1404
 
 
1405
    def test_base_setUp_not_called_causes_failure(self):
 
1406
        class TestCaseWithBrokenSetUp(TestCase):
 
1407
            def setUp(self):
 
1408
                pass # does not call TestCase.setUp
 
1409
            def test_foo(self):
 
1410
                pass
 
1411
        test = TestCaseWithBrokenSetUp('test_foo')
 
1412
        result = unittest.TestResult()
 
1413
        test.run(result)
 
1414
        self.assertFalse(result.wasSuccessful())
 
1415
        self.assertEqual(1, result.testsRun)
 
1416
 
 
1417
    def test_base_tearDown_not_called_causes_failure(self):
 
1418
        class TestCaseWithBrokenTearDown(TestCase):
 
1419
            def tearDown(self):
 
1420
                pass # does not call TestCase.tearDown
 
1421
            def test_foo(self):
 
1422
                pass
 
1423
        test = TestCaseWithBrokenTearDown('test_foo')
 
1424
        result = unittest.TestResult()
 
1425
        test.run(result)
 
1426
        self.assertFalse(result.wasSuccessful())
 
1427
        self.assertEqual(1, result.testsRun)
 
1428
 
1355
1429
    def test_debug_flags_sanitised(self):
1356
1430
        """The bzrlib debug flags should be sanitised by setUp."""
1357
1431
        if 'allow_debug' in tests.selftest_debug_flags:
1610
1684
            self.assertListRaises, _TestException, success_generator)
1611
1685
 
1612
1686
 
1613
 
@symbol_versioning.deprecated_function(zero_eleven)
 
1687
# NB: Don't delete this; it's not actually from 0.11!
 
1688
@deprecated_function(deprecated_in((0, 11, 0)))
1614
1689
def sample_deprecated_function():
1615
1690
    """A deprecated function to test applyDeprecated with."""
1616
1691
    return 2
1623
1698
class ApplyDeprecatedHelper(object):
1624
1699
    """A helper class for ApplyDeprecated tests."""
1625
1700
 
1626
 
    @symbol_versioning.deprecated_method(zero_eleven)
 
1701
    @deprecated_method(deprecated_in((0, 11, 0)))
1627
1702
    def sample_deprecated_method(self, param_one):
1628
1703
        """A deprecated method for testing with."""
1629
1704
        return param_one
1631
1706
    def sample_normal_method(self):
1632
1707
        """A undeprecated method."""
1633
1708
 
1634
 
    @symbol_versioning.deprecated_method(zero_ten)
 
1709
    @deprecated_method(deprecated_in((0, 10, 0)))
1635
1710
    def sample_nested_deprecation(self):
1636
1711
        return sample_deprecated_function()
1637
1712
 
1652
1727
    def test_applyDeprecated_not_deprecated(self):
1653
1728
        sample_object = ApplyDeprecatedHelper()
1654
1729
        # calling an undeprecated callable raises an assertion
1655
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
1730
        self.assertRaises(AssertionError, self.applyDeprecated,
 
1731
            deprecated_in((0, 11, 0)),
1656
1732
            sample_object.sample_normal_method)
1657
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
 
1733
        self.assertRaises(AssertionError, self.applyDeprecated,
 
1734
            deprecated_in((0, 11, 0)),
1658
1735
            sample_undeprecated_function, "a param value")
1659
1736
        # calling a deprecated callable (function or method) with the wrong
1660
1737
        # expected deprecation fails.
1661
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
1738
        self.assertRaises(AssertionError, self.applyDeprecated,
 
1739
            deprecated_in((0, 10, 0)),
1662
1740
            sample_object.sample_deprecated_method, "a param value")
1663
 
        self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
 
1741
        self.assertRaises(AssertionError, self.applyDeprecated,
 
1742
            deprecated_in((0, 10, 0)),
1664
1743
            sample_deprecated_function)
1665
1744
        # calling a deprecated callable (function or method) with the right
1666
1745
        # expected deprecation returns the functions result.
1667
 
        self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
 
1746
        self.assertEqual("a param value",
 
1747
            self.applyDeprecated(deprecated_in((0, 11, 0)),
1668
1748
            sample_object.sample_deprecated_method, "a param value"))
1669
 
        self.assertEqual(2, self.applyDeprecated(zero_eleven,
 
1749
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 11, 0)),
1670
1750
            sample_deprecated_function))
1671
1751
        # calling a nested deprecation with the wrong deprecation version
1672
1752
        # fails even if a deeper nested function was deprecated with the
1673
1753
        # supplied version.
1674
1754
        self.assertRaises(AssertionError, self.applyDeprecated,
1675
 
            zero_eleven, sample_object.sample_nested_deprecation)
 
1755
            deprecated_in((0, 11, 0)), sample_object.sample_nested_deprecation)
1676
1756
        # calling a nested deprecation with the right deprecation value
1677
1757
        # returns the calls result.
1678
 
        self.assertEqual(2, self.applyDeprecated(zero_ten,
 
1758
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 10, 0)),
1679
1759
            sample_object.sample_nested_deprecation))
1680
1760
 
1681
1761
    def test_callDeprecated(self):
1823
1903
class TestSelftestFiltering(TestCase):
1824
1904
 
1825
1905
    def setUp(self):
 
1906
        TestCase.setUp(self)
1826
1907
        self.suite = TestUtil.TestSuite()
1827
1908
        self.loader = TestUtil.TestLoader()
1828
1909
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
2297
2378
                calls.append(test)
2298
2379
                return ExtendedTestResult(self.stream, self.descriptions,
2299
2380
                    self.verbosity)
2300
 
        run_suite(suite, runner_class=MyRunner)
 
2381
        run_suite(suite, runner_class=MyRunner, stream=StringIO())
2301
2382
        self.assertEqual(calls, [suite])
 
2383
 
 
2384
    def test_done(self):
 
2385
        """run_suite should call result.done()"""
 
2386
        self.calls = 0
 
2387
        def one_more_call(): self.calls += 1
 
2388
        def test_function():
 
2389
            pass
 
2390
        test = unittest.FunctionTestCase(test_function)
 
2391
        class InstrumentedTestResult(ExtendedTestResult):
 
2392
            def done(self): one_more_call()
 
2393
        class MyRunner(TextTestRunner):
 
2394
            def run(self, test):
 
2395
                return InstrumentedTestResult(self.stream, self.descriptions,
 
2396
                                              self.verbosity)
 
2397
        run_suite(test, runner_class=MyRunner, stream=StringIO())
 
2398
        self.assertEquals(1, self.calls)