Updated bundle_root to work wherever ithe root is in relation to the library
This commit is contained in:
+128
-10
@@ -1,4 +1,5 @@
|
||||
"""Tests for ``ki18n.paths`` (PyInstaller bundle detection, per-user data directories)."""
|
||||
"""Tests for ``ki18n.paths`` (PyInstaller bundle detection, frozen detection
|
||||
and application root, per-user data directories)."""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@@ -8,6 +9,27 @@ 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
|
||||
@@ -23,26 +45,122 @@ def test_is_frozen_returns_bool(monkeypatch):
|
||||
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
|
||||
# ---------------------------------------------------------------------------
|
||||
# bundle_root: frozen applications
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_frozen_uses_meipass(monkeypatch, tmp_path):
|
||||
def test_frozen_uses_meipass(monkeypatch, root):
|
||||
monkeypatch.setattr(sys, "frozen", True, raising=False)
|
||||
monkeypatch.setattr(sys, "_MEIPASS", str(tmp_path), raising=False)
|
||||
assert paths.bundle_root() == tmp_path
|
||||
monkeypatch.setattr(sys, "_MEIPASS", str(root), raising=False)
|
||||
assert paths.bundle_root() == root
|
||||
|
||||
|
||||
def test_frozen_without_meipass_uses_executable_directory(monkeypatch, tmp_path):
|
||||
executable = tmp_path.resolve() / "bin" / "app"
|
||||
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_bundle_root_returns_path():
|
||||
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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user