0

I am working with the following bit of Python code. The code is fairly simple, pexpect sends echo testcase to the terminal, then monitors the terminal for the subsequent appearance of testcase, then verifies that it saw the echo by setting bool_value=True (assuming it sees the expected output).

#!/usr/bin/python
import pexpect

print('Testing case where pexpect finds the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('testcase')
print bool_value
testcase.close()

print('Testing case where pexpect does not find the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('someothervalue')
print bool_value
testcase.close()

What I expect to see is that after the first test case, bool_value would print True. In this case it does, but only after adding the odd hack of setting bool_value = not testcase.expect('testcase') (setting it to the inverse of what expect() returns).

In the second test case, testcase.expect() does not see the expected value of someothervalue, so I would expect it to return false. Instead, it returns an exception:

Traceback (most recent call last):
  File "./echotest.py", line 12, in <module>
    bool_value = not testcase.expect('someothervalue')
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1521, in expect_loop
    raise EOF(str(err) + '\n' + str(self))
pexpect.EOF: End Of File (EOF). Exception style platform.
<pexpect.spawn object at 0x7f7667a9db10>
version: 3.1
command: /bin/echo
args: ['/bin/echo', 'testcase']
searcher: <pexpect.searcher_re object at 0x7f7667a9d790>
buffer (last 100 chars): ''
before (last 100 chars): 'testcase\r\n'
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 0
flag_eof: True
pid: 4619
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

I'm really not sure how to correct this behavior based on what the documentation is telling me. Adding testcase.expect(pexpect.EOF()) per this previous implementation returns the same error.

How do I correctly implement this function?

Community
  • 1
  • 1
Bradley Evans
  • 399
  • 1
  • 5
  • 17
  • 2
    Maybe because `echo` adds a newline by default? Try to use `echo -n` or use `expect('testcase\n')`. – Bakuriu Aug 12 '16 at 17:53
  • I made the suggested change to `echo -n`. The only change in the exception thrown was that the line `before (last 100 chars): 'testcase\r\n'` became `before (last 100 chars): 'testcase'`. The exception is unchanged by using `expect('testcase\n')` or `expect('testcase\r\n')` or adding a new `expect('\r\n')` line in the code. – Bradley Evans Aug 12 '16 at 17:57

1 Answers1

0

It turns out that when the system was not outputting something pexpect was looking for, it would hit pexpect.EOF without triggering any sort of action. Because I don't tell it what to do when it hits EOF without seeing any other strings it's expecting, it returns an exception instead.

Here is my code with corrections and test cases.

#!/usr/bin/python
import pexpect

"""
Test for some known good value.
"""
print('Testing case where pexpect finds the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()

"""
Test for some known bad response
"""
print('Testing case where pexpect finds an expected bad value: ')
testcase = pexpect.spawn('echo badcase')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()

"""
If it gets no expected response at all, it will reach EOF. We have to define
some behavior for that case.
"""
print('Testing case where pexpect does not find any expected value: ')
testcase = pexpect.spawn('echo unknown')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()
Bradley Evans
  • 399
  • 1
  • 5
  • 17