summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJeff Quast <contact@jeffquast.com>2015-09-20 19:43:31 -0700
committerJeff Quast <contact@jeffquast.com>2015-09-20 19:43:31 -0700
commitb6e25ba148a4ffdf5ba0e4ba61aa78f449773cca (patch)
treeba2eec8a8bd1df7f0b21115f8ae8896db9ad364c /tests
parent7f046a6cf86d8f60a6cf23c40ef625e5acbc1a32 (diff)
parentf5993888e092bd8cecc98ac9558700d4fe8624cf (diff)
downloadpexpect-git-document-blocking-write.tar.gz
Merge remote-tracking branch 'origin/master' into document-blocking-writedocument-blocking-write
Diffstat (limited to 'tests')
-rwxr-xr-xtests/getch.py6
-rwxr-xr-xtests/interact.py16
-rw-r--r--tests/interact_unicode.py24
-rwxr-xr-xtests/test_interact.py67
-rwxr-xr-xtests/test_isalive.py27
5 files changed, 71 insertions, 69 deletions
diff --git a/tests/getch.py b/tests/getch.py
index 41e3224..a362e52 100755
--- a/tests/getch.py
+++ b/tests/getch.py
@@ -18,6 +18,7 @@ PEXPECT LICENSE
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
+from __future__ import print_function
import sys, tty, termios
if hasattr(sys.stdin, 'buffer'):
@@ -27,13 +28,13 @@ else:
stdin = sys.stdin
def main():
- print('READY')
+ print('READY', end='\r\n')
while True:
try:
val = ord(stdin.read(1))
except KeyboardInterrupt:
val = 3
- sys.stdout.write('%d<STOP>\r\n' % (val,))
+ print('%d<STOP>' % (val,), end='\r\n')
if val == 0:
# StopIteration equivalent is ctrl+' ' (\x00, NUL)
break
@@ -46,3 +47,4 @@ if __name__ == '__main__':
main()
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
+ sys.stdout.flush()
diff --git a/tests/interact.py b/tests/interact.py
index 9f8e672..a839e95 100755
--- a/tests/interact.py
+++ b/tests/interact.py
@@ -31,9 +31,21 @@ import sys
def main():
- p = pexpect.spawn(sys.executable + ' echo_w_prompt.py',
+ p = pexpect.spawn('{sys.executable} getch.py'.format(sys=sys),
env=no_coverage_env())
- p.interact()
+
+ # defaults matches api
+ escape_character = chr(29)
+ encoding = None
+
+ if len(sys.argv) > 1 and '--no-escape' in sys.argv:
+ escape_character = None
+
+ if len(sys.argv) > 1 and '--utf8' in sys.argv:
+ encoding = 'utf8'
+
+ p.interact(escape_character=escape_character)
+
print("Escaped interact")
if __name__ == '__main__':
diff --git a/tests/interact_unicode.py b/tests/interact_unicode.py
deleted file mode 100644
index f4c1f55..0000000
--- a/tests/interact_unicode.py
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env python
-'''
-Just like interact.py, but using spawnu instead of spawn
-'''
-try:
- # This allows coverage to measure code run in this process
- import coverage
- coverage.process_startup()
-except ImportError:
- pass
-
-from utils import no_coverage_env
-import pexpect
-import sys
-
-
-def main():
- p = pexpect.spawnu(sys.executable + ' echo_w_prompt.py',
- env=no_coverage_env())
- p.interact()
- print("Escaped interact")
-
-if __name__ == '__main__':
- main()
diff --git a/tests/test_interact.py b/tests/test_interact.py
index e635cb0..865353b 100755
--- a/tests/test_interact.py
+++ b/tests/test_interact.py
@@ -41,15 +41,12 @@ class InteractTestCase (PexpectTestCase.PexpectTestCase):
else:
env['PYTHONPATH'] = self.project_dir
- self.interact_py = ' '.join((sys.executable,
- 'interact.py',))
- self.interact_ucs_py = ' '.join((sys.executable,
- 'interact_unicode.py',))
+ self.interact_py = ('{sys.executable} interact.py'.format(sys=sys))
def test_interact_escape(self):
" Ensure `escape_character' value exits interactive mode. "
p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
- p.expect('<in >')
+ p.expect('READY')
p.sendcontrol(']') # chr(29), the default `escape_character'
# value of pexpect.interact().
p.expect_exact('Escaped interact')
@@ -57,39 +54,43 @@ class InteractTestCase (PexpectTestCase.PexpectTestCase):
assert not p.isalive()
assert p.exitstatus == 0
- def test_interact_spawn_eof(self):
- " Ensure subprocess receives EOF and exit. "
- p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
- p.expect('<in >')
- p.sendline(b'alpha')
- p.sendline(b'beta')
- p.expect(b'<out>alpha')
- p.expect(b'<out>beta')
- p.sendeof()
- # strangely, on travis-ci, sendeof() terminates the subprocess,
- # it doesn't receive ^D, just immediately throws EOF.
- idx = p.expect_exact(['<eof>', pexpect.EOF])
- if idx == 0:
+ def test_interact_escape_None(self):
+ " Return only after Termination when `escape_character=None'. "
+ p = pexpect.spawn('{self.interact_py} --no-escape'.format(self=self),
+ timeout=5, env=self.env)
+ p.expect('READY')
+ p.sendcontrol(']')
+ p.expect('29<STOP>')
+ p.send('\x00')
+ if not os.environ.get('TRAVIS', None):
+ # on Travis-CI, we sometimes miss trailing stdout from the
+ # chain of child processes, not entirely sure why. So this
+ # is skipped on such systems.
+ p.expect('0<STOP>')
p.expect_exact('Escaped interact')
- p.expect(pexpect.EOF)
+ p.expect(pexpect.EOF)
assert not p.isalive()
assert p.exitstatus == 0
- def test_interact_spawnu_eof(self):
- " Ensure subprocess receives unicode, EOF, and exit. "
- p = pexpect.spawnu(self.interact_ucs_py, timeout=5, env=self.env)
- p.expect('<in >')
- p.sendline('ɑlpha')
- p.sendline('Βeta')
- p.expect('<out>ɑlpha')
- p.expect('<out>Βeta')
- p.sendeof()
- # strangely, on travis-ci, sendeof() terminates the subprocess,
- # it doesn't receive ^D, just immediately throws EOF.
- idx = p.expect_exact(['<eof>', pexpect.EOF])
- if idx == 0:
+ def test_interact_exit_unicode(self):
+ " Ensure subprocess receives utf8. "
+ p = pexpect.spawnu('{self.interact_py} --utf8'.format(self=self),
+ timeout=5, env=self.env)
+ p.expect('READY')
+ p.send('ɑ') # >>> map(ord, u'ɑ'.encode('utf8'))
+ p.expect('201<STOP>') # [201, 145]
+ p.expect('145<STOP>')
+ p.send('Β') # >>> map(ord, u'Β'.encode('utf8'))
+ p.expect('206<STOP>') # [206, 146]
+ p.expect('146<STOP>')
+ p.send('\x00')
+ if not os.environ.get('TRAVIS', None):
+ # on Travis-CI, we sometimes miss trailing stdout from the
+ # chain of child processes, not entirely sure why. So this
+ # is skipped on such systems.
+ p.expect('0<STOP>')
p.expect_exact('Escaped interact')
- p.expect(pexpect.EOF)
+ p.expect(pexpect.EOF)
assert not p.isalive()
assert p.exitstatus == 0
diff --git a/tests/test_isalive.py b/tests/test_isalive.py
index 5168a52..cd79d09 100755
--- a/tests/test_isalive.py
+++ b/tests/test_isalive.py
@@ -25,22 +25,33 @@ import sys
import time
from . import PexpectTestCase
+
class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
+ """Various tests for the running status of processes."""
- def test_expect_wait (self):
- '''This tests that calling wait on a finished process works as expected.
- '''
- p = pexpect.spawn('sleep 3')
+ def test_expect_wait(self):
+ """Ensure consistency in wait() and isalive()."""
+ p = pexpect.spawn('sleep 1')
assert p.isalive()
- p.wait()
+ assert p.wait() == 0
assert not p.isalive()
+ # In previous versions of ptyprocess/pexpect, calling wait() a second
+ # time would raise an exception, but not since v4.0
+ assert p.wait() == 0
+ def test_expect_wait_after_termination(self):
+ """Ensure wait on a process terminated by kill -9."""
p = pexpect.spawn('sleep 3')
assert p.isalive()
p.kill(9)
time.sleep(1)
- with self.assertRaises(pexpect.ExceptionPexpect):
- p.wait()
+
+ # when terminated, the exitstatus is None, but p.signalstatus
+ # and p.terminated reflects that the kill -9 nature.
+ assert p.wait() is None
+ assert p.signalstatus == 9
+ assert p.terminated == True
+ assert not p.isalive()
def test_signal_wait(self):
'''Test calling wait with a process terminated by a signal.'''
@@ -102,7 +113,7 @@ class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
p = pexpect.spawn('cat')
assert p.isalive()
assert p.isalive()
- p.kill(9)
+ p.sendeof()
p.expect(pexpect.EOF)
assert not p.isalive()
assert not p.isalive()