Show More
@@ -1,174 +1,174 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 | # stdlib |
|
15 | # stdlib | |
16 | import os |
|
16 | import os | |
17 | import sys |
|
17 | import sys | |
18 | import tempfile |
|
18 | import tempfile | |
19 |
|
19 | |||
20 | # third-party |
|
20 | # third-party | |
21 | import nose.tools as nt |
|
21 | import nose.tools as nt | |
22 |
|
22 | |||
23 | # our own |
|
23 | # our own | |
24 | from IPython.utils.platutils import find_cmd |
|
24 | from IPython.utils.platutils import find_cmd | |
25 | from IPython.utils import genutils |
|
25 | from IPython.utils import genutils | |
26 | from IPython.testing import decorators as dec |
|
26 | from IPython.testing import decorators as dec | |
27 | from IPython.testing import tools as tt |
|
27 | from IPython.testing import tools as tt | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Test functions begin |
|
30 | # Test functions begin | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | def doctest_refbug(): |
|
33 | def doctest_refbug(): | |
34 | """Very nasty problem with references held by multiple runs of a script. |
|
34 | """Very nasty problem with references held by multiple runs of a script. | |
35 | See: https://bugs.launchpad.net/ipython/+bug/269966 |
|
35 | See: https://bugs.launchpad.net/ipython/+bug/269966 | |
36 |
|
36 | |||
37 | In [1]: _ip.clear_main_mod_cache() |
|
37 | In [1]: _ip.clear_main_mod_cache() | |
38 | # random |
|
38 | # random | |
39 |
|
39 | |||
40 | In [2]: %run refbug |
|
40 | In [2]: %run refbug | |
41 |
|
41 | |||
42 | In [3]: call_f() |
|
42 | In [3]: call_f() | |
43 | lowercased: hello |
|
43 | lowercased: hello | |
44 |
|
44 | |||
45 | In [4]: %run refbug |
|
45 | In [4]: %run refbug | |
46 |
|
46 | |||
47 | In [5]: call_f() |
|
47 | In [5]: call_f() | |
48 | lowercased: hello |
|
48 | lowercased: hello | |
49 | lowercased: hello |
|
49 | lowercased: hello | |
50 | """ |
|
50 | """ | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def doctest_run_builtins(): |
|
53 | def doctest_run_builtins(): | |
54 | r"""Check that %run doesn't damage __builtins__. |
|
54 | r"""Check that %run doesn't damage __builtins__. | |
55 |
|
55 | |||
56 | In [1]: import tempfile |
|
56 | In [1]: import tempfile | |
57 |
|
57 | |||
58 | In [2]: bid1 = id(__builtins__) |
|
58 | In [2]: bid1 = id(__builtins__) | |
59 |
|
59 | |||
60 | In [3]: fname = tempfile.mkstemp('.py')[1] |
|
60 | In [3]: fname = tempfile.mkstemp('.py')[1] | |
61 |
|
61 | |||
62 | In [3]: f = open(fname,'w') |
|
62 | In [3]: f = open(fname,'w') | |
63 |
|
63 | |||
64 | In [4]: f.write('pass\n') |
|
64 | In [4]: f.write('pass\n') | |
65 |
|
65 | |||
66 | In [5]: f.flush() |
|
66 | In [5]: f.flush() | |
67 |
|
67 | |||
68 | In [6]: t1 = type(__builtins__) |
|
68 | In [6]: t1 = type(__builtins__) | |
69 |
|
69 | |||
70 | In [7]: %run "$fname" |
|
70 | In [7]: %run "$fname" | |
71 |
|
71 | |||
72 | In [7]: f.close() |
|
72 | In [7]: f.close() | |
73 |
|
73 | |||
74 | In [8]: bid2 = id(__builtins__) |
|
74 | In [8]: bid2 = id(__builtins__) | |
75 |
|
75 | |||
76 | In [9]: t2 = type(__builtins__) |
|
76 | In [9]: t2 = type(__builtins__) | |
77 |
|
77 | |||
78 | In [10]: t1 == t2 |
|
78 | In [10]: t1 == t2 | |
79 | Out[10]: True |
|
79 | Out[10]: True | |
80 |
|
80 | |||
81 | In [10]: bid1 == bid2 |
|
81 | In [10]: bid1 == bid2 | |
82 | Out[10]: True |
|
82 | Out[10]: True | |
83 |
|
83 | |||
84 | In [12]: try: |
|
84 | In [12]: try: | |
85 | ....: os.unlink(fname) |
|
85 | ....: os.unlink(fname) | |
86 | ....: except: |
|
86 | ....: except: | |
87 | ....: pass |
|
87 | ....: pass | |
88 | ....: |
|
88 | ....: | |
89 | """ |
|
89 | """ | |
90 |
|
90 | |||
91 | # For some tests, it will be handy to organize them in a class with a common |
|
91 | # For some tests, it will be handy to organize them in a class with a common | |
92 | # setup that makes a temp file |
|
92 | # setup that makes a temp file | |
93 |
|
93 | |||
94 | class TestMagicRunPass(tt.TempFileMixin): |
|
94 | class TestMagicRunPass(tt.TempFileMixin): | |
95 |
|
95 | |||
96 | def setup(self): |
|
96 | def setup(self): | |
97 | """Make a valid python temp file.""" |
|
97 | """Make a valid python temp file.""" | |
98 | self.mktmp('pass\n') |
|
98 | self.mktmp('pass\n') | |
99 |
|
99 | |||
100 | def run_tmpfile(self): |
|
100 | def run_tmpfile(self): | |
101 | _ip = get_ipython() |
|
101 | _ip = get_ipython() | |
102 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
102 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. | |
103 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
103 | # See below and ticket https://bugs.launchpad.net/bugs/366353 | |
104 | _ip.magic('run "%s"' % self.fname) |
|
104 | _ip.magic('run "%s"' % self.fname) | |
105 |
|
105 | |||
106 | def test_builtins_id(self): |
|
106 | def test_builtins_id(self): | |
107 | """Check that %run doesn't damage __builtins__ """ |
|
107 | """Check that %run doesn't damage __builtins__ """ | |
108 | _ip = get_ipython() |
|
108 | _ip = get_ipython() | |
109 | # Test that the id of __builtins__ is not modified by %run |
|
109 | # Test that the id of __builtins__ is not modified by %run | |
110 | bid1 = id(_ip.user_ns['__builtins__']) |
|
110 | bid1 = id(_ip.user_ns['__builtins__']) | |
111 | self.run_tmpfile() |
|
111 | self.run_tmpfile() | |
112 | bid2 = id(_ip.user_ns['__builtins__']) |
|
112 | bid2 = id(_ip.user_ns['__builtins__']) | |
113 | tt.assert_equals(bid1, bid2) |
|
113 | tt.assert_equals(bid1, bid2) | |
114 |
|
114 | |||
115 | def test_builtins_type(self): |
|
115 | def test_builtins_type(self): | |
116 | """Check that the type of __builtins__ doesn't change with %run. |
|
116 | """Check that the type of __builtins__ doesn't change with %run. | |
117 |
|
117 | |||
118 | However, the above could pass if __builtins__ was already modified to |
|
118 | However, the above could pass if __builtins__ was already modified to | |
119 | be a dict (it should be a module) by a previous use of %run. So we |
|
119 | be a dict (it should be a module) by a previous use of %run. So we | |
120 | also check explicitly that it really is a module: |
|
120 | also check explicitly that it really is a module: | |
121 | """ |
|
121 | """ | |
122 | _ip = get_ipython() |
|
122 | _ip = get_ipython() | |
123 | self.run_tmpfile() |
|
123 | self.run_tmpfile() | |
124 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) |
|
124 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) | |
125 |
|
125 | |||
126 | def test_prompts(self): |
|
126 | def test_prompts(self): | |
127 | """Test that prompts correctly generate after %run""" |
|
127 | """Test that prompts correctly generate after %run""" | |
128 | self.run_tmpfile() |
|
128 | self.run_tmpfile() | |
129 | _ip = get_ipython() |
|
129 | _ip = get_ipython() | |
130 | p2 = str(_ip.outputcache.prompt2).strip() |
|
130 | p2 = str(_ip.outputcache.prompt2).strip() | |
131 | nt.assert_equals(p2[:3], '...') |
|
131 | nt.assert_equals(p2[:3], '...') | |
132 |
|
132 | |||
133 |
|
133 | |||
134 | class TestMagicRunSimple(tt.TempFileMixin): |
|
134 | class TestMagicRunSimple(tt.TempFileMixin): | |
135 |
|
135 | |||
136 | def test_simpledef(self): |
|
136 | def test_simpledef(self): | |
137 | """Test that simple class definitions work.""" |
|
137 | """Test that simple class definitions work.""" | |
138 | src = ("class foo: pass\n" |
|
138 | src = ("class foo: pass\n" | |
139 | "def f(): return foo()") |
|
139 | "def f(): return foo()") | |
140 | self.mktmp(src) |
|
140 | self.mktmp(src) | |
141 |
_ip.magic('run |
|
141 | _ip.magic('run %s' % self.fname) | |
142 | _ip.runlines('t = isinstance(f(), foo)') |
|
142 | _ip.runlines('t = isinstance(f(), foo)') | |
143 | nt.assert_true(_ip.user_ns['t']) |
|
143 | nt.assert_true(_ip.user_ns['t']) | |
144 |
|
144 | |||
145 | # We have to skip these in win32 because genutils.getoutputerr() crashes, |
|
145 | # We have to skip these in win32 because genutils.getoutputerr() crashes, | |
146 | # due to the fact that subprocess does not support close_fds when |
|
146 | # due to the fact that subprocess does not support close_fds when | |
147 | # redirecting stdout/err. So unless someone who knows more tells us how to |
|
147 | # redirecting stdout/err. So unless someone who knows more tells us how to | |
148 | # implement genutils.getoutputerr() in win32, we're stuck avoiding these. |
|
148 | # implement genutils.getoutputerr() in win32, we're stuck avoiding these. | |
149 | @dec.skip_win32 |
|
149 | @dec.skip_win32 | |
150 | def test_obj_del(self): |
|
150 | def test_obj_del(self): | |
151 | """Test that object's __del__ methods are called on exit.""" |
|
151 | """Test that object's __del__ methods are called on exit.""" | |
152 |
|
152 | |||
153 | # This test is known to fail on win32. |
|
153 | # This test is known to fail on win32. | |
154 | # See ticket https://bugs.launchpad.net/bugs/366334 |
|
154 | # See ticket https://bugs.launchpad.net/bugs/366334 | |
155 | src = ("class A(object):\n" |
|
155 | src = ("class A(object):\n" | |
156 | " def __del__(self):\n" |
|
156 | " def __del__(self):\n" | |
157 | " print 'object A deleted'\n" |
|
157 | " print 'object A deleted'\n" | |
158 | "a = A()\n") |
|
158 | "a = A()\n") | |
159 | self.mktmp(src) |
|
159 | self.mktmp(src) | |
160 | tt.ipexec_validate(self.fname, 'object A deleted') |
|
160 | tt.ipexec_validate(self.fname, 'object A deleted') | |
161 |
|
161 | |||
162 | @dec.skip_win32 |
|
162 | @dec.skip_win32 | |
163 | def test_tclass(self): |
|
163 | def test_tclass(self): | |
164 | mydir = os.path.dirname(__file__) |
|
164 | mydir = os.path.dirname(__file__) | |
165 | tc = os.path.join(mydir, 'tclass') |
|
165 | tc = os.path.join(mydir, 'tclass') | |
166 | src = ("%%run '%s' C-first\n" |
|
166 | src = ("%%run '%s' C-first\n" | |
167 | "%%run '%s' C-second\n") % (tc, tc) |
|
167 | "%%run '%s' C-second\n") % (tc, tc) | |
168 | self.mktmp(src, '.ipy') |
|
168 | self.mktmp(src, '.ipy') | |
169 | out = """\ |
|
169 | out = """\ | |
170 | ARGV 1-: ['C-first'] |
|
170 | ARGV 1-: ['C-first'] | |
171 | ARGV 1-: ['C-second'] |
|
171 | ARGV 1-: ['C-second'] | |
172 | tclass.py: deleting object: C-first |
|
172 | tclass.py: deleting object: C-first | |
173 | """ |
|
173 | """ | |
174 | tt.ipexec_validate(self.fname, out) |
|
174 | tt.ipexec_validate(self.fname, out) |
@@ -1,61 +1,74 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | Tests for testing.tools |
|
4 | Tests for testing.tools | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008-2009 The IPython Development Team |
|
8 | # Copyright (C) 2008-2009 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | from __future__ import with_statement | |||
17 |
|
18 | |||
18 | import os |
|
19 | import os | |
19 | import sys |
|
20 | import sys | |
20 |
|
21 | |||
21 | import nose.tools as nt |
|
22 | import nose.tools as nt | |
22 |
|
23 | |||
23 | from IPython.testing import decorators as dec |
|
24 | from IPython.testing import decorators as dec | |
24 |
from IPython.testing |
|
25 | from IPython.testing import tools as tt | |
25 |
|
26 | |||
26 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
27 | # Tests |
|
28 | # Tests | |
28 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
29 |
|
30 | |||
30 | @dec.skip_win32 |
|
31 | @dec.skip_win32 | |
31 | def test_full_path_posix(): |
|
32 | def test_full_path_posix(): | |
32 | spath = '/foo/bar.py' |
|
33 | spath = '/foo/bar.py' | |
33 | result = full_path(spath,['a.txt','b.txt']) |
|
34 | result = tt.full_path(spath,['a.txt','b.txt']) | |
34 | nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt']) |
|
35 | nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt']) | |
35 | spath = '/foo' |
|
36 | spath = '/foo' | |
36 | result = full_path(spath,['a.txt','b.txt']) |
|
37 | result = tt.full_path(spath,['a.txt','b.txt']) | |
37 | nt.assert_equal(result, ['/a.txt', '/b.txt']) |
|
38 | nt.assert_equal(result, ['/a.txt', '/b.txt']) | |
38 | result = full_path(spath,'a.txt') |
|
39 | result = tt.full_path(spath,'a.txt') | |
39 | nt.assert_equal(result, ['/a.txt']) |
|
40 | nt.assert_equal(result, ['/a.txt']) | |
40 |
|
41 | |||
41 |
|
42 | |||
42 | @dec.skip_if_not_win32 |
|
43 | @dec.skip_if_not_win32 | |
43 | def test_full_path_win32(): |
|
44 | def test_full_path_win32(): | |
44 | spath = 'c:\\foo\\bar.py' |
|
45 | spath = 'c:\\foo\\bar.py' | |
45 | result = full_path(spath,['a.txt','b.txt']) |
|
46 | result = tt.full_path(spath,['a.txt','b.txt']) | |
46 | nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt']) |
|
47 | nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt']) | |
47 | spath = 'c:\\foo' |
|
48 | spath = 'c:\\foo' | |
48 | result = full_path(spath,['a.txt','b.txt']) |
|
49 | result = tt.full_path(spath,['a.txt','b.txt']) | |
49 | nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt']) |
|
50 | nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt']) | |
50 | result = full_path(spath,'a.txt') |
|
51 | result = tt.full_path(spath,'a.txt') | |
51 | nt.assert_equal(result, ['c:\\a.txt']) |
|
52 | nt.assert_equal(result, ['c:\\a.txt']) | |
52 |
|
53 | |||
53 |
|
54 | |||
|
55 | @dec.parametric | |||
54 | def test_parser(): |
|
56 | def test_parser(): | |
55 | err = ("FAILED (errors=1)", 1, 0) |
|
57 | err = ("FAILED (errors=1)", 1, 0) | |
56 | fail = ("FAILED (failures=1)", 0, 1) |
|
58 | fail = ("FAILED (failures=1)", 0, 1) | |
57 | both = ("FAILED (errors=1, failures=1)", 1, 1) |
|
59 | both = ("FAILED (errors=1, failures=1)", 1, 1) | |
58 | for txt, nerr, nfail in [err, fail, both]: |
|
60 | for txt, nerr, nfail in [err, fail, both]: | |
59 | nerr1, nfail1 = parse_test_output(txt) |
|
61 | nerr1, nfail1 = tt.parse_test_output(txt) | |
60 |
yield |
|
62 | yield nt.assert_equal(nerr, nerr1) | |
61 |
yield |
|
63 | yield nt.assert_equal(nfail, nfail1) | |
|
64 | ||||
|
65 | ||||
|
66 | @dec.parametric | |||
|
67 | def test_temp_pyfile(): | |||
|
68 | src = 'pass\n' | |||
|
69 | fname, fh = tt.temp_pyfile(src) | |||
|
70 | yield nt.assert_true(os.path.isfile(fname)) | |||
|
71 | fh.close() | |||
|
72 | with open(fname) as fh2: | |||
|
73 | src2 = fh2.read() | |||
|
74 | yield nt.assert_equal(src2, src) |
General Comments 0
You need to be logged in to leave comments.
Login now