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