From c60c728ebabb0de043c78f6c6cb3393ad95e6656 Mon Sep 17 00:00:00 2001 From: Kemal Zebari Date: Tue, 20 Jun 2023 16:38:31 -0700 Subject: [PATCH] Have user_runtime_dir return /var/run/user/uid for *BSD Instead of /run/user since /run is not a directory known to FreeBSD, OpenBSD, and NetBSD. --- src/platformdirs/unix.py | 10 ++++++++-- tests/test_unix.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index cd55058..c93bb48 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -150,11 +150,17 @@ def user_music_dir(self) -> str: def user_runtime_dir(self) -> str: """ :return: runtime directory tied to the user, e.g. ``/run/user/$(id -u)/$appname/$version`` or - ``$XDG_RUNTIME_DIR/$appname/$version`` + ``$XDG_RUNTIME_DIR/$appname/$version``. + + For FreeBSD/OpenBSD/NetBSD, it would return ``/var/run/user/$(id -u)/$appname/$version`` if + ``$XDG_RUNTIME_DIR`` is not set. """ path = os.environ.get("XDG_RUNTIME_DIR", "") if not path.strip(): - path = f"/run/user/{getuid()}" + if sys.platform.startswith(("freebsd", "openbsd", "netbsd")): + path = f"/var/run/user/{getuid()}" + else: + path = f"/run/user/{getuid()}" return self._append_app_name_and_version(path) @property diff --git a/tests/test_unix.py b/tests/test_unix.py index 0ef607d..196ef6b 100644 --- a/tests/test_unix.py +++ b/tests/test_unix.py @@ -142,6 +142,20 @@ def test_xdg_variable_custom_value(monkeypatch: pytest.MonkeyPatch, dirs_instanc assert result == "/custom-dir" +@pytest.mark.usefixtures("_getuid") +def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None: + monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False) + mocker.patch("sys.platform", "freebsd") + expected_path = "/var/run/user/1234" + assert Unix().user_runtime_dir == expected_path + + mocker.patch("sys.platform", "openbsd") + assert Unix().user_runtime_dir == expected_path + + mocker.patch("sys.platform", "netbsd") + assert Unix().user_runtime_dir == expected_path + + def test_platform_on_win32(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None: monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False) mocker.patch("sys.platform", "win32")