291 lines
11 KiB
Python
291 lines
11 KiB
Python
"""Tests for ``ki18n.paths`` (PyInstaller bundle detection, frozen detection
|
|
and application root, per-user data directories)."""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from ki18n import paths
|
|
|
|
|
|
@pytest.fixture
|
|
def root(tmp_path) -> Path:
|
|
"""A resolved temporary directory (avoids symlink differences, e.g. on macOS)."""
|
|
return tmp_path.resolve()
|
|
|
|
|
|
@pytest.fixture
|
|
def not_frozen(monkeypatch):
|
|
monkeypatch.setattr(sys, "frozen", False, raising=False)
|
|
|
|
|
|
def run_as(monkeypatch, script: Path) -> None:
|
|
"""Pretend that ``script`` is the program that was started."""
|
|
monkeypatch.setattr(sys, "argv", [str(script)])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# is_frozen
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
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
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# bundle_root: frozen applications
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_frozen_uses_meipass(monkeypatch, root):
|
|
monkeypatch.setattr(sys, "frozen", True, raising=False)
|
|
monkeypatch.setattr(sys, "_MEIPASS", str(root), raising=False)
|
|
assert paths.bundle_root() == root
|
|
|
|
|
|
def test_frozen_without_meipass_uses_executable_directory(monkeypatch, root):
|
|
executable = root / "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_frozen_ignores_main_script_and_i18n_folders(monkeypatch, root):
|
|
(root / "app" / "i18n").mkdir(parents=True)
|
|
run_as(monkeypatch, root / "app" / "main.py")
|
|
bundle = root / "bundle"
|
|
bundle.mkdir()
|
|
monkeypatch.setattr(sys, "frozen", True, raising=False)
|
|
monkeypatch.setattr(sys, "_MEIPASS", str(bundle), raising=False)
|
|
assert paths.bundle_root() == bundle
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# bundle_root: source checkout (not frozen)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_returns_directory_of_main_script_that_contains_i18n(not_frozen, monkeypatch, root):
|
|
(root / "i18n").mkdir()
|
|
run_as(monkeypatch, root / "main.py")
|
|
assert paths.bundle_root() == root
|
|
|
|
|
|
def test_walks_up_from_a_main_script_in_a_subdirectory(not_frozen, monkeypatch, root):
|
|
(root / "i18n").mkdir()
|
|
(root / "tools" / "cli").mkdir(parents=True)
|
|
run_as(monkeypatch, root / "tools" / "cli" / "main.py")
|
|
assert paths.bundle_root() == root
|
|
|
|
|
|
def test_nearest_directory_with_i18n_wins(not_frozen, monkeypatch, root):
|
|
(root / "i18n").mkdir()
|
|
(root / "app" / "i18n").mkdir(parents=True)
|
|
(root / "app" / "src").mkdir()
|
|
run_as(monkeypatch, root / "app" / "src" / "main.py")
|
|
assert paths.bundle_root() == root / "app"
|
|
|
|
|
|
def test_without_i18n_folder_returns_directory_of_main_script(not_frozen, monkeypatch, root):
|
|
(root / "app" / "src").mkdir(parents=True)
|
|
run_as(monkeypatch, root / "app" / "src" / "main.py")
|
|
assert paths.bundle_root() == root / "app" / "src"
|
|
|
|
|
|
def test_i18n_must_be_a_directory(not_frozen, monkeypatch, root):
|
|
(root / "i18n").write_text("not a directory", encoding="utf-8")
|
|
(root / "app").mkdir()
|
|
run_as(monkeypatch, root / "app" / "main.py")
|
|
assert paths.bundle_root() == root / "app"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"location",
|
|
[
|
|
"src/ki18n",
|
|
"lib/vendor/third_party/ki18n",
|
|
"a/b/c/d/e/f/ki18n",
|
|
"venv/lib/python3.12/site-packages/ki18n",
|
|
],
|
|
)
|
|
def test_result_does_not_depend_on_where_ki18n_is_located(
|
|
not_frozen, monkeypatch, root, location
|
|
):
|
|
(root / "app" / "i18n").mkdir(parents=True)
|
|
run_as(monkeypatch, root / "app" / "main.py")
|
|
# Pretend that the library lives at a different depth of the hierarchy.
|
|
monkeypatch.setattr(paths, "__file__", str(root / "app" / location / "paths.py"))
|
|
assert paths.bundle_root() == root / "app"
|
|
|
|
|
|
def test_relative_main_script_is_resolved_against_cwd(not_frozen, monkeypatch, root):
|
|
(root / "app" / "i18n").mkdir(parents=True)
|
|
monkeypatch.chdir(root / "app")
|
|
monkeypatch.setattr(sys, "argv", ["main.py"])
|
|
assert paths.bundle_root() == root / "app"
|
|
|
|
|
|
def test_matches_the_default_project_root_of_ki18n(not_frozen, monkeypatch, root):
|
|
from ki18n.ki18n import Ki18n
|
|
from support import EN, write_language
|
|
|
|
app = root / "app"
|
|
write_language(app, "en", EN)
|
|
run_as(monkeypatch, app / "main.py")
|
|
manager = Ki18n("en", local_root=root / "user")
|
|
assert manager.project_root == paths.bundle_root()
|
|
|
|
|
|
@pytest.mark.parametrize("argv", [[], [""], ["-c"], ["-"]])
|
|
def test_without_a_script_the_current_directory_is_used(not_frozen, monkeypatch, root, argv):
|
|
(root / "i18n").mkdir()
|
|
(root / "work").mkdir()
|
|
monkeypatch.chdir(root / "work")
|
|
monkeypatch.setattr(sys, "argv", argv)
|
|
# No i18n in the working directory: the walk up finds the parent's folder.
|
|
assert paths.bundle_root() == root
|
|
|
|
|
|
def test_bundle_root_returns_path(not_frozen):
|
|
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)
|