Skip to content

Commit

Permalink
yet another attempt to fix ttyTest on travis
Browse files Browse the repository at this point in the history
  • Loading branch information
knipknap committed Mar 27, 2017
1 parent 22ec363 commit dcaaf8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
24 changes: 12 additions & 12 deletions Exscript/util/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ def get_terminal_size(default_rows=25, default_cols=80):
os.close(fd)

# Try `stty size`
devnull = open(os.devnull, 'w')
try:
process = Popen(['stty', 'size'], stderr=devnull, stdout=PIPE)
except OSError:
pass
else:
errcode = process.wait()
output = process.stdout.read()
devnull.close()
with open(os.devnull, 'w') as devnull:
try:
rows, cols = output.split()
return int(rows), int(cols)
except (ValueError, TypeError):
process = Popen(['stty', 'size'], stderr=devnull, stdout=PIPE,
close_fds=True)
except OSError:
pass
else:
errcode = process.wait()
output = process.stdout.read()
try:
rows, cols = output.split()
return int(rows), int(cols)
except (ValueError, TypeError):
pass

# Try environment variables.
try:
Expand Down
19 changes: 11 additions & 8 deletions tests/Exscript/util/ttyTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))

import tempfile
from subprocess import Popen, PIPE
import Exscript.util.tty


Expand Down Expand Up @@ -45,28 +46,30 @@ def testGetTerminalSize(self):
# now fail, and the default values are returned.
os.environ['LINES'] = ''
os.environ['COLUMNS'] = ''
self.assertEqual(get_terminal_size(), (int(25), int(80)))
self.assertEqual(get_terminal_size(10, 10), (int(10), int(10)))
self.assertEqual(get_terminal_size(), (25, 80))
self.assertEqual(get_terminal_size(10, 10), (10, 10))

# If the LINES and COLUMNS variables are set, they should be used.
os.environ['LINES'] = '1000'
os.environ['COLUMNS'] = '1000'
self.assertEqual(get_terminal_size(), (int(1000), int(1000)))
self.assertEqual(get_terminal_size(10, 10), (int(1000), int(1000)))
self.assertEqual(get_terminal_size(), (1000, 1000))
self.assertEqual(get_terminal_size(10, 10), (1000, 1000))

# If the stty program exists, it should be used.
os.environ['PATH'] = oldpath
try:
self.assertNotEqual(get_terminal_size(), (int(1000), int(1000)))
self.assertNotEqual(get_terminal_size(10, 10), (int(1000), int(1000)))
with Popen(['stty', 'size'], stdout=PIPE, stderr=PIPE,
close_fds=True):
self.assertNotEqual(get_terminal_size(), (1000, 1000))
self.assertNotEqual(get_terminal_size(10, 10), (1000, 1000))
except OSError:
pass # "stty" not found.

# Lastly, if stdin/stderr/stdout exist, they should tell us something.
os.environ['PATH'] = ''
self._unredirect()
self.assertNotEqual(get_terminal_size(), (int(1000), int(1000)))
self.assertNotEqual(get_terminal_size(10, 10), (int(1000), int(1000)))
self.assertNotEqual(get_terminal_size(), (1000, 1000))
self.assertNotEqual(get_terminal_size(10, 10), (1000, 1000))
os.environ['PATH'] = oldpath


Expand Down

0 comments on commit dcaaf8f

Please sign in to comment.