##// END OF EJS Templates
Fixed broken test in :mod:`IPython.utils.tests.test_path`.
Brian Granger -
Show More
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,260 +1,260 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2008 The IPython Development Team
5 # Copyright (C) 2008 The IPython Development Team
6 #
6 #
7 # Distributed under the terms of the BSD License. The full license is in
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import shutil
16 import shutil
17 import sys
17 import sys
18 import tempfile
18 import tempfile
19
19
20 from os.path import join, abspath, split
20 from os.path import join, abspath, split
21
21
22 import nose.tools as nt
22 import nose.tools as nt
23
23
24 from nose import with_setup
24 from nose import with_setup
25
25
26 import IPython
26 import IPython
27 from IPython.testing import decorators as dec
27 from IPython.testing import decorators as dec
28 from IPython.testing.decorators import skip_if_not_win32
28 from IPython.testing.decorators import skip_if_not_win32
29 from IPython.utils.path import (
29 from IPython.utils import path
30 get_home_dir,
31 HomeDirError,
32 get_ipython_dir,
33 get_ipython_package_dir,
34 get_ipython_module_path,
35 filefind,
36 get_long_path_name
37 )
38
30
39 # Platform-dependent imports
31 # Platform-dependent imports
40 try:
32 try:
41 import _winreg as wreg
33 import _winreg as wreg
42 except ImportError:
34 except ImportError:
43 #Fake _winreg module on none windows platforms
35 #Fake _winreg module on none windows platforms
44 import new
36 import new
45 sys.modules["_winreg"] = new.module("_winreg")
37 sys.modules["_winreg"] = new.module("_winreg")
46 import _winreg as wreg
38 import _winreg as wreg
47 #Add entries that needs to be stubbed by the testing code
39 #Add entries that needs to be stubbed by the testing code
48 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
40 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
49
41
50 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
51 # Globals
43 # Globals
52 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
53 env = os.environ
45 env = os.environ
54 TEST_FILE_PATH = split(abspath(__file__))[0]
46 TEST_FILE_PATH = split(abspath(__file__))[0]
55 TMP_TEST_DIR = tempfile.mkdtemp()
47 TMP_TEST_DIR = tempfile.mkdtemp()
56 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
48 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
57 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
49 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
58 #
50 #
59 # Setup/teardown functions/decorators
51 # Setup/teardown functions/decorators
60 #
52 #
61
53
62 def setup():
54 def setup():
63 """Setup testenvironment for the module:
55 """Setup testenvironment for the module:
64
56
65 - Adds dummy home dir tree
57 - Adds dummy home dir tree
66 """
58 """
67 # Do not mask exceptions here. In particular, catching WindowsError is a
59 # Do not mask exceptions here. In particular, catching WindowsError is a
68 # problem because that exception is only defined on Windows...
60 # problem because that exception is only defined on Windows...
69 os.makedirs(IP_TEST_DIR)
61 os.makedirs(IP_TEST_DIR)
70
62
63
71 def teardown():
64 def teardown():
72 """Teardown testenvironment for the module:
65 """Teardown testenvironment for the module:
73
66
74 - Remove dummy home dir tree
67 - Remove dummy home dir tree
75 """
68 """
76 # Note: we remove the parent test dir, which is the root of all test
69 # Note: we remove the parent test dir, which is the root of all test
77 # subdirs we may have created. Use shutil instead of os.removedirs, so
70 # subdirs we may have created. Use shutil instead of os.removedirs, so
78 # that non-empty directories are all recursively removed.
71 # that non-empty directories are all recursively removed.
79 shutil.rmtree(TMP_TEST_DIR)
72 shutil.rmtree(TMP_TEST_DIR)
80
73
81
74
82 def setup_environment():
75 def setup_environment():
83 """Setup testenvironment for some functions that are tested
76 """Setup testenvironment for some functions that are tested
84 in this module. In particular this functions stores attributes
77 in this module. In particular this functions stores attributes
85 and other things that we need to stub in some test functions.
78 and other things that we need to stub in some test functions.
86 This needs to be done on a function level and not module level because
79 This needs to be done on a function level and not module level because
87 each testfunction needs a pristine environment.
80 each testfunction needs a pristine environment.
88 """
81 """
89 global oldstuff, platformstuff
82 global oldstuff, platformstuff
90 oldstuff = (env.copy(), os.name, get_home_dir, IPython.__file__)
83 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__)
91
84
92 if os.name == 'nt':
85 if os.name == 'nt':
93 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
86 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
94
87
95
88
96 def teardown_environment():
89 def teardown_environment():
97 """Restore things that were remebered by the setup_environment function
90 """Restore things that were remebered by the setup_environment function
98 """
91 """
99 (oldenv, os.name, get_home_dir, IPython.__file__,) = oldstuff
92 (oldenv, os.name, get_home_dir, IPython.__file__,) = oldstuff
100
93
101 for key in env.keys():
94 for key in env.keys():
102 if key not in oldenv:
95 if key not in oldenv:
103 del env[key]
96 del env[key]
104 env.update(oldenv)
97 env.update(oldenv)
105 if hasattr(sys, 'frozen'):
98 if hasattr(sys, 'frozen'):
106 del sys.frozen
99 del sys.frozen
107 if os.name == 'nt':
100 if os.name == 'nt':
108 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
101 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
109
102
110 # Build decorator that uses the setup_environment/setup_environment
103 # Build decorator that uses the setup_environment/setup_environment
111 with_environment = with_setup(setup_environment, teardown_environment)
104 with_environment = with_setup(setup_environment, teardown_environment)
112
105
113
106
114 @skip_if_not_win32
107 @skip_if_not_win32
115 @with_environment
108 @with_environment
116 def test_get_home_dir_1():
109 def test_get_home_dir_1():
117 """Testcase for py2exe logic, un-compressed lib
110 """Testcase for py2exe logic, un-compressed lib
118 """
111 """
119 sys.frozen = True
112 sys.frozen = True
120
113
121 #fake filename for IPython.__init__
114 #fake filename for IPython.__init__
122 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
115 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
123
116
124 home_dir = get_home_dir()
117 path.home_dir = get_home_dir()
125 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
118 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
126
119
120
127 @skip_if_not_win32
121 @skip_if_not_win32
128 @with_environment
122 @with_environment
129 def test_get_home_dir_2():
123 def test_get_home_dir_2():
130 """Testcase for py2exe logic, compressed lib
124 """Testcase for py2exe logic, compressed lib
131 """
125 """
132 sys.frozen = True
126 sys.frozen = True
133 #fake filename for IPython.__init__
127 #fake filename for IPython.__init__
134 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
128 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
135
129
136 home_dir = get_home_dir()
130 home_dir = path.get_home_dir()
137 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
131 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
138
132
133
139 @with_environment
134 @with_environment
140 def test_get_home_dir_3():
135 def test_get_home_dir_3():
141 """Testcase $HOME is set, then use its value as home directory."""
136 """Testcase $HOME is set, then use its value as home directory."""
142 env["HOME"] = HOME_TEST_DIR
137 env["HOME"] = HOME_TEST_DIR
143 home_dir = get_home_dir()
138 home_dir = path.get_home_dir()
144 nt.assert_equal(home_dir, env["HOME"])
139 nt.assert_equal(home_dir, env["HOME"])
145
140
141
146 @with_environment
142 @with_environment
147 def test_get_home_dir_4():
143 def test_get_home_dir_4():
148 """Testcase $HOME is not set, os=='poix'.
144 """Testcase $HOME is not set, os=='posix'.
149 This should fail with HomeDirError"""
145 This should fail with HomeDirError"""
150
146
151 os.name = 'posix'
147 os.name = 'posix'
152 if 'HOME' in env: del env['HOME']
148 if 'HOME' in env: del env['HOME']
153 nt.assert_raises(HomeDirError, get_home_dir)
149 nt.assert_raises(path.HomeDirError, path.get_home_dir)
150
154
151
155 @skip_if_not_win32
152 @skip_if_not_win32
156 @with_environment
153 @with_environment
157 def test_get_home_dir_5():
154 def test_get_home_dir_5():
158 """Testcase $HOME is not set, os=='nt'
155 """Testcase $HOME is not set, os=='nt'
159 env['HOMEDRIVE'],env['HOMEPATH'] points to path."""
156 env['HOMEDRIVE'],env['HOMEPATH'] points to path."""
160
157
161 os.name = 'nt'
158 os.name = 'nt'
162 if 'HOME' in env: del env['HOME']
159 if 'HOME' in env: del env['HOME']
163 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
160 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
164
161
165 home_dir = get_home_dir()
162 home_dir = path.get_home_dir()
166 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
163 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
167
164
165
168 @skip_if_not_win32
166 @skip_if_not_win32
169 @with_environment
167 @with_environment
170 def test_get_home_dir_6():
168 def test_get_home_dir_6():
171 """Testcase $HOME is not set, os=='nt'
169 """Testcase $HOME is not set, os=='nt'
172 env['HOMEDRIVE'],env['HOMEPATH'] do not point to path.
170 env['HOMEDRIVE'],env['HOMEPATH'] do not point to path.
173 env['USERPROFILE'] points to path
171 env['USERPROFILE'] points to path
174 """
172 """
175
173
176 os.name = 'nt'
174 os.name = 'nt'
177 if 'HOME' in env: del env['HOME']
175 if 'HOME' in env: del env['HOME']
178 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST"
176 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST"
179 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
177 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
180
178
181 home_dir = get_home_dir()
179 home_dir = path.get_home_dir()
182 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
180 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
183
181
182
184 # Should we stub wreg fully so we can run the test on all platforms?
183 # Should we stub wreg fully so we can run the test on all platforms?
185 @skip_if_not_win32
184 @skip_if_not_win32
186 @with_environment
185 @with_environment
187 def test_get_home_dir_7():
186 def test_get_home_dir_7():
188 """Testcase $HOME is not set, os=='nt'
187 """Testcase $HOME is not set, os=='nt'
189
188
190 env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] and others missing
189 env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] and others missing
191 """
190 """
192 os.name = 'nt'
191 os.name = 'nt'
193 # Remove from stub environment all keys that may be set
192 # Remove from stub environment all keys that may be set
194 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
193 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
195 env.pop(key, None)
194 env.pop(key, None)
196
195
197 #Stub windows registry functions
196 #Stub windows registry functions
198 def OpenKey(x, y):
197 def OpenKey(x, y):
199 class key:
198 class key:
200 def Close(self):
199 def Close(self):
201 pass
200 pass
202 return key()
201 return key()
203 def QueryValueEx(x, y):
202 def QueryValueEx(x, y):
204 return [abspath(HOME_TEST_DIR)]
203 return [abspath(HOME_TEST_DIR)]
205
204
206 wreg.OpenKey = OpenKey
205 wreg.OpenKey = OpenKey
207 wreg.QueryValueEx = QueryValueEx
206 wreg.QueryValueEx = QueryValueEx
208
207
209 home_dir = get_home_dir()
208 home_dir = path.get_home_dir()
210 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
209 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
211
210
212
211
213 @with_environment
212 @with_environment
214 def test_get_ipython_dir_1():
213 def test_get_ipython_dir_1():
215 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
214 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
216 env['IPYTHON_DIR'] = "someplace/.ipython"
215 env['IPYTHON_DIR'] = "someplace/.ipython"
217 ipdir = get_ipython_dir()
216 ipdir = path.get_ipython_dir()
218 nt.assert_equal(ipdir, "someplace/.ipython")
217 nt.assert_equal(ipdir, "someplace/.ipython")
219
218
220
219
221 @with_environment
220 @with_environment
222 def test_get_ipython_dir_2():
221 def test_get_ipython_dir_2():
223 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
222 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
224 get_home_dir = lambda : "someplace"
223 path.get_home_dir = lambda : "someplace"
225 os.name = "posix"
224 os.name = "posix"
226 env.pop('IPYTHON_DIR', None)
225 env.pop('IPYTHON_DIR', None)
227 env.pop('IPYTHONDIR', None)
226 env.pop('IPYTHONDIR', None)
228 ipdir = get_ipython_dir()
227 ipdir = path.get_ipython_dir()
229 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
228 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
230
229
231
230
232 def test_filefind():
231 def test_filefind():
233 """Various tests for filefind"""
232 """Various tests for filefind"""
234 f = tempfile.NamedTemporaryFile()
233 f = tempfile.NamedTemporaryFile()
235 print 'fname:',f.name
234 # print 'fname:',f.name
236 alt_dirs = get_ipython_dir()
235 alt_dirs = path.get_ipython_dir()
237 t = filefind(f.name, alt_dirs)
236 t = path.filefind(f.name, alt_dirs)
238 print 'found:',t
237 # print 'found:',t
239
238
240
239
241 def test_get_ipython_package_dir():
240 def test_get_ipython_package_dir():
242 ipdir = get_ipython_package_dir()
241 ipdir = path.get_ipython_package_dir()
243 nt.assert_true(os.path.isdir(ipdir))
242 nt.assert_true(os.path.isdir(ipdir))
244
243
244
245 def test_get_ipython_module_path():
245 def test_get_ipython_module_path():
246 ipapp_path = get_ipython_module_path('IPython.core.ipapp')
246 ipapp_path = path.get_ipython_module_path('IPython.core.ipapp')
247 nt.assert_true(os.path.isfile(ipapp_path))
247 nt.assert_true(os.path.isfile(ipapp_path))
248
248
249
249 @dec.skip_if_not_win32
250 @dec.skip_if_not_win32
250 def test_get_long_path_name_win32():
251 def test_get_long_path_name_win32():
251 p = get_long_path_name('c:\\docume~1')
252 p = path.get_long_path_name('c:\\docume~1')
252 nt.assert_equals(p,u'c:\\Documents and Settings')
253 nt.assert_equals(p,u'c:\\Documents and Settings')
253
254
254
255
255 @dec.skip_win32
256 @dec.skip_win32
256 def test_get_long_path_name():
257 def test_get_long_path_name():
257 p = get_long_path_name('/usr/local')
258 p = path.get_long_path_name('/usr/local')
258 nt.assert_equals(p,'/usr/local')
259 nt.assert_equals(p,'/usr/local')
259
260
260
General Comments 0
You need to be logged in to leave comments. Login now