##// END OF EJS Templates
nobody pexpects the Spanish Inquisition
Paul Ivanov -
Show More
@@ -1,133 +1,132 b''
1 """Test embedding of IPython"""
1 """Test embedding of IPython"""
2
2
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2013 The IPython Development Team
4 # Copyright (C) 2013 The IPython Development Team
5 #
5 #
6 # Distributed under the terms of the BSD License. The full license is in
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
7 # the file COPYING, distributed as part of this software.
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 import os
14 import os
15 import subprocess
15 import subprocess
16 import sys
16 import sys
17 import nose.tools as nt
17 import nose.tools as nt
18 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
18 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
19 from IPython.testing.decorators import skip_win32
19 from IPython.testing.decorators import skip_win32
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Tests
22 # Tests
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25
25
26 _sample_embed = b"""
26 _sample_embed = b"""
27 import IPython
27 import IPython
28
28
29 a = 3
29 a = 3
30 b = 14
30 b = 14
31 print(a, '.', b)
31 print(a, '.', b)
32
32
33 IPython.embed()
33 IPython.embed()
34
34
35 print('bye!')
35 print('bye!')
36 """
36 """
37
37
38 _exit = b"exit\r"
38 _exit = b"exit\r"
39
39
40 def test_ipython_embed():
40 def test_ipython_embed():
41 """test that `IPython.embed()` works"""
41 """test that `IPython.embed()` works"""
42 with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
42 with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
43 f.write(_sample_embed)
43 f.write(_sample_embed)
44 f.flush()
44 f.flush()
45 f.close() # otherwise msft won't be able to read the file
45 f.close() # otherwise msft won't be able to read the file
46
46
47 # run `python file_with_embed.py`
47 # run `python file_with_embed.py`
48 cmd = [sys.executable, f.name]
48 cmd = [sys.executable, f.name]
49 env = os.environ.copy()
49 env = os.environ.copy()
50 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
50 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
51
51
52 p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE,
52 p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE,
53 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
53 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54 out, err = p.communicate(_exit)
54 out, err = p.communicate(_exit)
55 std = out.decode('UTF-8')
55 std = out.decode('UTF-8')
56
56
57 nt.assert_equal(p.returncode, 0)
57 nt.assert_equal(p.returncode, 0)
58 nt.assert_in('3 . 14', std)
58 nt.assert_in('3 . 14', std)
59 if os.name != 'nt':
59 if os.name != 'nt':
60 # TODO: Fix up our different stdout references, see issue gh-14
60 # TODO: Fix up our different stdout references, see issue gh-14
61 nt.assert_in('IPython', std)
61 nt.assert_in('IPython', std)
62 nt.assert_in('bye!', std)
62 nt.assert_in('bye!', std)
63
63
64 @skip_win32
64 @skip_win32
65 def test_nest_embed():
65 def test_nest_embed():
66 """test that `IPython.embed()` is nestable"""
66 """test that `IPython.embed()` is nestable"""
67 import pexpect
67 import pexpect
68 ipy_prompt = r']:' #ansi color codes give problems matching beyond this
68 ipy_prompt = r']:' #ansi color codes give problems matching beyond this
69 env = os.environ.copy()
69 env = os.environ.copy()
70 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
70 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
71
71
72
72
73 child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'],
73 child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'],
74 env=env)
74 env=env)
75 child.expect(ipy_prompt)
75 child.expect(ipy_prompt)
76 child.expect(ipy_prompt)
77 child.sendline("import IPython")
76 child.sendline("import IPython")
78 child.expect(ipy_prompt)
77 child.expect(ipy_prompt)
79 child.sendline("ip0 = get_ipython()")
78 child.sendline("ip0 = get_ipython()")
80 #enter first nested embed
79 #enter first nested embed
81 child.sendline("IPython.embed()")
80 child.sendline("IPython.embed()")
82 #skip the banner until we get to a prompt
81 #skip the banner until we get to a prompt
83 try:
82 try:
84 prompted = -1
83 prompted = -1
85 while prompted != 0:
84 while prompted != 0:
86 prompted = child.expect([ipy_prompt, '\r\n'])
85 prompted = child.expect([ipy_prompt, '\r\n'])
87 except pexpect.TIMEOUT as e:
86 except pexpect.TIMEOUT as e:
88 print(e)
87 print(e)
89 #child.interact()
88 #child.interact()
90 child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt)
89 child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt)
91 child.sendline("print('true' if embed1 is not ip0 else 'false')")
90 child.sendline("print('true' if embed1 is not ip0 else 'false')")
92 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
91 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
93 child.expect(ipy_prompt)
92 child.expect(ipy_prompt)
94 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
93 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
95 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
94 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
96 child.expect(ipy_prompt)
95 child.expect(ipy_prompt)
97 #enter second nested embed
96 #enter second nested embed
98 child.sendline("IPython.embed()")
97 child.sendline("IPython.embed()")
99 #skip the banner until we get to a prompt
98 #skip the banner until we get to a prompt
100 try:
99 try:
101 prompted = -1
100 prompted = -1
102 while prompted != 0:
101 while prompted != 0:
103 prompted = child.expect([ipy_prompt, '\r\n'])
102 prompted = child.expect([ipy_prompt, '\r\n'])
104 except pexpect.TIMEOUT as e:
103 except pexpect.TIMEOUT as e:
105 print(e)
104 print(e)
106 #child.interact()
105 #child.interact()
107 child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt)
106 child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt)
108 child.sendline("print('true' if embed2 is not embed1 else 'false')")
107 child.sendline("print('true' if embed2 is not embed1 else 'false')")
109 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
108 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
110 child.expect(ipy_prompt)
109 child.expect(ipy_prompt)
111 child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')")
110 child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')")
112 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
111 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
113 child.expect(ipy_prompt)
112 child.expect(ipy_prompt)
114 child.sendline('exit')
113 child.sendline('exit')
115 #back at first embed
114 #back at first embed
116 child.expect(ipy_prompt)
115 child.expect(ipy_prompt)
117 child.sendline("print('true' if get_ipython() is embed1 else 'false')")
116 child.sendline("print('true' if get_ipython() is embed1 else 'false')")
118 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
117 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
119 child.expect(ipy_prompt)
118 child.expect(ipy_prompt)
120 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
119 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
121 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
120 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
122 child.expect(ipy_prompt)
121 child.expect(ipy_prompt)
123 child.sendline('exit')
122 child.sendline('exit')
124 #back at launching scope
123 #back at launching scope
125 child.expect(ipy_prompt)
124 child.expect(ipy_prompt)
126 child.sendline("print('true' if get_ipython() is ip0 else 'false')")
125 child.sendline("print('true' if get_ipython() is ip0 else 'false')")
127 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
126 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
128 child.expect(ipy_prompt)
127 child.expect(ipy_prompt)
129 child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')")
128 child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')")
130 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
129 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
131 child.expect(ipy_prompt)
130 child.expect(ipy_prompt)
132 child.sendline('exit')
131 child.sendline('exit')
133 child.close()
132 child.close()
General Comments 0
You need to be logged in to leave comments. Login now