Show More
@@ -16,6 +16,7 b' Utilities for path handling.' | |||
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import sys |
|
19 | import tempfile | |
|
19 | 20 | from hashlib import md5 |
|
20 | 21 | |
|
21 | 22 | import IPython |
@@ -322,6 +323,19 b' def get_ipython_dir():' | |||
|
322 | 323 | ipdir = home_ipdir |
|
323 | 324 | |
|
324 | 325 | ipdir = os.path.normpath(os.path.expanduser(ipdir)) |
|
326 | ||
|
327 | if os.path.exists(ipdir) and not _writable_dir(ipdir): | |
|
328 | # ipdir exists, but is not writable | |
|
329 | warn.warn("IPython dir '%s' is not a writable location," | |
|
330 | " using a temp directory."%ipdir) | |
|
331 | ipdir = tempfile.mkdtemp() | |
|
332 | elif not os.path.exists(ipdir): | |
|
333 | parent = ipdir.rsplit(os.path.sep, 1)[0] | |
|
334 | if not _writable_dir(parent): | |
|
335 | # ipdir does not exist and parent isn't writable | |
|
336 | warn.warn("IPython parent '%s' is not a writable location," | |
|
337 | " using a temp directory."%parent) | |
|
338 | ipdir = tempfile.mkdtemp() | |
|
325 | 339 | |
|
326 | 340 | return _cast_unicode(ipdir, fs_encoding) |
|
327 | 341 |
@@ -16,6 +16,7 b' import os' | |||
|
16 | 16 | import shutil |
|
17 | 17 | import sys |
|
18 | 18 | import tempfile |
|
19 | import StringIO | |
|
19 | 20 | |
|
20 | 21 | from os.path import join, abspath, split |
|
21 | 22 | |
@@ -26,7 +27,7 b' from nose import with_setup' | |||
|
26 | 27 | import IPython |
|
27 | 28 | from IPython.testing import decorators as dec |
|
28 | 29 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
29 | from IPython.utils import path | |
|
30 | from IPython.utils import path, io | |
|
30 | 31 | |
|
31 | 32 | # Platform-dependent imports |
|
32 | 33 | try: |
@@ -92,7 +93,8 b' def teardown_environment():' | |||
|
92 | 93 | """Restore things that were remebered by the setup_environment function |
|
93 | 94 | """ |
|
94 | 95 | (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff |
|
95 | ||
|
96 | reload(path) | |
|
97 | ||
|
96 | 98 | for key in env.keys(): |
|
97 | 99 | if key not in oldenv: |
|
98 | 100 | del env[key] |
@@ -229,6 +231,7 b' def test_get_home_dir_8():' | |||
|
229 | 231 | def test_get_ipython_dir_1(): |
|
230 | 232 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
231 | 233 | env_ipdir = os.path.join("someplace", ".ipython") |
|
234 | path._writable_dir = lambda path: True | |
|
232 | 235 | env['IPYTHON_DIR'] = env_ipdir |
|
233 | 236 | ipdir = path.get_ipython_dir() |
|
234 | 237 | nt.assert_equal(ipdir, env_ipdir) |
@@ -238,6 +241,8 b' def test_get_ipython_dir_1():' | |||
|
238 | 241 | def test_get_ipython_dir_2(): |
|
239 | 242 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
240 | 243 | path.get_home_dir = lambda : "someplace" |
|
244 | path.get_xdg_dir = lambda : None | |
|
245 | path._writable_dir = lambda path: True | |
|
241 | 246 | os.name = "posix" |
|
242 | 247 | env.pop('IPYTHON_DIR', None) |
|
243 | 248 | env.pop('IPYTHONDIR', None) |
@@ -249,6 +254,7 b' def test_get_ipython_dir_2():' | |||
|
249 | 254 | def test_get_ipython_dir_3(): |
|
250 | 255 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
251 | 256 | path.get_home_dir = lambda : "someplace" |
|
257 | path._writable_dir = lambda path: True | |
|
252 | 258 | os.name = "posix" |
|
253 | 259 | env.pop('IPYTHON_DIR', None) |
|
254 | 260 | env.pop('IPYTHONDIR', None) |
@@ -283,18 +289,23 b' def test_get_ipython_dir_5():' | |||
|
283 | 289 | @with_environment |
|
284 | 290 | def test_get_ipython_dir_6(): |
|
285 | 291 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
286 | path.get_home_dir = lambda : 'somehome' | |
|
287 | path.get_xdg_dir = lambda : 'somexdg' | |
|
292 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | |
|
293 | os.mkdir(xdg) | |
|
294 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | |
|
295 | path.get_home_dir = lambda : HOME_TEST_DIR | |
|
296 | path.get_xdg_dir = lambda : xdg | |
|
288 | 297 | os.name = "posix" |
|
289 | 298 | env.pop('IPYTHON_DIR', None) |
|
290 | 299 | env.pop('IPYTHONDIR', None) |
|
291 | xdg_ipdir = os.path.join("somexdg", "ipython") | |
|
300 | env.pop('XDG_CONFIG_HOME', None) | |
|
301 | xdg_ipdir = os.path.join(xdg, "ipython") | |
|
292 | 302 | ipdir = path.get_ipython_dir() |
|
293 | 303 | nt.assert_equal(ipdir, xdg_ipdir) |
|
294 | 304 | |
|
295 | 305 | @with_environment |
|
296 | 306 | def test_get_ipython_dir_7(): |
|
297 | 307 | """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR""" |
|
308 | path._writable_dir = lambda path: True | |
|
298 | 309 | home_dir = os.path.expanduser('~') |
|
299 | 310 | env['IPYTHON_DIR'] = os.path.join('~', 'somewhere') |
|
300 | 311 | ipdir = path.get_ipython_dir() |
@@ -305,6 +316,7 b' def test_get_ipython_dir_7():' | |||
|
305 | 316 | def test_get_xdg_dir_1(): |
|
306 | 317 | """test_get_xdg_dir_1, check xdg_dir""" |
|
307 | 318 | reload(path) |
|
319 | path._writable_dir = lambda path: True | |
|
308 | 320 | path.get_home_dir = lambda : 'somewhere' |
|
309 | 321 | os.name = "posix" |
|
310 | 322 | env.pop('IPYTHON_DIR', None) |
@@ -369,3 +381,23 b' def test_get_long_path_name():' | |||
|
369 | 381 | p = path.get_long_path_name('/usr/local') |
|
370 | 382 | nt.assert_equals(p,'/usr/local') |
|
371 | 383 | |
|
384 | @with_environment | |
|
385 | def test_not_writable_ipdir(): | |
|
386 | tmpdir = tempfile.mkdtemp() | |
|
387 | os.name = "posix" | |
|
388 | env.pop('IPYTHON_DIR', None) | |
|
389 | env.pop('IPYTHONDIR', None) | |
|
390 | env.pop('XDG_CONFIG_HOME', None) | |
|
391 | env['HOME'] = tmpdir | |
|
392 | ipdir = os.path.join(tmpdir, '.ipython') | |
|
393 | os.mkdir(ipdir) | |
|
394 | os.chmod(ipdir, 600) | |
|
395 | stderr = io.stderr | |
|
396 | pipe = StringIO.StringIO() | |
|
397 | io.stderr = pipe | |
|
398 | ipdir = path.get_ipython_dir() | |
|
399 | io.stderr.flush() | |
|
400 | io.stderr = stderr | |
|
401 | nt.assert_true('WARNING' in pipe.getvalue()) | |
|
402 | env.pop('IPYTHON_DIR', None) | |
|
403 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now