test_paths.py
204 lines
| 7.3 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r21041 | import errno | ||
import os | ||||
import shutil | ||||
import sys | ||||
import tempfile | ||||
import warnings | ||||
Srinivas Reddy Thatiparthy
|
r23064 | from unittest.mock import patch | ||
Thomas Kluyver
|
r21041 | |||
import nose.tools as nt | ||||
Thomas Kluyver
|
r21334 | from testpath import modified_env, assert_isdir, assert_isfile | ||
Thomas Kluyver
|
r21041 | |||
from IPython import paths | ||||
from IPython.testing.decorators import skip_win32 | ||||
from IPython.utils.tempdir import TemporaryDirectory | ||||
Kenneth Hoste
|
r22812 | TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp()) | ||
Thomas Kluyver
|
r21041 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") | ||
XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") | ||||
XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") | ||||
IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') | ||||
Matthias Bussonnier
|
r25082 | def setup_module(): | ||
Thomas Kluyver
|
r21041 | """Setup testenvironment for the module: | ||
- Adds dummy home dir tree | ||||
""" | ||||
# Do not mask exceptions here. In particular, catching WindowsError is a | ||||
# problem because that exception is only defined on Windows... | ||||
os.makedirs(IP_TEST_DIR) | ||||
os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | ||||
os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | ||||
Matthias Bussonnier
|
r25082 | def teardown_module(): | ||
Thomas Kluyver
|
r21041 | """Teardown testenvironment for the module: | ||
- Remove dummy home dir tree | ||||
""" | ||||
# Note: we remove the parent test dir, which is the root of all test | ||||
# subdirs we may have created. Use shutil instead of os.removedirs, so | ||||
# that non-empty directories are all recursively removed. | ||||
shutil.rmtree(TMP_TEST_DIR) | ||||
def patch_get_home_dir(dirpath): | ||||
Thomas Kluyver
|
r21333 | return patch.object(paths, 'get_home_dir', return_value=dirpath) | ||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_dir_1(): | ||||
"""test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||||
env_ipdir = os.path.join("someplace", ".ipython") | ||||
Thomas Kluyver
|
r21333 | with patch.object(paths, '_writable_dir', return_value=True), \ | ||
modified_env({'IPYTHONDIR': env_ipdir}): | ||||
Thomas Kluyver
|
r21332 | ipdir = paths.get_ipython_dir() | ||
Thomas Kluyver
|
r21041 | |||
Thomas Kluyver
|
r21332 | nt.assert_equal(ipdir, env_ipdir) | ||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_dir_2(): | ||||
"""test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||||
Thomas Kluyver
|
r21333 | with patch_get_home_dir('someplace'), \ | ||
patch.object(paths, 'get_xdg_dir', return_value=None), \ | ||||
patch.object(paths, '_writable_dir', return_value=True), \ | ||||
patch('os.name', "posix"), \ | ||||
modified_env({'IPYTHON_DIR': None, | ||||
'IPYTHONDIR': None, | ||||
'XDG_CONFIG_HOME': None | ||||
}): | ||||
ipdir = paths.get_ipython_dir() | ||||
Thomas Kluyver
|
r21332 | |||
Thomas Kluyver
|
r21333 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | ||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_dir_3(): | ||||
"""test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" | ||||
tmphome = TemporaryDirectory() | ||||
try: | ||||
Thomas Kluyver
|
r21333 | with patch_get_home_dir(tmphome.name), \ | ||
patch('os.name', 'posix'), \ | ||||
modified_env({ | ||||
'IPYTHON_DIR': None, | ||||
'IPYTHONDIR': None, | ||||
'XDG_CONFIG_HOME': XDG_TEST_DIR, | ||||
}), warnings.catch_warnings(record=True) as w: | ||||
ipdir = paths.get_ipython_dir() | ||||
Thomas Kluyver
|
r21041 | |||
Thomas Kluyver
|
r21333 | nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) | ||
if sys.platform != 'darwin': | ||||
nt.assert_equal(len(w), 1) | ||||
nt.assert_in('Moving', str(w[0])) | ||||
Thomas Kluyver
|
r21041 | finally: | ||
tmphome.cleanup() | ||||
def test_get_ipython_dir_4(): | ||||
"""test_get_ipython_dir_4, warn if XDG and home both exist.""" | ||||
Thomas Kluyver
|
r21333 | with patch_get_home_dir(HOME_TEST_DIR), \ | ||
patch('os.name', 'posix'): | ||||
Thomas Kluyver
|
r21041 | try: | ||
os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) | ||||
except OSError as e: | ||||
if e.errno != errno.EEXIST: | ||||
raise | ||||
Thomas Kluyver
|
r21332 | |||
with modified_env({ | ||||
'IPYTHON_DIR': None, | ||||
'IPYTHONDIR': None, | ||||
'XDG_CONFIG_HOME': XDG_TEST_DIR, | ||||
}), warnings.catch_warnings(record=True) as w: | ||||
Thomas Kluyver
|
r21041 | ipdir = paths.get_ipython_dir() | ||
nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) | ||||
if sys.platform != 'darwin': | ||||
nt.assert_equal(len(w), 1) | ||||
nt.assert_in('Ignoring', str(w[0])) | ||||
def test_get_ipython_dir_5(): | ||||
"""test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | ||||
Thomas Kluyver
|
r21333 | with patch_get_home_dir(HOME_TEST_DIR), \ | ||
patch('os.name', 'posix'): | ||||
Thomas Kluyver
|
r21041 | try: | ||
os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | ||||
except OSError as e: | ||||
if e.errno != errno.ENOENT: | ||||
raise | ||||
Thomas Kluyver
|
r21332 | |||
with modified_env({ | ||||
'IPYTHON_DIR': None, | ||||
'IPYTHONDIR': None, | ||||
'XDG_CONFIG_HOME': XDG_TEST_DIR, | ||||
}): | ||||
ipdir = paths.get_ipython_dir() | ||||
Thomas Kluyver
|
r21041 | nt.assert_equal(ipdir, IP_TEST_DIR) | ||
def test_get_ipython_dir_6(): | ||||
"""test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" | ||||
xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | ||||
os.mkdir(xdg) | ||||
shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | ||||
Thomas Kluyver
|
r21333 | print(paths._writable_dir) | ||
with patch_get_home_dir(HOME_TEST_DIR), \ | ||||
patch.object(paths, 'get_xdg_dir', return_value=xdg), \ | ||||
patch('os.name', 'posix'), \ | ||||
modified_env({ | ||||
Thomas Kluyver
|
r21332 | 'IPYTHON_DIR': None, | ||
'IPYTHONDIR': None, | ||||
'XDG_CONFIG_HOME': None, | ||||
}), warnings.catch_warnings(record=True) as w: | ||||
Thomas Kluyver
|
r21333 | ipdir = paths.get_ipython_dir() | ||
Thomas Kluyver
|
r21041 | |||
Thomas Kluyver
|
r21333 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) | ||
nt.assert_equal(len(w), 0) | ||||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_dir_7(): | ||||
"""test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" | ||||
home_dir = os.path.normpath(os.path.expanduser('~')) | ||||
Thomas Kluyver
|
r21333 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ | ||
patch.object(paths, '_writable_dir', return_value=True): | ||||
Thomas Kluyver
|
r21332 | ipdir = paths.get_ipython_dir() | ||
Thomas Kluyver
|
r21041 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) | ||
@skip_win32 | ||||
def test_get_ipython_dir_8(): | ||||
"""test_get_ipython_dir_8, test / home directory""" | ||||
Bibo Hao
|
r26839 | if not os.access("/", os.W_OK): | ||
Bibo Hao
|
r26837 | # test only when HOME directory actually writable | ||
return | ||||
Thomas Kluyver
|
r21333 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ | ||
patch.object(paths, 'get_xdg_dir', return_value=None), \ | ||||
modified_env({ | ||||
'IPYTHON_DIR': None, | ||||
'IPYTHONDIR': None, | ||||
'HOME': '/', | ||||
}): | ||||
nt.assert_equal(paths.get_ipython_dir(), '/.ipython') | ||||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_cache_dir(): | ||||
Thomas Kluyver
|
r21332 | with modified_env({'HOME': HOME_TEST_DIR}): | ||
if os.name == 'posix' and sys.platform != 'darwin': | ||||
# test default | ||||
os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | ||||
with modified_env({'XDG_CACHE_HOME': None}): | ||||
ipdir = paths.get_ipython_cache_dir() | ||||
nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | ||||
ipdir) | ||||
Thomas Kluyver
|
r21334 | assert_isdir(ipdir) | ||
Thomas Kluyver
|
r21332 | |||
# test env override | ||||
with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): | ||||
ipdir = paths.get_ipython_cache_dir() | ||||
Thomas Kluyver
|
r21334 | assert_isdir(ipdir) | ||
Thomas Kluyver
|
r21332 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | ||
else: | ||||
nt.assert_equal(paths.get_ipython_cache_dir(), | ||||
paths.get_ipython_dir()) | ||||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_package_dir(): | ||||
ipdir = paths.get_ipython_package_dir() | ||||
Thomas Kluyver
|
r21334 | assert_isdir(ipdir) | ||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_module_path(): | ||||
ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') | ||||
Thomas Kluyver
|
r21334 | assert_isfile(ipapp_path) | ||