Skip to content

Commit

Permalink
Unix: Fallback to default if XDG environment variable is empty (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
papr authored Jul 29, 2021
1 parent f9c2074 commit 0a61690
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
platformdirs Changelog
======================

platformdirs 2.2.0
------------------
- Unix: Fallback to default if XDG environment variable is empty

platformdirs 2.1.0
------------------
- Add ``readthedocs.org`` documentation via Sphinx
Expand Down
30 changes: 12 additions & 18 deletions src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def user_data_dir(self) -> str:
:return: data directory tied to the user, e.g. ``~/.local/share/$appname/$version`` or
``$XDG_DATA_HOME/$appname/$version``
"""
if "XDG_DATA_HOME" in os.environ:
path = os.environ["XDG_DATA_HOME"]
else:
path = os.environ.get("XDG_DATA_HOME", "")
if not path.strip():
path = os.path.expanduser("~/.local/share")
return self._append_app_name_and_version(path)

Expand All @@ -36,9 +35,8 @@ def site_data_dir(self) -> str:
path separator), e.g. ``/usr/local/share/$appname/$version`` or ``/usr/share/$appname/$version``
"""
# XDG default for $XDG_DATA_DIRS; only first, if multipath is False
if "XDG_DATA_DIRS" in os.environ:
path = os.environ["XDG_DATA_DIRS"]
else:
path = os.environ.get("XDG_DATA_DIRS", "")
if not path.strip():
path = f"/usr/local/share{os.pathsep}/usr/share"
return self._with_multi_path(path)

Expand All @@ -55,9 +53,8 @@ def user_config_dir(self) -> str:
:return: config directory tied to the user, e.g. ``~/.config/$appname/$version`` or
``$XDG_CONFIG_HOME/$appname/$version``
"""
if "XDG_CONFIG_HOME" in os.environ:
path = os.environ["XDG_CONFIG_HOME"]
else:
path = os.environ.get("XDG_CONFIG_HOME", "")
if not path.strip():
path = os.path.expanduser("~/.config")
return self._append_app_name_and_version(path)

Expand All @@ -69,9 +66,8 @@ def site_config_dir(self) -> str:
path separator), e.g. ``/etc/xdg/$appname/$version``
"""
# XDG default for $XDG_CONFIG_DIRS only first, if multipath is False
if "XDG_CONFIG_DIRS" in os.environ:
path = os.environ["XDG_CONFIG_DIRS"]
else:
path = os.environ.get("XDG_CONFIG_DIRS", "")
if not path.strip():
path = "/etc/xdg"
return self._with_multi_path(path)

Expand All @@ -81,9 +77,8 @@ def user_cache_dir(self) -> str:
:return: cache directory tied to the user, e.g. ``~/.cache/$appname/$version`` or
``~/$XDG_CACHE_HOME/$appname/$version``
"""
if "XDG_CACHE_HOME" in os.environ:
path = os.environ["XDG_CACHE_HOME"]
else:
path = os.environ.get("XDG_CACHE_HOME", "")
if not path.strip():
path = os.path.expanduser("~/.cache")
return self._append_app_name_and_version(path)

Expand All @@ -93,9 +88,8 @@ def user_state_dir(self) -> str:
:return: state directory tied to the user, e.g. ``~/.local/state/$appname/$version`` or
``$XDG_STATE_HOME/$appname/$version``
"""
if "XDG_STATE_HOME" in os.environ:
path = os.environ["XDG_STATE_HOME"]
else:
path = os.environ.get("XDG_STATE_HOME", "")
if not path.strip():
path = os.path.expanduser("~/.local/state")
return self._append_app_name_and_version(path)

Expand Down
51 changes: 51 additions & 0 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import typing

import pytest
from _pytest.monkeypatch import MonkeyPatch

from platformdirs.unix import Unix


class XDGVariable(typing.NamedTuple):
name: str
default_value: str


def _func_to_path(func: str) -> XDGVariable:
mapping = {
"user_data_dir": XDGVariable("XDG_DATA_HOME", "~/.local/share"),
"site_data_dir": XDGVariable("XDG_DATA_DIRS", f"/usr/local/share{os.pathsep}/usr/share"),
"user_config_dir": XDGVariable("XDG_CONFIG_HOME", "~/.config"),
"site_config_dir": XDGVariable("XDG_CONFIG_DIRS", "/etc/xdg"),
"user_cache_dir": XDGVariable("XDG_CACHE_HOME", "~/.cache"),
"user_state_dir": XDGVariable("XDG_STATE_HOME", "~/.local/state"),
"user_log_dir": XDGVariable("XDG_CACHE_HOME", "~/.cache"),
}
return mapping[func]


@pytest.fixture()
def dirs_instance() -> Unix:
return Unix(multipath=True, opinion=False)


def test_xdg_variable_not_set(monkeypatch: MonkeyPatch, dirs_instance: Unix, func: str) -> None:
xdg_variable = _func_to_path(func)
monkeypatch.delenv(xdg_variable.name, raising=False)
result = getattr(dirs_instance, func)
assert result == os.path.expanduser(xdg_variable.default_value)


def test_xdg_variable_empty_value(monkeypatch: MonkeyPatch, dirs_instance: Unix, func: str) -> None:
xdg_variable = _func_to_path(func)
monkeypatch.setenv(xdg_variable.name, "")
result = getattr(dirs_instance, func)
assert result == os.path.expanduser(xdg_variable.default_value)


def test_xdg_variable_custom_value(monkeypatch: MonkeyPatch, dirs_instance: Unix, func: str) -> None:
xdg_variable = _func_to_path(func)
monkeypatch.setenv(xdg_variable.name, "/tmp/custom-dir")
result = getattr(dirs_instance, func)
assert result == "/tmp/custom-dir"
3 changes: 0 additions & 3 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ Dirs
func
highbit
HKEY
impl
intersphinx
isfunction
jnius
kernel32
lru
macos
multipath
normpath
ord
Expand All @@ -28,7 +26,6 @@ pathlib
pathsep
platformdirs
pyjnius
Runtime
setenv
shell32
typehints
Expand Down

0 comments on commit 0a61690

Please sign in to comment.