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