/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: 2008-02-06 04:06:42 UTC
  • mfrom: (3216 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3217.
  • Revision ID: robertc@robertcollins.net-20080206040642-2efx3l4iv5f95lxp
Merge up with bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    osutils,
33
33
    repository,
34
34
    symbol_versioning,
 
35
    tests,
35
36
    )
36
37
from bzrlib.progress import _BaseProgressBar
37
38
from bzrlib.repofmt import weaverepo
38
39
from bzrlib.symbol_versioning import (
39
 
        zero_ten,
40
 
        zero_eleven,
41
 
        )
 
40
    one_zero,
 
41
    zero_eleven,
 
42
    zero_ten,
 
43
    )
42
44
from bzrlib.tests import (
43
45
                          ChrootedTestCase,
44
46
                          ExtendedTestResult,
54
56
                          TestUtil,
55
57
                          TextTestRunner,
56
58
                          UnavailableFeature,
 
59
                          condition_id_re,
 
60
                          condition_isinstance,
 
61
                          exclude_tests_by_condition,
 
62
                          exclude_tests_by_re,
 
63
                          filter_suite_by_condition,
 
64
                          filter_suite_by_re,
57
65
                          iter_suite_tests,
58
 
                          filter_suite_by_re,
 
66
                          preserve_input,
 
67
                          randomize_suite,
59
68
                          sort_suite_by_re,
 
69
                          split_suite_by_re,
60
70
                          test_lsprof,
61
71
                          test_suite,
62
72
                          )
521
531
        # because each optimiser can be direction specific, we need to test
522
532
        # each optimiser in its chosen direction.
523
533
        # unlike the TestProviderAdapter we dont want to automatically add a
524
 
        # parameterised one for WorkingTree - the optimisers will tell us what
 
534
        # parameterized one for WorkingTree - the optimisers will tell us what
525
535
        # ones to add.
526
536
        from bzrlib.tests.tree_implementations import (
527
537
            return_parameter,
647
657
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
648
658
 
649
659
    def test_get_readonly_url_http(self):
650
 
        from bzrlib.tests.HttpServer import HttpServer
 
660
        from bzrlib.tests.http_server import HttpServer
651
661
        from bzrlib.transport import get_transport
652
662
        from bzrlib.transport.local import LocalURLServer
653
663
        from bzrlib.transport.http import HttpTransportBase
1211
1221
            revision_id = workingtree.get_parent_ids()[0]
1212
1222
            self.assertEndsWith(output_string.rstrip(), revision_id)
1213
1223
 
 
1224
    def assertLogDeleted(self, test):
 
1225
        log = test._get_log()
 
1226
        self.assertEqual("DELETED log file to reduce memory footprint", log)
 
1227
        self.assertEqual('', test._log_contents)
 
1228
        self.assertIs(None, test._log_file_name)
 
1229
 
1214
1230
    def test_success_log_deleted(self):
1215
1231
        """Successful tests have their log deleted"""
1216
1232
 
1224
1240
        test = LogTester('test_success')
1225
1241
        result = self.run_test_runner(runner, test)
1226
1242
 
1227
 
        log = test._get_log()
1228
 
        self.assertEqual("DELETED log file to reduce memory footprint", log)
1229
 
        self.assertEqual('', test._log_contents)
1230
 
        self.assertIs(None, test._log_file_name)
 
1243
        self.assertLogDeleted(test)
 
1244
 
 
1245
    def test_skipped_log_deleted(self):
 
1246
        """Skipped tests have their log deleted"""
 
1247
 
 
1248
        class LogTester(TestCase):
 
1249
 
 
1250
            def test_skipped(self):
 
1251
                self.log('this will be removed\n')
 
1252
                raise tests.TestSkipped()
 
1253
 
 
1254
        sio = cStringIO.StringIO()
 
1255
        runner = TextTestRunner(stream=sio)
 
1256
        test = LogTester('test_skipped')
 
1257
        result = self.run_test_runner(runner, test)
 
1258
 
 
1259
        self.assertLogDeleted(test)
 
1260
 
 
1261
    def test_not_aplicable_log_deleted(self):
 
1262
        """Not applicable tests have their log deleted"""
 
1263
 
 
1264
        class LogTester(TestCase):
 
1265
 
 
1266
            def test_not_applicable(self):
 
1267
                self.log('this will be removed\n')
 
1268
                raise tests.TestNotApplicable()
 
1269
 
 
1270
        sio = cStringIO.StringIO()
 
1271
        runner = TextTestRunner(stream=sio)
 
1272
        test = LogTester('test_not_applicable')
 
1273
        result = self.run_test_runner(runner, test)
 
1274
 
 
1275
        self.assertLogDeleted(test)
 
1276
 
 
1277
    def test_known_failure_log_deleted(self):
 
1278
        """Know failure tests have their log deleted"""
 
1279
 
 
1280
        class LogTester(TestCase):
 
1281
 
 
1282
            def test_known_failure(self):
 
1283
                self.log('this will be removed\n')
 
1284
                raise tests.KnownFailure()
 
1285
 
 
1286
        sio = cStringIO.StringIO()
 
1287
        runner = TextTestRunner(stream=sio)
 
1288
        test = LogTester('test_known_failure')
 
1289
        result = self.run_test_runner(runner, test)
 
1290
 
 
1291
        self.assertLogDeleted(test)
1231
1292
 
1232
1293
    def test_fail_log_kept(self):
1233
1294
        """Failed tests have their log kept"""
1660
1721
        self.loader = TestUtil.TestLoader()
1661
1722
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1662
1723
            'bzrlib.tests.test_selftest']))
1663
 
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
 
1724
        self.all_names = self._test_ids(self.suite)
 
1725
 
 
1726
    def _test_ids(self, test_suite):
 
1727
        """Get the ids for the tests in a test suite."""
 
1728
        return [t.id() for t in iter_suite_tests(test_suite)]
 
1729
 
 
1730
    def test_condition_id_re(self):
 
1731
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1732
            'test_condition_id_re')
 
1733
        filtered_suite = filter_suite_by_condition(self.suite,
 
1734
            condition_id_re('test_condition_id_re'))
 
1735
        self.assertEqual([test_name], self._test_ids(filtered_suite))
 
1736
 
 
1737
    def test_condition_id_in_list(self):
 
1738
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1739
                      'test_condition_id_in_list']
 
1740
        id_list = tests.TestIdList(test_names)
 
1741
        filtered_suite = filter_suite_by_condition(
 
1742
            self.suite, tests.condition_id_in_list(id_list))
 
1743
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
 
1744
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
 
1745
        self.assertEqual(self._test_ids(re_filtered),
 
1746
                         self._test_ids(filtered_suite))
 
1747
 
 
1748
    def test_condition_isinstance(self):
 
1749
        filtered_suite = filter_suite_by_condition(self.suite,
 
1750
            condition_isinstance(self.__class__))
 
1751
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1752
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
 
1753
        self.assertEqual(self._test_ids(re_filtered),
 
1754
            self._test_ids(filtered_suite))
 
1755
 
 
1756
    def test_exclude_tests_by_condition(self):
 
1757
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1758
            'test_exclude_tests_by_condition')
 
1759
        filtered_suite = exclude_tests_by_condition(self.suite,
 
1760
            lambda x:x.id() == excluded_name)
 
1761
        self.assertEqual(len(self.all_names) - 1,
 
1762
            filtered_suite.countTestCases())
 
1763
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1764
        remaining_names = list(self.all_names)
 
1765
        remaining_names.remove(excluded_name)
 
1766
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1767
 
 
1768
    def test_exclude_tests_by_re(self):
 
1769
        self.all_names = self._test_ids(self.suite)
 
1770
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
 
1771
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1772
            'test_exclude_tests_by_re')
 
1773
        self.assertEqual(len(self.all_names) - 1,
 
1774
            filtered_suite.countTestCases())
 
1775
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1776
        remaining_names = list(self.all_names)
 
1777
        remaining_names.remove(excluded_name)
 
1778
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1779
 
 
1780
    def test_filter_suite_by_condition(self):
 
1781
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1782
            'test_filter_suite_by_condition')
 
1783
        filtered_suite = filter_suite_by_condition(self.suite,
 
1784
            lambda x:x.id() == test_name)
 
1785
        self.assertEqual([test_name], self._test_ids(filtered_suite))
1664
1786
 
1665
1787
    def test_filter_suite_by_re(self):
1666
 
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1667
 
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
 
1788
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1789
        filtered_names = self._test_ids(filtered_suite)
1668
1790
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1669
1791
            'TestSelftestFiltering.test_filter_suite_by_re'])
1670
 
            
 
1792
 
 
1793
    def test_filter_suite_by_id_list(self):
 
1794
        test_list = ['bzrlib.tests.test_selftest.'
 
1795
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
 
1796
        filtered_suite = tests.filter_suite_by_id_list(
 
1797
            self.suite, tests.TestIdList(test_list))
 
1798
        filtered_names = self._test_ids(filtered_suite)
 
1799
        self.assertEqual(
 
1800
            filtered_names,
 
1801
            ['bzrlib.tests.test_selftest.'
 
1802
             'TestSelftestFiltering.test_filter_suite_by_id_list'])
 
1803
 
 
1804
    def test_preserve_input(self):
 
1805
        # NB: Surely this is something in the stdlib to do this?
 
1806
        self.assertTrue(self.suite is preserve_input(self.suite))
 
1807
        self.assertTrue("@#$" is preserve_input("@#$"))
 
1808
 
 
1809
    def test_randomize_suite(self):
 
1810
        randomized_suite = randomize_suite(self.suite)
 
1811
        # randomizing should not add or remove test names.
 
1812
        self.assertEqual(set(self._test_ids(self.suite)),
 
1813
            set(self._test_ids(randomized_suite)))
 
1814
        # Technically, this *can* fail, because random.shuffle(list) can be
 
1815
        # equal to list. Trying multiple times just pushes the frequency back.
 
1816
        # As its len(self.all_names)!:1, the failure frequency should be low
 
1817
        # enough to ignore. RBC 20071021.
 
1818
        # It should change the order.
 
1819
        self.assertNotEqual(self.all_names, self._test_ids(randomized_suite))
 
1820
        # But not the length. (Possibly redundant with the set test, but not
 
1821
        # necessarily.)
 
1822
        self.assertEqual(len(self.all_names),
 
1823
            len(self._test_ids(randomized_suite)))
 
1824
 
1671
1825
    def test_sort_suite_by_re(self):
1672
 
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1673
 
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
 
1826
        sorted_suite = self.applyDeprecated(one_zero,
 
1827
            sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
 
1828
        sorted_names = self._test_ids(sorted_suite)
1674
1829
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1675
1830
            'TestSelftestFiltering.test_filter_suite_by_re')
1676
1831
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1677
1832
 
 
1833
    def test_split_suit_by_re(self):
 
1834
        self.all_names = self._test_ids(self.suite)
 
1835
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1836
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1837
            'test_filter_suite_by_re')
 
1838
        self.assertEqual([filtered_name], self._test_ids(split_suite[0]))
 
1839
        self.assertFalse(filtered_name in self._test_ids(split_suite[1]))
 
1840
        remaining_names = list(self.all_names)
 
1841
        remaining_names.remove(filtered_name)
 
1842
        self.assertEqual(remaining_names, self._test_ids(split_suite[1]))
 
1843
 
1678
1844
 
1679
1845
class TestCheckInventoryShape(TestCaseWithTransport):
1680
1846
 
1713
1879
        # code.
1714
1880
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
1715
1881
        self.assertEqual(out, '')
1716
 
        self.assertEqual(err, 'bzr: ERROR: Not a branch: "/nonexistantpath/".\n')
 
1882
        self.assertContainsRe(err,
 
1883
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
 
1884
 
 
1885
 
 
1886
class TestTestLoader(TestCase):
 
1887
    """Tests for the test loader."""
 
1888
 
 
1889
    def _get_loader_and_module(self):
 
1890
        """Gets a TestLoader and a module with one test in it."""
 
1891
        loader = TestUtil.TestLoader()
 
1892
        module = {}
 
1893
        class Stub(TestCase):
 
1894
            def test_foo(self):
 
1895
                pass
 
1896
        class MyModule(object):
 
1897
            pass
 
1898
        MyModule.a_class = Stub
 
1899
        module = MyModule()
 
1900
        return loader, module
 
1901
 
 
1902
    def test_module_no_load_tests_attribute_loads_classes(self):
 
1903
        loader, module = self._get_loader_and_module()
 
1904
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
 
1905
 
 
1906
    def test_module_load_tests_attribute_gets_called(self):
 
1907
        loader, module = self._get_loader_and_module()
 
1908
        # 'self' is here because we're faking the module with a class. Regular
 
1909
        # load_tests do not need that :)
 
1910
        def load_tests(self, standard_tests, module, loader):
 
1911
            result = loader.suiteClass()
 
1912
            for test in iter_suite_tests(standard_tests):
 
1913
                result.addTests([test, test])
 
1914
            return result
 
1915
        # add a load_tests() method which multiplies the tests from the module.
 
1916
        module.__class__.load_tests = load_tests
 
1917
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
 
1918
 
 
1919
 
 
1920
class TestTestIdList(tests.TestCase):
 
1921
 
 
1922
    def _create_id_list(self, test_list):
 
1923
        return tests.TestIdList(test_list)
 
1924
 
 
1925
    def _create_suite(self, test_id_list):
 
1926
 
 
1927
        class Stub(TestCase):
 
1928
            def test_foo(self):
 
1929
                pass
 
1930
 
 
1931
        def _create_test_id(id):
 
1932
            return lambda: id
 
1933
 
 
1934
        suite = TestUtil.TestSuite()
 
1935
        for id in test_id_list:
 
1936
            t  = Stub('test_foo')
 
1937
            t.id = _create_test_id(id)
 
1938
            suite.addTest(t)
 
1939
        return suite
 
1940
 
 
1941
    def _test_ids(self, test_suite):
 
1942
        """Get the ids for the tests in a test suite."""
 
1943
        return [t.id() for t in iter_suite_tests(test_suite)]
 
1944
 
 
1945
    def test_empty_list(self):
 
1946
        id_list = self._create_id_list([])
 
1947
        self.assertEquals({}, id_list.tests)
 
1948
        self.assertEquals({}, id_list.modules)
 
1949
 
 
1950
    def test_valid_list(self):
 
1951
        id_list = self._create_id_list(
 
1952
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
 
1953
             'mod1.func1', 'mod1.cl2.meth2',
 
1954
             'mod1.submod1',
 
1955
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
 
1956
             ])
 
1957
        self.assertTrue(id_list.is_module_name_used('mod1'))
 
1958
        self.assertTrue(id_list.is_module_name_used('mod1.submod1'))
 
1959
        self.assertTrue(id_list.is_module_name_used('mod1.submod2'))
 
1960
        self.assertTrue(id_list.test_in('mod1.cl1.meth1'))
 
1961
        self.assertTrue(id_list.test_in('mod1.submod1'))
 
1962
        self.assertTrue(id_list.test_in('mod1.func1'))
 
1963
 
 
1964
    def test_bad_chars_in_params(self):
 
1965
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
 
1966
        self.assertTrue(id_list.is_module_name_used('mod1'))
 
1967
        self.assertTrue(id_list.test_in('mod1.cl1.meth1(xx.yy)'))
 
1968
 
 
1969
    def test_module_used(self):
 
1970
        id_list = self._create_id_list(['mod.class.meth'])
 
1971
        self.assertTrue(id_list.is_module_name_used('mod'))
 
1972
        self.assertTrue(id_list.is_module_name_used('mod.class'))
 
1973
        self.assertTrue(id_list.is_module_name_used('mod.class.meth'))
 
1974
 
 
1975
    def test_test_suite(self):
 
1976
        # This test is slow, so we do a single test with one test in each
 
1977
        # category
 
1978
        test_list = [
 
1979
            # testmod_names
 
1980
            'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
 
1981
            # transport implementations
 
1982
            'bzrlib.tests.test_transport_implementations.TransportTests'
 
1983
            '.test_abspath(LocalURLServer)',
 
1984
            # packages_to_test()
 
1985
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
 
1986
            # MODULES_TO_DOCTEST
 
1987
            'bzrlib.timestamp.format_highres_date',
 
1988
            # plugins can't be tested that way since selftest may be run with
 
1989
            # --no-plugins
 
1990
            ]
 
1991
        suite = tests.test_suite(test_list)
 
1992
        self.assertEquals(test_list, self._test_ids(suite))
 
1993
 
 
1994
 
 
1995
class TestLoadTestIdList(tests.TestCaseInTempDir):
 
1996
 
 
1997
    def _create_test_list_file(self, file_name, content):
 
1998
        fl = open(file_name, 'wt')
 
1999
        fl.write(content)
 
2000
        fl.close()
 
2001
 
 
2002
    def test_load_unknown(self):
 
2003
        self.assertRaises(errors.NoSuchFile,
 
2004
                          tests.load_test_id_list, 'i_do_not_exist')
 
2005
 
 
2006
    def test_load_test_list(self):
 
2007
        test_list_fname = 'test.list'
 
2008
        self._create_test_list_file(test_list_fname,
 
2009
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
 
2010
        tlist = tests.load_test_id_list(test_list_fname)
 
2011
        self.assertEquals(2, len(tlist))
 
2012
        self.assertEquals('mod1.cl1.meth1', tlist[0])
 
2013
        self.assertEquals('mod2.cl2.meth2', tlist[1])
 
2014
 
 
2015
    def test_load_dirty_file(self):
 
2016
        test_list_fname = 'test.list'
 
2017
        self._create_test_list_file(test_list_fname,
 
2018
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
 
2019
                                    'bar baz\n')
 
2020
        tlist = tests.load_test_id_list(test_list_fname)
 
2021
        self.assertEquals(4, len(tlist))
 
2022
        self.assertEquals('mod1.cl1.meth1', tlist[0])
 
2023
        self.assertEquals('', tlist[1])
 
2024
        self.assertEquals('mod2.cl2.meth2', tlist[2])
 
2025
        self.assertEquals('bar baz', tlist[3])
 
2026
 
 
2027