Skip to content
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
3 changes: 2 additions & 1 deletion src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def _get_user_dirs_folder(key: str) -> str | None:
See https://freedesktop.org/wiki/Software/xdg-user-dirs/.

"""
user_dirs_config_path = Path(os.path.expanduser("~/.config")) / "user-dirs.dirs" # noqa: PTH111
config_home = os.environ.get("XDG_CONFIG_HOME", "").strip() or os.path.expanduser("~/.config") # noqa: PTH111
user_dirs_config_path = Path(config_home) / "user-dirs.dirs"
if user_dirs_config_path.exists():
parser = ConfigParser()

Expand Down
12 changes: 12 additions & 0 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ def test_user_media_dir_no_user_dirs_file(
assert Unix().user_documents_dir == "/nonexistent/path/Documents"


def test_user_dirs_respects_xdg_config_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("XDG_DOCUMENTS_DIR", raising=False)
custom_config = tmp_path / "custom_config"
custom_config.mkdir()
user_dirs_file = custom_config / "user-dirs.dirs"
user_dirs_file.write_text('XDG_DOCUMENTS_DIR="$HOME/CustomDocs"\n')
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setenv("USERPROFILE", str(tmp_path))
monkeypatch.setenv("XDG_CONFIG_HOME", str(custom_config))
assert Unix().user_documents_dir == f"{tmp_path}/CustomDocs"


_SITE_REDIRECT_CASES: list[tuple[str, str]] = [
("user_data_dir", os.path.join("/usr/local/share", "foo")), # noqa: PTH118
("user_config_dir", os.path.join("/etc/xdg", "foo")), # noqa: PTH118
Expand Down