From 1ba286fc604ad6665d7cc3edbb2db3bff54b7a82 Mon Sep 17 00:00:00 2001 From: jquast Date: Sat, 28 Jun 2014 21:37:38 -0700 Subject: This resolves two issues with replwrap, * 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``. --- pexpect/replwrap.py | 6 ++++-- tests/test_replwrap.py | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pexpect/replwrap.py b/pexpect/replwrap.py index 83a09c2..1b91c67 100644 --- a/pexpect/replwrap.py +++ b/pexpect/replwrap.py @@ -88,9 +88,11 @@ class REPLWrapper(object): 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 @@ -100,7 +102,7 @@ class REPLWrapper(object): 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.""" diff --git a/tests/test_replwrap.py b/tests/test_replwrap.py index 28c7599..6e56af0 100644 --- a/tests/test_replwrap.py +++ b/tests/test_replwrap.py @@ -6,6 +6,8 @@ import os import pexpect from pexpect import replwrap +skip_pypy = "This test fails on PyPy because of REPL differences" + class REPLWrapTestCase(unittest.TestCase): def setUp(self): @@ -25,10 +27,24 @@ class REPLWrapTestCase(unittest.TestCase): res = bash.run_command("time") assert 'real' in res, res - # PAGER should be set to cat, otherwise man hangs + def test_pager_as_cat(self): + " PAGER is set to cat, to prevent timeout in ``man sleep``. " + bash = replwrap.bash() res = bash.run_command('man sleep', timeout=5) assert 'SLEEP' in res, res + def test_long_running_multiline(self): + " ensure the default timeout is used for multi-line commands. " + 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): + " also ensure timeout when used within continuation prompts. " + 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'") @@ -57,7 +73,7 @@ class REPLWrapTestCase(unittest.TestCase): 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') @@ -68,7 +84,7 @@ class REPLWrapTestCase(unittest.TestCase): 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 @@ -79,6 +95,5 @@ class REPLWrapTestCase(unittest.TestCase): res = py.run_command("for a in range(3): print(a)\n") assert res.strip().splitlines() == ['0', '1', '2'] - if __name__ == '__main__': unittest.main() -- cgit v1.2.1