Show More
@@ -1,250 +1,249 b'' | |||||
1 | """Tests for various magic functions. |
|
1 | """Tests for various magic functions. | |
2 |
|
2 | |||
3 | Needs to be run by nose (to make ipython session available). |
|
3 | Needs to be run by nose (to make ipython session available). | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | import os |
|
6 | import os | |
7 | import sys |
|
7 | import sys | |
8 | import tempfile |
|
8 | import tempfile | |
9 | import types |
|
9 | import types | |
10 |
|
10 | |||
11 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
12 |
|
12 | |||
13 | from IPython.platutils import find_cmd, get_long_path_name |
|
13 | from IPython.platutils import find_cmd, get_long_path_name | |
14 | from IPython.testing import decorators as dec |
|
14 | from IPython.testing import decorators as dec | |
15 | from IPython.testing import tools as tt |
|
15 | from IPython.testing import tools as tt | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Test functions begin |
|
18 | # Test functions begin | |
19 |
|
19 | |||
20 | def test_rehashx(): |
|
20 | def test_rehashx(): | |
21 | # clear up everything |
|
21 | # clear up everything | |
22 | _ip.IP.alias_table.clear() |
|
22 | _ip.IP.alias_table.clear() | |
23 | del _ip.db['syscmdlist'] |
|
23 | del _ip.db['syscmdlist'] | |
24 |
|
24 | |||
25 | _ip.magic('rehashx') |
|
25 | _ip.magic('rehashx') | |
26 | # Practically ALL ipython development systems will have more than 10 aliases |
|
26 | # Practically ALL ipython development systems will have more than 10 aliases | |
27 |
|
27 | |||
28 | assert len(_ip.IP.alias_table) > 10 |
|
28 | assert len(_ip.IP.alias_table) > 10 | |
29 | for key, val in _ip.IP.alias_table.items(): |
|
29 | for key, val in _ip.IP.alias_table.items(): | |
30 | # we must strip dots from alias names |
|
30 | # we must strip dots from alias names | |
31 | assert '.' not in key |
|
31 | assert '.' not in key | |
32 |
|
32 | |||
33 | # rehashx must fill up syscmdlist |
|
33 | # rehashx must fill up syscmdlist | |
34 | scoms = _ip.db['syscmdlist'] |
|
34 | scoms = _ip.db['syscmdlist'] | |
35 | assert len(scoms) > 10 |
|
35 | assert len(scoms) > 10 | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def doctest_hist_f(): |
|
38 | def doctest_hist_f(): | |
39 | """Test %hist -f with temporary filename. |
|
39 | """Test %hist -f with temporary filename. | |
40 |
|
40 | |||
41 | In [9]: import tempfile |
|
41 | In [9]: import tempfile | |
42 |
|
42 | |||
43 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
43 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') | |
44 |
|
44 | |||
45 | In [11]: %history -n -f $tfile 3 |
|
45 | In [11]: %history -n -f $tfile 3 | |
46 | """ |
|
46 | """ | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def doctest_hist_r(): |
|
49 | def doctest_hist_r(): | |
50 | """Test %hist -r |
|
50 | """Test %hist -r | |
51 |
|
51 | |||
52 | XXX - This test is not recording the output correctly. Not sure why... |
|
52 | XXX - This test is not recording the output correctly. Not sure why... | |
53 |
|
53 | |||
54 | In [6]: x=1 |
|
54 | In [6]: x=1 | |
55 |
|
55 | |||
56 | In [7]: hist -n -r 2 |
|
56 | In [7]: hist -n -r 2 | |
57 | x=1 # random |
|
57 | x=1 # random | |
58 | hist -n -r 2 # random |
|
58 | hist -n -r 2 # random | |
59 | """ |
|
59 | """ | |
60 |
|
60 | |||
61 | # This is skipped for now because getoutput doesn't find the ipython |
|
61 | # This test is known to fail on win32. | |
62 |
# |
|
62 | # See ticket https://bugs.launchpad.net/bugs/366334 | |
63 | @dec.skip_win32 |
|
|||
64 | def test_obj_del(): |
|
63 | def test_obj_del(): | |
65 | """Test that object's __del__ methods are called on exit.""" |
|
64 | """Test that object's __del__ methods are called on exit.""" | |
66 | test_dir = os.path.dirname(__file__) |
|
65 | test_dir = os.path.dirname(__file__) | |
67 | del_file = os.path.join(test_dir,'obj_del.py') |
|
66 | del_file = os.path.join(test_dir,'obj_del.py') | |
68 | ipython_cmd = find_cmd('ipython') |
|
67 | ipython_cmd = find_cmd('ipython') | |
69 | out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file)) |
|
68 | out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file)) | |
70 | nt.assert_equals(out,'obj_del.py: object A deleted') |
|
69 | nt.assert_equals(out,'obj_del.py: object A deleted') | |
71 |
|
70 | |||
72 |
|
71 | |||
73 | def test_shist(): |
|
72 | def test_shist(): | |
74 | # Simple tests of ShadowHist class - test generator. |
|
73 | # Simple tests of ShadowHist class - test generator. | |
75 | import os, shutil, tempfile |
|
74 | import os, shutil, tempfile | |
76 |
|
75 | |||
77 | from IPython.Extensions import pickleshare |
|
76 | from IPython.Extensions import pickleshare | |
78 | from IPython.history import ShadowHist |
|
77 | from IPython.history import ShadowHist | |
79 |
|
78 | |||
80 | tfile = tempfile.mktemp('','tmp-ipython-') |
|
79 | tfile = tempfile.mktemp('','tmp-ipython-') | |
81 |
|
80 | |||
82 | db = pickleshare.PickleShareDB(tfile) |
|
81 | db = pickleshare.PickleShareDB(tfile) | |
83 | s = ShadowHist(db) |
|
82 | s = ShadowHist(db) | |
84 | s.add('hello') |
|
83 | s.add('hello') | |
85 | s.add('world') |
|
84 | s.add('world') | |
86 | s.add('hello') |
|
85 | s.add('hello') | |
87 | s.add('hello') |
|
86 | s.add('hello') | |
88 | s.add('karhu') |
|
87 | s.add('karhu') | |
89 |
|
88 | |||
90 | yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')] |
|
89 | yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')] | |
91 |
|
90 | |||
92 | yield nt.assert_equal,s.get(2),'world' |
|
91 | yield nt.assert_equal,s.get(2),'world' | |
93 |
|
92 | |||
94 | shutil.rmtree(tfile) |
|
93 | shutil.rmtree(tfile) | |
95 |
|
94 | |||
96 | @dec.skipif_not_numpy |
|
95 | @dec.skipif_not_numpy | |
97 | def test_numpy_clear_array_undec(): |
|
96 | def test_numpy_clear_array_undec(): | |
98 | _ip.ex('import numpy as np') |
|
97 | _ip.ex('import numpy as np') | |
99 | _ip.ex('a = np.empty(2)') |
|
98 | _ip.ex('a = np.empty(2)') | |
100 |
|
99 | |||
101 | yield nt.assert_true,'a' in _ip.user_ns |
|
100 | yield nt.assert_true,'a' in _ip.user_ns | |
102 | _ip.magic('clear array') |
|
101 | _ip.magic('clear array') | |
103 | yield nt.assert_false,'a' in _ip.user_ns |
|
102 | yield nt.assert_false,'a' in _ip.user_ns | |
104 |
|
103 | |||
105 |
|
104 | |||
106 | @dec.skip() |
|
105 | @dec.skip() | |
107 | def test_fail_dec(*a,**k): |
|
106 | def test_fail_dec(*a,**k): | |
108 | yield nt.assert_true, False |
|
107 | yield nt.assert_true, False | |
109 |
|
108 | |||
110 | @dec.skip('This one shouldn not run') |
|
109 | @dec.skip('This one shouldn not run') | |
111 | def test_fail_dec2(*a,**k): |
|
110 | def test_fail_dec2(*a,**k): | |
112 | yield nt.assert_true, False |
|
111 | yield nt.assert_true, False | |
113 |
|
112 | |||
114 | @dec.skipknownfailure |
|
113 | @dec.skipknownfailure | |
115 | def test_fail_dec3(*a,**k): |
|
114 | def test_fail_dec3(*a,**k): | |
116 | yield nt.assert_true, False |
|
115 | yield nt.assert_true, False | |
117 |
|
116 | |||
118 |
|
117 | |||
119 | def doctest_refbug(): |
|
118 | def doctest_refbug(): | |
120 | """Very nasty problem with references held by multiple runs of a script. |
|
119 | """Very nasty problem with references held by multiple runs of a script. | |
121 | See: https://bugs.launchpad.net/ipython/+bug/269966 |
|
120 | See: https://bugs.launchpad.net/ipython/+bug/269966 | |
122 |
|
121 | |||
123 | In [1]: _ip.IP.clear_main_mod_cache() |
|
122 | In [1]: _ip.IP.clear_main_mod_cache() | |
124 |
|
123 | |||
125 | In [2]: run refbug |
|
124 | In [2]: run refbug | |
126 |
|
125 | |||
127 | In [3]: call_f() |
|
126 | In [3]: call_f() | |
128 | lowercased: hello |
|
127 | lowercased: hello | |
129 |
|
128 | |||
130 | In [4]: run refbug |
|
129 | In [4]: run refbug | |
131 |
|
130 | |||
132 | In [5]: call_f() |
|
131 | In [5]: call_f() | |
133 | lowercased: hello |
|
132 | lowercased: hello | |
134 | lowercased: hello |
|
133 | lowercased: hello | |
135 | """ |
|
134 | """ | |
136 |
|
135 | |||
137 | #----------------------------------------------------------------------------- |
|
136 | #----------------------------------------------------------------------------- | |
138 | # Tests for %run |
|
137 | # Tests for %run | |
139 | #----------------------------------------------------------------------------- |
|
138 | #----------------------------------------------------------------------------- | |
140 |
|
139 | |||
141 | # %run is critical enough that it's a good idea to have a solid collection of |
|
140 | # %run is critical enough that it's a good idea to have a solid collection of | |
142 | # tests for it, some as doctests and some as normal tests. |
|
141 | # tests for it, some as doctests and some as normal tests. | |
143 |
|
142 | |||
144 | def doctest_run_ns(): |
|
143 | def doctest_run_ns(): | |
145 | """Classes declared %run scripts must be instantiable afterwards. |
|
144 | """Classes declared %run scripts must be instantiable afterwards. | |
146 |
|
145 | |||
147 | In [11]: run tclass foo |
|
146 | In [11]: run tclass foo | |
148 |
|
147 | |||
149 | In [12]: isinstance(f(),foo) |
|
148 | In [12]: isinstance(f(),foo) | |
150 | Out[12]: True |
|
149 | Out[12]: True | |
151 | """ |
|
150 | """ | |
152 |
|
151 | |||
153 |
|
152 | |||
154 | def doctest_run_ns2(): |
|
153 | def doctest_run_ns2(): | |
155 | """Classes declared %run scripts must be instantiable afterwards. |
|
154 | """Classes declared %run scripts must be instantiable afterwards. | |
156 |
|
155 | |||
157 | In [4]: run tclass C-first_pass |
|
156 | In [4]: run tclass C-first_pass | |
158 |
|
157 | |||
159 | In [5]: run tclass C-second_pass |
|
158 | In [5]: run tclass C-second_pass | |
160 | tclass.py: deleting object: C-first_pass |
|
159 | tclass.py: deleting object: C-first_pass | |
161 | """ |
|
160 | """ | |
162 |
|
161 | |||
163 | @dec.skip_win32 |
|
162 | @dec.skip_win32 | |
164 | def doctest_run_builtins(): |
|
163 | def doctest_run_builtins(): | |
165 | """Check that %run doesn't damage __builtins__ via a doctest. |
|
164 | """Check that %run doesn't damage __builtins__ via a doctest. | |
166 |
|
165 | |||
167 | This is similar to the test_run_builtins, but I want *both* forms of the |
|
166 | This is similar to the test_run_builtins, but I want *both* forms of the | |
168 | test to catch any possible glitches in our testing machinery, since that |
|
167 | test to catch any possible glitches in our testing machinery, since that | |
169 | modifies %run somewhat. So for this, we have both a normal test (below) |
|
168 | modifies %run somewhat. So for this, we have both a normal test (below) | |
170 | and a doctest (this one). |
|
169 | and a doctest (this one). | |
171 |
|
170 | |||
172 | In [1]: import tempfile |
|
171 | In [1]: import tempfile | |
173 |
|
172 | |||
174 | In [2]: bid1 = id(__builtins__) |
|
173 | In [2]: bid1 = id(__builtins__) | |
175 |
|
174 | |||
176 | In [3]: f = tempfile.NamedTemporaryFile() |
|
175 | In [3]: f = tempfile.NamedTemporaryFile() | |
177 |
|
176 | |||
178 | In [4]: f.write('pass\\n') |
|
177 | In [4]: f.write('pass\\n') | |
179 |
|
178 | |||
180 | In [5]: f.flush() |
|
179 | In [5]: f.flush() | |
181 |
|
180 | |||
182 | In [6]: print 'B1:',type(__builtins__) |
|
181 | In [6]: print 'B1:',type(__builtins__) | |
183 | B1: <type 'module'> |
|
182 | B1: <type 'module'> | |
184 |
|
183 | |||
185 | In [7]: %run $f.name |
|
184 | In [7]: %run $f.name | |
186 |
|
185 | |||
187 | In [8]: bid2 = id(__builtins__) |
|
186 | In [8]: bid2 = id(__builtins__) | |
188 |
|
187 | |||
189 | In [9]: print 'B2:',type(__builtins__) |
|
188 | In [9]: print 'B2:',type(__builtins__) | |
190 | B2: <type 'module'> |
|
189 | B2: <type 'module'> | |
191 |
|
190 | |||
192 | In [10]: bid1 == bid2 |
|
191 | In [10]: bid1 == bid2 | |
193 | Out[10]: True |
|
192 | Out[10]: True | |
194 | """ |
|
193 | """ | |
195 |
|
194 | |||
196 | # For some tests, it will be handy to organize them in a class with a common |
|
195 | # For some tests, it will be handy to organize them in a class with a common | |
197 | # setup that makes a temp file |
|
196 | # setup that makes a temp file | |
198 |
|
197 | |||
199 | class TestMagicRun(object): |
|
198 | class TestMagicRun(object): | |
200 |
|
199 | |||
201 | def setup(self): |
|
200 | def setup(self): | |
202 | """Make a valid python temp file.""" |
|
201 | """Make a valid python temp file.""" | |
203 | f = tempfile.NamedTemporaryFile() |
|
202 | f = tempfile.NamedTemporaryFile() | |
204 | f.write('pass\n') |
|
203 | f.write('pass\n') | |
205 | f.flush() |
|
204 | f.flush() | |
206 | self.tmpfile = f |
|
205 | self.tmpfile = f | |
207 |
|
206 | |||
208 | def run_tmpfile(self): |
|
207 | def run_tmpfile(self): | |
209 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
208 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. | |
210 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
209 | # See below and ticket https://bugs.launchpad.net/bugs/366353 | |
211 | _ip.magic('run %s' % self.tmpfile.name) |
|
210 | _ip.magic('run %s' % self.tmpfile.name) | |
212 |
|
211 | |||
213 | # See https://bugs.launchpad.net/bugs/366353 |
|
212 | # See https://bugs.launchpad.net/bugs/366353 | |
214 | @dec.skip_if_not_win32 |
|
213 | @dec.skip_if_not_win32 | |
215 | def test_run_tempfile_path(self): |
|
214 | def test_run_tempfile_path(self): | |
216 | tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.") |
|
215 | tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.") | |
217 |
|
216 | |||
218 | # See https://bugs.launchpad.net/bugs/366353 |
|
217 | # See https://bugs.launchpad.net/bugs/366353 | |
219 | @dec.skip_win32 |
|
218 | @dec.skip_win32 | |
220 | def test_builtins_id(self): |
|
219 | def test_builtins_id(self): | |
221 | """Check that %run doesn't damage __builtins__ """ |
|
220 | """Check that %run doesn't damage __builtins__ """ | |
222 |
|
221 | |||
223 | # Test that the id of __builtins__ is not modified by %run |
|
222 | # Test that the id of __builtins__ is not modified by %run | |
224 | bid1 = id(_ip.user_ns['__builtins__']) |
|
223 | bid1 = id(_ip.user_ns['__builtins__']) | |
225 | self.run_tmpfile() |
|
224 | self.run_tmpfile() | |
226 | bid2 = id(_ip.user_ns['__builtins__']) |
|
225 | bid2 = id(_ip.user_ns['__builtins__']) | |
227 | tt.assert_equals(bid1, bid2) |
|
226 | tt.assert_equals(bid1, bid2) | |
228 |
|
227 | |||
229 | # See https://bugs.launchpad.net/bugs/366353 |
|
228 | # See https://bugs.launchpad.net/bugs/366353 | |
230 | @dec.skip_win32 |
|
229 | @dec.skip_win32 | |
231 | def test_builtins_type(self): |
|
230 | def test_builtins_type(self): | |
232 | """Check that the type of __builtins__ doesn't change with %run. |
|
231 | """Check that the type of __builtins__ doesn't change with %run. | |
233 |
|
232 | |||
234 | However, the above could pass if __builtins__ was already modified to |
|
233 | However, the above could pass if __builtins__ was already modified to | |
235 | be a dict (it should be a module) by a previous use of %run. So we |
|
234 | be a dict (it should be a module) by a previous use of %run. So we | |
236 | also check explicitly that it really is a module: |
|
235 | also check explicitly that it really is a module: | |
237 | """ |
|
236 | """ | |
238 | self.run_tmpfile() |
|
237 | self.run_tmpfile() | |
239 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) |
|
238 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) | |
240 |
|
239 | |||
241 | # See https://bugs.launchpad.net/bugs/366353 |
|
240 | # See https://bugs.launchpad.net/bugs/366353 | |
242 | @dec.skip_win32 |
|
241 | @dec.skip_win32 | |
243 | def test_prompts(self): |
|
242 | def test_prompts(self): | |
244 | """Test that prompts correctly generate after %run""" |
|
243 | """Test that prompts correctly generate after %run""" | |
245 | self.run_tmpfile() |
|
244 | self.run_tmpfile() | |
246 | p2 = str(_ip.IP.outputcache.prompt2).strip() |
|
245 | p2 = str(_ip.IP.outputcache.prompt2).strip() | |
247 | nt.assert_equals(p2[:3], '...') |
|
246 | nt.assert_equals(p2[:3], '...') | |
248 |
|
247 | |||
249 | def teardown(self): |
|
248 | def teardown(self): | |
250 | self.tmpfile.close() |
|
249 | self.tmpfile.close() |
General Comments 0
You need to be logged in to leave comments.
Login now