diff options
author | jquast <contact@jeffquast.com> | 2014-06-07 20:54:18 -0700 |
---|---|---|
committer | jquast <contact@jeffquast.com> | 2014-06-07 20:54:18 -0700 |
commit | 6a144134788a0c6601262ec008b83e4288bbb096 (patch) | |
tree | 024ffc4f0a7f9648f81265867cbdd852d69d5bf1 | |
parent | 8b04d95b94ce59e9a2a8ad91585e91ef40ec1aa0 (diff) | |
download | pexpect-git-6a144134788a0c6601262ec008b83e4288bbb096.tar.gz |
Fixing various prompt issues with bash ReplWrapper
allow matching a regular expression,
use setUp and tearDown to putenv & getenv PS1, PS2
unset PROMPT_COMMAND
-rw-r--r-- | pexpect/replwrap.py | 16 | ||||
-rw-r--r-- | tests/test_replwrap.py | 26 |
2 files changed, 31 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..3ee9f0c 100644 --- a/tests/test_replwrap.py +++ b/tests/test_replwrap.py @@ -1,11 +1,28 @@ 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', '\$') + 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): + os.putenv('PS1', r'\$') + os.putenv('PS2', r'>') bash = replwrap.bash() res = bash.run_command("time") assert 'real' in res, res @@ -28,9 +45,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 +68,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("... ")) |