Skip to content

Commit

Permalink
Better handling for UNIX support (#183)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcel Telka <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 11, 2023
1 parent 6582847 commit b8c42dd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
13 changes: 7 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
- id: add-trailing-comma
args: [--py36-plus]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.2
rev: v3.4.0
hooks:
- id: pyupgrade
args: ["--py37-plus"]
Expand All @@ -44,6 +44,11 @@ repos:
hooks:
- id: tox-ini-fmt
args: ["-p", "fix"]
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "0.11.2"
hooks:
- id: pyproject-fmt
additional_dependencies: [tox>=4.5.1]
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
Expand All @@ -66,14 +71,10 @@ repos:
- "@prettier/[email protected]"
args: ["--print-width=120", "--prose-wrap=always"]
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.33.0
rev: v0.34.0
hooks:
- id: markdownlint
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "0.10.0"
hooks:
- id: pyproject-fmt
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
build-backend = "hatchling.build"
requires = [
"hatch-vcs>=0.3",
"hatchling>=1.14",
"hatchling>=1.14.1",
]

[project]
Expand Down Expand Up @@ -31,13 +31,13 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand All @@ -51,7 +51,7 @@ dependencies = [
optional-dependencies.docs = [
"furo>=2023.3.27",
"proselint>=0.13",
"sphinx>=6.1.3",
"sphinx>=6.2.1",
"sphinx-autodoc-typehints!=1.23.4,>=1.23",
]
optional-dependencies.test = [
Expand Down
9 changes: 5 additions & 4 deletions src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

from .api import PlatformDirsABC

if sys.platform.startswith("linux"): # pragma: no branch # no op check, only to please the type checker
from os import getuid
else:
if sys.platform == "win32":

def getuid() -> int:
raise RuntimeError("should only be used on Linux")
raise RuntimeError("should only be used on Unix")

else:
from os import getuid


class Unix(PlatformDirsABC):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_comp_with_appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def test_compatibility(params: dict[str, Any], func: str) -> None:
}
if func in msg: # pragma: no cover
pytest.skip(f"`appdirs.{func}` {msg[func]} on macOS") # pragma: no cover
if sys.platform == "linux":
elif sys.platform != "win32":
msg = { # pragma: no cover
"user_log_dir": "Uses XDG_STATE_DIR instead of appdirs.user_data_dir per the XDG spec",
}
if func in msg: # pragma: no cover
pytest.skip(f"`appdirs.{func}` {msg[func]} on Linux") # pragma: no cover
pytest.skip(f"`appdirs.{func}` {msg[func]} on Unix") # pragma: no cover

new = getattr(platformdirs, func)(*params)
old = getattr(appdirs, func)(*params)
Expand Down
17 changes: 8 additions & 9 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from _pytest.monkeypatch import MonkeyPatch
from pytest_mock import MockerFixture

from platformdirs import unix
from platformdirs.unix import Unix


Expand Down Expand Up @@ -128,18 +129,16 @@ def test_xdg_variable_custom_value(monkeypatch: MonkeyPatch, dirs_instance: Unix
assert result == "/tmp/custom-dir"


def test_platform_non_linux(monkeypatch: MonkeyPatch) -> None:
from platformdirs import unix

def test_platform_on_win32(monkeypatch: MonkeyPatch, mocker: MockerFixture) -> None:
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
mocker.patch("sys.platform", "win32")
prev_unix = unix
importlib.reload(unix)
try:
with monkeypatch.context() as context:
context.setattr(sys, "platform", "magic")
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
importlib.reload(unix)
with pytest.raises(RuntimeError, match="should only be used on Linux"):
with pytest.raises(RuntimeError, match="should only be used on Unix"):
unix.Unix().user_runtime_dir
finally:
importlib.reload(unix)
sys.modules["platformdirs.unix"] = prev_unix


def test_ensure_exists_creates_folder(mocker: MockerFixture, tmp_path: Path) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ commands =
description = run static analysis and style check using flake8
skip_install = true
deps =
pre-commit>=3.2.2
pre-commit>=3.3.1
pass_env =
HOMEPATH
PROGRAMDATA
Expand All @@ -63,7 +63,7 @@ description = combine coverage files and generate diff (against DIFF_AGAINST def
skip_install = true
deps =
covdefaults>=2.3
coverage[toml]>=7.2.3
coverage[toml]>=7.2.5
diff-cover>=7.5
extras =
parallel_show_output = true
Expand Down
1 change: 1 addition & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ setenv
shell32
typehints
usefixtures
win32
winreg
xdg

0 comments on commit b8c42dd

Please sign in to comment.