/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_osutils.py

More robusts tests for osutils.terminal_width().

* bzrlib/tests/test_osutils.py:
(TestTerminalWidth): Make the tests more robust.

* bzrlib/osutils.py:
(terminal_width): Document the implemented behavior. Delegate the
OS specific part to _terminal_size.
(_ioctl_terminal_size): The unix implementation for
_terminal_size.
(_win32_terminal_size): The windows implementation for
_terminal_size.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1928
1928
 
1929
1929
class TestTerminalWidth(tests.TestCase):
1930
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
 
1931
1945
    def test_default_values(self):
1932
 
        self.assertEquals(80, osutils.default_terminal_width)
 
1946
        self.assertEqual(80, osutils.default_terminal_width)
1933
1947
 
1934
1948
    def test_defaults_to_BZR_COLUMNS(self):
1935
1949
        # BZR_COLUMNS is set by the test framework
1936
 
        self.assertEquals('80', os.environ['BZR_COLUMNS'])
 
1950
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1937
1951
        os.environ['BZR_COLUMNS'] = '12'
1938
 
        self.assertEquals(12, osutils.terminal_width())
 
1952
        self.assertEqual(12, osutils.terminal_width())
1939
1953
 
1940
1954
    def test_falls_back_to_COLUMNS(self):
1941
1955
        del os.environ['BZR_COLUMNS']
 
1956
        self.assertNotEqual('42', os.environ['COLUMNS'])
1942
1957
        os.environ['COLUMNS'] = '42'
1943
 
        self.assertEquals(42, osutils.terminal_width())
 
1958
        self.assertEqual(42, osutils.terminal_width())
1944
1959
 
1945
1960
    def test_tty_default_without_columns(self):
1946
1961
        del os.environ['BZR_COLUMNS']
1947
1962
        del os.environ['COLUMNS']
1948
 
        orig_stdout = sys.stdout
1949
 
        def restore():
1950
 
            sys.stdout = orig_stdout
1951
 
        self.addCleanup(restore)
1952
1963
 
1953
1964
        class I_am_a_tty(object):
1954
1965
            def isatty(self):
1955
1966
                return True
1956
1967
 
1957
 
        sys.stdout = I_am_a_tty()
1958
 
        self.assertEquals(None, osutils.terminal_width())
 
1968
        def terminal_size(w, h):
 
1969
            return 42, 42
 
1970
 
 
1971
        self.replace_stdout(I_am_a_tty())
 
1972
        # We need to override the osutils definition as it depends on the
 
1973
        # running environment that we can't control (PQM running without a
 
1974
        # controlling terminal is one example).
 
1975
        self.replace__terminal_size(terminal_size)
 
1976
        self.assertEqual(42, osutils.terminal_width())
1959
1977
 
1960
1978
    def test_non_tty_default_without_columns(self):
1961
1979
        del os.environ['BZR_COLUMNS']
1962
1980
        del os.environ['COLUMNS']
1963
 
        orig_stdout = sys.stdout
1964
 
        def restore():
1965
 
            sys.stdout = orig_stdout
1966
 
        self.addCleanup(restore)
1967
 
        sys.stdout = None
1968
 
        self.assertEquals(None, osutils.terminal_width())
 
1981
        self.replace_stdout(None)
 
1982
        self.assertEqual(None, osutils.terminal_width())
1969
1983
 
1970
1984
    def test_no_TIOCGWINSZ(self):
1971
1985
        self.requireFeature(TermIOSFeature)
1983
1997
            del termios.TIOCGWINSZ
1984
1998
        del os.environ['BZR_COLUMNS']
1985
1999
        del os.environ['COLUMNS']
1986
 
        self.assertIs(None, osutils.terminal_width())
 
2000
        # Whatever the result is, if we don't raise an exception, it's ok.
 
2001
        osutils.terminal_width()