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