Files
ki18n/test/test_paths.py
T

173 lines
6.6 KiB
Python

"""Tests for ``ki18n.paths`` (PyInstaller bundle detection, per-user data directories)."""
import sys
from pathlib import Path
import pytest
from ki18n import paths
def test_not_frozen_by_default(monkeypatch):
monkeypatch.setattr(sys, "frozen", False, raising=False)
assert paths.is_frozen() is False
def test_frozen_flag_detected(monkeypatch):
monkeypatch.setattr(sys, "frozen", True, raising=False)
assert paths.is_frozen() is True
def test_is_frozen_returns_bool(monkeypatch):
monkeypatch.setattr(sys, "frozen", 1, raising=False)
assert paths.is_frozen() is True
def test_not_frozen_uses_parent_of_package_directory(monkeypatch):
monkeypatch.setattr(sys, "frozen", False, raising=False)
assert paths.bundle_root() == Path(paths.__file__).resolve().parent.parent
def test_frozen_uses_meipass(monkeypatch, tmp_path):
monkeypatch.setattr(sys, "frozen", True, raising=False)
monkeypatch.setattr(sys, "_MEIPASS", str(tmp_path), raising=False)
assert paths.bundle_root() == tmp_path
def test_frozen_without_meipass_uses_executable_directory(monkeypatch, tmp_path):
executable = tmp_path.resolve() / "bin" / "app"
monkeypatch.setattr(sys, "frozen", True, raising=False)
monkeypatch.setattr(sys, "_MEIPASS", None, raising=False)
monkeypatch.setattr(sys, "executable", str(executable))
assert paths.bundle_root() == executable.parent
def test_bundle_root_returns_path():
assert isinstance(paths.bundle_root(), Path)
# ---------------------------------------------------------------------------
# Per-user data directories
# ---------------------------------------------------------------------------
@pytest.fixture
def home(tmp_path, monkeypatch) -> Path:
"""Pseudo home directory; XDG_DATA_HOME and LOCALAPPDATA are removed."""
home_dir = tmp_path.resolve()
monkeypatch.setattr(Path, "home", staticmethod(lambda: home_dir))
monkeypatch.setenv("HOME", str(home_dir))
monkeypatch.setenv("USERPROFILE", str(home_dir))
monkeypatch.delenv("XDG_DATA_HOME", raising=False)
monkeypatch.delenv("LOCALAPPDATA", raising=False)
return home_dir
def set_platform(monkeypatch, platform: str) -> None:
monkeypatch.setattr(sys, "platform", platform)
class TestBaseDataDirLinux:
@pytest.mark.parametrize("platform", ["linux", "freebsd14", "openbsd7"])
def test_default_without_xdg(self, home, monkeypatch, platform):
set_platform(monkeypatch, platform)
assert paths.base_data_dir() == home / ".local" / "share"
def test_xdg_data_home_is_used_when_set(self, home, monkeypatch):
set_platform(monkeypatch, "linux")
monkeypatch.setenv("XDG_DATA_HOME", str(home / "xdg"))
assert paths.base_data_dir() == home / "xdg"
def test_xdg_data_home_expands_user(self, home, monkeypatch):
set_platform(monkeypatch, "linux")
monkeypatch.setenv("XDG_DATA_HOME", "~/share")
assert paths.base_data_dir() == home / "share"
def test_empty_xdg_data_home_is_treated_as_unset(self, home, monkeypatch):
set_platform(monkeypatch, "linux")
monkeypatch.setenv("XDG_DATA_HOME", "")
assert paths.base_data_dir() == home / ".local" / "share"
def test_localappdata_is_ignored(self, home, monkeypatch):
set_platform(monkeypatch, "linux")
monkeypatch.setenv("LOCALAPPDATA", str(home / "local-app-data"))
assert paths.base_data_dir() == home / ".local" / "share"
class TestBaseDataDirDarwin:
def test_default_without_xdg(self, home, monkeypatch):
set_platform(monkeypatch, "darwin")
assert paths.base_data_dir() == home / "Library" / "Application Support"
def test_xdg_data_home_takes_precedence(self, home, monkeypatch):
set_platform(monkeypatch, "darwin")
monkeypatch.setenv("XDG_DATA_HOME", str(home / "xdg"))
assert paths.base_data_dir() == home / "xdg"
def test_empty_xdg_data_home_is_treated_as_unset(self, home, monkeypatch):
set_platform(monkeypatch, "darwin")
monkeypatch.setenv("XDG_DATA_HOME", "")
assert paths.base_data_dir() == home / "Library" / "Application Support"
class TestBaseDataDirWindows:
def test_localappdata_is_used_when_set(self, home, monkeypatch):
set_platform(monkeypatch, "win32")
monkeypatch.setenv("LOCALAPPDATA", str(home / "local-app-data"))
assert paths.base_data_dir() == home / "local-app-data"
def test_fallback_without_localappdata(self, home, monkeypatch):
set_platform(monkeypatch, "win32")
assert paths.base_data_dir() == home / "AppData" / "Local"
def test_empty_localappdata_is_treated_as_unset(self, home, monkeypatch):
set_platform(monkeypatch, "win32")
monkeypatch.setenv("LOCALAPPDATA", "")
assert paths.base_data_dir() == home / "AppData" / "Local"
def test_xdg_data_home_is_ignored(self, home, monkeypatch):
set_platform(monkeypatch, "win32")
monkeypatch.setenv("XDG_DATA_HOME", str(home / "xdg"))
assert paths.base_data_dir() == home / "AppData" / "Local"
def test_base_data_dir_returns_path(home):
assert isinstance(paths.base_data_dir(), Path)
class TestDataDir:
@pytest.mark.parametrize(
("platform", "expected"),
[
("linux", Path(".local") / "share" / "ki18n"),
("darwin", Path("Library") / "Application Support" / "ki18n"),
("win32", Path("AppData") / "Local" / "ki18n"),
],
)
def test_appends_name_to_base_directory(self, home, monkeypatch, platform, expected):
set_platform(monkeypatch, platform)
assert paths.data_dir("ki18n") == home / expected
def test_uses_base_data_dir(self, tmp_path, monkeypatch):
monkeypatch.setattr(paths, "base_data_dir", lambda: tmp_path)
assert paths.data_dir("my-app") == tmp_path / "my-app"
def test_returns_path(self, home):
assert isinstance(paths.data_dir("ki18n"), Path)
def test_name_with_space_is_kept(self, tmp_path, monkeypatch):
monkeypatch.setattr(paths, "base_data_dir", lambda: tmp_path)
assert paths.data_dir("My App") == tmp_path / "My App"
def test_directory_is_not_created(self, tmp_path, monkeypatch):
monkeypatch.setattr(paths, "base_data_dir", lambda: tmp_path)
result = paths.data_dir("ki18n")
assert not result.exists()
assert list(tmp_path.iterdir()) == []
@pytest.mark.parametrize("name", ["", ".", "..", "a/b", "a\\b", "/abs", "../x"])
def test_invalid_name_is_rejected(self, tmp_path, monkeypatch, name):
monkeypatch.setattr(paths, "base_data_dir", lambda: tmp_path)
with pytest.raises(ValueError):
paths.data_dir(name)