Show More
@@ -1,249 +1,274 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Tests for genutils.py""" |
|
3 | """Tests for genutils.py""" | |
4 |
|
4 | |||
5 | __docformat__ = "restructuredtext en" |
|
5 | __docformat__ = "restructuredtext en" | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008 The IPython Development Team |
|
8 | # Copyright (C) 2008 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 |
|
17 | |||
18 | from IPython import genutils |
|
18 | from IPython import genutils | |
19 | from IPython.testing.decorators import skipif |
|
19 | from IPython.testing.decorators import skipif | |
20 | from nose import with_setup |
|
20 | from nose import with_setup | |
21 | from nose.tools import raises |
|
21 | from nose.tools import raises | |
22 |
|
22 | |||
23 | from os.path import join, abspath, split |
|
23 | from os.path import join, abspath, split | |
24 | import os, sys, IPython |
|
24 | import os, sys, IPython | |
25 | import nose.tools as nt |
|
25 | import nose.tools as nt | |
26 |
|
26 | |||
27 | env = os.environ |
|
27 | env = os.environ | |
28 |
|
28 | |||
29 | try: |
|
29 | try: | |
30 | import _winreg as wreg |
|
30 | import _winreg as wreg | |
31 | except ImportError: |
|
31 | except ImportError: | |
32 | #Fake _winreg module on none windows platforms |
|
32 | #Fake _winreg module on none windows platforms | |
33 | import new |
|
33 | import new | |
34 | sys.modules["_winreg"] = new.module("_winreg") |
|
34 | sys.modules["_winreg"] = new.module("_winreg") | |
35 | import _winreg as wreg |
|
35 | import _winreg as wreg | |
36 | #Add entries that needs to be stubbed by the testing code |
|
36 | #Add entries that needs to be stubbed by the testing code | |
37 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
37 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
38 |
|
38 | |||
39 | test_file_path = split(abspath(__file__))[0] |
|
39 | test_file_path = split(abspath(__file__))[0] | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | # |
|
42 | # | |
43 | # Setup/teardown functions/decorators |
|
43 | # Setup/teardown functions/decorators | |
44 | # |
|
44 | # | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | def setup(): |
|
47 | def setup(): | |
|
48 | """Setup testenvironment for the module: | |||
|
49 | ||||
|
50 | - Adds dummy home dir tree | |||
|
51 | """ | |||
48 | try: |
|
52 | try: | |
49 | os.makedirs("home_test_dir/_ipython") |
|
53 | os.makedirs("home_test_dir/_ipython") | |
50 | except WindowsError: |
|
54 | except WindowsError: | |
51 | pass #Or should we complain that the test directory already exists?? |
|
55 | pass #Or should we complain that the test directory already exists?? | |
52 |
|
56 | |||
53 | def teardown(): |
|
57 | def teardown(): | |
|
58 | """Teardown testenvironment for the module: | |||
|
59 | ||||
|
60 | - Remove dummy home dir tree | |||
|
61 | """ | |||
54 | try: |
|
62 | try: | |
55 | os.removedirs("home_test_dir/_ipython") |
|
63 | os.removedirs("home_test_dir/_ipython") | |
56 | except WindowsError: |
|
64 | except WindowsError: | |
57 | pass #Or should we complain that the test directory already exists?? |
|
65 | pass #Or should we complain that the test directory already exists?? | |
58 |
|
66 | |||
59 |
|
67 | |||
60 | def setup_environment(): |
|
68 | def setup_environment(): | |
|
69 | """Setup testenvironment for some functions that are tested | |||
|
70 | in this module. In particular this functions stores attributes | |||
|
71 | and other things that we need to stub in some test functions. | |||
|
72 | This needs to be done on a function level and not module level because | |||
|
73 | each testfunction needs a pristine environment. | |||
|
74 | """ | |||
61 | global oldstuff, platformstuff |
|
75 | global oldstuff, platformstuff | |
62 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) |
|
76 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) | |
63 |
|
77 | |||
64 | if os.name == 'nt': |
|
78 | if os.name == 'nt': | |
65 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
79 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | |
66 |
|
80 | |||
67 | if 'IPYTHONDIR' in env: |
|
81 | if 'IPYTHONDIR' in env: | |
68 | del env['IPYTHONDIR'] |
|
82 | del env['IPYTHONDIR'] | |
69 |
|
83 | |||
70 | def teardown_environment(): |
|
84 | def teardown_environment(): | |
|
85 | """Restore things that were remebered by the setup_environment function | |||
|
86 | """ | |||
71 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff |
|
87 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff | |
72 | for key in env.keys(): |
|
88 | for key in env.keys(): | |
73 | if key not in oldenv: |
|
89 | if key not in oldenv: | |
74 | del env[key] |
|
90 | del env[key] | |
75 | env.update(oldenv) |
|
91 | env.update(oldenv) | |
76 | if hasattr(sys, 'frozen'): |
|
92 | if hasattr(sys, 'frozen'): | |
77 | del sys.frozen |
|
93 | del sys.frozen | |
78 | if os.name == 'nt': |
|
94 | if os.name == 'nt': | |
79 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
95 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | |
80 |
|
96 | |||
81 | with_enivronment = with_setup(setup_environment, teardown_environment) |
|
97 | # Build decorator that uses the setup_environment/setup_environment | |
|
98 | with_enivronment = with_setup(setup_environment, setup_environment) | |||
82 |
|
99 | |||
83 |
|
100 | |||
84 | # |
|
101 | # | |
85 | # Tests for get_home_dir |
|
102 | # Tests for get_home_dir | |
86 | # |
|
103 | # | |
87 |
|
104 | |||
88 | @with_enivronment |
|
105 | @with_enivronment | |
89 | def test_get_home_dir_1(): |
|
106 | def test_get_home_dir_1(): | |
90 | """Testcase for py2exe logic, un-compressed lib |
|
107 | """Testcase for py2exe logic, un-compressed lib | |
91 | """ |
|
108 | """ | |
92 | sys.frozen = True |
|
109 | sys.frozen = True | |
93 |
|
110 | |||
94 | #fake filename for IPython.__init__ |
|
111 | #fake filename for IPython.__init__ | |
95 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Lib/IPython/__init__.py")) |
|
112 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Lib/IPython/__init__.py")) | |
96 |
|
113 | |||
97 | home_dir = genutils.get_home_dir() |
|
114 | home_dir = genutils.get_home_dir() | |
98 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
115 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
99 |
|
116 | |||
100 | @with_enivronment |
|
117 | @with_enivronment | |
101 | def test_get_home_dir_2(): |
|
118 | def test_get_home_dir_2(): | |
102 | """Testcase for py2exe logic, compressed lib |
|
119 | """Testcase for py2exe logic, compressed lib | |
103 | """ |
|
120 | """ | |
104 | sys.frozen = True |
|
121 | sys.frozen = True | |
105 | #fake filename for IPython.__init__ |
|
122 | #fake filename for IPython.__init__ | |
106 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Library.zip/IPython/__init__.py")) |
|
123 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Library.zip/IPython/__init__.py")) | |
107 |
|
124 | |||
108 | home_dir = genutils.get_home_dir() |
|
125 | home_dir = genutils.get_home_dir() | |
109 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")).lower()) |
|
126 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")).lower()) | |
110 |
|
127 | |||
111 | @with_enivronment |
|
128 | @with_enivronment | |
112 | def test_get_home_dir_3(): |
|
129 | def test_get_home_dir_3(): | |
113 | """Testcase $HOME is set, then use its value as home directory.""" |
|
130 | """Testcase $HOME is set, then use its value as home directory.""" | |
114 | env["HOME"] = join(test_file_path, "home_test_dir") |
|
131 | env["HOME"] = join(test_file_path, "home_test_dir") | |
115 | home_dir = genutils.get_home_dir() |
|
132 | home_dir = genutils.get_home_dir() | |
116 | nt.assert_equal(home_dir, env["HOME"]) |
|
133 | nt.assert_equal(home_dir, env["HOME"]) | |
117 |
|
134 | |||
118 | @with_enivronment |
|
135 | @with_enivronment | |
119 | def test_get_home_dir_4(): |
|
136 | def test_get_home_dir_4(): | |
120 | """Testcase $HOME is not set, os=='posix'. |
|
137 | """Testcase $HOME is not set, os=='posix'. | |
121 | This should fail with HomeDirError""" |
|
138 | This should fail with HomeDirError""" | |
122 |
|
139 | |||
123 | os.name = 'posix' |
|
140 | os.name = 'posix' | |
124 | del os.environ["HOME"] |
|
141 | del os.environ["HOME"] | |
125 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) |
|
142 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) | |
126 |
|
143 | |||
127 | @with_enivronment |
|
144 | @with_enivronment | |
128 | def test_get_home_dir_5(): |
|
145 | def test_get_home_dir_5(): | |
129 | """Testcase $HOME is not set, os=='nt' |
|
146 | """Testcase $HOME is not set, os=='nt' | |
130 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
147 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" | |
131 |
|
148 | |||
132 | os.name = 'nt' |
|
149 | os.name = 'nt' | |
133 | del os.environ["HOME"] |
|
150 | del os.environ["HOME"] | |
134 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "home_test_dir" |
|
151 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "home_test_dir" | |
135 |
|
152 | |||
136 | home_dir = genutils.get_home_dir() |
|
153 | home_dir = genutils.get_home_dir() | |
137 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
154 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
138 |
|
155 | |||
139 | @with_enivronment |
|
156 | @with_enivronment | |
140 | def test_get_home_dir_6(): |
|
157 | def test_get_home_dir_6(): | |
141 | """Testcase $HOME is not set, os=='nt' |
|
158 | """Testcase $HOME is not set, os=='nt' | |
142 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
159 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. | |
143 | env['USERPROFILE'] points to path |
|
160 | env['USERPROFILE'] points to path | |
144 | """ |
|
161 | """ | |
145 |
|
162 | |||
146 | os.name = 'nt' |
|
163 | os.name = 'nt' | |
147 | del os.environ["HOME"] |
|
164 | del os.environ["HOME"] | |
148 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "DOES NOT EXIST" |
|
165 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "DOES NOT EXIST" | |
149 | env["USERPROFILE"] = abspath(join(test_file_path, "home_test_dir")) |
|
166 | env["USERPROFILE"] = abspath(join(test_file_path, "home_test_dir")) | |
150 |
|
167 | |||
151 | home_dir = genutils.get_home_dir() |
|
168 | home_dir = genutils.get_home_dir() | |
152 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
169 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
153 |
|
170 | |||
154 | # Should we stub wreg fully so we can run the test on all platforms? |
|
171 | # Should we stub wreg fully so we can run the test on all platforms? | |
155 | #@skip_if_not_win32 |
|
172 | #@skip_if_not_win32 | |
156 | @with_enivronment |
|
173 | @with_enivronment | |
157 | def test_get_home_dir_7(): |
|
174 | def test_get_home_dir_7(): | |
158 | """Testcase $HOME is not set, os=='nt' |
|
175 | """Testcase $HOME is not set, os=='nt' | |
159 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
176 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing | |
160 | """ |
|
177 | """ | |
161 | os.name = 'nt' |
|
178 | os.name = 'nt' | |
162 | del env["HOME"], env['HOMEDRIVE'] |
|
179 | del env["HOME"], env['HOMEDRIVE'] | |
163 |
|
180 | |||
164 | #Stub windows registry functions |
|
181 | #Stub windows registry functions | |
165 | def OpenKey(x, y): |
|
182 | def OpenKey(x, y): | |
166 | class key: |
|
183 | class key: | |
167 | def Close(self): |
|
184 | def Close(self): | |
168 | pass |
|
185 | pass | |
169 | return key() |
|
186 | return key() | |
170 | def QueryValueEx(x, y): |
|
187 | def QueryValueEx(x, y): | |
171 | return [abspath(join(test_file_path, "home_test_dir"))] |
|
188 | return [abspath(join(test_file_path, "home_test_dir"))] | |
172 |
|
189 | |||
173 | wreg.OpenKey = OpenKey |
|
190 | wreg.OpenKey = OpenKey | |
174 | wreg.QueryValueEx = QueryValueEx |
|
191 | wreg.QueryValueEx = QueryValueEx | |
175 |
|
192 | |||
176 | home_dir = genutils.get_home_dir() |
|
193 | home_dir = genutils.get_home_dir() | |
177 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
194 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
178 |
|
195 | |||
179 |
|
196 | |||
180 | # |
|
197 | # | |
181 | # Tests for get_ipython_dir |
|
198 | # Tests for get_ipython_dir | |
182 | # |
|
199 | # | |
183 |
|
200 | |||
184 | @with_enivronment |
|
201 | @with_enivronment | |
185 | def test_get_ipython_dir_1(): |
|
202 | def test_get_ipython_dir_1(): | |
186 |
""" |
|
203 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
187 | env['IPYTHONDIR'] = "someplace/.ipython" |
|
204 | env['IPYTHONDIR'] = "someplace/.ipython" | |
188 | ipdir = genutils.get_ipython_dir() |
|
205 | ipdir = genutils.get_ipython_dir() | |
189 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) |
|
206 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) | |
190 |
|
207 | |||
191 |
|
208 | |||
192 | @with_enivronment |
|
209 | @with_enivronment | |
193 | def test_get_ipython_dir_2(): |
|
210 | def test_get_ipython_dir_2(): | |
194 |
""" |
|
211 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
195 | genutils.get_home_dir = lambda : "someplace" |
|
212 | genutils.get_home_dir = lambda : "someplace" | |
196 | os.name = "posix" |
|
213 | os.name = "posix" | |
197 | ipdir = genutils.get_ipython_dir() |
|
214 | ipdir = genutils.get_ipython_dir() | |
198 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) |
|
215 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) | |
199 |
|
216 | |||
200 | @with_enivronment |
|
217 | @with_enivronment | |
201 | def test_get_ipython_dir_3(): |
|
218 | def test_get_ipython_dir_3(): | |
202 |
""" |
|
219 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
203 | genutils.get_home_dir = lambda : "someplace" |
|
220 | genutils.get_home_dir = lambda : "someplace" | |
204 | os.name = "nt" |
|
221 | os.name = "nt" | |
205 | ipdir = genutils.get_ipython_dir() |
|
222 | ipdir = genutils.get_ipython_dir() | |
206 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) |
|
223 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) | |
207 |
|
224 | |||
208 |
|
225 | |||
209 | # |
|
226 | # | |
210 | # Tests for get_security_dir |
|
227 | # Tests for get_security_dir | |
211 | # |
|
228 | # | |
212 |
|
229 | |||
213 | @with_enivronment |
|
230 | @with_enivronment | |
214 | def test_get_security_dir(): |
|
231 | def test_get_security_dir(): | |
215 | """Testcase to see if we can call get_security_dir without Exceptions.""" |
|
232 | """Testcase to see if we can call get_security_dir without Exceptions.""" | |
216 | sdir = genutils.get_security_dir() |
|
233 | sdir = genutils.get_security_dir() | |
217 |
|
234 | |||
218 |
|
235 | |||
219 | # |
|
236 | # | |
220 | # Tests for popkey |
|
237 | # Tests for popkey | |
221 | # |
|
238 | # | |
222 |
|
239 | |||
223 | def test_popkey_1(): |
|
240 | def test_popkey_1(): | |
|
241 | """test_popkey_1, Basic usage test of popkey | |||
|
242 | """ | |||
224 | dct = dict(a=1, b=2, c=3) |
|
243 | dct = dict(a=1, b=2, c=3) | |
225 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
244 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
226 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
245 | nt.assert_equal(dct, dict(b=2, c=3)) | |
227 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
246 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
228 | nt.assert_equal(dct, dict(c=3)) |
|
247 | nt.assert_equal(dct, dict(c=3)) | |
229 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
248 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
230 | nt.assert_equal(dct, dict()) |
|
249 | nt.assert_equal(dct, dict()) | |
231 |
|
250 | |||
232 | def test_popkey_2(): |
|
251 | def test_popkey_2(): | |
|
252 | """test_popkey_2, Test to see that popkey of non occuring keys | |||
|
253 | generates a KeyError exception | |||
|
254 | """ | |||
233 | dct = dict(a=1, b=2, c=3) |
|
255 | dct = dict(a=1, b=2, c=3) | |
234 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") |
|
256 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") | |
235 |
|
257 | |||
236 | def test_popkey_3(): |
|
258 | def test_popkey_3(): | |
|
259 | """test_popkey_3, Tests to see that popkey calls returns the correct value | |||
|
260 | and that the key/value was removed from the dict. | |||
|
261 | """ | |||
237 | dct = dict(a=1, b=2, c=3) |
|
262 | dct = dict(a=1, b=2, c=3) | |
238 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) |
|
263 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) | |
239 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
264 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
240 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) |
|
265 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) | |
241 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
266 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
242 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) |
|
267 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) | |
243 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
268 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
244 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
269 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
245 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
270 | nt.assert_equal(dct, dict(b=2, c=3)) | |
246 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
271 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
247 | nt.assert_equal(dct, dict(c=3)) |
|
272 | nt.assert_equal(dct, dict(c=3)) | |
248 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
273 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
249 | nt.assert_equal(dct, dict()) |
|
274 | nt.assert_equal(dct, dict()) |
General Comments 0
You need to be logged in to leave comments.
Login now