2019-09-23 13:58:02 +00:00
|
|
|
import wexpect
|
|
|
|
import time
|
|
|
|
import unittest
|
2020-02-22 22:03:28 +00:00
|
|
|
from tests import PexpectTestCase
|
2019-09-23 13:58:02 +00:00
|
|
|
|
|
|
|
class InteractTestCase(PexpectTestCase.PexpectTestCase):
|
2020-01-25 15:59:19 +00:00
|
|
|
|
2020-01-31 14:49:48 +00:00
|
|
|
@unittest.skipIf(not (hasattr(wexpect, 'legacy_wexpect')) or (hasattr(wexpect.spawn, 'interact')), "spawn does not support runtime interact switching.")
|
2019-12-05 16:27:02 +00:00
|
|
|
def test_interact(self):
|
2019-09-23 13:58:02 +00:00
|
|
|
# Path of cmd executable:
|
|
|
|
cmd_exe = 'cmd'
|
|
|
|
cmdPrompt = '>'
|
|
|
|
|
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(cmd_exe)
|
|
|
|
|
|
|
|
# Wait for prompt
|
|
|
|
p.expect(cmdPrompt)
|
|
|
|
|
|
|
|
p.interact()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Send a command
|
|
|
|
p.sendline('echo hello')
|
|
|
|
p.expect(cmdPrompt)
|
2019-11-29 09:17:51 +00:00
|
|
|
|
|
|
|
p.stop_interact()
|
2019-09-23 13:58:02 +00:00
|
|
|
|
|
|
|
self.assertEqual('hello', p.before.splitlines()[1])
|
2020-01-25 15:59:19 +00:00
|
|
|
|
2020-01-31 14:49:48 +00:00
|
|
|
@unittest.skipIf(not (hasattr(wexpect, 'legacy_wexpect')) or (hasattr(wexpect.spawn, 'interact')), "spawn does not support runtime interact switching.")
|
2019-12-05 16:27:02 +00:00
|
|
|
def test_interact_dead(self):
|
|
|
|
# Path of cmd executable:
|
|
|
|
echo = 'echo hello'
|
|
|
|
|
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(echo)
|
|
|
|
|
|
|
|
p.expect('hello')
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(wexpect.ExceptionPexpect):
|
|
|
|
p.interact()
|
|
|
|
|
|
|
|
with self.assertRaises(wexpect.ExceptionPexpect):
|
|
|
|
p.stop_interact()
|
2019-09-23 13:58:02 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
suite = unittest.makeSuite(InteractTestCase,'test')
|