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