Skip to content

Return canonical path when using %APPDATA% on NT #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jupyter_core/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import errno
import tempfile
import warnings
from pathlib import Path

from contextlib import contextmanager

Expand All @@ -37,7 +38,7 @@ def get_home_dir():
homedir = os.path.expanduser('~')
# Next line will make things work even when /home/ is a symlink to
# /usr/home as it is on FreeBSD, for example
homedir = os.path.realpath(homedir)
homedir = str(Path(homedir).resolve())
return homedir

_dtemps = {}
Expand Down Expand Up @@ -90,7 +91,7 @@ def jupyter_data_dir():
elif os.name == 'nt':
appdata = os.environ.get('APPDATA', None)
if appdata:
return pjoin(appdata, 'jupyter')
return str(Path(appdata, 'jupyter').resolve())
else:
return pjoin(jupyter_config_dir(), 'data')
else:
Expand Down
5 changes: 0 additions & 5 deletions jupyter_core/tests/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ def __exit__(self, *args):
patch.object(os, 'name', 'posix'),
patch.object(sys, 'platform', 'linux2'),
)

windows = MultiPatch(
patch.object(os, 'name', 'nt'),
patch.object(sys, 'platform', 'win32'),
)
44 changes: 23 additions & 21 deletions jupyter_core/tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
secure_write, is_hidden, is_file_hidden
)

from .mocking import darwin, windows, linux
from .mocking import darwin, linux

pjoin = os.path.join

Expand All @@ -36,8 +36,6 @@
'XDG_RUNTIME_DIR': '',
})

appdata = patch.dict('os.environ', {'APPDATA': 'appdata'})

no_config_env = patch.dict('os.environ', {
'JUPYTER_CONFIG_DIR': '',
'JUPYTER_DATA_DIR': '',
Expand All @@ -50,7 +48,7 @@


def realpath(path):
return os.path.realpath(os.path.expanduser(path))
return os.path.abspath(os.path.realpath(os.path.expanduser(path)))

home_jupyter = realpath('~/.jupyter')

Expand All @@ -65,6 +63,7 @@ def test_envset():
assert not paths.envset(f"FOO_{v}")
assert not paths.envset("THIS_VARIABLE_SHOULD_NOT_BE_SET")

@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_config_dir_darwin():
with darwin, no_config_env:
config = jupyter_config_dir()
Expand All @@ -74,23 +73,24 @@ def test_config_dir_darwin():
config = jupyter_config_dir()
assert config == jupyter_config_env


@pytest.mark.skipif(sys.platform != "win32", reason="only run on windows")
def test_config_dir_windows():
with windows, no_config_env:
with no_config_env:
config = jupyter_config_dir()
assert config == home_jupyter

with windows, config_env:
with config_env:
config = jupyter_config_dir()
assert config == jupyter_config_env


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_config_dir_linux():
with windows, no_config_env:
with linux, no_config_env:
config = jupyter_config_dir()
assert config == home_jupyter

with windows, config_env:
with linux, config_env:
config = jupyter_config_dir()
assert config == jupyter_config_env

Expand All @@ -102,6 +102,7 @@ def test_data_dir_env():
assert data == data_env


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_data_dir_darwin():
with darwin:
data = jupyter_data_dir()
Expand All @@ -112,18 +113,18 @@ def test_data_dir_darwin():
data = jupyter_data_dir()
assert data == realpath('~/Library/Jupyter')


@pytest.mark.skipif(sys.platform != "win32", reason="only run on windows")
def test_data_dir_windows():
with windows, appdata:
data = jupyter_data_dir()
assert data == pjoin('appdata', 'jupyter')
data = jupyter_data_dir()
assert data == realpath(pjoin(os.environ.get('APPDATA', None), 'jupyter'))

with windows, appdata, xdg:
with xdg:
# windows should ignore xdg
data = jupyter_data_dir()
assert data == pjoin('appdata', 'jupyter')
assert data == realpath(pjoin(os.environ.get('APPDATA', None), 'jupyter'))


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_data_dir_linux():
with linux, no_xdg:
data = jupyter_data_dir()
Expand All @@ -141,6 +142,7 @@ def test_runtime_dir_env():
assert runtime == rtd_env


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_runtime_dir_darwin():
with darwin:
runtime = jupyter_runtime_dir()
Expand All @@ -151,18 +153,18 @@ def test_runtime_dir_darwin():
runtime = jupyter_runtime_dir()
assert runtime == realpath('~/Library/Jupyter/runtime')


@pytest.mark.skipif(sys.platform != "win32", reason="only run on windows")
def test_runtime_dir_windows():
with windows, appdata:
runtime = jupyter_runtime_dir()
assert runtime == pjoin('appdata', 'jupyter', 'runtime')
runtime = jupyter_runtime_dir()
assert runtime == realpath(pjoin(os.environ.get('APPDATA', None), 'jupyter', 'runtime'))

with windows, appdata, xdg:
with xdg:
# windows should ignore xdg
runtime = jupyter_runtime_dir()
assert runtime == pjoin('appdata', 'jupyter', 'runtime')
assert runtime == realpath(pjoin(os.environ.get('APPDATA', None), 'jupyter', 'runtime'))


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_runtime_dir_linux():
with linux, no_xdg:
runtime = jupyter_runtime_dir()
Expand Down