Skip to content

Commit 5f7eb47

Browse files
authored
BSD: provide a fallback for user_runtime_dir (#201)
1 parent efc8a76 commit 5f7eb47

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/platformdirs/unix.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,15 @@ def user_runtime_dir(self) -> str:
153153
``$XDG_RUNTIME_DIR/$appname/$version``.
154154
155155
For FreeBSD/OpenBSD/NetBSD, it would return ``/var/run/user/$(id -u)/$appname/$version`` if
156-
``$XDG_RUNTIME_DIR`` is not set.
156+
exists, otherwise ``/tmp/runtime-$(id -u)/$appname/$version``, if``$XDG_RUNTIME_DIR``
157+
is not set.
157158
"""
158159
path = os.environ.get("XDG_RUNTIME_DIR", "")
159160
if not path.strip():
160161
if sys.platform.startswith(("freebsd", "openbsd", "netbsd")):
161162
path = f"/var/run/user/{getuid()}"
163+
if not Path(path).exists():
164+
path = f"/tmp/runtime-{getuid()}" # noqa: S108
162165
else:
163166
path = f"/run/user/{getuid()}"
164167
return self._append_app_name_and_version(path)

tests/test_unix.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,16 @@ def test_xdg_variable_custom_value(monkeypatch: pytest.MonkeyPatch, dirs_instanc
143143

144144

145145
@pytest.mark.usefixtures("_getuid")
146-
def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
146+
@pytest.mark.parametrize("platform", ["freebsd", "openbsd", "netbsd"])
147+
def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture, platform: str) -> None:
147148
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
149+
mocker.patch("sys.platform", platform)
151150

152-
mocker.patch("sys.platform", "openbsd")
153-
assert Unix().user_runtime_dir == expected_path
151+
mocker.patch("pathlib.Path.exists", return_value=True)
152+
assert Unix().user_runtime_dir == "/var/run/user/1234"
154153

155-
mocker.patch("sys.platform", "netbsd")
156-
assert Unix().user_runtime_dir == expected_path
154+
mocker.patch("pathlib.Path.exists", return_value=False)
155+
assert Unix().user_runtime_dir == "/tmp/runtime-1234" # noqa: S108
157156

158157

159158
def test_platform_on_win32(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)