Debian Codegraph @

tests/test_spawn.py

raw symbols SCIP
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 # given,
23 p = ptyp.spawn(['sh'], env=self.env)
24 p.write(cmd)
25
26 # exercise,
27 while True:
28 try:
29 outp += p.read()
30 except EOFError:
31 break
32
33 # verify, input is echo to output
34 assert cmd.strip() in outp
35
36 # result of echo $ENV_KEY in output
37 assert env_value in outp
38
39 # exit successfully (exit 0)
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 # so far only reproducible on Solaris 11, spawning a process
56 # that exits very quickly raised an exception at 'inst.setwinsize',
57 # because the pty file descriptor was quickly lost after exec().
58 PtyProcess.spawn(['true'])
59
60 def _interactive_repl_unicode(self, echo):
61 """Test Call and response with echo ON/OFF."""
62 # given,
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 # gnu-bc will display a long FSF banner on startup,
68 # whereas bsd-bc (on FreeBSD, Solaris) display no
69 # banner at all. To ensure we've read up to our
70 # current prompt, read until the response of '2^16' is found.
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 # exercise,
80 bc.write(given_input)
81
82 while self._canread(bc.fd, timeout=2):
83 outp += bc.read()
84
85 # with echo ON, we should see our input.
86 #
87 # note: we cannot assert the reverse: on Solaris, FreeBSD,
88 # and OSX, our input is echoed to output even with echo=False,
89 # something to do with the non-gnu version of bc(1), perhaps.
90 if echo:
91 assert given_input.strip() in outp
92
93 # we should most certainly see the result output.
94 assert expected_output in outp
95
96 # exercise sending EOF
97 bc.sendeof()
98
99 # validate EOF on read
100 while True:
101 try:
102 bc.read()
103 except EOFError:
104 break
105
106 # validate exit status,
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 # Temporary files are CLOEXEC by default
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 # You can write with pass_fds
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 # You can't write without pass_fds
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'