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