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