OLD | NEW |
(Empty) | |
| 1 import platform |
| 2 import unittest |
| 3 import re |
| 4 import os |
| 5 |
| 6 import pexpect |
| 7 from pexpect import replwrap |
| 8 |
| 9 skip_pypy = "This test fails on PyPy because of REPL differences" |
| 10 |
| 11 |
| 12 class REPLWrapTestCase(unittest.TestCase): |
| 13 def setUp(self): |
| 14 super(REPLWrapTestCase, self).setUp() |
| 15 self.save_ps1 = os.getenv('PS1', r'\$') |
| 16 self.save_ps2 = os.getenv('PS2', '>') |
| 17 os.putenv('PS1', r'\$') |
| 18 os.putenv('PS2', '>') |
| 19 |
| 20 def tearDown(self): |
| 21 super(REPLWrapTestCase, self).tearDown() |
| 22 os.putenv('PS1', self.save_ps1) |
| 23 os.putenv('PS2', self.save_ps2) |
| 24 |
| 25 def test_bash(self): |
| 26 bash = replwrap.bash() |
| 27 res = bash.run_command("time") |
| 28 assert 'real' in res, res |
| 29 |
| 30 def test_pager_as_cat(self): |
| 31 " PAGER is set to cat, to prevent timeout in ``man sleep``. " |
| 32 bash = replwrap.bash() |
| 33 res = bash.run_command('man sleep', timeout=5) |
| 34 assert 'SLEEP' in res, res |
| 35 |
| 36 def test_long_running_multiline(self): |
| 37 " ensure the default timeout is used for multi-line commands. " |
| 38 bash = replwrap.bash() |
| 39 res = bash.run_command("echo begin\r\nsleep 2\r\necho done") |
| 40 self.assertEqual(res.strip().splitlines(), ['begin', 'done']) |
| 41 |
| 42 def test_long_running_continuation(self): |
| 43 " also ensure timeout when used within continuation prompts. " |
| 44 bash = replwrap.bash() |
| 45 # The two extra '\\' in the following expression force a continuation |
| 46 # prompt: |
| 47 # $ echo begin\ |
| 48 # + ; |
| 49 # $ sleep 2 |
| 50 # $ echo done |
| 51 res = bash.run_command("echo begin\\\n;sleep 2\r\necho done") |
| 52 self.assertEqual(res.strip().splitlines(), ['begin', 'done']) |
| 53 |
| 54 def test_multiline(self): |
| 55 bash = replwrap.bash() |
| 56 res = bash.run_command("echo '1 2\n3 4'") |
| 57 self.assertEqual(res.strip().splitlines(), ['1 2', '3 4']) |
| 58 |
| 59 # Should raise ValueError if input is incomplete |
| 60 try: |
| 61 bash.run_command("echo '5 6") |
| 62 except ValueError: |
| 63 pass |
| 64 else: |
| 65 assert False, "Didn't raise ValueError for incomplete input" |
| 66 |
| 67 # Check that the REPL was reset (SIGINT) after the incomplete input |
| 68 res = bash.run_command("echo '1 2\n3 4'") |
| 69 self.assertEqual(res.strip().splitlines(), ['1 2', '3 4']) |
| 70 |
| 71 def test_existing_spawn(self): |
| 72 child = pexpect.spawn("bash", timeout=5, echo=False, encoding='utf-8') |
| 73 repl = replwrap.REPLWrapper(child, re.compile('[$#]'), |
| 74 "PS1='{0}' PS2='{1}' " |
| 75 "PROMPT_COMMAND=''") |
| 76 |
| 77 res = repl.run_command("echo $HOME") |
| 78 assert res.startswith('/'), res |
| 79 |
| 80 def test_python(self): |
| 81 if platform.python_implementation() == 'PyPy': |
| 82 raise unittest.SkipTest(skip_pypy) |
| 83 |
| 84 p = replwrap.python() |
| 85 res = p.run_command('4+7') |
| 86 assert res.strip() == '11' |
| 87 |
| 88 res = p.run_command('for a in range(3): print(a)\n') |
| 89 assert res.strip().splitlines() == ['0', '1', '2'] |
| 90 |
| 91 def test_no_change_prompt(self): |
| 92 if platform.python_implementation() == 'PyPy': |
| 93 raise unittest.SkipTest(skip_pypy) |
| 94 |
| 95 child = pexpect.spawn('python', echo=False, timeout=5, encoding='utf-8') |
| 96 # prompt_change=None should mean no prompt change |
| 97 py = replwrap.REPLWrapper(child, u">>> ", prompt_change=None, |
| 98 continuation_prompt=u"... ") |
| 99 assert py.prompt == ">>> " |
| 100 |
| 101 res = py.run_command("for a in range(3): print(a)\n") |
| 102 assert res.strip().splitlines() == ['0', '1', '2'] |
| 103 |
| 104 if __name__ == '__main__': |
| 105 unittest.main() |
OLD | NEW |