1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/usr/bin/env python
import pexpect
import unittest
import commands
import sys, os, time, tty
import PexpectTestCase
import thread
import threading
def start_interact (p):
p.interact()
class InteractTestCase (PexpectTestCase.PexpectTestCase):
def test_interact_thread (self):
# I can't believe this actually works...
# Note that I had to add a delay in the swapcase_echo.py script.
# I'm not sure why this helped.
p = pexpect.spawn('%s swapcase_echo.py' % self.PYTHONBIN)
mode = tty.tcgetattr(p.STDIN_FILENO)
t = threading.Thread (target=start_interact, args=(p,))
t.start()
#thread.start_new_thread (start_interact, (p,))
time.sleep(1)
p.sendline ('Hello')
#time.sleep(1)
try:
p.expect ('hELLO', timeout=4)
except Exception, e:
p.close(force = False)
tty.tcsetattr(p.STDIN_FILENO, tty.TCSAFLUSH, mode)
print str(p)
raise e
p.close(force = True)
tty.tcsetattr(p.STDIN_FILENO, tty.TCSAFLUSH, mode)
# def test_interact_thread (self):
# # I can't believe this actually works...
# p = pexpect.spawn('%s swapcase_echo.py' % self.PYTHONBIN)
# mode = tty.tcgetattr(p.STDIN_FILENO)
# thread.start_new_thread (start_interact, (p,))
# time.sleep(1)
# p.sendline ('Hello')
# time.sleep(2)
# p.close(force = False)
# tty.tcsetattr(p.STDIN_FILENO, tty.TCSAFLUSH, mode)
def test_interact (self):
p = pexpect.spawn('%s interact.py' % self.PYTHONBIN)
p.sendline ('Hello')
p.sendline ('there')
p.sendline ('Mr. Python')
p.expect ('Hello')
p.expect ('there')
p.expect ('Mr. Python')
p.sendeof ()
p.expect (pexpect.EOF)
if __name__ == '__main__':
unittest.main()
suite = unittest.makeSuite(InteractTestCase,'test')
|