| 1 |
import fcntl |
| 2 |
import os |
| 3 |
import time |
| 4 |
import select |
| 5 |
import tempfile |
| 6 |
import unittest |
| 7 |
from ptyprocess.ptyprocess import which |
| 8 |
from ptyprocess import PtyProcess, PtyProcessUnicode |
| 9 |
|
| 10 |
class PtyTestCase(unittest.TestCase): |
| 11 |
def setUp(self): |
| 12 |
self.cmd = u'echo $ENV_KEY; exit 0\n' |
| 13 |
self.env = os.environ.copy() |
| 14 |
self.env_key = u'ENV_KEY' |
| 15 |
self.env_value = u'env_value' |
| 16 |
self.env[self.env_key] = self.env_value |
| 17 |
|
| 18 |
def _canread(self, fd, timeout=1): |
| 19 |
return fd in select.select([fd], [], [], timeout)[0] |
| 20 |
|
| 21 |
def _spawn_sh(self, ptyp, cmd, outp, env_value): |
| 22 |
|
| 23 |
p = ptyp.spawn(['sh'], env=self.env) |
| 24 |
p.write(cmd) |
| 25 |
|
| 26 |
|
| 27 |
while True: |
| 28 |
try: |
| 29 |
outp += p.read() |
| 30 |
except EOFError: |
| 31 |
break |
| 32 |
|
| 33 |
|
| 34 |
assert cmd.strip() in outp |
| 35 |
|
| 36 |
|
| 37 |
assert env_value in outp |
| 38 |
|
| 39 |
|
| 40 |
assert p.wait() == 0 |
| 41 |
|
| 42 |
|
| 43 |
def test_spawn_sh(self): |
| 44 |
outp = b'' |
| 45 |
self._spawn_sh(PtyProcess, self.cmd.encode('ascii'), |
| 46 |
outp, self.env_value.encode('ascii')) |
| 47 |
|
| 48 |
def test_spawn_sh_unicode(self): |
| 49 |
outp = u'' |
| 50 |
self._spawn_sh(PtyProcessUnicode, self.cmd, |
| 51 |
outp, self.env_value) |
| 52 |
|
| 53 |
def test_quick_spawn(self): |
| 54 |
"""Spawn a very short-lived process.""" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
PtyProcess.spawn(['true']) |
| 59 |
|
| 60 |
def _interactive_repl_unicode(self, echo): |
| 61 |
"""Test Call and response with echo ON/OFF.""" |
| 62 |
|
| 63 |
bc = PtyProcessUnicode.spawn(['bc'], echo=echo) |
| 64 |
given_input = u'2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2\n' |
| 65 |
expected_output = u'40' |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
time.sleep(1) |
| 72 |
|
| 73 |
bc.write(u'2^16\n') |
| 74 |
outp = u'' |
| 75 |
while self._canread(bc.fd): |
| 76 |
outp += bc.read() |
| 77 |
assert u'65536' in outp |
| 78 |
|
| 79 |
|
| 80 |
bc.write(given_input) |
| 81 |
|
| 82 |
while self._canread(bc.fd, timeout=2): |
| 83 |
outp += bc.read() |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if echo: |
| 91 |
assert given_input.strip() in outp |
| 92 |
|
| 93 |
|
| 94 |
assert expected_output in outp |
| 95 |
|
| 96 |
|
| 97 |
bc.sendeof() |
| 98 |
|
| 99 |
|
| 100 |
while True: |
| 101 |
try: |
| 102 |
bc.read() |
| 103 |
except EOFError: |
| 104 |
break |
| 105 |
|
| 106 |
|
| 107 |
assert bc.wait() == 0 |
| 108 |
|
| 109 |
@unittest.skipIf(which('bc') is None, "bc(1) not found on this server.") |
| 110 |
def test_interactive_repl_unicode_noecho(self): |
| 111 |
self._interactive_repl_unicode(echo=False) |
| 112 |
|
| 113 |
@unittest.skipIf(which('bc') is None, "bc(1) not found on this server.") |
| 114 |
def test_interactive_repl_unicode_echo(self): |
| 115 |
self._interactive_repl_unicode(echo=True) |
| 116 |
|
| 117 |
def test_pass_fds(self): |
| 118 |
with tempfile.NamedTemporaryFile() as temp_file: |
| 119 |
temp_file_fd = temp_file.fileno() |
| 120 |
temp_file_name = temp_file.name |
| 121 |
|
| 122 |
|
| 123 |
fcntl.fcntl(temp_file_fd, |
| 124 |
fcntl.F_SETFD, |
| 125 |
fcntl.fcntl(temp_file_fd, fcntl.F_GETFD) & |
| 126 |
~fcntl.FD_CLOEXEC) |
| 127 |
|
| 128 |
|
| 129 |
p = PtyProcess.spawn(['bash', |
| 130 |
'-c', |
| 131 |
'printf hello >&{}'.format(temp_file_fd)], |
| 132 |
echo=True, |
| 133 |
pass_fds=(temp_file_fd,)) |
| 134 |
p.wait() |
| 135 |
assert p.status == 0 |
| 136 |
|
| 137 |
with open(temp_file_name, 'r') as temp_file_r: |
| 138 |
assert temp_file_r.read() == 'hello' |
| 139 |
|
| 140 |
|
| 141 |
p = PtyProcess.spawn(['bash', |
| 142 |
'-c', |
| 143 |
'printf bye >&{}'.format(temp_file_fd)], |
| 144 |
echo=True) |
| 145 |
p.wait() |
| 146 |
assert p.status != 0 |
| 147 |
|
| 148 |
with open(temp_file_name, 'r') as temp_file_r: |
| 149 |
assert temp_file_r.read() == 'hello' |