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