summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Quast <contact@jeffquast.com>2014-11-24 20:17:41 -0800
committerJeff Quast <contact@jeffquast.com>2014-11-24 20:17:41 -0800
commit47c6e415d2e8ab0f020c39cb0d8fa0cb7aafbbfb (patch)
treeb1aea5cc0f89013ee1fc2b7c18ddfbb7c35db58e
parent63bc227ffe1d0cb3ec029a2992fe43aa7671aa12 (diff)
downloadpexpect-git-47c6e415d2e8ab0f020c39cb0d8fa0cb7aafbbfb.tar.gz
Fix failing tests, on OSX at least. Pushing ..
-rw-r--r--tests/test_maxcanon.py140
-rwxr-xr-xtools/display-terminalinfo.py200
2 files changed, 340 insertions, 0 deletions
diff --git a/tests/test_maxcanon.py b/tests/test_maxcanon.py
new file mode 100644
index 0000000..4cb628c
--- /dev/null
+++ b/tests/test_maxcanon.py
@@ -0,0 +1,140 @@
+""" Module for canonical-mode tests. """
+import sys
+import os
+
+
+import pexpect
+from . import PexpectTestCase
+
+
+class TestCaseCanon(PexpectTestCase.PexpectTestCase):
+ """
+ Test expected Canonical mode behavior (limited input line length).
+
+ All systems use the value of MAX_CANON which can be found using
+ fpathconf(3) value PC_MAX_CANON -- with the exception of Linux.
+
+ Linux, though defining a value of 255, actually honors the value
+ of 4096 from linux kernel include file tty.h definition
+ N_TTY_BUF_SIZE.
+
+ Linux also does not honor IMAXBEL. termios(3) states, "Linux does not
+ implement this bit, and acts as if it is always set." Although these
+ tests ensure it is enabled, this is a non-op for Linux.
+
+ These tests only ensure the correctness of the behavior described by
+ the sendline() docstring. pexpect is not particularly involved in
+ these scenarios, though if we wish to expose some kind of interface
+ to tty.setraw, for example, these tests may be re-purposed as such.
+
+XXX Lastly, these tests are skipped on Travis-CI. It produces unexpected
+XXX behavior, seemingly differences in build machines and/or python
+XXX interpreters without any deterministic results.
+ """
+
+ def setUp(self):
+ super(TestCaseCanon, self).setUp()
+
+ if sys.platform.lower().startswith('linux'):
+ # linux is 4096, N_TTY_BUF_SIZE.
+ self.max_input = 4096
+ elif sys.platform.lower().startswith('sunos'):
+ # SunOS allows PC_MAX_CANON + 1; see
+ # https://bitbucket.org/illumos/illumos-gate/src/d07a59219ab7fd2a7f39eb47c46cf083c88e932f/usr/src/uts/common/io/ldterm.c?at=default#cl-1888
+ self.max_input = os.fpathconf(0, 'PC_MAX_CANON') + 1
+ else:
+ # All others (probably) limit exactly at PC_MAX_CANON
+ self.max_input = os.fpathconf(0, 'PC_MAX_CANON')
+
+ def test_under_max_canon(self):
+ " BEL is not sent by terminal driver at maximum bytes - 1. "
+ # given,
+ child = pexpect.spawn('bash', echo=False, timeout=5)
+ child.sendline('echo READY')
+ child.sendline('stty icanon imaxbel')
+ child.sendline('echo BEGIN; cat')
+
+ # some systems BEL on (maximum - 1), not able to receive CR,
+ # even though all characters up until then were received, they
+ # simply cannot be transmitted, as CR is part of the transmission.
+ send_bytes = self.max_input - 1
+
+ # exercise,
+ child.sendline('_' * send_bytes)
+
+ # fast forward beyond 'cat' command, as ^G can be found as part of
+ # set-xterm-title sequence of $PROMPT_COMMAND or $PS1.
+ child.expect_exact('BEGIN')
+
+ # verify, all input is found in echo output,
+ child.expect_exact('_' * send_bytes)
+
+ # BEL is not found,
+ with self.assertRaises(pexpect.TIMEOUT, timeout=5):
+ child.expect_exact('\a')
+
+ # cleanup,
+ child.sendeof() # exit cat(1)
+ child.sendeof() # exit bash(1)
+ child.expect(pexpect.EOF)
+ assert not child.isalive()
+ assert child.exitstatus == 0
+
+ def test_beyond_max_icanon(self):
+ " a single BEL is sent when maximum bytes is reached. "
+ # given,
+ child = pexpect.spawn('bash', echo=False, timeout=5)
+ child.sendline('stty icanon imaxbel erase ^H')
+ child.sendline('cat')
+ send_bytes = self.max_input
+
+ # exercise,
+ child.sendline('_' * send_bytes)
+ child.expect_exact('\a')
+
+ # exercise, we must now backspace to send CR.
+ child.sendcontrol('h')
+ child.sendline()
+
+ # verify the length of (maximum - 1) received by cat(1),
+ # which has written it back out,
+ child.expect_exact('_' * (send_bytes - 1))
+ # and not a byte more.
+ with self.assertRaises(pexpect.TIMEOUT):
+ child.expect_exact('_', timeout=1)
+
+ # cleanup,
+ child.sendeof() # exit cat(1)
+ child.sendeof() # exit bash(1)
+ child.expect_exact(pexpect.EOF)
+ assert not child.isalive()
+ assert child.exitstatus == 0
+
+ def test_max_no_icanon(self):
+ " may exceed maximum input bytes if canonical mode is disabled. "
+ # given,
+ child = pexpect.spawn('bash', echo=False, timeout=5)
+ child.sendline('stty -icanon imaxbel')
+ child.sendline('echo BEGIN; cat')
+ send_bytes = self.max_input + 11
+
+ # exercise,
+ child.sendline('_' * send_bytes)
+
+ # fast forward beyond 'cat' command, as ^G can be found as part of
+ # set-xterm-title sequence of $PROMPT_COMMAND or $PS1.
+ child.expect_exact('BEGIN')
+
+ # BEL is *not* found,
+ with self.assertRaises(pexpect.TIMEOUT):
+ child.expect_exact('\a', timeout=1)
+
+ # verify, all input is found in output,
+ child.expect_exact('_' * send_bytes)
+
+ # cleanup,
+ child.sendcontrol('c') # exit cat(1) (eof wont work in -icanon)
+ child.sendline('exit 0') # exit bash(1)
+ child.expect(pexpect.EOF)
+ assert not child.isalive()
+ assert child.exitstatus == 0
diff --git a/tools/display-terminalinfo.py b/tools/display-terminalinfo.py
new file mode 100755
index 0000000..b3aca73
--- /dev/null
+++ b/tools/display-terminalinfo.py
@@ -0,0 +1,200 @@
+#!/usr/bin/env python
+""" Display known information about our terminal. """
+from __future__ import print_function
+import termios
+import sys
+import os
+
+BITMAP_IFLAG = {
+ 'IGNBRK': 'ignore BREAK condition',
+ 'BRKINT': 'map BREAK to SIGINTR',
+ 'IGNPAR': 'ignore (discard) parity errors',
+ 'PARMRK': 'mark parity and framing errors',
+ 'INPCK': 'enable checking of parity errors',
+ 'ISTRIP': 'strip 8th bit off chars',
+ 'INLCR': 'map NL into CR',
+ 'IGNCR': 'ignore CR',
+ 'ICRNL': 'map CR to NL (ala CRMOD)',
+ 'IXON': 'enable output flow control',
+ 'IXOFF': 'enable input flow control',
+ 'IXANY': 'any char will restart after stop',
+ 'IMAXBEL': 'ring bell on input queue full',
+ 'IUCLC': 'translate upper case to lower case',
+}
+
+BITMAP_OFLAG = {
+ 'OPOST': 'enable following output processing',
+ 'ONLCR': 'map NL to CR-NL (ala CRMOD)',
+ 'OXTABS': 'expand tabs to spaces',
+ 'ONOEOT': 'discard EOT\'s `^D\' on output)',
+ 'OCRNL': 'map CR to NL',
+ 'OLCUC': 'translate lower case to upper case',
+ 'ONOCR': 'No CR output at column 0',
+ 'ONLRET': 'NL performs CR function',
+}
+
+BITMAP_CFLAG = {
+ 'CSIZE': 'character size mask',
+ 'CS5': '5 bits (pseudo)',
+ 'CS6': '6 bits',
+ 'CS7': '7 bits',
+ 'CS8': '8 bits',
+ 'CSTOPB': 'send 2 stop bits',
+ 'CREAD': 'enable receiver',
+ 'PARENB': 'parity enable',
+ 'PARODD': 'odd parity, else even',
+ 'HUPCL': 'hang up on last close',
+ 'CLOCAL': 'ignore modem status lines',
+ 'CCTS_OFLOW': 'CTS flow control of output',
+ 'CRTSCTS': 'same as CCTS_OFLOW',
+ 'CRTS_IFLOW': 'RTS flow control of input',
+ 'MDMBUF': 'flow control output via Carrier',
+}
+
+BITMAP_LFLAG = {
+ 'ECHOKE': 'visual erase for line kill',
+ 'ECHOE': 'visually erase chars',
+ 'ECHO': 'enable echoing',
+ 'ECHONL': 'echo NL even if ECHO is off',
+ 'ECHOPRT': 'visual erase mode for hardcopy',
+ 'ECHOCTL': 'echo control chars as ^(Char)',
+ 'ISIG': 'enable signals INTR, QUIT, [D]SUSP',
+ 'ICANON': 'canonicalize input lines',
+ 'ALTWERASE': 'use alternate WERASE algorithm',
+ 'IEXTEN': 'enable DISCARD and LNEXT',
+ 'EXTPROC': 'external processing',
+ 'TOSTOP': 'stop background jobs from output',
+ 'FLUSHO': 'output being flushed (state)',
+ 'NOKERNINFO': 'no kernel output from VSTATUS',
+ 'PENDIN': 'XXX retype pending input (state)',
+ 'NOFLSH': 'don\'t flush after interrupt',
+}
+
+CTLCHAR_INDEX = {
+ 'VEOF': 'EOF',
+ 'VEOL': 'EOL',
+ 'VEOL2': 'EOL2',
+ 'VERASE': 'ERASE',
+ 'VWERASE': 'WERASE',
+ 'VKILL': 'KILL',
+ 'VREPRINT': 'REPRINT',
+ 'VINTR': 'INTR',
+ 'VQUIT': 'QUIT',
+ 'VSUSP': 'SUSP',
+ 'VDSUSP': 'DSUSP',
+ 'VSTART': 'START',
+ 'VSTOP': 'STOP',
+ 'VLNEXT': 'LNEXT',
+ 'VDISCARD': 'DISCARD',
+ 'VMIN': '---',
+ 'VTIME': '---',
+ 'VSTATUS': 'STATUS',
+}
+
+
+
+def display_bitmask(kind, bitmap, value):
+ """ Display all matching bitmask values for ``value`` given ``bitmap``. """
+ col1_width = max(map(len, bitmap.keys() + [kind]))
+ col2_width = 7
+ FMT = '{name:>{col1_width}} {value:>{col2_width}} {description}'
+ print(FMT.format(name=kind,
+ value='Value',
+ description='Description',
+ col1_width=col1_width,
+ col2_width=col2_width))
+ print('{0} {1} {2}'.format('-' * col1_width,
+ '-' * col2_width,
+ '-' * max(map(len, bitmap.values()))))
+ for flag_name, description in bitmap.items():
+ try:
+ bitmask = getattr(termios, flag_name)
+ bit_val = 'on' if bool(value & bitmask) else 'off'
+ except AttributeError:
+ bit_val = 'undef'
+ print(FMT.format(name=flag_name,
+ value=bit_val,
+ description=description,
+ col1_width=col1_width,
+ col2_width=col2_width))
+ print()
+
+
+def display_ctl_chars(index, cc):
+ """ Display all control character indicies, names, and values. """
+ title = 'Special Character'
+ col1_width = len(title)
+ col2_width = max(map(len, index.values()))
+ FMT = '{idx:<{col1_width}} {name:<{col2_width}} {value}'
+ print('Special line Characters'.center(40).rstrip())
+ print(FMT.format(idx='Index',
+ name='Name',
+ value='Value',
+ col1_width=col1_width,
+ col2_width=col2_width))
+ print('{0} {1} {2}'.format('-' * col1_width,
+ '-' * col2_width,
+ '-' * 10))
+ for index_name, name in index.items():
+ try:
+ index = getattr(termios, index_name)
+ value = cc[index]
+ if value == b'\xff':
+ value = '_POSIX_VDISABLE'
+ else:
+ value = repr(value)
+ except AttributeError:
+ value = 'undef'
+ print(FMT.format(idx=index_name,
+ name=name,
+ value=value,
+ col1_width=col1_width,
+ col2_width=col2_width))
+ print()
+
+
+def display_conf(kind, names, getter):
+ col1_width = max(map(len, names))
+ FMT = '{name:>{col1_width}} {value}'
+ print(FMT.format(name=kind,
+ value='value',
+ col1_width=col1_width))
+ print('{0} {1}'.format('-' * col1_width, '-' * 27))
+ for name in names:
+ try:
+ value = getter(name)
+ except OSError as err:
+ value = err
+ print(FMT.format(name=name, value=value, col1_width=col1_width))
+ print()
+
+
+def main():
+ fd = sys.stdin.fileno()
+
+ display_conf(kind='pathconf',
+ names=os.pathconf_names,
+ getter=lambda name: os.fpathconf(fd, name))
+
+ (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(fd)
+ display_bitmask(kind='Input Mode',
+ bitmap=BITMAP_IFLAG,
+ value=iflag)
+ display_bitmask(kind='Output Mode',
+ bitmap=BITMAP_OFLAG,
+ value=oflag)
+ display_bitmask(kind='Control Mode',
+ bitmap=BITMAP_CFLAG,
+ value=cflag)
+ display_bitmask(kind='Local Mode',
+ bitmap=BITMAP_LFLAG,
+ value=lflag)
+ display_ctl_chars(index=CTLCHAR_INDEX,
+ cc=cc)
+ print('os.isatty({0}) => {1}'.format(fd, os.isatty(fd)))
+ print('os.ttyname({0}) => {1}'.format(fd, os.ttyname(fd)))
+ print('os.ctermid() => {0}'.format(os.ttyname(fd)))
+
+
+if __name__ == '__main__':
+ main()