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