Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSD: provide a fallback for user_runtime_dir #201

Merged
merged 1 commit into from
Jul 6, 2023
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
5 changes: 4 additions & 1 deletion src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ def user_runtime_dir(self) -> str:
``$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.
exists, otherwise ``/tmp/runtime-$(id -u)/$appname/$version``, if``$XDG_RUNTIME_DIR``
is not set.
"""
path = os.environ.get("XDG_RUNTIME_DIR", "")
if not path.strip():
if sys.platform.startswith(("freebsd", "openbsd", "netbsd")):
path = f"/var/run/user/{getuid()}"
if not Path(path).exists():
path = f"/tmp/runtime-{getuid()}" # noqa: S108
else:
path = f"/run/user/{getuid()}"
return self._append_app_name_and_version(path)
Expand Down
15 changes: 7 additions & 8 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,16 @@ def test_xdg_variable_custom_value(monkeypatch: pytest.MonkeyPatch, dirs_instanc


@pytest.mark.usefixtures("_getuid")
def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
@pytest.mark.parametrize("platform", ["freebsd", "openbsd", "netbsd"])
def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture, platform: str) -> 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", platform)

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

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


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