##// END OF EJS Templates
don't check writability in test for get_home_dir when HOME is undefined...
MinRK -
Show More
@@ -1,447 +1,446 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, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
96 oldstuff = (env.copy(), os.name, sys.platform, 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, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
105 (oldenv, os.name, sys.platform, 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 # get_home_dir expands symlinks
153 # get_home_dir expands symlinks
154 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
154 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
155
155
156
156
157 @with_environment
157 @with_environment
158 def test_get_home_dir_4():
158 def test_get_home_dir_4():
159 """get_home_dir() still works if $HOME is not set"""
159 """get_home_dir() still works if $HOME is not set"""
160
160
161 if 'HOME' in env: del env['HOME']
161 if 'HOME' in env: del env['HOME']
162 # this should still succeed, but we don't know what the answer should be
162 # this should still succeed, but we don't care what the answer is
163 home = path.get_home_dir(True)
163 home = path.get_home_dir(False)
164 nt.assert_true(path._writable_dir(home))
165
164
166 @with_environment
165 @with_environment
167 def test_get_home_dir_5():
166 def test_get_home_dir_5():
168 """raise HomeDirError if $HOME is specified, but not a writable dir"""
167 """raise HomeDirError if $HOME is specified, but not a writable dir"""
169 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
168 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
170 # set os.name = posix, to prevent My Documents fallback on Windows
169 # set os.name = posix, to prevent My Documents fallback on Windows
171 os.name = 'posix'
170 os.name = 'posix'
172 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
171 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
173
172
174
173
175 # Should we stub wreg fully so we can run the test on all platforms?
174 # Should we stub wreg fully so we can run the test on all platforms?
176 @skip_if_not_win32
175 @skip_if_not_win32
177 @with_environment
176 @with_environment
178 def test_get_home_dir_8():
177 def test_get_home_dir_8():
179 """Using registry hack for 'My Documents', os=='nt'
178 """Using registry hack for 'My Documents', os=='nt'
180
179
181 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
180 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
182 """
181 """
183 os.name = 'nt'
182 os.name = 'nt'
184 # Remove from stub environment all keys that may be set
183 # Remove from stub environment all keys that may be set
185 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
184 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
186 env.pop(key, None)
185 env.pop(key, None)
187
186
188 #Stub windows registry functions
187 #Stub windows registry functions
189 def OpenKey(x, y):
188 def OpenKey(x, y):
190 class key:
189 class key:
191 def Close(self):
190 def Close(self):
192 pass
191 pass
193 return key()
192 return key()
194 def QueryValueEx(x, y):
193 def QueryValueEx(x, y):
195 return [abspath(HOME_TEST_DIR)]
194 return [abspath(HOME_TEST_DIR)]
196
195
197 wreg.OpenKey = OpenKey
196 wreg.OpenKey = OpenKey
198 wreg.QueryValueEx = QueryValueEx
197 wreg.QueryValueEx = QueryValueEx
199
198
200 home_dir = path.get_home_dir()
199 home_dir = path.get_home_dir()
201 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
200 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
202
201
203
202
204 @with_environment
203 @with_environment
205 def test_get_ipython_dir_1():
204 def test_get_ipython_dir_1():
206 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
205 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
207 env_ipdir = os.path.join("someplace", ".ipython")
206 env_ipdir = os.path.join("someplace", ".ipython")
208 path._writable_dir = lambda path: True
207 path._writable_dir = lambda path: True
209 env['IPYTHONDIR'] = env_ipdir
208 env['IPYTHONDIR'] = env_ipdir
210 ipdir = path.get_ipython_dir()
209 ipdir = path.get_ipython_dir()
211 nt.assert_equal(ipdir, env_ipdir)
210 nt.assert_equal(ipdir, env_ipdir)
212
211
213
212
214 @with_environment
213 @with_environment
215 def test_get_ipython_dir_2():
214 def test_get_ipython_dir_2():
216 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
215 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
217 path.get_home_dir = lambda : "someplace"
216 path.get_home_dir = lambda : "someplace"
218 path.get_xdg_dir = lambda : None
217 path.get_xdg_dir = lambda : None
219 path._writable_dir = lambda path: True
218 path._writable_dir = lambda path: True
220 os.name = "posix"
219 os.name = "posix"
221 env.pop('IPYTHON_DIR', None)
220 env.pop('IPYTHON_DIR', None)
222 env.pop('IPYTHONDIR', None)
221 env.pop('IPYTHONDIR', None)
223 env.pop('XDG_CONFIG_HOME', None)
222 env.pop('XDG_CONFIG_HOME', None)
224 ipdir = path.get_ipython_dir()
223 ipdir = path.get_ipython_dir()
225 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
224 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
226
225
227 @with_environment
226 @with_environment
228 def test_get_ipython_dir_3():
227 def test_get_ipython_dir_3():
229 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
228 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
230 path.get_home_dir = lambda : "someplace"
229 path.get_home_dir = lambda : "someplace"
231 path._writable_dir = lambda path: True
230 path._writable_dir = lambda path: True
232 os.name = "posix"
231 os.name = "posix"
233 env.pop('IPYTHON_DIR', None)
232 env.pop('IPYTHON_DIR', None)
234 env.pop('IPYTHONDIR', None)
233 env.pop('IPYTHONDIR', None)
235 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
234 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
236 ipdir = path.get_ipython_dir()
235 ipdir = path.get_ipython_dir()
237 if sys.platform == "darwin":
236 if sys.platform == "darwin":
238 expected = os.path.join("someplace", ".ipython")
237 expected = os.path.join("someplace", ".ipython")
239 else:
238 else:
240 expected = os.path.join(XDG_TEST_DIR, "ipython")
239 expected = os.path.join(XDG_TEST_DIR, "ipython")
241 nt.assert_equal(ipdir, expected)
240 nt.assert_equal(ipdir, expected)
242
241
243 @with_environment
242 @with_environment
244 def test_get_ipython_dir_4():
243 def test_get_ipython_dir_4():
245 """test_get_ipython_dir_4, use XDG if both exist."""
244 """test_get_ipython_dir_4, use XDG if both exist."""
246 path.get_home_dir = lambda : HOME_TEST_DIR
245 path.get_home_dir = lambda : HOME_TEST_DIR
247 os.name = "posix"
246 os.name = "posix"
248 env.pop('IPYTHON_DIR', None)
247 env.pop('IPYTHON_DIR', None)
249 env.pop('IPYTHONDIR', None)
248 env.pop('IPYTHONDIR', None)
250 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
249 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
251 ipdir = path.get_ipython_dir()
250 ipdir = path.get_ipython_dir()
252 if sys.platform == "darwin":
251 if sys.platform == "darwin":
253 expected = os.path.join(HOME_TEST_DIR, ".ipython")
252 expected = os.path.join(HOME_TEST_DIR, ".ipython")
254 else:
253 else:
255 expected = os.path.join(XDG_TEST_DIR, "ipython")
254 expected = os.path.join(XDG_TEST_DIR, "ipython")
256 nt.assert_equal(ipdir, expected)
255 nt.assert_equal(ipdir, expected)
257
256
258 @with_environment
257 @with_environment
259 def test_get_ipython_dir_5():
258 def test_get_ipython_dir_5():
260 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
259 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
261 path.get_home_dir = lambda : HOME_TEST_DIR
260 path.get_home_dir = lambda : HOME_TEST_DIR
262 os.name = "posix"
261 os.name = "posix"
263 env.pop('IPYTHON_DIR', None)
262 env.pop('IPYTHON_DIR', None)
264 env.pop('IPYTHONDIR', None)
263 env.pop('IPYTHONDIR', None)
265 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
264 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
266 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
265 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
267 ipdir = path.get_ipython_dir()
266 ipdir = path.get_ipython_dir()
268 nt.assert_equal(ipdir, IP_TEST_DIR)
267 nt.assert_equal(ipdir, IP_TEST_DIR)
269
268
270 @with_environment
269 @with_environment
271 def test_get_ipython_dir_6():
270 def test_get_ipython_dir_6():
272 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
271 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
273 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
272 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
274 os.mkdir(xdg)
273 os.mkdir(xdg)
275 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
274 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
276 path.get_home_dir = lambda : HOME_TEST_DIR
275 path.get_home_dir = lambda : HOME_TEST_DIR
277 path.get_xdg_dir = lambda : xdg
276 path.get_xdg_dir = lambda : xdg
278 os.name = "posix"
277 os.name = "posix"
279 env.pop('IPYTHON_DIR', None)
278 env.pop('IPYTHON_DIR', None)
280 env.pop('IPYTHONDIR', None)
279 env.pop('IPYTHONDIR', None)
281 env.pop('XDG_CONFIG_HOME', None)
280 env.pop('XDG_CONFIG_HOME', None)
282 xdg_ipdir = os.path.join(xdg, "ipython")
281 xdg_ipdir = os.path.join(xdg, "ipython")
283 ipdir = path.get_ipython_dir()
282 ipdir = path.get_ipython_dir()
284 nt.assert_equal(ipdir, xdg_ipdir)
283 nt.assert_equal(ipdir, xdg_ipdir)
285
284
286 @with_environment
285 @with_environment
287 def test_get_ipython_dir_7():
286 def test_get_ipython_dir_7():
288 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
287 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
289 path._writable_dir = lambda path: True
288 path._writable_dir = lambda path: True
290 home_dir = os.path.expanduser('~')
289 home_dir = os.path.expanduser('~')
291 env['IPYTHONDIR'] = os.path.join('~', 'somewhere')
290 env['IPYTHONDIR'] = os.path.join('~', 'somewhere')
292 ipdir = path.get_ipython_dir()
291 ipdir = path.get_ipython_dir()
293 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
292 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
294
293
295
294
296 @with_environment
295 @with_environment
297 def test_get_xdg_dir_0():
296 def test_get_xdg_dir_0():
298 """test_get_xdg_dir_0, check xdg_dir"""
297 """test_get_xdg_dir_0, check xdg_dir"""
299 reload(path)
298 reload(path)
300 path._writable_dir = lambda path: True
299 path._writable_dir = lambda path: True
301 path.get_home_dir = lambda : 'somewhere'
300 path.get_home_dir = lambda : 'somewhere'
302 os.name = "posix"
301 os.name = "posix"
303 sys.platform = "linux2"
302 sys.platform = "linux2"
304 env.pop('IPYTHON_DIR', None)
303 env.pop('IPYTHON_DIR', None)
305 env.pop('IPYTHONDIR', None)
304 env.pop('IPYTHONDIR', None)
306 env.pop('XDG_CONFIG_HOME', None)
305 env.pop('XDG_CONFIG_HOME', None)
307
306
308 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
307 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
309
308
310
309
311 @with_environment
310 @with_environment
312 def test_get_xdg_dir_1():
311 def test_get_xdg_dir_1():
313 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
312 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
314 reload(path)
313 reload(path)
315 path.get_home_dir = lambda : HOME_TEST_DIR
314 path.get_home_dir = lambda : HOME_TEST_DIR
316 os.name = "posix"
315 os.name = "posix"
317 sys.platform = "linux2"
316 sys.platform = "linux2"
318 env.pop('IPYTHON_DIR', None)
317 env.pop('IPYTHON_DIR', None)
319 env.pop('IPYTHONDIR', None)
318 env.pop('IPYTHONDIR', None)
320 env.pop('XDG_CONFIG_HOME', None)
319 env.pop('XDG_CONFIG_HOME', None)
321 nt.assert_equal(path.get_xdg_dir(), None)
320 nt.assert_equal(path.get_xdg_dir(), None)
322
321
323 @with_environment
322 @with_environment
324 def test_get_xdg_dir_2():
323 def test_get_xdg_dir_2():
325 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
324 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
326 reload(path)
325 reload(path)
327 path.get_home_dir = lambda : HOME_TEST_DIR
326 path.get_home_dir = lambda : HOME_TEST_DIR
328 os.name = "posix"
327 os.name = "posix"
329 sys.platform = "linux2"
328 sys.platform = "linux2"
330 env.pop('IPYTHON_DIR', None)
329 env.pop('IPYTHON_DIR', None)
331 env.pop('IPYTHONDIR', None)
330 env.pop('IPYTHONDIR', None)
332 env.pop('XDG_CONFIG_HOME', None)
331 env.pop('XDG_CONFIG_HOME', None)
333 cfgdir=os.path.join(path.get_home_dir(), '.config')
332 cfgdir=os.path.join(path.get_home_dir(), '.config')
334 if not os.path.exists(cfgdir):
333 if not os.path.exists(cfgdir):
335 os.makedirs(cfgdir)
334 os.makedirs(cfgdir)
336
335
337 nt.assert_equal(path.get_xdg_dir(), cfgdir)
336 nt.assert_equal(path.get_xdg_dir(), cfgdir)
338
337
339 @with_environment
338 @with_environment
340 def test_get_xdg_dir_3():
339 def test_get_xdg_dir_3():
341 """test_get_xdg_dir_3, check xdg_dir not used on OS X"""
340 """test_get_xdg_dir_3, check xdg_dir not used on OS X"""
342 reload(path)
341 reload(path)
343 path.get_home_dir = lambda : HOME_TEST_DIR
342 path.get_home_dir = lambda : HOME_TEST_DIR
344 os.name = "posix"
343 os.name = "posix"
345 sys.platform = "darwin"
344 sys.platform = "darwin"
346 env.pop('IPYTHON_DIR', None)
345 env.pop('IPYTHON_DIR', None)
347 env.pop('IPYTHONDIR', None)
346 env.pop('IPYTHONDIR', None)
348 env.pop('XDG_CONFIG_HOME', None)
347 env.pop('XDG_CONFIG_HOME', None)
349 cfgdir=os.path.join(path.get_home_dir(), '.config')
348 cfgdir=os.path.join(path.get_home_dir(), '.config')
350 if not os.path.exists(cfgdir):
349 if not os.path.exists(cfgdir):
351 os.makedirs(cfgdir)
350 os.makedirs(cfgdir)
352
351
353 nt.assert_equal(path.get_xdg_dir(), None)
352 nt.assert_equal(path.get_xdg_dir(), None)
354
353
355 def test_filefind():
354 def test_filefind():
356 """Various tests for filefind"""
355 """Various tests for filefind"""
357 f = tempfile.NamedTemporaryFile()
356 f = tempfile.NamedTemporaryFile()
358 # print 'fname:',f.name
357 # print 'fname:',f.name
359 alt_dirs = path.get_ipython_dir()
358 alt_dirs = path.get_ipython_dir()
360 t = path.filefind(f.name, alt_dirs)
359 t = path.filefind(f.name, alt_dirs)
361 # print 'found:',t
360 # print 'found:',t
362
361
363
362
364 def test_get_ipython_package_dir():
363 def test_get_ipython_package_dir():
365 ipdir = path.get_ipython_package_dir()
364 ipdir = path.get_ipython_package_dir()
366 nt.assert_true(os.path.isdir(ipdir))
365 nt.assert_true(os.path.isdir(ipdir))
367
366
368
367
369 def test_get_ipython_module_path():
368 def test_get_ipython_module_path():
370 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
369 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
371 nt.assert_true(os.path.isfile(ipapp_path))
370 nt.assert_true(os.path.isfile(ipapp_path))
372
371
373
372
374 @dec.skip_if_not_win32
373 @dec.skip_if_not_win32
375 def test_get_long_path_name_win32():
374 def test_get_long_path_name_win32():
376 p = path.get_long_path_name('c:\\docume~1')
375 p = path.get_long_path_name('c:\\docume~1')
377 nt.assert_equals(p,u'c:\\Documents and Settings')
376 nt.assert_equals(p,u'c:\\Documents and Settings')
378
377
379
378
380 @dec.skip_win32
379 @dec.skip_win32
381 def test_get_long_path_name():
380 def test_get_long_path_name():
382 p = path.get_long_path_name('/usr/local')
381 p = path.get_long_path_name('/usr/local')
383 nt.assert_equals(p,'/usr/local')
382 nt.assert_equals(p,'/usr/local')
384
383
385 @dec.skip_win32 # can't create not-user-writable dir on win
384 @dec.skip_win32 # can't create not-user-writable dir on win
386 @with_environment
385 @with_environment
387 def test_not_writable_ipdir():
386 def test_not_writable_ipdir():
388 tmpdir = tempfile.mkdtemp()
387 tmpdir = tempfile.mkdtemp()
389 os.name = "posix"
388 os.name = "posix"
390 env.pop('IPYTHON_DIR', None)
389 env.pop('IPYTHON_DIR', None)
391 env.pop('IPYTHONDIR', None)
390 env.pop('IPYTHONDIR', None)
392 env.pop('XDG_CONFIG_HOME', None)
391 env.pop('XDG_CONFIG_HOME', None)
393 env['HOME'] = tmpdir
392 env['HOME'] = tmpdir
394 ipdir = os.path.join(tmpdir, '.ipython')
393 ipdir = os.path.join(tmpdir, '.ipython')
395 os.mkdir(ipdir)
394 os.mkdir(ipdir)
396 os.chmod(ipdir, 600)
395 os.chmod(ipdir, 600)
397 with AssertPrints('is not a writable location', channel='stderr'):
396 with AssertPrints('is not a writable location', channel='stderr'):
398 ipdir = path.get_ipython_dir()
397 ipdir = path.get_ipython_dir()
399 env.pop('IPYTHON_DIR', None)
398 env.pop('IPYTHON_DIR', None)
400
399
401 def test_unquote_filename():
400 def test_unquote_filename():
402 for win32 in (True, False):
401 for win32 in (True, False):
403 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
402 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
404 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
403 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
405 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
404 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
406 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
405 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
407 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
406 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
408 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
407 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
409 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
408 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
410 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
409 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
411 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
410 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
412 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
411 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
413
412
414 @with_environment
413 @with_environment
415 def test_get_py_filename():
414 def test_get_py_filename():
416 os.chdir(TMP_TEST_DIR)
415 os.chdir(TMP_TEST_DIR)
417 for win32 in (True, False):
416 for win32 in (True, False):
418 with make_tempfile('foo.py'):
417 with make_tempfile('foo.py'):
419 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
418 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
420 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
419 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
421 with make_tempfile('foo'):
420 with make_tempfile('foo'):
422 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
421 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
423 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
422 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
424 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
423 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
425 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
424 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
426 true_fn = 'foo with spaces.py'
425 true_fn = 'foo with spaces.py'
427 with make_tempfile(true_fn):
426 with make_tempfile(true_fn):
428 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
427 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
429 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
428 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
430 if win32:
429 if win32:
431 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
430 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
432 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
431 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
433 else:
432 else:
434 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
433 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
435 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
434 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
436
435
437 def test_unicode_in_filename():
436 def test_unicode_in_filename():
438 """When a file doesn't exist, the exception raised should be safe to call
437 """When a file doesn't exist, the exception raised should be safe to call
439 str() on - i.e. in Python 2 it must only have ASCII characters.
438 str() on - i.e. in Python 2 it must only have ASCII characters.
440
439
441 https://github.com/ipython/ipython/issues/875
440 https://github.com/ipython/ipython/issues/875
442 """
441 """
443 try:
442 try:
444 # these calls should not throw unicode encode exceptions
443 # these calls should not throw unicode encode exceptions
445 path.get_py_filename(u'fooéè.py', force_win32=False)
444 path.get_py_filename(u'fooéè.py', force_win32=False)
446 except IOError as ex:
445 except IOError as ex:
447 str(ex)
446 str(ex)
General Comments 0
You need to be logged in to leave comments. Login now