Skip to content

Commit a02fb04

Browse files
authored
Have user_runtime_dir return /var/run/user/uid for *BSD (#194)
1 parent 23d3dca commit a02fb04

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/platformdirs/unix.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,17 @@ def user_music_dir(self) -> str:
150150
def user_runtime_dir(self) -> str:
151151
"""
152152
:return: runtime directory tied to the user, e.g. ``/run/user/$(id -u)/$appname/$version`` or
153-
``$XDG_RUNTIME_DIR/$appname/$version``
153+
``$XDG_RUNTIME_DIR/$appname/$version``.
154+
155+
For FreeBSD/OpenBSD/NetBSD, it would return ``/var/run/user/$(id -u)/$appname/$version`` if
156+
``$XDG_RUNTIME_DIR`` is not set.
154157
"""
155158
path = os.environ.get("XDG_RUNTIME_DIR", "")
156159
if not path.strip():
157-
path = f"/run/user/{getuid()}"
160+
if sys.platform.startswith(("freebsd", "openbsd", "netbsd")):
161+
path = f"/var/run/user/{getuid()}"
162+
else:
163+
path = f"/run/user/{getuid()}"
158164
return self._append_app_name_and_version(path)
159165

160166
@property

tests/test_unix.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ def test_xdg_variable_custom_value(monkeypatch: pytest.MonkeyPatch, dirs_instanc
142142
assert result == "/custom-dir"
143143

144144

145+
@pytest.mark.usefixtures("_getuid")
146+
def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
147+
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
148+
mocker.patch("sys.platform", "freebsd")
149+
expected_path = "/var/run/user/1234"
150+
assert Unix().user_runtime_dir == expected_path
151+
152+
mocker.patch("sys.platform", "openbsd")
153+
assert Unix().user_runtime_dir == expected_path
154+
155+
mocker.patch("sys.platform", "netbsd")
156+
assert Unix().user_runtime_dir == expected_path
157+
158+
145159
def test_platform_on_win32(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
146160
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
147161
mocker.patch("sys.platform", "win32")

0 commit comments

Comments
 (0)