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