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