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_os.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_os.py')
-rw-r--r-- | Lib/test/test_os.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 4d27c2b..8dd745a 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1840,6 +1840,43 @@ class Win32DeprecatedBytesAPI(unittest.TestCase): os.symlink, filename, filename) +@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size") +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. + """ + try: + size = os.get_terminal_size() + except OSError as e: + if e.errno == errno.EINVAL or sys.platform == "win32": + # Under win32 a generic OSError can be thrown if the + # handle cannot be retrieved + self.skipTest("failed to query terminal size") + raise + + self.assertGreater(size.columns, 0) + self.assertGreater(size.lines, 0) + + def test_stty_match(self): + """Check if stty returns the same results + + stty actually tests stdin, so get_terminal_size is invoked on + stdin explicitly. If stty succeeded, then get_terminal_size() + should work too. + """ + 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 + + actual = os.get_terminal_size(sys.__stdin__.fileno()) + self.assertEqual(expected, actual) + + @support.reap_threads def test_main(): support.run_unittest( @@ -1866,6 +1903,7 @@ def test_main(): ProgramPriorityTests, ExtendedAttributeTests, Win32DeprecatedBytesAPI, + TermsizeTests, ) if __name__ == "__main__": |