~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Andrew Bennetts
  • Date: 2010-01-15 03:58:20 UTC
  • mfrom: (4963 +trunk)
  • mto: (4973.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4975.
  • Revision ID: andrew.bennetts@canonical.com-20100115035820-ilb3t36swgzq6v1l
MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import socket
24
24
import stat
25
25
import sys
26
 
import termios
27
26
import time
28
27
 
29
28
from bzrlib import (
54
53
 
55
54
UTF8DirReaderFeature = _UTF8DirReaderFeature()
56
55
 
 
56
term_ios_feature = tests.ModuleAvailableFeature('termios')
 
57
 
57
58
 
58
59
def _already_unicode(s):
59
60
    return s
81
82
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
82
83
                               _native_to_unicode=_utf8_to_unicode)))
83
84
 
84
 
    if test__walkdirs_win32.Win32ReadDirFeature.available():
 
85
    if test__walkdirs_win32.win32_readdir_feature.available():
85
86
        try:
86
87
            from bzrlib import _walkdirs_win32
87
88
            scenarios.append(
988
989
 
989
990
    def test_osutils_binding(self):
990
991
        from bzrlib.tests import test__chunks_to_lines
991
 
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
 
992
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
992
993
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
993
994
        else:
994
995
            from bzrlib._chunks_to_lines_py import chunks_to_lines
1180
1181
 
1181
1182
    def test_force_walkdirs_utf8_nt(self):
1182
1183
        # Disabled because the thunk of the whole walkdirs api is disabled.
1183
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1184
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1184
1185
        self._save_platform_info()
1185
1186
        win32utils.winver = 'Windows NT'
1186
1187
        from bzrlib._walkdirs_win32 import Win32ReadDir
1187
1188
        self.assertDirReaderIs(Win32ReadDir)
1188
1189
 
1189
1190
    def test_force_walkdirs_utf8_98(self):
1190
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1191
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1191
1192
        self._save_platform_info()
1192
1193
        win32utils.winver = 'Windows 98'
1193
1194
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1344
1345
        self.assertEqual(expected_dirblocks, result)
1345
1346
 
1346
1347
    def test__walkdirs_utf8_win32readdir(self):
1347
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1348
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1348
1349
        self.requireFeature(tests.UnicodeFilenameFeature)
1349
1350
        from bzrlib._walkdirs_win32 import Win32ReadDir
1350
1351
        self._save_platform_info()
1401
1402
 
1402
1403
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1403
1404
        """make sure our Stat values are valid"""
1404
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1405
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1405
1406
        self.requireFeature(tests.UnicodeFilenameFeature)
1406
1407
        from bzrlib._walkdirs_win32 import Win32ReadDir
1407
1408
        name0u = u'0file-\xb6'
1425
1426
 
1426
1427
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1427
1428
        """make sure our Stat values are valid"""
1428
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1429
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1429
1430
        self.requireFeature(tests.UnicodeFilenameFeature)
1430
1431
        from bzrlib._walkdirs_win32 import Win32ReadDir
1431
1432
        name0u = u'0dir-\u062c\u0648'
1927
1928
 
1928
1929
class TestTerminalWidth(tests.TestCase):
1929
1930
 
 
1931
    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
 
1937
 
 
1938
    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
 
1944
 
 
1945
    def set_fake_tty(self):
 
1946
 
 
1947
        class I_am_a_tty(object):
 
1948
            def isatty(self):
 
1949
                return True
 
1950
 
 
1951
        self.replace_stdout(I_am_a_tty())
 
1952
 
1930
1953
    def test_default_values(self):
1931
 
        self.assertEquals(80, osutils.default_terminal_width)
 
1954
        self.assertEqual(80, osutils.default_terminal_width)
1932
1955
 
1933
1956
    def test_defaults_to_BZR_COLUMNS(self):
1934
1957
        # BZR_COLUMNS is set by the test framework
1935
 
        self.assertEquals('80', os.environ['BZR_COLUMNS'])
 
1958
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1936
1959
        os.environ['BZR_COLUMNS'] = '12'
1937
 
        self.assertEquals(12, osutils.terminal_width())
 
1960
        self.assertEqual(12, osutils.terminal_width())
 
1961
 
 
1962
    def test_falls_back_to_COLUMNS(self):
 
1963
        del os.environ['BZR_COLUMNS']
 
1964
        self.assertNotEqual('42', os.environ['COLUMNS'])
 
1965
        self.set_fake_tty()
 
1966
        os.environ['COLUMNS'] = '42'
 
1967
        self.assertEqual(42, osutils.terminal_width())
1938
1968
 
1939
1969
    def test_tty_default_without_columns(self):
1940
1970
        del os.environ['BZR_COLUMNS']
1941
1971
        del os.environ['COLUMNS']
1942
 
        orig_stdout = sys.stdout
1943
 
        def restore():
1944
 
            sys.stdout = orig_stdout
1945
 
        self.addCleanup(restore)
1946
 
 
1947
 
        class I_am_a_tty(object):
1948
 
            def isatty(self):
1949
 
                return True
1950
 
 
1951
 
        sys.stdout = I_am_a_tty()
1952
 
        self.assertEquals(None, osutils.terminal_width())
 
1972
 
 
1973
        def terminal_size(w, h):
 
1974
            return 42, 42
 
1975
 
 
1976
        self.set_fake_tty()
 
1977
        # We need to override the osutils definition as it depends on the
 
1978
        # running environment that we can't control (PQM running without a
 
1979
        # controlling terminal is one example).
 
1980
        self.replace__terminal_size(terminal_size)
 
1981
        self.assertEqual(42, osutils.terminal_width())
1953
1982
 
1954
1983
    def test_non_tty_default_without_columns(self):
1955
1984
        del os.environ['BZR_COLUMNS']
1956
1985
        del os.environ['COLUMNS']
1957
 
        orig_stdout = sys.stdout
1958
 
        def restore():
1959
 
            sys.stdout = orig_stdout
1960
 
        self.addCleanup(restore)
1961
 
        sys.stdout = None
1962
 
        self.assertEquals(None, osutils.terminal_width())
 
1986
        self.replace_stdout(None)
 
1987
        self.assertEqual(None, osutils.terminal_width())
1963
1988
 
1964
 
    def test_TIOCGWINSZ(self):
 
1989
    def test_no_TIOCGWINSZ(self):
 
1990
        self.requireFeature(term_ios_feature)
 
1991
        termios = term_ios_feature.module
1965
1992
        # bug 63539 is about a termios without TIOCGWINSZ attribute
1966
 
        exist = True
1967
1993
        try:
1968
1994
            orig = termios.TIOCGWINSZ
1969
1995
        except AttributeError:
1970
 
            exist = False
1971
 
 
1972
 
        def restore():
1973
 
            if exist:
 
1996
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
 
1997
            pass
 
1998
        else:
 
1999
            def restore():
1974
2000
                termios.TIOCGWINSZ = orig
1975
 
        self.addCleanup(restore)
1976
 
 
1977
 
        del termios.TIOCGWINSZ
 
2001
            self.addCleanup(restore)
 
2002
            del termios.TIOCGWINSZ
1978
2003
        del os.environ['BZR_COLUMNS']
1979
2004
        del os.environ['COLUMNS']
1980
 
        self.assertEquals(None, osutils.terminal_width())
 
2005
        # Whatever the result is, if we don't raise an exception, it's ok.
 
2006
        osutils.terminal_width()