~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-26 10:49:57 UTC
  • mfrom: (4987.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100126104957-dmtqnc0pckuruyla
(vila,
        jam) Implement TestCase.overrideAttr to simplify tests setUp/cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1135
1135
            dirblock[:] = new_dirblock
1136
1136
 
1137
1137
    def _save_platform_info(self):
1138
 
        cur_winver = win32utils.winver
1139
 
        cur_fs_enc = osutils._fs_enc
1140
 
        cur_dir_reader = osutils._selected_dir_reader
1141
 
        def restore():
1142
 
            win32utils.winver = cur_winver
1143
 
            osutils._fs_enc = cur_fs_enc
1144
 
            osutils._selected_dir_reader = cur_dir_reader
1145
 
        self.addCleanup(restore)
 
1138
        self.overrideAttr(win32utils, 'winver')
 
1139
        self.overrideAttr(osutils, '_fs_enc')
 
1140
        self.overrideAttr(osutils, '_selected_dir_reader')
1146
1141
 
1147
1142
    def assertDirReaderIs(self, expected):
1148
1143
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1581
1576
        def cleanup():
1582
1577
            if 'BZR_TEST_ENV_VAR' in os.environ:
1583
1578
                del os.environ['BZR_TEST_ENV_VAR']
1584
 
 
1585
1579
        self.addCleanup(cleanup)
1586
1580
 
1587
1581
    def test_set(self):
1698
1692
 
1699
1693
    def setUp(self):
1700
1694
        tests.TestCaseInTempDir.setUp(self)
1701
 
 
1702
 
        # Save platform specific info and reset it
1703
 
        cur_dir_reader = osutils._selected_dir_reader
1704
 
 
1705
 
        def restore():
1706
 
            osutils._selected_dir_reader = cur_dir_reader
1707
 
        self.addCleanup(restore)
1708
 
 
1709
 
        osutils._selected_dir_reader = self._dir_reader_class()
 
1695
        self.overrideAttr(osutils,
 
1696
                          '_selected_dir_reader', self._dir_reader_class())
1710
1697
 
1711
1698
    def _get_ascii_tree(self):
1712
1699
        tree = [
1859
1846
 
1860
1847
    def setUp(self):
1861
1848
        super(TestConcurrency, self).setUp()
1862
 
        orig = osutils._cached_local_concurrency
1863
 
        def restore():
1864
 
            osutils._cached_local_concurrency = orig
1865
 
        self.addCleanup(restore)
 
1849
        self.overrideAttr(osutils, '_cached_local_concurrency')
1866
1850
 
1867
1851
    def test_local_concurrency(self):
1868
1852
        concurrency = osutils.local_concurrency()
1895
1879
 
1896
1880
    def setUp(self):
1897
1881
        super(TestFailedToLoadExtension, self).setUp()
1898
 
        self.saved_failures = osutils._extension_load_failures[:]
1899
 
        del osutils._extension_load_failures[:]
1900
 
        self.addCleanup(self.restore_failures)
1901
 
 
1902
 
    def restore_failures(self):
1903
 
        osutils._extension_load_failures = self.saved_failures
 
1882
        self.overrideAttr(osutils, '_extension_load_failures', [])
1904
1883
 
1905
1884
    def test_failure_to_load(self):
1906
1885
        self._try_loading()
1929
1908
class TestTerminalWidth(tests.TestCase):
1930
1909
 
1931
1910
    def replace_stdout(self, new):
1932
 
        orig_stdout = sys.stdout
1933
 
        def restore():
1934
 
            sys.stdout = orig_stdout
1935
 
        self.addCleanup(restore)
1936
 
        sys.stdout = new
 
1911
        self.overrideAttr(sys, 'stdout', new)
1937
1912
 
1938
1913
    def replace__terminal_size(self, new):
1939
 
        orig__terminal_size = osutils._terminal_size
1940
 
        def restore():
1941
 
            osutils._terminal_size = orig__terminal_size
1942
 
        self.addCleanup(restore)
1943
 
        osutils._terminal_size = new
 
1914
        self.overrideAttr(osutils, '_terminal_size', new)
1944
1915
 
1945
1916
    def set_fake_tty(self):
1946
1917
 
1996
1967
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1997
1968
            pass
1998
1969
        else:
1999
 
            def restore():
2000
 
                termios.TIOCGWINSZ = orig
2001
 
            self.addCleanup(restore)
 
1970
            self.overrideAttr(termios, 'TIOCGWINSZ')
2002
1971
            del termios.TIOCGWINSZ
2003
1972
        del os.environ['BZR_COLUMNS']
2004
1973
        del os.environ['COLUMNS']