7 from pexpect import replwrap
10 class REPLWrapTestCase(unittest.TestCase):
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'\$')
19 super(REPLWrapTestCase, self).tearDown()
20 os.putenv('PS1', self.save_ps1)
21 os.putenv('PS2', self.save_ps2)
24 bash = replwrap.bash()
25 res = bash.run_command("time")
26 assert 'real' in res, res
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
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'])
37 # Should raise ValueError if input is incomplete
39 bash.run_command("echo '5 6")
43 assert False, "Didn't raise ValueError for incomplete input"
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'])
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}' "
55 res = repl.run_command("echo $HOME")
56 assert res.startswith('/'), res
58 def test_python(self):
59 if platform.python_implementation() == 'PyPy':
60 raise unittest.SkipTest("This test fails on PyPy because of REPL differences")
63 res = p.run_command('4+7')
64 assert res.strip() == '11'
66 res = p.run_command('for a in range(3): print(a)\n')
67 assert res.strip().splitlines() == ['0', '1', '2']
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")
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 == ">>> "
79 res = py.run_command("for a in range(3): print(a)\n")
80 assert res.strip().splitlines() == ['0', '1', '2']
83 if __name__ == '__main__':