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