##// END OF EJS Templates
added the skip_known decorator
Paul Ivanov -
Show More
@@ -1,85 +1,88 b''
1 """Tests for the key interactiveshell module.
1 """Tests for the key interactiveshell module.
2
2
3 Historically the main classes in interactiveshell have been under-tested. This
3 Historically the main classes in interactiveshell have been under-tested. This
4 module should grow as many single-method tests as possible to trap many of the
4 module should grow as many single-method tests as possible to trap many of the
5 recurring bugs we seem to encounter with high-level interaction.
5 recurring bugs we seem to encounter with high-level interaction.
6
6
7 Authors
7 Authors
8 -------
8 -------
9 * Fernando Perez
9 * Fernando Perez
10 """
10 """
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2011 The IPython Development Team
12 # Copyright (C) 2011 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # stdlib
21 # stdlib
22 import unittest
22 import unittest
23 from IPython.testing import decorators as dec
23
24
24 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
25 # Tests
26 # Tests
26 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
27
28
28 class InteractiveShellTestCase(unittest.TestCase):
29 class InteractiveShellTestCase(unittest.TestCase):
29 def test_naked_string_cells(self):
30 def test_naked_string_cells(self):
30 """Test that cells with only naked strings are fully executed"""
31 """Test that cells with only naked strings are fully executed"""
31 ip = get_ipython()
32 ip = get_ipython()
32 # First, single-line inputs
33 # First, single-line inputs
33 ip.run_cell('"a"\n')
34 ip.run_cell('"a"\n')
34 self.assertEquals(ip.user_ns['_'], 'a')
35 self.assertEquals(ip.user_ns['_'], 'a')
35 # And also multi-line cells
36 # And also multi-line cells
36 ip.run_cell('"""a\nb"""\n')
37 ip.run_cell('"""a\nb"""\n')
37 self.assertEquals(ip.user_ns['_'], 'a\nb')
38 self.assertEquals(ip.user_ns['_'], 'a\nb')
38
39
39 def test_run_empty_cell(self):
40 def test_run_empty_cell(self):
40 """Just make sure we don't get a horrible error with a blank
41 """Just make sure we don't get a horrible error with a blank
41 cell of input. Yes, I did overlook that."""
42 cell of input. Yes, I did overlook that."""
42 ip = get_ipython()
43 ip = get_ipython()
43 ip.run_cell('')
44 ip.run_cell('')
44
45
45 def test_run_cell_multiline(self):
46 def test_run_cell_multiline(self):
46 """Multi-block, multi-line cells must execute correctly.
47 """Multi-block, multi-line cells must execute correctly.
47 """
48 """
48 ip = get_ipython()
49 ip = get_ipython()
49 src = '\n'.join(["x=1",
50 src = '\n'.join(["x=1",
50 "y=2",
51 "y=2",
51 "if 1:",
52 "if 1:",
52 " x += 1",
53 " x += 1",
53 " y += 1",])
54 " y += 1",])
54 ip.run_cell(src)
55 ip.run_cell(src)
55 self.assertEquals(ip.user_ns['x'], 2)
56 self.assertEquals(ip.user_ns['x'], 2)
56 self.assertEquals(ip.user_ns['y'], 3)
57 self.assertEquals(ip.user_ns['y'], 3)
57
58
59 @dec.skip_known_failure
58 def test_multiline_string_cells(self):
60 def test_multiline_string_cells(self):
59 "Code sprinkled with multiline strings should execute (GH-306)"
61 "Code sprinkled with multiline strings should execute (GH-306)"
60 ip = get_ipython()
62 ip = get_ipython()
61 ip.run_cell('tmp=0')
63 ip.run_cell('tmp=0')
62 self.assertEquals(ip.user_ns['tmp'], 0)
64 self.assertEquals(ip.user_ns['tmp'], 0)
63 ip.run_cell('tmp=1;"""a\nb"""\n')
65 ip.run_cell('tmp=1;"""a\nb"""\n')
64 self.assertEquals(ip.user_ns['tmp'], 1)
66 self.assertEquals(ip.user_ns['tmp'], 1)
65
67
68 @dec.skip_known_failure
66 def test_dont_cache_with_semicolon(self):
69 def test_dont_cache_with_semicolon(self):
67 "Ending a line with semicolon should not cache the returned object (GH-307)"
70 "Ending a line with semicolon should not cache the returned object (GH-307)"
68 ip = get_ipython()
71 ip = get_ipython()
69 oldlen = len(ip.user_ns['Out'])
72 oldlen = len(ip.user_ns['Out'])
70 a = ip.run_cell('1;')
73 a = ip.run_cell('1;')
71 newlen = len(ip.user_ns['Out'])
74 newlen = len(ip.user_ns['Out'])
72 self.assertEquals(oldlen, newlen)
75 self.assertEquals(oldlen, newlen)
73 #also test the default caching behavior
76 #also test the default caching behavior
74 ip.run_cell('1')
77 ip.run_cell('1')
75 newlen = len(ip.user_ns['Out'])
78 newlen = len(ip.user_ns['Out'])
76 self.assertEquals(oldlen+1, newlen)
79 self.assertEquals(oldlen+1, newlen)
77
80
78 def test_In_variable(self):
81 def test_In_variable(self):
79 "Verify that In variable grows with user input (GH-284)"
82 "Verify that In variable grows with user input (GH-284)"
80 ip = get_ipython()
83 ip = get_ipython()
81 oldlen = len(ip.user_ns['In'])
84 oldlen = len(ip.user_ns['In'])
82 ip.run_cell('1;')
85 ip.run_cell('1;')
83 newlen = len(ip.user_ns['In'])
86 newlen = len(ip.user_ns['In'])
84 self.assertEquals(oldlen+1, newlen)
87 self.assertEquals(oldlen+1, newlen)
85 self.assertEquals(ip.user_ns['In'][-1],'1;')
88 self.assertEquals(ip.user_ns['In'][-1],'1;')
@@ -1,205 +1,206 b''
1 """Tests for code execution (%run and related), which is particularly tricky.
1 """Tests for code execution (%run and related), which is particularly tricky.
2
2
3 Because of how %run manages namespaces, and the fact that we are trying here to
3 Because of how %run manages namespaces, and the fact that we are trying here to
4 verify subtle object deletion and reference counting issues, the %run tests
4 verify subtle object deletion and reference counting issues, the %run tests
5 will be kept in this separate file. This makes it easier to aggregate in one
5 will be kept in this separate file. This makes it easier to aggregate in one
6 place the tricks needed to handle it; most other magics are much easier to test
6 place the tricks needed to handle it; most other magics are much easier to test
7 and we do so in a common test_magic file.
7 and we do so in a common test_magic file.
8 """
8 """
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import sys
16 import sys
17 import tempfile
17 import tempfile
18
18
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 from IPython.testing import decorators as dec
21 from IPython.testing import decorators as dec
22 from IPython.testing import tools as tt
22 from IPython.testing import tools as tt
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Test functions begin
25 # Test functions begin
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 def doctest_refbug():
28 def doctest_refbug():
29 """Very nasty problem with references held by multiple runs of a script.
29 """Very nasty problem with references held by multiple runs of a script.
30 See: https://bugs.launchpad.net/ipython/+bug/269966
30 See: https://bugs.launchpad.net/ipython/+bug/269966
31
31
32 In [1]: _ip.clear_main_mod_cache()
32 In [1]: _ip.clear_main_mod_cache()
33 # random
33 # random
34
34
35 In [2]: %run refbug
35 In [2]: %run refbug
36
36
37 In [3]: call_f()
37 In [3]: call_f()
38 lowercased: hello
38 lowercased: hello
39
39
40 In [4]: %run refbug
40 In [4]: %run refbug
41
41
42 In [5]: call_f()
42 In [5]: call_f()
43 lowercased: hello
43 lowercased: hello
44 lowercased: hello
44 lowercased: hello
45 """
45 """
46
46
47
47
48 def doctest_run_builtins():
48 def doctest_run_builtins():
49 r"""Check that %run doesn't damage __builtins__.
49 r"""Check that %run doesn't damage __builtins__.
50
50
51 In [1]: import tempfile
51 In [1]: import tempfile
52
52
53 In [2]: bid1 = id(__builtins__)
53 In [2]: bid1 = id(__builtins__)
54
54
55 In [3]: fname = tempfile.mkstemp('.py')[1]
55 In [3]: fname = tempfile.mkstemp('.py')[1]
56
56
57 In [3]: f = open(fname,'w')
57 In [3]: f = open(fname,'w')
58
58
59 In [4]: f.write('pass\n')
59 In [4]: f.write('pass\n')
60
60
61 In [5]: f.flush()
61 In [5]: f.flush()
62
62
63 In [6]: t1 = type(__builtins__)
63 In [6]: t1 = type(__builtins__)
64
64
65 In [7]: %run $fname
65 In [7]: %run $fname
66
66
67 In [7]: f.close()
67 In [7]: f.close()
68
68
69 In [8]: bid2 = id(__builtins__)
69 In [8]: bid2 = id(__builtins__)
70
70
71 In [9]: t2 = type(__builtins__)
71 In [9]: t2 = type(__builtins__)
72
72
73 In [10]: t1 == t2
73 In [10]: t1 == t2
74 Out[10]: True
74 Out[10]: True
75
75
76 In [10]: bid1 == bid2
76 In [10]: bid1 == bid2
77 Out[10]: True
77 Out[10]: True
78
78
79 In [12]: try:
79 In [12]: try:
80 ....: os.unlink(fname)
80 ....: os.unlink(fname)
81 ....: except:
81 ....: except:
82 ....: pass
82 ....: pass
83 ....:
83 ....:
84 """
84 """
85
85
86 def doctest_reset_del():
86 def doctest_reset_del():
87 """Test that resetting doesn't cause errors in __del__ methods.
87 """Test that resetting doesn't cause errors in __del__ methods.
88
88
89 In [2]: class A(object):
89 In [2]: class A(object):
90 ...: def __del__(self):
90 ...: def __del__(self):
91 ...: print str("Hi")
91 ...: print str("Hi")
92 ...:
92 ...:
93
93
94 In [3]: a = A()
94 In [3]: a = A()
95
95
96 In [4]: get_ipython().reset()
96 In [4]: get_ipython().reset()
97 Hi
97 Hi
98
98
99 In [5]: 1+1
99 In [5]: 1+1
100 Out[5]: 2
100 Out[5]: 2
101 """
101 """
102
102
103 # For some tests, it will be handy to organize them in a class with a common
103 # For some tests, it will be handy to organize them in a class with a common
104 # setup that makes a temp file
104 # setup that makes a temp file
105
105
106 class TestMagicRunPass(tt.TempFileMixin):
106 class TestMagicRunPass(tt.TempFileMixin):
107
107
108 def setup(self):
108 def setup(self):
109 """Make a valid python temp file."""
109 """Make a valid python temp file."""
110 self.mktmp('pass\n')
110 self.mktmp('pass\n')
111
111
112 def run_tmpfile(self):
112 def run_tmpfile(self):
113 _ip = get_ipython()
113 _ip = get_ipython()
114 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
114 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
115 # See below and ticket https://bugs.launchpad.net/bugs/366353
115 # See below and ticket https://bugs.launchpad.net/bugs/366353
116 _ip.magic('run %s' % self.fname)
116 _ip.magic('run %s' % self.fname)
117
117
118 def test_builtins_id(self):
118 def test_builtins_id(self):
119 """Check that %run doesn't damage __builtins__ """
119 """Check that %run doesn't damage __builtins__ """
120 _ip = get_ipython()
120 _ip = get_ipython()
121 # Test that the id of __builtins__ is not modified by %run
121 # Test that the id of __builtins__ is not modified by %run
122 bid1 = id(_ip.user_ns['__builtins__'])
122 bid1 = id(_ip.user_ns['__builtins__'])
123 self.run_tmpfile()
123 self.run_tmpfile()
124 bid2 = id(_ip.user_ns['__builtins__'])
124 bid2 = id(_ip.user_ns['__builtins__'])
125 tt.assert_equals(bid1, bid2)
125 tt.assert_equals(bid1, bid2)
126
126
127 def test_builtins_type(self):
127 def test_builtins_type(self):
128 """Check that the type of __builtins__ doesn't change with %run.
128 """Check that the type of __builtins__ doesn't change with %run.
129
129
130 However, the above could pass if __builtins__ was already modified to
130 However, the above could pass if __builtins__ was already modified to
131 be a dict (it should be a module) by a previous use of %run. So we
131 be a dict (it should be a module) by a previous use of %run. So we
132 also check explicitly that it really is a module:
132 also check explicitly that it really is a module:
133 """
133 """
134 _ip = get_ipython()
134 _ip = get_ipython()
135 self.run_tmpfile()
135 self.run_tmpfile()
136 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
136 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
137
137
138 def test_prompts(self):
138 def test_prompts(self):
139 """Test that prompts correctly generate after %run"""
139 """Test that prompts correctly generate after %run"""
140 self.run_tmpfile()
140 self.run_tmpfile()
141 _ip = get_ipython()
141 _ip = get_ipython()
142 p2 = str(_ip.displayhook.prompt2).strip()
142 p2 = str(_ip.displayhook.prompt2).strip()
143 nt.assert_equals(p2[:3], '...')
143 nt.assert_equals(p2[:3], '...')
144
144
145
145
146 class TestMagicRunSimple(tt.TempFileMixin):
146 class TestMagicRunSimple(tt.TempFileMixin):
147
147
148 def test_simpledef(self):
148 def test_simpledef(self):
149 """Test that simple class definitions work."""
149 """Test that simple class definitions work."""
150 src = ("class foo: pass\n"
150 src = ("class foo: pass\n"
151 "def f(): return foo()")
151 "def f(): return foo()")
152 self.mktmp(src)
152 self.mktmp(src)
153 _ip.magic('run %s' % self.fname)
153 _ip.magic('run %s' % self.fname)
154 _ip.runlines('t = isinstance(f(), foo)')
154 _ip.runlines('t = isinstance(f(), foo)')
155 nt.assert_true(_ip.user_ns['t'])
155 nt.assert_true(_ip.user_ns['t'])
156
156
157 # We have to skip these in win32 because getoutputerr() crashes,
157 # We have to skip these in win32 because getoutputerr() crashes,
158 # due to the fact that subprocess does not support close_fds when
158 # due to the fact that subprocess does not support close_fds when
159 # redirecting stdout/err. So unless someone who knows more tells us how to
159 # redirecting stdout/err. So unless someone who knows more tells us how to
160 # implement getoutputerr() in win32, we're stuck avoiding these.
160 # implement getoutputerr() in win32, we're stuck avoiding these.
161 @dec.skip_win32
161 @dec.skip_win32
162 def test_obj_del(self):
162 def test_obj_del(self):
163 """Test that object's __del__ methods are called on exit."""
163 """Test that object's __del__ methods are called on exit."""
164
164
165 # This test is known to fail on win32.
165 # This test is known to fail on win32.
166 # See ticket https://bugs.launchpad.net/bugs/366334
166 # See ticket https://bugs.launchpad.net/bugs/366334
167 src = ("class A(object):\n"
167 src = ("class A(object):\n"
168 " def __del__(self):\n"
168 " def __del__(self):\n"
169 " print 'object A deleted'\n"
169 " print 'object A deleted'\n"
170 "a = A()\n")
170 "a = A()\n")
171 self.mktmp(src)
171 self.mktmp(src)
172 tt.ipexec_validate(self.fname, 'object A deleted')
172 tt.ipexec_validate(self.fname, 'object A deleted')
173
173
174 @dec.skip_known_failure
174 def test_aggressive_namespace_cleanup(self):
175 def test_aggressive_namespace_cleanup(self):
175 """Test that namespace cleanup is not too aggressive GH-238
176 """Test that namespace cleanup is not too aggressive GH-238
176
177
177 returning from another run magic deletes the namespace"""
178 Returning from another run magic deletes the namespace"""
178 # see ticket https://github.com/ipython/ipython/issues/238
179 # see ticket https://github.com/ipython/ipython/issues/238
179 class secondtmp(tt.TempFileMixin): pass
180 class secondtmp(tt.TempFileMixin): pass
180 empty = secondtmp()
181 empty = secondtmp()
181 empty.mktmp('')
182 empty.mktmp('')
182 src = ("ip = get_ipython()\n"
183 src = ("ip = get_ipython()\n"
183 "for i in range(5):\n"
184 "for i in range(5):\n"
184 " try:\n"
185 " try:\n"
185 " ip.magic('run %s')\n"
186 " ip.magic('run %s')\n"
186 " except NameError, e:\n"
187 " except NameError, e:\n"
187 " print i;break\n" % empty.fname)
188 " print i;break\n" % empty.fname)
188 self.mktmp(src)
189 self.mktmp(src)
189 _ip.magic('run %s' % self.fname)
190 _ip.magic('run %s' % self.fname)
190 _ip.runlines('ip == get_ipython()')
191 _ip.runlines('ip == get_ipython()')
191 tt.assert_equals(_ip.user_ns['i'], 5)
192 tt.assert_equals(_ip.user_ns['i'], 5)
192
193
193 @dec.skip_win32
194 @dec.skip_win32
194 def test_tclass(self):
195 def test_tclass(self):
195 mydir = os.path.dirname(__file__)
196 mydir = os.path.dirname(__file__)
196 tc = os.path.join(mydir, 'tclass')
197 tc = os.path.join(mydir, 'tclass')
197 src = ("%%run '%s' C-first\n"
198 src = ("%%run '%s' C-first\n"
198 "%%run '%s' C-second\n") % (tc, tc)
199 "%%run '%s' C-second\n") % (tc, tc)
199 self.mktmp(src, '.ipy')
200 self.mktmp(src, '.ipy')
200 out = """\
201 out = """\
201 ARGV 1-: [u'C-first']
202 ARGV 1-: [u'C-first']
202 ARGV 1-: [u'C-second']
203 ARGV 1-: [u'C-second']
203 tclass.py: deleting object: C-first
204 tclass.py: deleting object: C-first
204 """
205 """
205 tt.ipexec_validate(self.fname, out)
206 tt.ipexec_validate(self.fname, out)
General Comments 0
You need to be logged in to leave comments. Login now