test_paths.py
200 lines
| 6.9 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r21041 | import errno | ||
import os | ||||
import shutil | ||||
import tempfile | ||||
import warnings | ||||
Srinivas Reddy Thatiparthy
|
r23064 | from unittest.mock import patch | ||
Thomas Kluyver
|
r21041 | |||
Matthias Bussonnier
|
r27509 | from tempfile import TemporaryDirectory | ||
from testpath import assert_isdir, assert_isfile, modified_env | ||||
Thomas Kluyver
|
r21041 | |||
from IPython import paths | ||||
from IPython.testing.decorators import skip_win32 | ||||
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 | |||
Samuel Gaist
|
r26905 | assert 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 | |||
Samuel Gaist
|
r26905 | assert ipdir == os.path.join("someplace", ".ipython") | ||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_dir_3(): | ||||
Julien Rabinow
|
r26956 | """test_get_ipython_dir_3, use XDG if defined and exists, and .ipython doesn't exist.""" | ||
Thomas Kluyver
|
r21041 | 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 | |||
Julien Rabinow
|
r26956 | assert ipdir == os.path.join(tmphome.name, XDG_TEST_DIR, "ipython") | ||
assert len(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() | ||
Julien Rabinow
|
r26956 | assert len(w) == 1 | ||
assert "Ignoring" in str(w[0]) | ||||
Samuel Gaist
|
r26905 | |||
Thomas Kluyver
|
r21041 | |||
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() | ||||
Samuel Gaist
|
r26905 | assert ipdir == IP_TEST_DIR | ||
Thomas Kluyver
|
r21041 | |||
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 | |||
Samuel Gaist
|
r26905 | assert ipdir == os.path.join(HOME_TEST_DIR, ".ipython") | ||
assert 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() | ||
Samuel Gaist
|
r26905 | assert ipdir == os.path.join(home_dir, "somewhere") | ||
Thomas Kluyver
|
r21041 | |||
@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 | ||||
Samuel Gaist
|
r26905 | 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": "/", | ||||
} | ||||
): | ||||
assert paths.get_ipython_dir() == "/.ipython" | ||||
Thomas Kluyver
|
r21041 | |||
def test_get_ipython_cache_dir(): | ||||
Thomas Kluyver
|
r21332 | with modified_env({'HOME': HOME_TEST_DIR}): | ||
Julien Rabinow
|
r26956 | if os.name == "posix": | ||
Thomas Kluyver
|
r21332 | # test default | ||
os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | ||||
with modified_env({'XDG_CACHE_HOME': None}): | ||||
ipdir = paths.get_ipython_cache_dir() | ||||
Samuel Gaist
|
r26905 | assert 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) | ||
Samuel Gaist
|
r26905 | assert ipdir == os.path.join(XDG_CACHE_DIR, "ipython") | ||
Thomas Kluyver
|
r21332 | else: | ||
Samuel Gaist
|
r26905 | assert 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) | ||