##// END OF EJS Templates
get_home_dir expands symlinks, adjust test accordingly
MinRK -
Show More
@@ -1,407 +1,408 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-2011 The IPython Development Team
5 # Copyright (C) 2008-2011 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 from __future__ import with_statement
15 from __future__ import with_statement
16
16
17 import os
17 import os
18 import shutil
18 import shutil
19 import sys
19 import sys
20 import tempfile
20 import tempfile
21 from io import StringIO
21 from io import StringIO
22
22
23 from os.path import join, abspath, split
23 from os.path import join, abspath, split
24
24
25 import nose.tools as nt
25 import nose.tools as nt
26
26
27 from nose import with_setup
27 from nose import with_setup
28
28
29 import IPython
29 import IPython
30 from IPython.testing import decorators as dec
30 from IPython.testing import decorators as dec
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 from IPython.testing.tools import make_tempfile, AssertPrints
32 from IPython.testing.tools import make_tempfile, AssertPrints
33 from IPython.utils import path, io
33 from IPython.utils import path, io
34 from IPython.utils import py3compat
34 from IPython.utils import py3compat
35
35
36 # Platform-dependent imports
36 # Platform-dependent imports
37 try:
37 try:
38 import _winreg as wreg
38 import _winreg as wreg
39 except ImportError:
39 except ImportError:
40 #Fake _winreg module on none windows platforms
40 #Fake _winreg module on none windows platforms
41 import types
41 import types
42 wr_name = "winreg" if py3compat.PY3 else "_winreg"
42 wr_name = "winreg" if py3compat.PY3 else "_winreg"
43 sys.modules[wr_name] = types.ModuleType(wr_name)
43 sys.modules[wr_name] = types.ModuleType(wr_name)
44 import _winreg as wreg
44 import _winreg as wreg
45 #Add entries that needs to be stubbed by the testing code
45 #Add entries that needs to be stubbed by the testing code
46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
47
47
48 try:
48 try:
49 reload
49 reload
50 except NameError: # Python 3
50 except NameError: # Python 3
51 from imp import reload
51 from imp import reload
52
52
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54 # Globals
54 # Globals
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 env = os.environ
56 env = os.environ
57 TEST_FILE_PATH = split(abspath(__file__))[0]
57 TEST_FILE_PATH = split(abspath(__file__))[0]
58 TMP_TEST_DIR = tempfile.mkdtemp()
58 TMP_TEST_DIR = tempfile.mkdtemp()
59 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
59 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
60 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
60 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
61 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
61 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
62 #
62 #
63 # Setup/teardown functions/decorators
63 # Setup/teardown functions/decorators
64 #
64 #
65
65
66 def setup():
66 def setup():
67 """Setup testenvironment for the module:
67 """Setup testenvironment for the module:
68
68
69 - Adds dummy home dir tree
69 - Adds dummy home dir tree
70 """
70 """
71 # Do not mask exceptions here. In particular, catching WindowsError is a
71 # Do not mask exceptions here. In particular, catching WindowsError is a
72 # problem because that exception is only defined on Windows...
72 # problem because that exception is only defined on Windows...
73 os.makedirs(IP_TEST_DIR)
73 os.makedirs(IP_TEST_DIR)
74 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
74 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
75
75
76
76
77 def teardown():
77 def teardown():
78 """Teardown testenvironment for the module:
78 """Teardown testenvironment for the module:
79
79
80 - Remove dummy home dir tree
80 - Remove dummy home dir tree
81 """
81 """
82 # Note: we remove the parent test dir, which is the root of all test
82 # Note: we remove the parent test dir, which is the root of all test
83 # subdirs we may have created. Use shutil instead of os.removedirs, so
83 # subdirs we may have created. Use shutil instead of os.removedirs, so
84 # that non-empty directories are all recursively removed.
84 # that non-empty directories are all recursively removed.
85 shutil.rmtree(TMP_TEST_DIR)
85 shutil.rmtree(TMP_TEST_DIR)
86
86
87
87
88 def setup_environment():
88 def setup_environment():
89 """Setup testenvironment for some functions that are tested
89 """Setup testenvironment for some functions that are tested
90 in this module. In particular this functions stores attributes
90 in this module. In particular this functions stores attributes
91 and other things that we need to stub in some test functions.
91 and other things that we need to stub in some test functions.
92 This needs to be done on a function level and not module level because
92 This needs to be done on a function level and not module level because
93 each testfunction needs a pristine environment.
93 each testfunction needs a pristine environment.
94 """
94 """
95 global oldstuff, platformstuff
95 global oldstuff, platformstuff
96 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
96 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
97
97
98 if os.name == 'nt':
98 if os.name == 'nt':
99 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
99 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
100
100
101
101
102 def teardown_environment():
102 def teardown_environment():
103 """Restore things that were remebered by the setup_environment function
103 """Restore things that were remebered by the setup_environment function
104 """
104 """
105 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
105 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
106 os.chdir(old_wd)
106 os.chdir(old_wd)
107 reload(path)
107 reload(path)
108
108
109 for key in env.keys():
109 for key in env.keys():
110 if key not in oldenv:
110 if key not in oldenv:
111 del env[key]
111 del env[key]
112 env.update(oldenv)
112 env.update(oldenv)
113 if hasattr(sys, 'frozen'):
113 if hasattr(sys, 'frozen'):
114 del sys.frozen
114 del sys.frozen
115 if os.name == 'nt':
115 if os.name == 'nt':
116 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
116 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
117
117
118 # Build decorator that uses the setup_environment/setup_environment
118 # Build decorator that uses the setup_environment/setup_environment
119 with_environment = with_setup(setup_environment, teardown_environment)
119 with_environment = with_setup(setup_environment, teardown_environment)
120
120
121 @skip_if_not_win32
121 @skip_if_not_win32
122 @with_environment
122 @with_environment
123 def test_get_home_dir_1():
123 def test_get_home_dir_1():
124 """Testcase for py2exe logic, un-compressed lib
124 """Testcase for py2exe logic, un-compressed lib
125 """
125 """
126 sys.frozen = True
126 sys.frozen = True
127
127
128 #fake filename for IPython.__init__
128 #fake filename for IPython.__init__
129 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
129 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
130
130
131 home_dir = path.get_home_dir()
131 home_dir = path.get_home_dir()
132 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
132 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
133
133
134
134
135 @skip_if_not_win32
135 @skip_if_not_win32
136 @with_environment
136 @with_environment
137 def test_get_home_dir_2():
137 def test_get_home_dir_2():
138 """Testcase for py2exe logic, compressed lib
138 """Testcase for py2exe logic, compressed lib
139 """
139 """
140 sys.frozen = True
140 sys.frozen = True
141 #fake filename for IPython.__init__
141 #fake filename for IPython.__init__
142 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
142 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
143
143
144 home_dir = path.get_home_dir(True)
144 home_dir = path.get_home_dir(True)
145 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
145 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
146
146
147
147
148 @with_environment
148 @with_environment
149 def test_get_home_dir_3():
149 def test_get_home_dir_3():
150 """get_home_dir() uses $HOME if set"""
150 """get_home_dir() uses $HOME if set"""
151 env["HOME"] = HOME_TEST_DIR
151 env["HOME"] = HOME_TEST_DIR
152 home_dir = path.get_home_dir(True)
152 home_dir = path.get_home_dir(True)
153 nt.assert_equal(home_dir, env["HOME"])
153 # get_home_dir expands symlinks
154 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
154
155
155
156
156 @with_environment
157 @with_environment
157 def test_get_home_dir_4():
158 def test_get_home_dir_4():
158 """get_home_dir() still works if $HOME is not set"""
159 """get_home_dir() still works if $HOME is not set"""
159
160
160 if 'HOME' in env: del env['HOME']
161 if 'HOME' in env: del env['HOME']
161 # this should still succeed, but we don't know what the answer should be
162 # this should still succeed, but we don't know what the answer should be
162 home = path.get_home_dir(True)
163 home = path.get_home_dir(True)
163 nt.assert_true(path._writable_dir(home))
164 nt.assert_true(path._writable_dir(home))
164
165
165 @with_environment
166 @with_environment
166 def test_get_home_dir_5():
167 def test_get_home_dir_5():
167 """raise HomeDirError if $HOME is specified, but not a writable dir"""
168 """raise HomeDirError if $HOME is specified, but not a writable dir"""
168 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
169 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
169 # set os.name = posix, to prevent My Documents fallback on Windows
170 # set os.name = posix, to prevent My Documents fallback on Windows
170 os.name = 'posix'
171 os.name = 'posix'
171 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
172 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
172
173
173
174
174 # Should we stub wreg fully so we can run the test on all platforms?
175 # Should we stub wreg fully so we can run the test on all platforms?
175 @skip_if_not_win32
176 @skip_if_not_win32
176 @with_environment
177 @with_environment
177 def test_get_home_dir_8():
178 def test_get_home_dir_8():
178 """Using registry hack for 'My Documents', os=='nt'
179 """Using registry hack for 'My Documents', os=='nt'
179
180
180 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
181 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
181 """
182 """
182 os.name = 'nt'
183 os.name = 'nt'
183 # Remove from stub environment all keys that may be set
184 # Remove from stub environment all keys that may be set
184 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
185 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
185 env.pop(key, None)
186 env.pop(key, None)
186
187
187 #Stub windows registry functions
188 #Stub windows registry functions
188 def OpenKey(x, y):
189 def OpenKey(x, y):
189 class key:
190 class key:
190 def Close(self):
191 def Close(self):
191 pass
192 pass
192 return key()
193 return key()
193 def QueryValueEx(x, y):
194 def QueryValueEx(x, y):
194 return [abspath(HOME_TEST_DIR)]
195 return [abspath(HOME_TEST_DIR)]
195
196
196 wreg.OpenKey = OpenKey
197 wreg.OpenKey = OpenKey
197 wreg.QueryValueEx = QueryValueEx
198 wreg.QueryValueEx = QueryValueEx
198
199
199 home_dir = path.get_home_dir()
200 home_dir = path.get_home_dir()
200 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
201 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
201
202
202
203
203 @with_environment
204 @with_environment
204 def test_get_ipython_dir_1():
205 def test_get_ipython_dir_1():
205 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
206 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
206 env_ipdir = os.path.join("someplace", ".ipython")
207 env_ipdir = os.path.join("someplace", ".ipython")
207 path._writable_dir = lambda path: True
208 path._writable_dir = lambda path: True
208 env['IPYTHON_DIR'] = env_ipdir
209 env['IPYTHON_DIR'] = env_ipdir
209 ipdir = path.get_ipython_dir()
210 ipdir = path.get_ipython_dir()
210 nt.assert_equal(ipdir, env_ipdir)
211 nt.assert_equal(ipdir, env_ipdir)
211
212
212
213
213 @with_environment
214 @with_environment
214 def test_get_ipython_dir_2():
215 def test_get_ipython_dir_2():
215 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
216 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
216 path.get_home_dir = lambda : "someplace"
217 path.get_home_dir = lambda : "someplace"
217 path.get_xdg_dir = lambda : None
218 path.get_xdg_dir = lambda : None
218 path._writable_dir = lambda path: True
219 path._writable_dir = lambda path: True
219 os.name = "posix"
220 os.name = "posix"
220 env.pop('IPYTHON_DIR', None)
221 env.pop('IPYTHON_DIR', None)
221 env.pop('IPYTHONDIR', None)
222 env.pop('IPYTHONDIR', None)
222 env.pop('XDG_CONFIG_HOME', None)
223 env.pop('XDG_CONFIG_HOME', None)
223 ipdir = path.get_ipython_dir()
224 ipdir = path.get_ipython_dir()
224 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
225 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
225
226
226 @with_environment
227 @with_environment
227 def test_get_ipython_dir_3():
228 def test_get_ipython_dir_3():
228 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
229 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
229 path.get_home_dir = lambda : "someplace"
230 path.get_home_dir = lambda : "someplace"
230 path._writable_dir = lambda path: True
231 path._writable_dir = lambda path: True
231 os.name = "posix"
232 os.name = "posix"
232 env.pop('IPYTHON_DIR', None)
233 env.pop('IPYTHON_DIR', None)
233 env.pop('IPYTHONDIR', None)
234 env.pop('IPYTHONDIR', None)
234 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
235 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
235 ipdir = path.get_ipython_dir()
236 ipdir = path.get_ipython_dir()
236 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
237 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
237
238
238 @with_environment
239 @with_environment
239 def test_get_ipython_dir_4():
240 def test_get_ipython_dir_4():
240 """test_get_ipython_dir_4, use XDG if both exist."""
241 """test_get_ipython_dir_4, use XDG if both exist."""
241 path.get_home_dir = lambda : HOME_TEST_DIR
242 path.get_home_dir = lambda : HOME_TEST_DIR
242 os.name = "posix"
243 os.name = "posix"
243 env.pop('IPYTHON_DIR', None)
244 env.pop('IPYTHON_DIR', None)
244 env.pop('IPYTHONDIR', None)
245 env.pop('IPYTHONDIR', None)
245 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
246 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
246 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
247 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
247 ipdir = path.get_ipython_dir()
248 ipdir = path.get_ipython_dir()
248 nt.assert_equal(ipdir, xdg_ipdir)
249 nt.assert_equal(ipdir, xdg_ipdir)
249
250
250 @with_environment
251 @with_environment
251 def test_get_ipython_dir_5():
252 def test_get_ipython_dir_5():
252 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
253 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
253 path.get_home_dir = lambda : HOME_TEST_DIR
254 path.get_home_dir = lambda : HOME_TEST_DIR
254 os.name = "posix"
255 os.name = "posix"
255 env.pop('IPYTHON_DIR', None)
256 env.pop('IPYTHON_DIR', None)
256 env.pop('IPYTHONDIR', None)
257 env.pop('IPYTHONDIR', None)
257 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
258 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
258 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
259 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
259 ipdir = path.get_ipython_dir()
260 ipdir = path.get_ipython_dir()
260 nt.assert_equal(ipdir, IP_TEST_DIR)
261 nt.assert_equal(ipdir, IP_TEST_DIR)
261
262
262 @with_environment
263 @with_environment
263 def test_get_ipython_dir_6():
264 def test_get_ipython_dir_6():
264 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
265 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
265 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
266 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
266 os.mkdir(xdg)
267 os.mkdir(xdg)
267 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
268 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
268 path.get_home_dir = lambda : HOME_TEST_DIR
269 path.get_home_dir = lambda : HOME_TEST_DIR
269 path.get_xdg_dir = lambda : xdg
270 path.get_xdg_dir = lambda : xdg
270 os.name = "posix"
271 os.name = "posix"
271 env.pop('IPYTHON_DIR', None)
272 env.pop('IPYTHON_DIR', None)
272 env.pop('IPYTHONDIR', None)
273 env.pop('IPYTHONDIR', None)
273 env.pop('XDG_CONFIG_HOME', None)
274 env.pop('XDG_CONFIG_HOME', None)
274 xdg_ipdir = os.path.join(xdg, "ipython")
275 xdg_ipdir = os.path.join(xdg, "ipython")
275 ipdir = path.get_ipython_dir()
276 ipdir = path.get_ipython_dir()
276 nt.assert_equal(ipdir, xdg_ipdir)
277 nt.assert_equal(ipdir, xdg_ipdir)
277
278
278 @with_environment
279 @with_environment
279 def test_get_ipython_dir_7():
280 def test_get_ipython_dir_7():
280 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
281 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
281 path._writable_dir = lambda path: True
282 path._writable_dir = lambda path: True
282 home_dir = os.path.expanduser('~')
283 home_dir = os.path.expanduser('~')
283 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
284 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
284 ipdir = path.get_ipython_dir()
285 ipdir = path.get_ipython_dir()
285 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
286 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
286
287
287
288
288 @with_environment
289 @with_environment
289 def test_get_xdg_dir_1():
290 def test_get_xdg_dir_1():
290 """test_get_xdg_dir_1, check xdg_dir"""
291 """test_get_xdg_dir_1, check xdg_dir"""
291 reload(path)
292 reload(path)
292 path._writable_dir = lambda path: True
293 path._writable_dir = lambda path: True
293 path.get_home_dir = lambda : 'somewhere'
294 path.get_home_dir = lambda : 'somewhere'
294 os.name = "posix"
295 os.name = "posix"
295 env.pop('IPYTHON_DIR', None)
296 env.pop('IPYTHON_DIR', None)
296 env.pop('IPYTHONDIR', None)
297 env.pop('IPYTHONDIR', None)
297 env.pop('XDG_CONFIG_HOME', None)
298 env.pop('XDG_CONFIG_HOME', None)
298
299
299 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
300 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
300
301
301
302
302 @with_environment
303 @with_environment
303 def test_get_xdg_dir_1():
304 def test_get_xdg_dir_1():
304 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
305 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
305 reload(path)
306 reload(path)
306 path.get_home_dir = lambda : HOME_TEST_DIR
307 path.get_home_dir = lambda : HOME_TEST_DIR
307 os.name = "posix"
308 os.name = "posix"
308 env.pop('IPYTHON_DIR', None)
309 env.pop('IPYTHON_DIR', None)
309 env.pop('IPYTHONDIR', None)
310 env.pop('IPYTHONDIR', None)
310 env.pop('XDG_CONFIG_HOME', None)
311 env.pop('XDG_CONFIG_HOME', None)
311 nt.assert_equal(path.get_xdg_dir(), None)
312 nt.assert_equal(path.get_xdg_dir(), None)
312
313
313 @with_environment
314 @with_environment
314 def test_get_xdg_dir_2():
315 def test_get_xdg_dir_2():
315 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
316 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
316 reload(path)
317 reload(path)
317 path.get_home_dir = lambda : HOME_TEST_DIR
318 path.get_home_dir = lambda : HOME_TEST_DIR
318 os.name = "posix"
319 os.name = "posix"
319 env.pop('IPYTHON_DIR', None)
320 env.pop('IPYTHON_DIR', None)
320 env.pop('IPYTHONDIR', None)
321 env.pop('IPYTHONDIR', None)
321 env.pop('XDG_CONFIG_HOME', None)
322 env.pop('XDG_CONFIG_HOME', None)
322 cfgdir=os.path.join(path.get_home_dir(), '.config')
323 cfgdir=os.path.join(path.get_home_dir(), '.config')
323 os.makedirs(cfgdir)
324 os.makedirs(cfgdir)
324
325
325 nt.assert_equal(path.get_xdg_dir(), cfgdir)
326 nt.assert_equal(path.get_xdg_dir(), cfgdir)
326
327
327 def test_filefind():
328 def test_filefind():
328 """Various tests for filefind"""
329 """Various tests for filefind"""
329 f = tempfile.NamedTemporaryFile()
330 f = tempfile.NamedTemporaryFile()
330 # print 'fname:',f.name
331 # print 'fname:',f.name
331 alt_dirs = path.get_ipython_dir()
332 alt_dirs = path.get_ipython_dir()
332 t = path.filefind(f.name, alt_dirs)
333 t = path.filefind(f.name, alt_dirs)
333 # print 'found:',t
334 # print 'found:',t
334
335
335
336
336 def test_get_ipython_package_dir():
337 def test_get_ipython_package_dir():
337 ipdir = path.get_ipython_package_dir()
338 ipdir = path.get_ipython_package_dir()
338 nt.assert_true(os.path.isdir(ipdir))
339 nt.assert_true(os.path.isdir(ipdir))
339
340
340
341
341 def test_get_ipython_module_path():
342 def test_get_ipython_module_path():
342 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
343 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
343 nt.assert_true(os.path.isfile(ipapp_path))
344 nt.assert_true(os.path.isfile(ipapp_path))
344
345
345
346
346 @dec.skip_if_not_win32
347 @dec.skip_if_not_win32
347 def test_get_long_path_name_win32():
348 def test_get_long_path_name_win32():
348 p = path.get_long_path_name('c:\\docume~1')
349 p = path.get_long_path_name('c:\\docume~1')
349 nt.assert_equals(p,u'c:\\Documents and Settings')
350 nt.assert_equals(p,u'c:\\Documents and Settings')
350
351
351
352
352 @dec.skip_win32
353 @dec.skip_win32
353 def test_get_long_path_name():
354 def test_get_long_path_name():
354 p = path.get_long_path_name('/usr/local')
355 p = path.get_long_path_name('/usr/local')
355 nt.assert_equals(p,'/usr/local')
356 nt.assert_equals(p,'/usr/local')
356
357
357 @dec.skip_win32 # can't create not-user-writable dir on win
358 @dec.skip_win32 # can't create not-user-writable dir on win
358 @with_environment
359 @with_environment
359 def test_not_writable_ipdir():
360 def test_not_writable_ipdir():
360 tmpdir = tempfile.mkdtemp()
361 tmpdir = tempfile.mkdtemp()
361 os.name = "posix"
362 os.name = "posix"
362 env.pop('IPYTHON_DIR', None)
363 env.pop('IPYTHON_DIR', None)
363 env.pop('IPYTHONDIR', None)
364 env.pop('IPYTHONDIR', None)
364 env.pop('XDG_CONFIG_HOME', None)
365 env.pop('XDG_CONFIG_HOME', None)
365 env['HOME'] = tmpdir
366 env['HOME'] = tmpdir
366 ipdir = os.path.join(tmpdir, '.ipython')
367 ipdir = os.path.join(tmpdir, '.ipython')
367 os.mkdir(ipdir)
368 os.mkdir(ipdir)
368 os.chmod(ipdir, 600)
369 os.chmod(ipdir, 600)
369 with AssertPrints('is not a writable location', channel='stderr'):
370 with AssertPrints('is not a writable location', channel='stderr'):
370 ipdir = path.get_ipython_dir()
371 ipdir = path.get_ipython_dir()
371 env.pop('IPYTHON_DIR', None)
372 env.pop('IPYTHON_DIR', None)
372
373
373 def test_unquote_filename():
374 def test_unquote_filename():
374 for win32 in (True, False):
375 for win32 in (True, False):
375 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
376 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
376 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
377 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
377 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
378 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
378 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
379 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
379 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
380 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
380 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
381 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
381 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
382 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
382 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
383 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
383 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
384 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
384 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
385 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
385
386
386 @with_environment
387 @with_environment
387 def test_get_py_filename():
388 def test_get_py_filename():
388 os.chdir(TMP_TEST_DIR)
389 os.chdir(TMP_TEST_DIR)
389 for win32 in (True, False):
390 for win32 in (True, False):
390 with make_tempfile('foo.py'):
391 with make_tempfile('foo.py'):
391 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
392 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
392 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
393 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
393 with make_tempfile('foo'):
394 with make_tempfile('foo'):
394 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
395 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
395 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
396 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
396 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
397 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
397 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
398 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
398 true_fn = 'foo with spaces.py'
399 true_fn = 'foo with spaces.py'
399 with make_tempfile(true_fn):
400 with make_tempfile(true_fn):
400 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
401 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
401 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
402 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
402 if win32:
403 if win32:
403 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
404 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
404 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
405 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
405 else:
406 else:
406 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
407 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
407 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
408 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
General Comments 0
You need to be logged in to leave comments. Login now