Skip to content

Commit

Permalink
This resolves two issues with replwrap,
Browse files Browse the repository at this point in the history
* for multiple commands, such as in
  ``'\n'.join((cmd, cmd2))``, cmd2 and beyond
  previously used a ``timeout`` of 1, instead of
  the specified or default ``timeout``.
* furthermore, the output of multi-command output
  was discarded, only the last-most command output
  was received.  Resolved by joining the result of
  ``self.child.before``.
  • Loading branch information
jquast committed Jun 29, 2014
1 parent a1f7266 commit 061f0ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions pexpect/replwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ def run_command(self, command, timeout=-1):
if not cmdlines:
raise ValueError("No command was given")

res = u('')
self.child.sendline(cmdlines[0])
for line in cmdlines[1:]:
self._expect_prompt(timeout=1)
self._expect_prompt(timeout=timeout)
res += self.child.before
self.child.sendline(line)

# Command was fully submitted, now wait for the next prompt
Expand All @@ -98,7 +100,7 @@ def run_command(self, command, timeout=-1):
self._expect_prompt(timeout=1)
raise ValueError("Continuation prompt found - input was incomplete:\n"
+ command)
return self.child.before
return res + self.child.before

def python(command="python"):
"""Start a Python shell and return a :class:`REPLWrapper` object."""
Expand Down
17 changes: 14 additions & 3 deletions tests/test_replwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pexpect
from pexpect import replwrap

skip_pypy = "This test fails on PyPy because of REPL differences"


class REPLWrapTestCase(unittest.TestCase):
def setUp(self):
Expand All @@ -29,6 +31,16 @@ def test_bash(self):
res = bash.run_command('man sleep', timeout=2)
assert 'SLEEP' in res, res

def test_long_running_multiline(self):
bash = replwrap.bash()
res = bash.run_command("echo begin\r\nsleep 2\r\necho done")
self.assertEqual(res.strip().splitlines(), ['begin', 'done'])

def test_long_running_continuation(self):
bash = replwrap.bash()
res = bash.run_command("echo begin\\\n;sleep 2\r\necho done")
self.assertEqual(res.strip().splitlines(), ['begin', 'done'])

def test_multiline(self):
bash = replwrap.bash()
res = bash.run_command("echo '1 2\n3 4'")
Expand Down Expand Up @@ -57,7 +69,7 @@ def test_existing_spawn(self):

def test_python(self):
if platform.python_implementation() == 'PyPy':
raise unittest.SkipTest("This test fails on PyPy because of REPL differences")
raise unittest.SkipTest(skip_pypy)

p = replwrap.python()
res = p.run_command('4+7')
Expand All @@ -68,7 +80,7 @@ def test_python(self):

def test_no_change_prompt(self):
if platform.python_implementation() == 'PyPy':
raise unittest.SkipTest("This test fails on PyPy because of REPL differences")
raise unittest.SkipTest(skip_pypy)

child = pexpect.spawnu('python', echo=False, timeout=5)
# prompt_change=None should mean no prompt change
Expand All @@ -79,6 +91,5 @@ def test_no_change_prompt(self):
res = py.run_command("for a in range(3): print(a)\n")
assert res.strip().splitlines() == ['0', '1', '2']


if __name__ == '__main__':
unittest.main()

0 comments on commit 061f0ab

Please sign in to comment.