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