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