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