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