##// END OF EJS Templates
add test_nest_embed...
Jason Newton -
Show More
@@ -1,57 +1,125 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 sys
15 import sys
16 import nose.tools as nt
16 import nose.tools as nt
17 from IPython.utils.process import process_handler
17 from IPython.utils.process import process_handler
18 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
18 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
19 from IPython.testing.decorators import skip_win32
19
20
20 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
21 # Tests
22 # Tests
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23
24
25
24 _sample_embed = b"""
26 _sample_embed = b"""
25 from __future__ import print_function
27 from __future__ import print_function
26 import IPython
28 import IPython
27
29
28 a = 3
30 a = 3
29 b = 14
31 b = 14
30 print(a, '.', b)
32 print(a, '.', b)
31
33
32 IPython.embed()
34 IPython.embed()
33
35
34 print('bye!')
36 print('bye!')
35 """
37 """
36
38
37 _exit = b"exit\r"
39 _exit = b"exit\r"
38
40
39 def test_ipython_embed():
41 def test_ipython_embed():
40 """test that `IPython.embed()` works"""
42 """test that `IPython.embed()` works"""
41 with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
43 with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
42 f.write(_sample_embed)
44 f.write(_sample_embed)
43 f.flush()
45 f.flush()
44 f.close() # otherwise msft won't be able to read the file
46 f.close() # otherwise msft won't be able to read the file
45
47
46 # run `python file_with_embed.py`
48 # run `python file_with_embed.py`
47 cmd = [sys.executable, f.name]
49 cmd = [sys.executable, f.name]
48
50
49 out, p = process_handler(cmd, lambda p: (p.communicate(_exit), p))
51 out, p = process_handler(cmd, lambda p: (p.communicate(_exit), p))
50 std = out[0].decode('UTF-8')
52 std = out[0].decode('UTF-8')
51 nt.assert_equal(p.returncode, 0)
53 nt.assert_equal(p.returncode, 0)
52 nt.assert_in('3 . 14', std)
54 nt.assert_in('3 . 14', std)
53 if os.name != 'nt':
55 if os.name != 'nt':
54 # TODO: Fix up our different stdout references, see issue gh-14
56 # TODO: Fix up our different stdout references, see issue gh-14
55 nt.assert_in('IPython', std)
57 nt.assert_in('IPython', std)
56 nt.assert_in('bye!', std)
58 nt.assert_in('bye!', std)
57
59
60 @skip_win32
61 def test_nest_embed():
62 """test that `IPython.embed()` is nestable"""
63 from IPython.external import pexpect
64 ipy_prompt = r']:' #ansi color codes give problems matching beyond this
65
66
67 child = pexpect.spawn('%s -m IPython'%(sys.executable, ))
68 child.expect(ipy_prompt)
69 child.sendline("from __future__ import print_function")
70 child.expect(ipy_prompt)
71 child.sendline("import IPython")
72 child.expect(ipy_prompt)
73 child.sendline("ip0 = get_ipython()")
74 #enter first nested embed
75 child.sendline("IPython.embed()")
76 #skip the banner until we get to a prompt
77 try:
78 prompted = -1
79 while prompted != 0:
80 prompted = child.expect([ipy_prompt, '\r\n'])
81 except pexpect.TIMEOUT as e:
82 print(e)
83 #child.interact()
84 child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt)
85 child.sendline("print('true' if embed1 is not ip0 else 'false')")
86 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
87 child.expect(ipy_prompt)
88 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
89 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
90 child.expect(ipy_prompt)
91 #enter second nested embed
92 child.sendline("IPython.embed()")
93 #skip the banner until we get to a prompt
94 try:
95 prompted = -1
96 while prompted != 0:
97 prompted = child.expect([ipy_prompt, '\r\n'])
98 except pexpect.TIMEOUT as e:
99 print(e)
100 #child.interact()
101 child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt)
102 child.sendline("print('true' if embed2 is not embed1 else 'false')")
103 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
104 child.expect(ipy_prompt)
105 child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')")
106 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
107 child.expect(ipy_prompt)
108 child.sendline('exit')
109 #back at first embed
110 child.expect(ipy_prompt)
111 child.sendline("print('true' if get_ipython() is embed1 else 'false')")
112 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
113 child.expect(ipy_prompt)
114 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
115 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
116 child.expect(ipy_prompt)
117 child.sendline('exit')
118 #back at launching scope
119 child.expect(ipy_prompt)
120 child.sendline("print('true' if get_ipython() is ip0 else 'false')")
121 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
122 child.expect(ipy_prompt)
123 child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')")
124 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
125 child.expect(ipy_prompt)
General Comments 0
You need to be logged in to leave comments. Login now