##// END OF EJS Templates
Add a test for reset behavior
Matthias Bussonnier -
Show More
@@ -1,1172 +1,1197 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6
6
7 import io
7 import io
8 import os
8 import os
9 import re
9 import re
10 import sys
10 import sys
11 import warnings
11 import warnings
12 from textwrap import dedent
12 from textwrap import dedent
13 from unittest import TestCase
13 from unittest import TestCase
14 from importlib import invalidate_caches
14 from importlib import invalidate_caches
15 from io import StringIO
15 from io import StringIO
16
16
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 import shlex
19 import shlex
20
20
21 from IPython import get_ipython
21 from IPython import get_ipython
22 from IPython.core import magic
22 from IPython.core import magic
23 from IPython.core.error import UsageError
23 from IPython.core.error import UsageError
24 from IPython.core.magic import (Magics, magics_class, line_magic,
24 from IPython.core.magic import (Magics, magics_class, line_magic,
25 cell_magic,
25 cell_magic,
26 register_line_magic, register_cell_magic)
26 register_line_magic, register_cell_magic)
27 from IPython.core.magics import execution, script, code, logging, osm
27 from IPython.core.magics import execution, script, code, logging, osm
28 from IPython.testing import decorators as dec
28 from IPython.testing import decorators as dec
29 from IPython.testing import tools as tt
29 from IPython.testing import tools as tt
30 from IPython.utils.io import capture_output
30 from IPython.utils.io import capture_output
31 from IPython.utils.tempdir import (TemporaryDirectory,
31 from IPython.utils.tempdir import (TemporaryDirectory,
32 TemporaryWorkingDirectory)
32 TemporaryWorkingDirectory)
33 from IPython.utils.process import find_cmd
33 from IPython.utils.process import find_cmd
34
34
35
35
36
36
37 _ip = get_ipython()
37 _ip = get_ipython()
38
38
39 @magic.magics_class
39 @magic.magics_class
40 class DummyMagics(magic.Magics): pass
40 class DummyMagics(magic.Magics): pass
41
41
42 def test_extract_code_ranges():
42 def test_extract_code_ranges():
43 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
43 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
44 expected = [(0, 1),
44 expected = [(0, 1),
45 (2, 3),
45 (2, 3),
46 (4, 6),
46 (4, 6),
47 (6, 9),
47 (6, 9),
48 (9, 14),
48 (9, 14),
49 (16, None),
49 (16, None),
50 (None, 9),
50 (None, 9),
51 (9, None),
51 (9, None),
52 (None, 13),
52 (None, 13),
53 (None, None)]
53 (None, None)]
54 actual = list(code.extract_code_ranges(instr))
54 actual = list(code.extract_code_ranges(instr))
55 nt.assert_equal(actual, expected)
55 nt.assert_equal(actual, expected)
56
56
57 def test_extract_symbols():
57 def test_extract_symbols():
58 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
58 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
59 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
59 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
60 expected = [([], ['a']),
60 expected = [([], ['a']),
61 (["def b():\n return 42\n"], []),
61 (["def b():\n return 42\n"], []),
62 (["class A: pass\n"], []),
62 (["class A: pass\n"], []),
63 (["class A: pass\n", "def b():\n return 42\n"], []),
63 (["class A: pass\n", "def b():\n return 42\n"], []),
64 (["class A: pass\n"], ['a']),
64 (["class A: pass\n"], ['a']),
65 ([], ['z'])]
65 ([], ['z'])]
66 for symbols, exp in zip(symbols_args, expected):
66 for symbols, exp in zip(symbols_args, expected):
67 nt.assert_equal(code.extract_symbols(source, symbols), exp)
67 nt.assert_equal(code.extract_symbols(source, symbols), exp)
68
68
69
69
70 def test_extract_symbols_raises_exception_with_non_python_code():
70 def test_extract_symbols_raises_exception_with_non_python_code():
71 source = ("=begin A Ruby program :)=end\n"
71 source = ("=begin A Ruby program :)=end\n"
72 "def hello\n"
72 "def hello\n"
73 "puts 'Hello world'\n"
73 "puts 'Hello world'\n"
74 "end")
74 "end")
75 with nt.assert_raises(SyntaxError):
75 with nt.assert_raises(SyntaxError):
76 code.extract_symbols(source, "hello")
76 code.extract_symbols(source, "hello")
77
77
78
78
79 def test_magic_not_found():
79 def test_magic_not_found():
80 # magic not found raises UsageError
80 # magic not found raises UsageError
81 with nt.assert_raises(UsageError):
81 with nt.assert_raises(UsageError):
82 _ip.magic('doesntexist')
82 _ip.magic('doesntexist')
83
83
84 # ensure result isn't success when a magic isn't found
84 # ensure result isn't success when a magic isn't found
85 result = _ip.run_cell('%doesntexist')
85 result = _ip.run_cell('%doesntexist')
86 assert isinstance(result.error_in_exec, UsageError)
86 assert isinstance(result.error_in_exec, UsageError)
87
87
88
88
89 def test_cell_magic_not_found():
89 def test_cell_magic_not_found():
90 # magic not found raises UsageError
90 # magic not found raises UsageError
91 with nt.assert_raises(UsageError):
91 with nt.assert_raises(UsageError):
92 _ip.run_cell_magic('doesntexist', 'line', 'cell')
92 _ip.run_cell_magic('doesntexist', 'line', 'cell')
93
93
94 # ensure result isn't success when a magic isn't found
94 # ensure result isn't success when a magic isn't found
95 result = _ip.run_cell('%%doesntexist')
95 result = _ip.run_cell('%%doesntexist')
96 assert isinstance(result.error_in_exec, UsageError)
96 assert isinstance(result.error_in_exec, UsageError)
97
97
98
98
99 def test_magic_error_status():
99 def test_magic_error_status():
100 def fail(shell):
100 def fail(shell):
101 1/0
101 1/0
102 _ip.register_magic_function(fail)
102 _ip.register_magic_function(fail)
103 result = _ip.run_cell('%fail')
103 result = _ip.run_cell('%fail')
104 assert isinstance(result.error_in_exec, ZeroDivisionError)
104 assert isinstance(result.error_in_exec, ZeroDivisionError)
105
105
106
106
107 def test_config():
107 def test_config():
108 """ test that config magic does not raise
108 """ test that config magic does not raise
109 can happen if Configurable init is moved too early into
109 can happen if Configurable init is moved too early into
110 Magics.__init__ as then a Config object will be registered as a
110 Magics.__init__ as then a Config object will be registered as a
111 magic.
111 magic.
112 """
112 """
113 ## should not raise.
113 ## should not raise.
114 _ip.magic('config')
114 _ip.magic('config')
115
115
116 def test_config_available_configs():
116 def test_config_available_configs():
117 """ test that config magic prints available configs in unique and
117 """ test that config magic prints available configs in unique and
118 sorted order. """
118 sorted order. """
119 with capture_output() as captured:
119 with capture_output() as captured:
120 _ip.magic('config')
120 _ip.magic('config')
121
121
122 stdout = captured.stdout
122 stdout = captured.stdout
123 config_classes = stdout.strip().split('\n')[1:]
123 config_classes = stdout.strip().split('\n')[1:]
124 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
124 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
125
125
126 def test_config_print_class():
126 def test_config_print_class():
127 """ test that config with a classname prints the class's options. """
127 """ test that config with a classname prints the class's options. """
128 with capture_output() as captured:
128 with capture_output() as captured:
129 _ip.magic('config TerminalInteractiveShell')
129 _ip.magic('config TerminalInteractiveShell')
130
130
131 stdout = captured.stdout
131 stdout = captured.stdout
132 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
132 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
133 print(stdout)
133 print(stdout)
134 raise AssertionError("1st line of stdout not like "
134 raise AssertionError("1st line of stdout not like "
135 "'TerminalInteractiveShell.* options'")
135 "'TerminalInteractiveShell.* options'")
136
136
137 def test_rehashx():
137 def test_rehashx():
138 # clear up everything
138 # clear up everything
139 _ip.alias_manager.clear_aliases()
139 _ip.alias_manager.clear_aliases()
140 del _ip.db['syscmdlist']
140 del _ip.db['syscmdlist']
141
141
142 _ip.magic('rehashx')
142 _ip.magic('rehashx')
143 # Practically ALL ipython development systems will have more than 10 aliases
143 # Practically ALL ipython development systems will have more than 10 aliases
144
144
145 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
145 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
146 for name, cmd in _ip.alias_manager.aliases:
146 for name, cmd in _ip.alias_manager.aliases:
147 # we must strip dots from alias names
147 # we must strip dots from alias names
148 nt.assert_not_in('.', name)
148 nt.assert_not_in('.', name)
149
149
150 # rehashx must fill up syscmdlist
150 # rehashx must fill up syscmdlist
151 scoms = _ip.db['syscmdlist']
151 scoms = _ip.db['syscmdlist']
152 nt.assert_true(len(scoms) > 10)
152 nt.assert_true(len(scoms) > 10)
153
153
154
154
155 def test_magic_parse_options():
155 def test_magic_parse_options():
156 """Test that we don't mangle paths when parsing magic options."""
156 """Test that we don't mangle paths when parsing magic options."""
157 ip = get_ipython()
157 ip = get_ipython()
158 path = 'c:\\x'
158 path = 'c:\\x'
159 m = DummyMagics(ip)
159 m = DummyMagics(ip)
160 opts = m.parse_options('-f %s' % path,'f:')[0]
160 opts = m.parse_options('-f %s' % path,'f:')[0]
161 # argv splitting is os-dependent
161 # argv splitting is os-dependent
162 if os.name == 'posix':
162 if os.name == 'posix':
163 expected = 'c:x'
163 expected = 'c:x'
164 else:
164 else:
165 expected = path
165 expected = path
166 nt.assert_equal(opts['f'], expected)
166 nt.assert_equal(opts['f'], expected)
167
167
168 def test_magic_parse_long_options():
168 def test_magic_parse_long_options():
169 """Magic.parse_options can handle --foo=bar long options"""
169 """Magic.parse_options can handle --foo=bar long options"""
170 ip = get_ipython()
170 ip = get_ipython()
171 m = DummyMagics(ip)
171 m = DummyMagics(ip)
172 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
172 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
173 nt.assert_in('foo', opts)
173 nt.assert_in('foo', opts)
174 nt.assert_in('bar', opts)
174 nt.assert_in('bar', opts)
175 nt.assert_equal(opts['bar'], "bubble")
175 nt.assert_equal(opts['bar'], "bubble")
176
176
177
177
178 @dec.skip_without('sqlite3')
178 @dec.skip_without('sqlite3')
179 def doctest_hist_f():
179 def doctest_hist_f():
180 """Test %hist -f with temporary filename.
180 """Test %hist -f with temporary filename.
181
181
182 In [9]: import tempfile
182 In [9]: import tempfile
183
183
184 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
184 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
185
185
186 In [11]: %hist -nl -f $tfile 3
186 In [11]: %hist -nl -f $tfile 3
187
187
188 In [13]: import os; os.unlink(tfile)
188 In [13]: import os; os.unlink(tfile)
189 """
189 """
190
190
191
191
192 @dec.skip_without('sqlite3')
192 @dec.skip_without('sqlite3')
193 def doctest_hist_r():
193 def doctest_hist_r():
194 """Test %hist -r
194 """Test %hist -r
195
195
196 XXX - This test is not recording the output correctly. For some reason, in
196 XXX - This test is not recording the output correctly. For some reason, in
197 testing mode the raw history isn't getting populated. No idea why.
197 testing mode the raw history isn't getting populated. No idea why.
198 Disabling the output checking for now, though at least we do run it.
198 Disabling the output checking for now, though at least we do run it.
199
199
200 In [1]: 'hist' in _ip.lsmagic()
200 In [1]: 'hist' in _ip.lsmagic()
201 Out[1]: True
201 Out[1]: True
202
202
203 In [2]: x=1
203 In [2]: x=1
204
204
205 In [3]: %hist -rl 2
205 In [3]: %hist -rl 2
206 x=1 # random
206 x=1 # random
207 %hist -r 2
207 %hist -r 2
208 """
208 """
209
209
210
210
211 @dec.skip_without('sqlite3')
211 @dec.skip_without('sqlite3')
212 def doctest_hist_op():
212 def doctest_hist_op():
213 """Test %hist -op
213 """Test %hist -op
214
214
215 In [1]: class b(float):
215 In [1]: class b(float):
216 ...: pass
216 ...: pass
217 ...:
217 ...:
218
218
219 In [2]: class s(object):
219 In [2]: class s(object):
220 ...: def __str__(self):
220 ...: def __str__(self):
221 ...: return 's'
221 ...: return 's'
222 ...:
222 ...:
223
223
224 In [3]:
224 In [3]:
225
225
226 In [4]: class r(b):
226 In [4]: class r(b):
227 ...: def __repr__(self):
227 ...: def __repr__(self):
228 ...: return 'r'
228 ...: return 'r'
229 ...:
229 ...:
230
230
231 In [5]: class sr(s,r): pass
231 In [5]: class sr(s,r): pass
232 ...:
232 ...:
233
233
234 In [6]:
234 In [6]:
235
235
236 In [7]: bb=b()
236 In [7]: bb=b()
237
237
238 In [8]: ss=s()
238 In [8]: ss=s()
239
239
240 In [9]: rr=r()
240 In [9]: rr=r()
241
241
242 In [10]: ssrr=sr()
242 In [10]: ssrr=sr()
243
243
244 In [11]: 4.5
244 In [11]: 4.5
245 Out[11]: 4.5
245 Out[11]: 4.5
246
246
247 In [12]: str(ss)
247 In [12]: str(ss)
248 Out[12]: 's'
248 Out[12]: 's'
249
249
250 In [13]:
250 In [13]:
251
251
252 In [14]: %hist -op
252 In [14]: %hist -op
253 >>> class b:
253 >>> class b:
254 ... pass
254 ... pass
255 ...
255 ...
256 >>> class s(b):
256 >>> class s(b):
257 ... def __str__(self):
257 ... def __str__(self):
258 ... return 's'
258 ... return 's'
259 ...
259 ...
260 >>>
260 >>>
261 >>> class r(b):
261 >>> class r(b):
262 ... def __repr__(self):
262 ... def __repr__(self):
263 ... return 'r'
263 ... return 'r'
264 ...
264 ...
265 >>> class sr(s,r): pass
265 >>> class sr(s,r): pass
266 >>>
266 >>>
267 >>> bb=b()
267 >>> bb=b()
268 >>> ss=s()
268 >>> ss=s()
269 >>> rr=r()
269 >>> rr=r()
270 >>> ssrr=sr()
270 >>> ssrr=sr()
271 >>> 4.5
271 >>> 4.5
272 4.5
272 4.5
273 >>> str(ss)
273 >>> str(ss)
274 's'
274 's'
275 >>>
275 >>>
276 """
276 """
277
277
278 def test_hist_pof():
278 def test_hist_pof():
279 ip = get_ipython()
279 ip = get_ipython()
280 ip.run_cell(u"1+2", store_history=True)
280 ip.run_cell(u"1+2", store_history=True)
281 #raise Exception(ip.history_manager.session_number)
281 #raise Exception(ip.history_manager.session_number)
282 #raise Exception(list(ip.history_manager._get_range_session()))
282 #raise Exception(list(ip.history_manager._get_range_session()))
283 with TemporaryDirectory() as td:
283 with TemporaryDirectory() as td:
284 tf = os.path.join(td, 'hist.py')
284 tf = os.path.join(td, 'hist.py')
285 ip.run_line_magic('history', '-pof %s' % tf)
285 ip.run_line_magic('history', '-pof %s' % tf)
286 assert os.path.isfile(tf)
286 assert os.path.isfile(tf)
287
287
288
288
289 @dec.skip_without('sqlite3')
289 @dec.skip_without('sqlite3')
290 def test_macro():
290 def test_macro():
291 ip = get_ipython()
291 ip = get_ipython()
292 ip.history_manager.reset() # Clear any existing history.
292 ip.history_manager.reset() # Clear any existing history.
293 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
293 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
294 for i, cmd in enumerate(cmds, start=1):
294 for i, cmd in enumerate(cmds, start=1):
295 ip.history_manager.store_inputs(i, cmd)
295 ip.history_manager.store_inputs(i, cmd)
296 ip.magic("macro test 1-3")
296 ip.magic("macro test 1-3")
297 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
297 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
298
298
299 # List macros
299 # List macros
300 nt.assert_in("test", ip.magic("macro"))
300 nt.assert_in("test", ip.magic("macro"))
301
301
302
302
303 @dec.skip_without('sqlite3')
303 @dec.skip_without('sqlite3')
304 def test_macro_run():
304 def test_macro_run():
305 """Test that we can run a multi-line macro successfully."""
305 """Test that we can run a multi-line macro successfully."""
306 ip = get_ipython()
306 ip = get_ipython()
307 ip.history_manager.reset()
307 ip.history_manager.reset()
308 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
308 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
309 for cmd in cmds:
309 for cmd in cmds:
310 ip.run_cell(cmd, store_history=True)
310 ip.run_cell(cmd, store_history=True)
311 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
311 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
312 with tt.AssertPrints("12"):
312 with tt.AssertPrints("12"):
313 ip.run_cell("test")
313 ip.run_cell("test")
314 with tt.AssertPrints("13"):
314 with tt.AssertPrints("13"):
315 ip.run_cell("test")
315 ip.run_cell("test")
316
316
317
317
318 def test_magic_magic():
318 def test_magic_magic():
319 """Test %magic"""
319 """Test %magic"""
320 ip = get_ipython()
320 ip = get_ipython()
321 with capture_output() as captured:
321 with capture_output() as captured:
322 ip.magic("magic")
322 ip.magic("magic")
323
323
324 stdout = captured.stdout
324 stdout = captured.stdout
325 nt.assert_in('%magic', stdout)
325 nt.assert_in('%magic', stdout)
326 nt.assert_in('IPython', stdout)
326 nt.assert_in('IPython', stdout)
327 nt.assert_in('Available', stdout)
327 nt.assert_in('Available', stdout)
328
328
329
329
330 @dec.skipif_not_numpy
330 @dec.skipif_not_numpy
331 def test_numpy_reset_array_undec():
331 def test_numpy_reset_array_undec():
332 "Test '%reset array' functionality"
332 "Test '%reset array' functionality"
333 _ip.ex('import numpy as np')
333 _ip.ex('import numpy as np')
334 _ip.ex('a = np.empty(2)')
334 _ip.ex('a = np.empty(2)')
335 nt.assert_in('a', _ip.user_ns)
335 nt.assert_in('a', _ip.user_ns)
336 _ip.magic('reset -f array')
336 _ip.magic('reset -f array')
337 nt.assert_not_in('a', _ip.user_ns)
337 nt.assert_not_in('a', _ip.user_ns)
338
338
339 def test_reset_out():
339 def test_reset_out():
340 "Test '%reset out' magic"
340 "Test '%reset out' magic"
341 _ip.run_cell("parrot = 'dead'", store_history=True)
341 _ip.run_cell("parrot = 'dead'", store_history=True)
342 # test '%reset -f out', make an Out prompt
342 # test '%reset -f out', make an Out prompt
343 _ip.run_cell("parrot", store_history=True)
343 _ip.run_cell("parrot", store_history=True)
344 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
344 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
345 _ip.magic('reset -f out')
345 _ip.magic('reset -f out')
346 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
346 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
347 nt.assert_equal(len(_ip.user_ns['Out']), 0)
347 nt.assert_equal(len(_ip.user_ns['Out']), 0)
348
348
349 def test_reset_in():
349 def test_reset_in():
350 "Test '%reset in' magic"
350 "Test '%reset in' magic"
351 # test '%reset -f in'
351 # test '%reset -f in'
352 _ip.run_cell("parrot", store_history=True)
352 _ip.run_cell("parrot", store_history=True)
353 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
353 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
354 _ip.magic('%reset -f in')
354 _ip.magic('%reset -f in')
355 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
355 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
356 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
356 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
357
357
358 def test_reset_dhist():
358 def test_reset_dhist():
359 "Test '%reset dhist' magic"
359 "Test '%reset dhist' magic"
360 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
360 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
361 _ip.magic('cd ' + os.path.dirname(nt.__file__))
361 _ip.magic('cd ' + os.path.dirname(nt.__file__))
362 _ip.magic('cd -')
362 _ip.magic('cd -')
363 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
363 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
364 _ip.magic('reset -f dhist')
364 _ip.magic('reset -f dhist')
365 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
365 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
366 _ip.run_cell("_dh = [d for d in tmp]") #restore
366 _ip.run_cell("_dh = [d for d in tmp]") #restore
367
367
368 def test_reset_in_length():
368 def test_reset_in_length():
369 "Test that '%reset in' preserves In[] length"
369 "Test that '%reset in' preserves In[] length"
370 _ip.run_cell("print 'foo'")
370 _ip.run_cell("print 'foo'")
371 _ip.run_cell("reset -f in")
371 _ip.run_cell("reset -f in")
372 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
372 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
373
373
374 class TestResetErrors(TestCase):
375
376 def test_reset_redefine(self):
377
378 @magics_class
379 class KernelMagics(Magics):
380 @line_magic
381 def less(self, shell): pass
382
383 _ip.register_magics(KernelMagics)
384
385 with self.assertLogs() as cm:
386 # hack, we want to just capture logs, but assertLogs fails if not
387 # logs get produce.
388 # so log one things we ignore.
389 import logging as log_mod
390 log = log_mod.getLogger()
391 log.info('Nothing')
392 # end hack.
393 _ip.run_cell("reset -f")
394
395 assert len(cm.output) == 1
396 for out in cm.output:
397 assert "Invalid alias" not in out
398
374 def test_tb_syntaxerror():
399 def test_tb_syntaxerror():
375 """test %tb after a SyntaxError"""
400 """test %tb after a SyntaxError"""
376 ip = get_ipython()
401 ip = get_ipython()
377 ip.run_cell("for")
402 ip.run_cell("for")
378
403
379 # trap and validate stdout
404 # trap and validate stdout
380 save_stdout = sys.stdout
405 save_stdout = sys.stdout
381 try:
406 try:
382 sys.stdout = StringIO()
407 sys.stdout = StringIO()
383 ip.run_cell("%tb")
408 ip.run_cell("%tb")
384 out = sys.stdout.getvalue()
409 out = sys.stdout.getvalue()
385 finally:
410 finally:
386 sys.stdout = save_stdout
411 sys.stdout = save_stdout
387 # trim output, and only check the last line
412 # trim output, and only check the last line
388 last_line = out.rstrip().splitlines()[-1].strip()
413 last_line = out.rstrip().splitlines()[-1].strip()
389 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
414 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
390
415
391
416
392 def test_time():
417 def test_time():
393 ip = get_ipython()
418 ip = get_ipython()
394
419
395 with tt.AssertPrints("Wall time: "):
420 with tt.AssertPrints("Wall time: "):
396 ip.run_cell("%time None")
421 ip.run_cell("%time None")
397
422
398 ip.run_cell("def f(kmjy):\n"
423 ip.run_cell("def f(kmjy):\n"
399 " %time print (2*kmjy)")
424 " %time print (2*kmjy)")
400
425
401 with tt.AssertPrints("Wall time: "):
426 with tt.AssertPrints("Wall time: "):
402 with tt.AssertPrints("hihi", suppress=False):
427 with tt.AssertPrints("hihi", suppress=False):
403 ip.run_cell("f('hi')")
428 ip.run_cell("f('hi')")
404
429
405
430
406 @dec.skip_win32
431 @dec.skip_win32
407 def test_time2():
432 def test_time2():
408 ip = get_ipython()
433 ip = get_ipython()
409
434
410 with tt.AssertPrints("CPU times: user "):
435 with tt.AssertPrints("CPU times: user "):
411 ip.run_cell("%time None")
436 ip.run_cell("%time None")
412
437
413 def test_time3():
438 def test_time3():
414 """Erroneous magic function calls, issue gh-3334"""
439 """Erroneous magic function calls, issue gh-3334"""
415 ip = get_ipython()
440 ip = get_ipython()
416 ip.user_ns.pop('run', None)
441 ip.user_ns.pop('run', None)
417
442
418 with tt.AssertNotPrints("not found", channel='stderr'):
443 with tt.AssertNotPrints("not found", channel='stderr'):
419 ip.run_cell("%%time\n"
444 ip.run_cell("%%time\n"
420 "run = 0\n"
445 "run = 0\n"
421 "run += 1")
446 "run += 1")
422
447
423 def test_multiline_time():
448 def test_multiline_time():
424 """Make sure last statement from time return a value."""
449 """Make sure last statement from time return a value."""
425 ip = get_ipython()
450 ip = get_ipython()
426 ip.user_ns.pop('run', None)
451 ip.user_ns.pop('run', None)
427
452
428 ip.run_cell(dedent("""\
453 ip.run_cell(dedent("""\
429 %%time
454 %%time
430 a = "ho"
455 a = "ho"
431 b = "hey"
456 b = "hey"
432 a+b
457 a+b
433 """))
458 """))
434 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
459 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
435
460
436 def test_time_local_ns():
461 def test_time_local_ns():
437 """
462 """
438 Test that local_ns is actually global_ns when running a cell magic
463 Test that local_ns is actually global_ns when running a cell magic
439 """
464 """
440 ip = get_ipython()
465 ip = get_ipython()
441 ip.run_cell("%%time\n"
466 ip.run_cell("%%time\n"
442 "myvar = 1")
467 "myvar = 1")
443 nt.assert_equal(ip.user_ns['myvar'], 1)
468 nt.assert_equal(ip.user_ns['myvar'], 1)
444 del ip.user_ns['myvar']
469 del ip.user_ns['myvar']
445
470
446 def test_doctest_mode():
471 def test_doctest_mode():
447 "Toggle doctest_mode twice, it should be a no-op and run without error"
472 "Toggle doctest_mode twice, it should be a no-op and run without error"
448 _ip.magic('doctest_mode')
473 _ip.magic('doctest_mode')
449 _ip.magic('doctest_mode')
474 _ip.magic('doctest_mode')
450
475
451
476
452 def test_parse_options():
477 def test_parse_options():
453 """Tests for basic options parsing in magics."""
478 """Tests for basic options parsing in magics."""
454 # These are only the most minimal of tests, more should be added later. At
479 # These are only the most minimal of tests, more should be added later. At
455 # the very least we check that basic text/unicode calls work OK.
480 # the very least we check that basic text/unicode calls work OK.
456 m = DummyMagics(_ip)
481 m = DummyMagics(_ip)
457 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
482 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
458 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
483 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
459
484
460
485
461 def test_dirops():
486 def test_dirops():
462 """Test various directory handling operations."""
487 """Test various directory handling operations."""
463 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
488 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
464 curpath = os.getcwd
489 curpath = os.getcwd
465 startdir = os.getcwd()
490 startdir = os.getcwd()
466 ipdir = os.path.realpath(_ip.ipython_dir)
491 ipdir = os.path.realpath(_ip.ipython_dir)
467 try:
492 try:
468 _ip.magic('cd "%s"' % ipdir)
493 _ip.magic('cd "%s"' % ipdir)
469 nt.assert_equal(curpath(), ipdir)
494 nt.assert_equal(curpath(), ipdir)
470 _ip.magic('cd -')
495 _ip.magic('cd -')
471 nt.assert_equal(curpath(), startdir)
496 nt.assert_equal(curpath(), startdir)
472 _ip.magic('pushd "%s"' % ipdir)
497 _ip.magic('pushd "%s"' % ipdir)
473 nt.assert_equal(curpath(), ipdir)
498 nt.assert_equal(curpath(), ipdir)
474 _ip.magic('popd')
499 _ip.magic('popd')
475 nt.assert_equal(curpath(), startdir)
500 nt.assert_equal(curpath(), startdir)
476 finally:
501 finally:
477 os.chdir(startdir)
502 os.chdir(startdir)
478
503
479
504
480 def test_cd_force_quiet():
505 def test_cd_force_quiet():
481 """Test OSMagics.cd_force_quiet option"""
506 """Test OSMagics.cd_force_quiet option"""
482 _ip.config.OSMagics.cd_force_quiet = True
507 _ip.config.OSMagics.cd_force_quiet = True
483 osmagics = osm.OSMagics(shell=_ip)
508 osmagics = osm.OSMagics(shell=_ip)
484
509
485 startdir = os.getcwd()
510 startdir = os.getcwd()
486 ipdir = os.path.realpath(_ip.ipython_dir)
511 ipdir = os.path.realpath(_ip.ipython_dir)
487
512
488 try:
513 try:
489 with tt.AssertNotPrints(ipdir):
514 with tt.AssertNotPrints(ipdir):
490 osmagics.cd('"%s"' % ipdir)
515 osmagics.cd('"%s"' % ipdir)
491 with tt.AssertNotPrints(startdir):
516 with tt.AssertNotPrints(startdir):
492 osmagics.cd('-')
517 osmagics.cd('-')
493 finally:
518 finally:
494 os.chdir(startdir)
519 os.chdir(startdir)
495
520
496
521
497 def test_xmode():
522 def test_xmode():
498 # Calling xmode three times should be a no-op
523 # Calling xmode three times should be a no-op
499 xmode = _ip.InteractiveTB.mode
524 xmode = _ip.InteractiveTB.mode
500 for i in range(4):
525 for i in range(4):
501 _ip.magic("xmode")
526 _ip.magic("xmode")
502 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
527 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
503
528
504 def test_reset_hard():
529 def test_reset_hard():
505 monitor = []
530 monitor = []
506 class A(object):
531 class A(object):
507 def __del__(self):
532 def __del__(self):
508 monitor.append(1)
533 monitor.append(1)
509 def __repr__(self):
534 def __repr__(self):
510 return "<A instance>"
535 return "<A instance>"
511
536
512 _ip.user_ns["a"] = A()
537 _ip.user_ns["a"] = A()
513 _ip.run_cell("a")
538 _ip.run_cell("a")
514
539
515 nt.assert_equal(monitor, [])
540 nt.assert_equal(monitor, [])
516 _ip.magic("reset -f")
541 _ip.magic("reset -f")
517 nt.assert_equal(monitor, [1])
542 nt.assert_equal(monitor, [1])
518
543
519 class TestXdel(tt.TempFileMixin):
544 class TestXdel(tt.TempFileMixin):
520 def test_xdel(self):
545 def test_xdel(self):
521 """Test that references from %run are cleared by xdel."""
546 """Test that references from %run are cleared by xdel."""
522 src = ("class A(object):\n"
547 src = ("class A(object):\n"
523 " monitor = []\n"
548 " monitor = []\n"
524 " def __del__(self):\n"
549 " def __del__(self):\n"
525 " self.monitor.append(1)\n"
550 " self.monitor.append(1)\n"
526 "a = A()\n")
551 "a = A()\n")
527 self.mktmp(src)
552 self.mktmp(src)
528 # %run creates some hidden references...
553 # %run creates some hidden references...
529 _ip.magic("run %s" % self.fname)
554 _ip.magic("run %s" % self.fname)
530 # ... as does the displayhook.
555 # ... as does the displayhook.
531 _ip.run_cell("a")
556 _ip.run_cell("a")
532
557
533 monitor = _ip.user_ns["A"].monitor
558 monitor = _ip.user_ns["A"].monitor
534 nt.assert_equal(monitor, [])
559 nt.assert_equal(monitor, [])
535
560
536 _ip.magic("xdel a")
561 _ip.magic("xdel a")
537
562
538 # Check that a's __del__ method has been called.
563 # Check that a's __del__ method has been called.
539 nt.assert_equal(monitor, [1])
564 nt.assert_equal(monitor, [1])
540
565
541 def doctest_who():
566 def doctest_who():
542 """doctest for %who
567 """doctest for %who
543
568
544 In [1]: %reset -f
569 In [1]: %reset -f
545
570
546 In [2]: alpha = 123
571 In [2]: alpha = 123
547
572
548 In [3]: beta = 'beta'
573 In [3]: beta = 'beta'
549
574
550 In [4]: %who int
575 In [4]: %who int
551 alpha
576 alpha
552
577
553 In [5]: %who str
578 In [5]: %who str
554 beta
579 beta
555
580
556 In [6]: %whos
581 In [6]: %whos
557 Variable Type Data/Info
582 Variable Type Data/Info
558 ----------------------------
583 ----------------------------
559 alpha int 123
584 alpha int 123
560 beta str beta
585 beta str beta
561
586
562 In [7]: %who_ls
587 In [7]: %who_ls
563 Out[7]: ['alpha', 'beta']
588 Out[7]: ['alpha', 'beta']
564 """
589 """
565
590
566 def test_whos():
591 def test_whos():
567 """Check that whos is protected against objects where repr() fails."""
592 """Check that whos is protected against objects where repr() fails."""
568 class A(object):
593 class A(object):
569 def __repr__(self):
594 def __repr__(self):
570 raise Exception()
595 raise Exception()
571 _ip.user_ns['a'] = A()
596 _ip.user_ns['a'] = A()
572 _ip.magic("whos")
597 _ip.magic("whos")
573
598
574 def doctest_precision():
599 def doctest_precision():
575 """doctest for %precision
600 """doctest for %precision
576
601
577 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
602 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
578
603
579 In [2]: %precision 5
604 In [2]: %precision 5
580 Out[2]: '%.5f'
605 Out[2]: '%.5f'
581
606
582 In [3]: f.float_format
607 In [3]: f.float_format
583 Out[3]: '%.5f'
608 Out[3]: '%.5f'
584
609
585 In [4]: %precision %e
610 In [4]: %precision %e
586 Out[4]: '%e'
611 Out[4]: '%e'
587
612
588 In [5]: f(3.1415927)
613 In [5]: f(3.1415927)
589 Out[5]: '3.141593e+00'
614 Out[5]: '3.141593e+00'
590 """
615 """
591
616
592 def test_psearch():
617 def test_psearch():
593 with tt.AssertPrints("dict.fromkeys"):
618 with tt.AssertPrints("dict.fromkeys"):
594 _ip.run_cell("dict.fr*?")
619 _ip.run_cell("dict.fr*?")
595
620
596 def test_timeit_shlex():
621 def test_timeit_shlex():
597 """test shlex issues with timeit (#1109)"""
622 """test shlex issues with timeit (#1109)"""
598 _ip.ex("def f(*a,**kw): pass")
623 _ip.ex("def f(*a,**kw): pass")
599 _ip.magic('timeit -n1 "this is a bug".count(" ")')
624 _ip.magic('timeit -n1 "this is a bug".count(" ")')
600 _ip.magic('timeit -r1 -n1 f(" ", 1)')
625 _ip.magic('timeit -r1 -n1 f(" ", 1)')
601 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
626 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
602 _ip.magic('timeit -r1 -n1 ("a " + "b")')
627 _ip.magic('timeit -r1 -n1 ("a " + "b")')
603 _ip.magic('timeit -r1 -n1 f("a " + "b")')
628 _ip.magic('timeit -r1 -n1 f("a " + "b")')
604 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
629 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
605
630
606
631
607 def test_timeit_special_syntax():
632 def test_timeit_special_syntax():
608 "Test %%timeit with IPython special syntax"
633 "Test %%timeit with IPython special syntax"
609 @register_line_magic
634 @register_line_magic
610 def lmagic(line):
635 def lmagic(line):
611 ip = get_ipython()
636 ip = get_ipython()
612 ip.user_ns['lmagic_out'] = line
637 ip.user_ns['lmagic_out'] = line
613
638
614 # line mode test
639 # line mode test
615 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
640 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
616 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
641 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
617 # cell mode test
642 # cell mode test
618 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
643 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
619 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
644 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
620
645
621 def test_timeit_return():
646 def test_timeit_return():
622 """
647 """
623 test whether timeit -o return object
648 test whether timeit -o return object
624 """
649 """
625
650
626 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
651 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
627 assert(res is not None)
652 assert(res is not None)
628
653
629 def test_timeit_quiet():
654 def test_timeit_quiet():
630 """
655 """
631 test quiet option of timeit magic
656 test quiet option of timeit magic
632 """
657 """
633 with tt.AssertNotPrints("loops"):
658 with tt.AssertNotPrints("loops"):
634 _ip.run_cell("%timeit -n1 -r1 -q 1")
659 _ip.run_cell("%timeit -n1 -r1 -q 1")
635
660
636 def test_timeit_return_quiet():
661 def test_timeit_return_quiet():
637 with tt.AssertNotPrints("loops"):
662 with tt.AssertNotPrints("loops"):
638 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
663 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
639 assert (res is not None)
664 assert (res is not None)
640
665
641 def test_timeit_invalid_return():
666 def test_timeit_invalid_return():
642 with nt.assert_raises_regex(SyntaxError, "outside function"):
667 with nt.assert_raises_regex(SyntaxError, "outside function"):
643 _ip.run_line_magic('timeit', 'return')
668 _ip.run_line_magic('timeit', 'return')
644
669
645 @dec.skipif(execution.profile is None)
670 @dec.skipif(execution.profile is None)
646 def test_prun_special_syntax():
671 def test_prun_special_syntax():
647 "Test %%prun with IPython special syntax"
672 "Test %%prun with IPython special syntax"
648 @register_line_magic
673 @register_line_magic
649 def lmagic(line):
674 def lmagic(line):
650 ip = get_ipython()
675 ip = get_ipython()
651 ip.user_ns['lmagic_out'] = line
676 ip.user_ns['lmagic_out'] = line
652
677
653 # line mode test
678 # line mode test
654 _ip.run_line_magic('prun', '-q %lmagic my line')
679 _ip.run_line_magic('prun', '-q %lmagic my line')
655 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
680 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
656 # cell mode test
681 # cell mode test
657 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
682 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
658 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
683 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
659
684
660 @dec.skipif(execution.profile is None)
685 @dec.skipif(execution.profile is None)
661 def test_prun_quotes():
686 def test_prun_quotes():
662 "Test that prun does not clobber string escapes (GH #1302)"
687 "Test that prun does not clobber string escapes (GH #1302)"
663 _ip.magic(r"prun -q x = '\t'")
688 _ip.magic(r"prun -q x = '\t'")
664 nt.assert_equal(_ip.user_ns['x'], '\t')
689 nt.assert_equal(_ip.user_ns['x'], '\t')
665
690
666 def test_extension():
691 def test_extension():
667 # Debugging information for failures of this test
692 # Debugging information for failures of this test
668 print('sys.path:')
693 print('sys.path:')
669 for p in sys.path:
694 for p in sys.path:
670 print(' ', p)
695 print(' ', p)
671 print('CWD', os.getcwd())
696 print('CWD', os.getcwd())
672
697
673 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
698 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
674 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
699 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
675 sys.path.insert(0, daft_path)
700 sys.path.insert(0, daft_path)
676 try:
701 try:
677 _ip.user_ns.pop('arq', None)
702 _ip.user_ns.pop('arq', None)
678 invalidate_caches() # Clear import caches
703 invalidate_caches() # Clear import caches
679 _ip.magic("load_ext daft_extension")
704 _ip.magic("load_ext daft_extension")
680 nt.assert_equal(_ip.user_ns['arq'], 185)
705 nt.assert_equal(_ip.user_ns['arq'], 185)
681 _ip.magic("unload_ext daft_extension")
706 _ip.magic("unload_ext daft_extension")
682 assert 'arq' not in _ip.user_ns
707 assert 'arq' not in _ip.user_ns
683 finally:
708 finally:
684 sys.path.remove(daft_path)
709 sys.path.remove(daft_path)
685
710
686
711
687 def test_notebook_export_json():
712 def test_notebook_export_json():
688 _ip = get_ipython()
713 _ip = get_ipython()
689 _ip.history_manager.reset() # Clear any existing history.
714 _ip.history_manager.reset() # Clear any existing history.
690 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
715 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
691 for i, cmd in enumerate(cmds, start=1):
716 for i, cmd in enumerate(cmds, start=1):
692 _ip.history_manager.store_inputs(i, cmd)
717 _ip.history_manager.store_inputs(i, cmd)
693 with TemporaryDirectory() as td:
718 with TemporaryDirectory() as td:
694 outfile = os.path.join(td, "nb.ipynb")
719 outfile = os.path.join(td, "nb.ipynb")
695 _ip.magic("notebook -e %s" % outfile)
720 _ip.magic("notebook -e %s" % outfile)
696
721
697
722
698 class TestEnv(TestCase):
723 class TestEnv(TestCase):
699
724
700 def test_env(self):
725 def test_env(self):
701 env = _ip.magic("env")
726 env = _ip.magic("env")
702 self.assertTrue(isinstance(env, dict))
727 self.assertTrue(isinstance(env, dict))
703
728
704 def test_env_get_set_simple(self):
729 def test_env_get_set_simple(self):
705 env = _ip.magic("env var val1")
730 env = _ip.magic("env var val1")
706 self.assertEqual(env, None)
731 self.assertEqual(env, None)
707 self.assertEqual(os.environ['var'], 'val1')
732 self.assertEqual(os.environ['var'], 'val1')
708 self.assertEqual(_ip.magic("env var"), 'val1')
733 self.assertEqual(_ip.magic("env var"), 'val1')
709 env = _ip.magic("env var=val2")
734 env = _ip.magic("env var=val2")
710 self.assertEqual(env, None)
735 self.assertEqual(env, None)
711 self.assertEqual(os.environ['var'], 'val2')
736 self.assertEqual(os.environ['var'], 'val2')
712
737
713 def test_env_get_set_complex(self):
738 def test_env_get_set_complex(self):
714 env = _ip.magic("env var 'val1 '' 'val2")
739 env = _ip.magic("env var 'val1 '' 'val2")
715 self.assertEqual(env, None)
740 self.assertEqual(env, None)
716 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
741 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
717 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
742 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
718 env = _ip.magic('env var=val2 val3="val4')
743 env = _ip.magic('env var=val2 val3="val4')
719 self.assertEqual(env, None)
744 self.assertEqual(env, None)
720 self.assertEqual(os.environ['var'], 'val2 val3="val4')
745 self.assertEqual(os.environ['var'], 'val2 val3="val4')
721
746
722 def test_env_set_bad_input(self):
747 def test_env_set_bad_input(self):
723 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
748 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
724
749
725 def test_env_set_whitespace(self):
750 def test_env_set_whitespace(self):
726 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
751 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
727
752
728
753
729 class CellMagicTestCase(TestCase):
754 class CellMagicTestCase(TestCase):
730
755
731 def check_ident(self, magic):
756 def check_ident(self, magic):
732 # Manually called, we get the result
757 # Manually called, we get the result
733 out = _ip.run_cell_magic(magic, 'a', 'b')
758 out = _ip.run_cell_magic(magic, 'a', 'b')
734 nt.assert_equal(out, ('a','b'))
759 nt.assert_equal(out, ('a','b'))
735 # Via run_cell, it goes into the user's namespace via displayhook
760 # Via run_cell, it goes into the user's namespace via displayhook
736 _ip.run_cell('%%' + magic +' c\nd\n')
761 _ip.run_cell('%%' + magic +' c\nd\n')
737 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
762 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
738
763
739 def test_cell_magic_func_deco(self):
764 def test_cell_magic_func_deco(self):
740 "Cell magic using simple decorator"
765 "Cell magic using simple decorator"
741 @register_cell_magic
766 @register_cell_magic
742 def cellm(line, cell):
767 def cellm(line, cell):
743 return line, cell
768 return line, cell
744
769
745 self.check_ident('cellm')
770 self.check_ident('cellm')
746
771
747 def test_cell_magic_reg(self):
772 def test_cell_magic_reg(self):
748 "Cell magic manually registered"
773 "Cell magic manually registered"
749 def cellm(line, cell):
774 def cellm(line, cell):
750 return line, cell
775 return line, cell
751
776
752 _ip.register_magic_function(cellm, 'cell', 'cellm2')
777 _ip.register_magic_function(cellm, 'cell', 'cellm2')
753 self.check_ident('cellm2')
778 self.check_ident('cellm2')
754
779
755 def test_cell_magic_class(self):
780 def test_cell_magic_class(self):
756 "Cell magics declared via a class"
781 "Cell magics declared via a class"
757 @magics_class
782 @magics_class
758 class MyMagics(Magics):
783 class MyMagics(Magics):
759
784
760 @cell_magic
785 @cell_magic
761 def cellm3(self, line, cell):
786 def cellm3(self, line, cell):
762 return line, cell
787 return line, cell
763
788
764 _ip.register_magics(MyMagics)
789 _ip.register_magics(MyMagics)
765 self.check_ident('cellm3')
790 self.check_ident('cellm3')
766
791
767 def test_cell_magic_class2(self):
792 def test_cell_magic_class2(self):
768 "Cell magics declared via a class, #2"
793 "Cell magics declared via a class, #2"
769 @magics_class
794 @magics_class
770 class MyMagics2(Magics):
795 class MyMagics2(Magics):
771
796
772 @cell_magic('cellm4')
797 @cell_magic('cellm4')
773 def cellm33(self, line, cell):
798 def cellm33(self, line, cell):
774 return line, cell
799 return line, cell
775
800
776 _ip.register_magics(MyMagics2)
801 _ip.register_magics(MyMagics2)
777 self.check_ident('cellm4')
802 self.check_ident('cellm4')
778 # Check that nothing is registered as 'cellm33'
803 # Check that nothing is registered as 'cellm33'
779 c33 = _ip.find_cell_magic('cellm33')
804 c33 = _ip.find_cell_magic('cellm33')
780 nt.assert_equal(c33, None)
805 nt.assert_equal(c33, None)
781
806
782 def test_file():
807 def test_file():
783 """Basic %%writefile"""
808 """Basic %%writefile"""
784 ip = get_ipython()
809 ip = get_ipython()
785 with TemporaryDirectory() as td:
810 with TemporaryDirectory() as td:
786 fname = os.path.join(td, 'file1')
811 fname = os.path.join(td, 'file1')
787 ip.run_cell_magic("writefile", fname, u'\n'.join([
812 ip.run_cell_magic("writefile", fname, u'\n'.join([
788 'line1',
813 'line1',
789 'line2',
814 'line2',
790 ]))
815 ]))
791 with open(fname) as f:
816 with open(fname) as f:
792 s = f.read()
817 s = f.read()
793 nt.assert_in('line1\n', s)
818 nt.assert_in('line1\n', s)
794 nt.assert_in('line2', s)
819 nt.assert_in('line2', s)
795
820
796 @dec.skip_win32
821 @dec.skip_win32
797 def test_file_single_quote():
822 def test_file_single_quote():
798 """Basic %%writefile with embedded single quotes"""
823 """Basic %%writefile with embedded single quotes"""
799 ip = get_ipython()
824 ip = get_ipython()
800 with TemporaryDirectory() as td:
825 with TemporaryDirectory() as td:
801 fname = os.path.join(td, '\'file1\'')
826 fname = os.path.join(td, '\'file1\'')
802 ip.run_cell_magic("writefile", fname, u'\n'.join([
827 ip.run_cell_magic("writefile", fname, u'\n'.join([
803 'line1',
828 'line1',
804 'line2',
829 'line2',
805 ]))
830 ]))
806 with open(fname) as f:
831 with open(fname) as f:
807 s = f.read()
832 s = f.read()
808 nt.assert_in('line1\n', s)
833 nt.assert_in('line1\n', s)
809 nt.assert_in('line2', s)
834 nt.assert_in('line2', s)
810
835
811 @dec.skip_win32
836 @dec.skip_win32
812 def test_file_double_quote():
837 def test_file_double_quote():
813 """Basic %%writefile with embedded double quotes"""
838 """Basic %%writefile with embedded double quotes"""
814 ip = get_ipython()
839 ip = get_ipython()
815 with TemporaryDirectory() as td:
840 with TemporaryDirectory() as td:
816 fname = os.path.join(td, '"file1"')
841 fname = os.path.join(td, '"file1"')
817 ip.run_cell_magic("writefile", fname, u'\n'.join([
842 ip.run_cell_magic("writefile", fname, u'\n'.join([
818 'line1',
843 'line1',
819 'line2',
844 'line2',
820 ]))
845 ]))
821 with open(fname) as f:
846 with open(fname) as f:
822 s = f.read()
847 s = f.read()
823 nt.assert_in('line1\n', s)
848 nt.assert_in('line1\n', s)
824 nt.assert_in('line2', s)
849 nt.assert_in('line2', s)
825
850
826 def test_file_var_expand():
851 def test_file_var_expand():
827 """%%writefile $filename"""
852 """%%writefile $filename"""
828 ip = get_ipython()
853 ip = get_ipython()
829 with TemporaryDirectory() as td:
854 with TemporaryDirectory() as td:
830 fname = os.path.join(td, 'file1')
855 fname = os.path.join(td, 'file1')
831 ip.user_ns['filename'] = fname
856 ip.user_ns['filename'] = fname
832 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
857 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
833 'line1',
858 'line1',
834 'line2',
859 'line2',
835 ]))
860 ]))
836 with open(fname) as f:
861 with open(fname) as f:
837 s = f.read()
862 s = f.read()
838 nt.assert_in('line1\n', s)
863 nt.assert_in('line1\n', s)
839 nt.assert_in('line2', s)
864 nt.assert_in('line2', s)
840
865
841 def test_file_unicode():
866 def test_file_unicode():
842 """%%writefile with unicode cell"""
867 """%%writefile with unicode cell"""
843 ip = get_ipython()
868 ip = get_ipython()
844 with TemporaryDirectory() as td:
869 with TemporaryDirectory() as td:
845 fname = os.path.join(td, 'file1')
870 fname = os.path.join(td, 'file1')
846 ip.run_cell_magic("writefile", fname, u'\n'.join([
871 ip.run_cell_magic("writefile", fname, u'\n'.join([
847 u'linΓ©1',
872 u'linΓ©1',
848 u'linΓ©2',
873 u'linΓ©2',
849 ]))
874 ]))
850 with io.open(fname, encoding='utf-8') as f:
875 with io.open(fname, encoding='utf-8') as f:
851 s = f.read()
876 s = f.read()
852 nt.assert_in(u'linΓ©1\n', s)
877 nt.assert_in(u'linΓ©1\n', s)
853 nt.assert_in(u'linΓ©2', s)
878 nt.assert_in(u'linΓ©2', s)
854
879
855 def test_file_amend():
880 def test_file_amend():
856 """%%writefile -a amends files"""
881 """%%writefile -a amends files"""
857 ip = get_ipython()
882 ip = get_ipython()
858 with TemporaryDirectory() as td:
883 with TemporaryDirectory() as td:
859 fname = os.path.join(td, 'file2')
884 fname = os.path.join(td, 'file2')
860 ip.run_cell_magic("writefile", fname, u'\n'.join([
885 ip.run_cell_magic("writefile", fname, u'\n'.join([
861 'line1',
886 'line1',
862 'line2',
887 'line2',
863 ]))
888 ]))
864 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
889 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
865 'line3',
890 'line3',
866 'line4',
891 'line4',
867 ]))
892 ]))
868 with open(fname) as f:
893 with open(fname) as f:
869 s = f.read()
894 s = f.read()
870 nt.assert_in('line1\n', s)
895 nt.assert_in('line1\n', s)
871 nt.assert_in('line3\n', s)
896 nt.assert_in('line3\n', s)
872
897
873 def test_file_spaces():
898 def test_file_spaces():
874 """%%file with spaces in filename"""
899 """%%file with spaces in filename"""
875 ip = get_ipython()
900 ip = get_ipython()
876 with TemporaryWorkingDirectory() as td:
901 with TemporaryWorkingDirectory() as td:
877 fname = "file name"
902 fname = "file name"
878 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
903 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
879 'line1',
904 'line1',
880 'line2',
905 'line2',
881 ]))
906 ]))
882 with open(fname) as f:
907 with open(fname) as f:
883 s = f.read()
908 s = f.read()
884 nt.assert_in('line1\n', s)
909 nt.assert_in('line1\n', s)
885 nt.assert_in('line2', s)
910 nt.assert_in('line2', s)
886
911
887 def test_script_config():
912 def test_script_config():
888 ip = get_ipython()
913 ip = get_ipython()
889 ip.config.ScriptMagics.script_magics = ['whoda']
914 ip.config.ScriptMagics.script_magics = ['whoda']
890 sm = script.ScriptMagics(shell=ip)
915 sm = script.ScriptMagics(shell=ip)
891 nt.assert_in('whoda', sm.magics['cell'])
916 nt.assert_in('whoda', sm.magics['cell'])
892
917
893 @dec.skip_win32
918 @dec.skip_win32
894 def test_script_out():
919 def test_script_out():
895 ip = get_ipython()
920 ip = get_ipython()
896 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
921 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
897 nt.assert_equal(ip.user_ns['output'], 'hi\n')
922 nt.assert_equal(ip.user_ns['output'], 'hi\n')
898
923
899 @dec.skip_win32
924 @dec.skip_win32
900 def test_script_err():
925 def test_script_err():
901 ip = get_ipython()
926 ip = get_ipython()
902 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
927 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
903 nt.assert_equal(ip.user_ns['error'], 'hello\n')
928 nt.assert_equal(ip.user_ns['error'], 'hello\n')
904
929
905 @dec.skip_win32
930 @dec.skip_win32
906 def test_script_out_err():
931 def test_script_out_err():
907 ip = get_ipython()
932 ip = get_ipython()
908 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
933 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
909 nt.assert_equal(ip.user_ns['output'], 'hi\n')
934 nt.assert_equal(ip.user_ns['output'], 'hi\n')
910 nt.assert_equal(ip.user_ns['error'], 'hello\n')
935 nt.assert_equal(ip.user_ns['error'], 'hello\n')
911
936
912 @dec.skip_win32
937 @dec.skip_win32
913 def test_script_bg_out():
938 def test_script_bg_out():
914 ip = get_ipython()
939 ip = get_ipython()
915 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
940 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
916
941
917 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
942 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
918 ip.user_ns['output'].close()
943 ip.user_ns['output'].close()
919
944
920 @dec.skip_win32
945 @dec.skip_win32
921 def test_script_bg_err():
946 def test_script_bg_err():
922 ip = get_ipython()
947 ip = get_ipython()
923 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
948 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
924 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
949 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
925 ip.user_ns['error'].close()
950 ip.user_ns['error'].close()
926
951
927 @dec.skip_win32
952 @dec.skip_win32
928 def test_script_bg_out_err():
953 def test_script_bg_out_err():
929 ip = get_ipython()
954 ip = get_ipython()
930 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
955 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
931 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
956 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
932 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
957 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
933 ip.user_ns['output'].close()
958 ip.user_ns['output'].close()
934 ip.user_ns['error'].close()
959 ip.user_ns['error'].close()
935
960
936 def test_script_defaults():
961 def test_script_defaults():
937 ip = get_ipython()
962 ip = get_ipython()
938 for cmd in ['sh', 'bash', 'perl', 'ruby']:
963 for cmd in ['sh', 'bash', 'perl', 'ruby']:
939 try:
964 try:
940 find_cmd(cmd)
965 find_cmd(cmd)
941 except Exception:
966 except Exception:
942 pass
967 pass
943 else:
968 else:
944 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
969 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
945
970
946
971
947 @magics_class
972 @magics_class
948 class FooFoo(Magics):
973 class FooFoo(Magics):
949 """class with both %foo and %%foo magics"""
974 """class with both %foo and %%foo magics"""
950 @line_magic('foo')
975 @line_magic('foo')
951 def line_foo(self, line):
976 def line_foo(self, line):
952 "I am line foo"
977 "I am line foo"
953 pass
978 pass
954
979
955 @cell_magic("foo")
980 @cell_magic("foo")
956 def cell_foo(self, line, cell):
981 def cell_foo(self, line, cell):
957 "I am cell foo, not line foo"
982 "I am cell foo, not line foo"
958 pass
983 pass
959
984
960 def test_line_cell_info():
985 def test_line_cell_info():
961 """%%foo and %foo magics are distinguishable to inspect"""
986 """%%foo and %foo magics are distinguishable to inspect"""
962 ip = get_ipython()
987 ip = get_ipython()
963 ip.magics_manager.register(FooFoo)
988 ip.magics_manager.register(FooFoo)
964 oinfo = ip.object_inspect('foo')
989 oinfo = ip.object_inspect('foo')
965 nt.assert_true(oinfo['found'])
990 nt.assert_true(oinfo['found'])
966 nt.assert_true(oinfo['ismagic'])
991 nt.assert_true(oinfo['ismagic'])
967
992
968 oinfo = ip.object_inspect('%%foo')
993 oinfo = ip.object_inspect('%%foo')
969 nt.assert_true(oinfo['found'])
994 nt.assert_true(oinfo['found'])
970 nt.assert_true(oinfo['ismagic'])
995 nt.assert_true(oinfo['ismagic'])
971 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
996 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
972
997
973 oinfo = ip.object_inspect('%foo')
998 oinfo = ip.object_inspect('%foo')
974 nt.assert_true(oinfo['found'])
999 nt.assert_true(oinfo['found'])
975 nt.assert_true(oinfo['ismagic'])
1000 nt.assert_true(oinfo['ismagic'])
976 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
1001 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
977
1002
978 def test_multiple_magics():
1003 def test_multiple_magics():
979 ip = get_ipython()
1004 ip = get_ipython()
980 foo1 = FooFoo(ip)
1005 foo1 = FooFoo(ip)
981 foo2 = FooFoo(ip)
1006 foo2 = FooFoo(ip)
982 mm = ip.magics_manager
1007 mm = ip.magics_manager
983 mm.register(foo1)
1008 mm.register(foo1)
984 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
1009 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
985 mm.register(foo2)
1010 mm.register(foo2)
986 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
1011 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
987
1012
988 def test_alias_magic():
1013 def test_alias_magic():
989 """Test %alias_magic."""
1014 """Test %alias_magic."""
990 ip = get_ipython()
1015 ip = get_ipython()
991 mm = ip.magics_manager
1016 mm = ip.magics_manager
992
1017
993 # Basic operation: both cell and line magics are created, if possible.
1018 # Basic operation: both cell and line magics are created, if possible.
994 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
1019 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
995 nt.assert_in('timeit_alias', mm.magics['line'])
1020 nt.assert_in('timeit_alias', mm.magics['line'])
996 nt.assert_in('timeit_alias', mm.magics['cell'])
1021 nt.assert_in('timeit_alias', mm.magics['cell'])
997
1022
998 # --cell is specified, line magic not created.
1023 # --cell is specified, line magic not created.
999 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
1024 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
1000 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
1025 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
1001 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1026 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1002
1027
1003 # Test that line alias is created successfully.
1028 # Test that line alias is created successfully.
1004 ip.run_line_magic('alias_magic', '--line env_alias env')
1029 ip.run_line_magic('alias_magic', '--line env_alias env')
1005 nt.assert_equal(ip.run_line_magic('env', ''),
1030 nt.assert_equal(ip.run_line_magic('env', ''),
1006 ip.run_line_magic('env_alias', ''))
1031 ip.run_line_magic('env_alias', ''))
1007
1032
1008 # Test that line alias with parameters passed in is created successfully.
1033 # Test that line alias with parameters passed in is created successfully.
1009 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1034 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1010 nt.assert_in('history_alias', mm.magics['line'])
1035 nt.assert_in('history_alias', mm.magics['line'])
1011
1036
1012
1037
1013 def test_save():
1038 def test_save():
1014 """Test %save."""
1039 """Test %save."""
1015 ip = get_ipython()
1040 ip = get_ipython()
1016 ip.history_manager.reset() # Clear any existing history.
1041 ip.history_manager.reset() # Clear any existing history.
1017 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1042 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1018 for i, cmd in enumerate(cmds, start=1):
1043 for i, cmd in enumerate(cmds, start=1):
1019 ip.history_manager.store_inputs(i, cmd)
1044 ip.history_manager.store_inputs(i, cmd)
1020 with TemporaryDirectory() as tmpdir:
1045 with TemporaryDirectory() as tmpdir:
1021 file = os.path.join(tmpdir, "testsave.py")
1046 file = os.path.join(tmpdir, "testsave.py")
1022 ip.run_line_magic("save", "%s 1-10" % file)
1047 ip.run_line_magic("save", "%s 1-10" % file)
1023 with open(file) as f:
1048 with open(file) as f:
1024 content = f.read()
1049 content = f.read()
1025 nt.assert_equal(content.count(cmds[0]), 1)
1050 nt.assert_equal(content.count(cmds[0]), 1)
1026 nt.assert_in('coding: utf-8', content)
1051 nt.assert_in('coding: utf-8', content)
1027 ip.run_line_magic("save", "-a %s 1-10" % file)
1052 ip.run_line_magic("save", "-a %s 1-10" % file)
1028 with open(file) as f:
1053 with open(file) as f:
1029 content = f.read()
1054 content = f.read()
1030 nt.assert_equal(content.count(cmds[0]), 2)
1055 nt.assert_equal(content.count(cmds[0]), 2)
1031 nt.assert_in('coding: utf-8', content)
1056 nt.assert_in('coding: utf-8', content)
1032
1057
1033
1058
1034 def test_store():
1059 def test_store():
1035 """Test %store."""
1060 """Test %store."""
1036 ip = get_ipython()
1061 ip = get_ipython()
1037 ip.run_line_magic('load_ext', 'storemagic')
1062 ip.run_line_magic('load_ext', 'storemagic')
1038
1063
1039 # make sure the storage is empty
1064 # make sure the storage is empty
1040 ip.run_line_magic('store', '-z')
1065 ip.run_line_magic('store', '-z')
1041 ip.user_ns['var'] = 42
1066 ip.user_ns['var'] = 42
1042 ip.run_line_magic('store', 'var')
1067 ip.run_line_magic('store', 'var')
1043 ip.user_ns['var'] = 39
1068 ip.user_ns['var'] = 39
1044 ip.run_line_magic('store', '-r')
1069 ip.run_line_magic('store', '-r')
1045 nt.assert_equal(ip.user_ns['var'], 42)
1070 nt.assert_equal(ip.user_ns['var'], 42)
1046
1071
1047 ip.run_line_magic('store', '-d var')
1072 ip.run_line_magic('store', '-d var')
1048 ip.user_ns['var'] = 39
1073 ip.user_ns['var'] = 39
1049 ip.run_line_magic('store' , '-r')
1074 ip.run_line_magic('store' , '-r')
1050 nt.assert_equal(ip.user_ns['var'], 39)
1075 nt.assert_equal(ip.user_ns['var'], 39)
1051
1076
1052
1077
1053 def _run_edit_test(arg_s, exp_filename=None,
1078 def _run_edit_test(arg_s, exp_filename=None,
1054 exp_lineno=-1,
1079 exp_lineno=-1,
1055 exp_contents=None,
1080 exp_contents=None,
1056 exp_is_temp=None):
1081 exp_is_temp=None):
1057 ip = get_ipython()
1082 ip = get_ipython()
1058 M = code.CodeMagics(ip)
1083 M = code.CodeMagics(ip)
1059 last_call = ['','']
1084 last_call = ['','']
1060 opts,args = M.parse_options(arg_s,'prxn:')
1085 opts,args = M.parse_options(arg_s,'prxn:')
1061 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1086 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1062
1087
1063 if exp_filename is not None:
1088 if exp_filename is not None:
1064 nt.assert_equal(exp_filename, filename)
1089 nt.assert_equal(exp_filename, filename)
1065 if exp_contents is not None:
1090 if exp_contents is not None:
1066 with io.open(filename, 'r', encoding='utf-8') as f:
1091 with io.open(filename, 'r', encoding='utf-8') as f:
1067 contents = f.read()
1092 contents = f.read()
1068 nt.assert_equal(exp_contents, contents)
1093 nt.assert_equal(exp_contents, contents)
1069 if exp_lineno != -1:
1094 if exp_lineno != -1:
1070 nt.assert_equal(exp_lineno, lineno)
1095 nt.assert_equal(exp_lineno, lineno)
1071 if exp_is_temp is not None:
1096 if exp_is_temp is not None:
1072 nt.assert_equal(exp_is_temp, is_temp)
1097 nt.assert_equal(exp_is_temp, is_temp)
1073
1098
1074
1099
1075 def test_edit_interactive():
1100 def test_edit_interactive():
1076 """%edit on interactively defined objects"""
1101 """%edit on interactively defined objects"""
1077 ip = get_ipython()
1102 ip = get_ipython()
1078 n = ip.execution_count
1103 n = ip.execution_count
1079 ip.run_cell(u"def foo(): return 1", store_history=True)
1104 ip.run_cell(u"def foo(): return 1", store_history=True)
1080
1105
1081 try:
1106 try:
1082 _run_edit_test("foo")
1107 _run_edit_test("foo")
1083 except code.InteractivelyDefined as e:
1108 except code.InteractivelyDefined as e:
1084 nt.assert_equal(e.index, n)
1109 nt.assert_equal(e.index, n)
1085 else:
1110 else:
1086 raise AssertionError("Should have raised InteractivelyDefined")
1111 raise AssertionError("Should have raised InteractivelyDefined")
1087
1112
1088
1113
1089 def test_edit_cell():
1114 def test_edit_cell():
1090 """%edit [cell id]"""
1115 """%edit [cell id]"""
1091 ip = get_ipython()
1116 ip = get_ipython()
1092
1117
1093 ip.run_cell(u"def foo(): return 1", store_history=True)
1118 ip.run_cell(u"def foo(): return 1", store_history=True)
1094
1119
1095 # test
1120 # test
1096 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1121 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1097
1122
1098 def test_bookmark():
1123 def test_bookmark():
1099 ip = get_ipython()
1124 ip = get_ipython()
1100 ip.run_line_magic('bookmark', 'bmname')
1125 ip.run_line_magic('bookmark', 'bmname')
1101 with tt.AssertPrints('bmname'):
1126 with tt.AssertPrints('bmname'):
1102 ip.run_line_magic('bookmark', '-l')
1127 ip.run_line_magic('bookmark', '-l')
1103 ip.run_line_magic('bookmark', '-d bmname')
1128 ip.run_line_magic('bookmark', '-d bmname')
1104
1129
1105 def test_ls_magic():
1130 def test_ls_magic():
1106 ip = get_ipython()
1131 ip = get_ipython()
1107 json_formatter = ip.display_formatter.formatters['application/json']
1132 json_formatter = ip.display_formatter.formatters['application/json']
1108 json_formatter.enabled = True
1133 json_formatter.enabled = True
1109 lsmagic = ip.magic('lsmagic')
1134 lsmagic = ip.magic('lsmagic')
1110 with warnings.catch_warnings(record=True) as w:
1135 with warnings.catch_warnings(record=True) as w:
1111 j = json_formatter(lsmagic)
1136 j = json_formatter(lsmagic)
1112 nt.assert_equal(sorted(j), ['cell', 'line'])
1137 nt.assert_equal(sorted(j), ['cell', 'line'])
1113 nt.assert_equal(w, []) # no warnings
1138 nt.assert_equal(w, []) # no warnings
1114
1139
1115 def test_strip_initial_indent():
1140 def test_strip_initial_indent():
1116 def sii(s):
1141 def sii(s):
1117 lines = s.splitlines()
1142 lines = s.splitlines()
1118 return '\n'.join(code.strip_initial_indent(lines))
1143 return '\n'.join(code.strip_initial_indent(lines))
1119
1144
1120 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1145 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1121 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1146 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1122 nt.assert_equal(sii("a\n b"), "a\n b")
1147 nt.assert_equal(sii("a\n b"), "a\n b")
1123
1148
1124 def test_logging_magic_quiet_from_arg():
1149 def test_logging_magic_quiet_from_arg():
1125 _ip.config.LoggingMagics.quiet = False
1150 _ip.config.LoggingMagics.quiet = False
1126 lm = logging.LoggingMagics(shell=_ip)
1151 lm = logging.LoggingMagics(shell=_ip)
1127 with TemporaryDirectory() as td:
1152 with TemporaryDirectory() as td:
1128 try:
1153 try:
1129 with tt.AssertNotPrints(re.compile("Activating.*")):
1154 with tt.AssertNotPrints(re.compile("Activating.*")):
1130 lm.logstart('-q {}'.format(
1155 lm.logstart('-q {}'.format(
1131 os.path.join(td, "quiet_from_arg.log")))
1156 os.path.join(td, "quiet_from_arg.log")))
1132 finally:
1157 finally:
1133 _ip.logger.logstop()
1158 _ip.logger.logstop()
1134
1159
1135 def test_logging_magic_quiet_from_config():
1160 def test_logging_magic_quiet_from_config():
1136 _ip.config.LoggingMagics.quiet = True
1161 _ip.config.LoggingMagics.quiet = True
1137 lm = logging.LoggingMagics(shell=_ip)
1162 lm = logging.LoggingMagics(shell=_ip)
1138 with TemporaryDirectory() as td:
1163 with TemporaryDirectory() as td:
1139 try:
1164 try:
1140 with tt.AssertNotPrints(re.compile("Activating.*")):
1165 with tt.AssertNotPrints(re.compile("Activating.*")):
1141 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1166 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1142 finally:
1167 finally:
1143 _ip.logger.logstop()
1168 _ip.logger.logstop()
1144
1169
1145
1170
1146 def test_logging_magic_not_quiet():
1171 def test_logging_magic_not_quiet():
1147 _ip.config.LoggingMagics.quiet = False
1172 _ip.config.LoggingMagics.quiet = False
1148 lm = logging.LoggingMagics(shell=_ip)
1173 lm = logging.LoggingMagics(shell=_ip)
1149 with TemporaryDirectory() as td:
1174 with TemporaryDirectory() as td:
1150 try:
1175 try:
1151 with tt.AssertPrints(re.compile("Activating.*")):
1176 with tt.AssertPrints(re.compile("Activating.*")):
1152 lm.logstart(os.path.join(td, "not_quiet.log"))
1177 lm.logstart(os.path.join(td, "not_quiet.log"))
1153 finally:
1178 finally:
1154 _ip.logger.logstop()
1179 _ip.logger.logstop()
1155
1180
1156
1181
1157 def test_time_no_var_expand():
1182 def test_time_no_var_expand():
1158 _ip.user_ns['a'] = 5
1183 _ip.user_ns['a'] = 5
1159 _ip.user_ns['b'] = []
1184 _ip.user_ns['b'] = []
1160 _ip.magic('time b.append("{a}")')
1185 _ip.magic('time b.append("{a}")')
1161 assert _ip.user_ns['b'] == ['{a}']
1186 assert _ip.user_ns['b'] == ['{a}']
1162
1187
1163
1188
1164 # this is slow, put at the end for local testing.
1189 # this is slow, put at the end for local testing.
1165 def test_timeit_arguments():
1190 def test_timeit_arguments():
1166 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1191 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1167 if sys.version_info < (3,7):
1192 if sys.version_info < (3,7):
1168 _ip.magic("timeit -n1 -r1 ('#')")
1193 _ip.magic("timeit -n1 -r1 ('#')")
1169 else:
1194 else:
1170 # 3.7 optimize no-op statement like above out, and complain there is
1195 # 3.7 optimize no-op statement like above out, and complain there is
1171 # nothing in the for loop.
1196 # nothing in the for loop.
1172 _ip.magic("timeit -n1 -r1 a=('#')")
1197 _ip.magic("timeit -n1 -r1 a=('#')")
General Comments 0
You need to be logged in to leave comments. Login now