diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-02-08 22:28:36 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-02-08 22:28:36 (GMT) |
commit | bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba (patch) | |
tree | 47232d9eb97758190b44700163d2706665224d7c /Lib/test/test_shutil.py | |
parent | 4195b5caea0fe1446160e78d69420732ead7e78b (diff) | |
download | cpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.zip cpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.tar.gz cpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.tar.bz2 |
Issue #13609: Add two functions to query the terminal size:
os.get_terminal_size (low level) and shutil.get_terminal_size (high level).
Patch by Zbigniew Jędrzejewski-Szmek.
Diffstat (limited to 'Lib/test/test_shutil.py')
-rw-r--r-- | Lib/test/test_shutil.py | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index c72bac2..4d0ef29 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -9,6 +9,7 @@ import os import os.path import errno import functools +import subprocess from test import support from test.support import TESTFN from os.path import splitdrive @@ -1267,10 +1268,55 @@ class TestCopyFile(unittest.TestCase): finally: os.rmdir(dst_dir) +class TermsizeTests(unittest.TestCase): + def test_does_not_crash(self): + """Check if get_terminal_size() returns a meaningful value. + + There's no easy portable way to actually check the size of the + terminal, so let's check if it returns something sensible instead. + """ + size = shutil.get_terminal_size() + self.assertGreater(size.columns, 0) + self.assertGreater(size.lines, 0) + + def test_os_environ_first(self): + "Check if environment variables have precedence" + + with support.EnvironmentVarGuard() as env: + env['COLUMNS'] = '777' + size = shutil.get_terminal_size() + self.assertEqual(size.columns, 777) + + with support.EnvironmentVarGuard() as env: + env['LINES'] = '888' + size = shutil.get_terminal_size() + self.assertEqual(size.lines, 888) + + @unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty") + def test_stty_match(self): + """Check if stty returns the same results ignoring env + + This test will fail if stdin and stdout are connected to + different terminals with different sizes. Nevertheless, such + situations should be pretty rare. + """ + try: + size = subprocess.check_output(['stty', 'size']).decode().split() + except (FileNotFoundError, subprocess.CalledProcessError): + self.skipTest("stty invocation failed") + expected = (int(size[1]), int(size[0])) # reversed order + + with support.EnvironmentVarGuard() as env: + del env['LINES'] + del env['COLUMNS'] + actual = shutil.get_terminal_size() + + self.assertEqual(expected, actual) def test_main(): - support.run_unittest(TestShutil, TestMove, TestCopyFile) + support.run_unittest(TestShutil, TestMove, TestCopyFile, + TermsizeTests) if __name__ == '__main__': test_main() |