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