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