6
This license is approved by the OSI and FSF as GPL-compatible.
7
http://opensource.org/licenses/isc-license.txt
9
Copyright (c) 2012, Noah Spurrier <noah@noah.org>
10
PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
11
PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
12
COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
13
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26
import PexpectTestCase
28
# TODO Many of these test cases blindly assume that sequential
29
# TODO listing of the /bin directory will yield the same results.
30
# TODO This may not always be true, but seems adequate for testing for now.
31
# TODO I should fix this at some point.
33
unicode_type = str if pexpect.PY3 else unicode
35
def timeout_callback (d):
36
# print d["event_count"],
37
if d["event_count"]>3:
41
class RunFuncTestCase(PexpectTestCase.PexpectTestCase):
42
runfunc = staticmethod(pexpect.run)
45
prep_subprocess_out = staticmethod(lambda x: x)
47
def test_run_exit (self):
48
(data, exitstatus) = self.runfunc('python exit1.py', withexitstatus=1)
49
assert exitstatus == 1, "Exit status of 'python exit1.py' should be 1."
52
the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
53
stdout=subprocess.PIPE).communicate()[0].rstrip()
54
(the_new_way, exitstatus) = self.runfunc('ls -l /bin', withexitstatus=1)
55
the_new_way = the_new_way.replace(self.cr, self.empty).rstrip()
56
self.assertEqual(self.prep_subprocess_out(the_old_way), the_new_way)
57
self.assertEqual(exitstatus, 0)
59
def test_run_callback (self): # TODO it seems like this test could block forever if run fails...
60
self.runfunc("cat", timeout=1, events={pexpect.TIMEOUT:timeout_callback})
62
def test_run_bad_exitstatus (self):
63
(the_new_way, exitstatus) = self.runfunc('ls -l /najoeufhdnzkxjd',
65
assert exitstatus != 0
67
class RunUnicodeFuncTestCase(RunFuncTestCase):
68
runfunc = staticmethod(pexpect.runu)
69
cr = b'\r'.decode('ascii')
70
empty = b''.decode('ascii')
71
prep_subprocess_out = staticmethod(lambda x: x.decode('utf-8', 'replace'))
72
def test_run_unicode(self):
77
c = unichr(254) # analysis:ignore
78
pattern = '<in >'.decode('ascii')
81
if d['event_count'] == 0:
84
return True # Stop the child process
86
output = pexpect.runu(sys.executable + ' echo_w_prompt.py',
87
env={'PYTHONIOENCODING':'utf-8'},
88
events={pattern:callback})
89
assert isinstance(output, unicode_type), type(output)
90
assert '<out>'+c in output, output
92
if __name__ == '__main__':