Increase timeout of replwrap test from 2=>5
[third_party/pexpect] / tests / test_replwrap.py
1 import platform
2 import unittest
3 import re
4 import os
5
6 import pexpect
7 from pexpect import replwrap
8
9
10 class REPLWrapTestCase(unittest.TestCase):
11     def setUp(self):
12         super(REPLWrapTestCase, self).setUp()
13         self.save_ps1 = os.getenv('PS1', r'\$')
14         self.save_ps2 = os.getenv('PS2', '>')
15         os.putenv('PS1', r'\$')
16         os.putenv('PS2', '>')
17
18     def tearDown(self):
19         super(REPLWrapTestCase, self).tearDown()
20         os.putenv('PS1', self.save_ps1)
21         os.putenv('PS2', self.save_ps2)
22
23     def test_bash(self):
24         bash = replwrap.bash()
25         res = bash.run_command("time")
26         assert 'real' in res, res
27
28         # PAGER should be set to cat, otherwise man hangs
29         res = bash.run_command('man sleep', timeout=5)
30         assert 'SLEEP' in res, res
31
32     def test_multiline(self):
33         bash = replwrap.bash()
34         res = bash.run_command("echo '1 2\n3 4'")
35         self.assertEqual(res.strip().splitlines(), ['1 2', '3 4'])
36
37         # Should raise ValueError if input is incomplete
38         try:
39             bash.run_command("echo '5 6")
40         except ValueError:
41             pass
42         else:
43             assert False, "Didn't raise ValueError for incomplete input"
44
45         # Check that the REPL was reset (SIGINT) after the incomplete input
46         res = bash.run_command("echo '1 2\n3 4'")
47         self.assertEqual(res.strip().splitlines(), ['1 2', '3 4'])
48
49     def test_existing_spawn(self):
50         child = pexpect.spawnu("bash", timeout=5, echo=False)
51         repl = replwrap.REPLWrapper(child, re.compile('[$#]'),
52                                     "PS1='{0}' PS2='{1}' "
53                                     "PROMPT_COMMAND=''")
54
55         res = repl.run_command("echo $HOME")
56         assert res.startswith('/'), res
57
58     def test_python(self):
59         if platform.python_implementation() == 'PyPy':
60             raise unittest.SkipTest("This test fails on PyPy because of REPL differences")
61
62         p = replwrap.python()
63         res = p.run_command('4+7')
64         assert res.strip() == '11'
65
66         res = p.run_command('for a in range(3): print(a)\n')
67         assert res.strip().splitlines() == ['0', '1', '2']
68
69     def test_no_change_prompt(self):
70         if platform.python_implementation() == 'PyPy':
71             raise unittest.SkipTest("This test fails on PyPy because of REPL differences")
72
73         child = pexpect.spawnu('python', echo=False, timeout=5)
74         # prompt_change=None should mean no prompt change
75         py = replwrap.REPLWrapper(child, replwrap.u(">>> "), prompt_change=None,
76                                   continuation_prompt=replwrap.u("... "))
77         assert py.prompt == ">>> "
78
79         res = py.run_command("for a in range(3): print(a)\n")
80         assert res.strip().splitlines() == ['0', '1', '2']
81
82
83 if __name__ == '__main__':
84     unittest.main()