summaryrefslogtreecommitdiff
path: root/Lib/test/test_os.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-02-08 23:28:36 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2012-02-08 23:28:36 +0100
commit6d6dfa8d36c2aa6b62deac49aef8a06b934accd2 (patch)
treee792f5b5ea8fc85c7e3eaa7bd88380850c9cf76d /Lib/test/test_os.py
parentfbaf23ff50450c0981433e2fb5874f9ff627d79d (diff)
downloadcpython-6d6dfa8d36c2aa6b62deac49aef8a06b934accd2.tar.gz
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.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 4d27c2b314..8dd745a9c7 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__":