diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 53cba33..0638700 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -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() diff --git a/tests/test_unix.py b/tests/test_unix.py index 10c8246..420b7a7 100644 --- a/tests/test_unix.py +++ b/tests/test_unix.py @@ -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