diff options
author | Thomas Kluyver <takowl@gmail.com> | 2014-06-10 12:24:52 -0700 |
---|---|---|
committer | Thomas Kluyver <takowl@gmail.com> | 2014-06-10 12:24:52 -0700 |
commit | f46de45cddbf6d0906000e3bf052051a85a15269 (patch) | |
tree | a627b4f266d2e902e37b8e8ef6df87c89f5c6787 | |
parent | 8aea7ddba7b8d9b1698f17a1895dcaf6730f79d9 (diff) | |
parent | a50dcfdec8eaabbfbbe1f460d5282a79596913dc (diff) | |
download | pexpect-git-f46de45cddbf6d0906000e3bf052051a85a15269.tar.gz |
Merge pull request #71 from pexpect/replwrap-test-fixes
Fixing various prompt issues with bash ReplWrapper
-rw-r--r-- | pexpect/replwrap.py | 16 | ||||
-rw-r--r-- | tests/test_replwrap.py | 24 |
2 files changed, 29 insertions, 11 deletions
diff --git a/pexpect/replwrap.py b/pexpect/replwrap.py index 965c790..af4f889 100644 --- a/pexpect/replwrap.py +++ b/pexpect/replwrap.py @@ -2,6 +2,7 @@ """ import signal import sys +import re import pexpect @@ -17,7 +18,7 @@ PEXPECT_CONTINUATION_PROMPT = u('[PEXPECT_PROMPT+') class REPLWrapper(object): """Wrapper for a REPL. - + :param cmd_or_spawn: This can either be an instance of :class:`pexpect.spawn` in which a REPL has already been started, or a str command to start a new REPL process. @@ -37,7 +38,8 @@ class REPLWrapper(object): else: self.child = cmd_or_spawn self.child.setecho(False) # Don't repeat our input. - + self.child.waitnoecho() + if prompt_change is None: self.prompt = orig_prompt else: @@ -49,16 +51,16 @@ class REPLWrapper(object): self._expect_prompt() def set_prompt(self, orig_prompt, prompt_change): - self.child.expect_exact(orig_prompt) + self.child.expect(orig_prompt) self.child.sendline(prompt_change) def _expect_prompt(self, timeout=-1): return self.child.expect_exact([self.prompt, self.continuation_prompt], timeout=timeout) - + def run_command(self, command, timeout=-1): """Send a command to the REPL, wait for and return output. - + :param str command: The command to send. Trailing newlines are not needed. This should be a complete block of input that will trigger execution; if a continuation prompt is found after sending input, :exc:`ValueError` @@ -93,6 +95,6 @@ def python(command="python"): """Start a Python shell and return a :class:`REPLWrapper` object.""" return REPLWrapper(command, u(">>> "), u("import sys; sys.ps1={0!r}; sys.ps2={1!r}")) -def bash(command="bash", orig_prompt=u("$")): +def bash(command="bash", orig_prompt=re.compile('[$#]')): """Start a bash shell and return a :class:`REPLWrapper` object.""" - return REPLWrapper(command, orig_prompt, u("PS1='{0}'; PS2='{1}'")) + return REPLWrapper(command, orig_prompt, u("PS1='{0}' PS2='{1}' PROMPT_COMMAND=''")) diff --git a/tests/test_replwrap.py b/tests/test_replwrap.py index 442c613..a6ea956 100644 --- a/tests/test_replwrap.py +++ b/tests/test_replwrap.py @@ -1,10 +1,25 @@ import platform import unittest +import re +import os import pexpect from pexpect import replwrap + class REPLWrapTestCase(unittest.TestCase): + def setUp(self): + super(REPLWrapTestCase, self).setUp() + self.save_ps1 = os.getenv('PS1', r'\$') + self.save_ps2 = os.getenv('PS2', '>') + os.putenv('PS1', r'\$') + os.putenv('PS2', '>') + + def tearDown(self): + super(REPLWrapTestCase, self).tearDown() + os.putenv('PS1', self.save_ps1) + os.putenv('PS2', self.save_ps2) + def test_bash(self): bash = replwrap.bash() res = bash.run_command("time") @@ -28,9 +43,10 @@ class REPLWrapTestCase(unittest.TestCase): self.assertEqual(res.strip().splitlines(), ['1 2', '3 4']) def test_existing_spawn(self): - child = pexpect.spawnu("bash") - repl = replwrap.REPLWrapper(child, replwrap.u("$ "), - "PS1='{0}'; PS2='{1}'") + child = pexpect.spawnu("bash", timeout=5) + repl = replwrap.REPLWrapper(child, re.compile('[$#]'), + "PS1='{0}' PS2='{1}' " + "PROMPT_COMMAND=''") res = repl.run_command("echo $HOME") assert res.startswith('/'), res @@ -50,7 +66,7 @@ class REPLWrapTestCase(unittest.TestCase): if platform.python_implementation() == 'PyPy': raise unittest.SkipTest("This test fails on PyPy because of REPL differences") - child = pexpect.spawnu('python') + child = pexpect.spawnu('python', timeout=5) # prompt_change=None should mean no prompt change py = replwrap.REPLWrapper(child, replwrap.u(">>> "), prompt_change=None, continuation_prompt=replwrap.u("... ")) |