diff options
Diffstat (limited to 'lib/project/tests/test_install.py')
| -rw-r--r-- | lib/project/tests/test_install.py | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/lib/project/tests/test_install.py b/lib/project/tests/test_install.py new file mode 100644 index 0000000..f1dfcf2 --- /dev/null +++ b/lib/project/tests/test_install.py @@ -0,0 +1,225 @@ +"""Data-dir resolution, contained tool environment, and launcher script checks.""" +import os +import subprocess +from pathlib import Path +import sys +import types + +import pytest + +from voiceforge import setup + +ROOT = Path(__file__).resolve().parents[3] +LAUNCHER = ROOT / "producer.sh" + + +@pytest.fixture +def shell_env(tmp_path): + env = os.environ.copy() + env.pop("VOICEFORGE_HOME", None) + env.pop("XDG_DATA_HOME", None) + env["HOME"] = str(tmp_path / "home") + return env + + +# --- data_dir resolution --------------------------------------------------- + +def test_data_dir_defaults_and_precedence(tmp_path, monkeypatch): + monkeypatch.delenv("VOICEFORGE_HOME", raising=False) + monkeypatch.delenv("XDG_DATA_HOME", raising=False) + monkeypatch.setenv("HOME", str(tmp_path)) + assert setup.data_dir() == tmp_path / ".local/share/voiceforge" + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "xdg")) + assert setup.data_dir() == tmp_path / "xdg/voiceforge" + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + assert setup.data_dir() == tmp_path / "prefix" + + +def test_relative_voiceforge_home_resolves_against_cwd(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", "voiceforge-home") + monkeypatch.chdir(tmp_path) + assert setup.data_dir() == tmp_path / "voiceforge-home" + + +def test_relative_xdg_data_home_falls_back_to_home(tmp_path, monkeypatch): + monkeypatch.delenv("VOICEFORGE_HOME", raising=False) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("XDG_DATA_HOME", "relative/path") + assert setup.data_dir() == tmp_path / ".local/share/voiceforge" + + +# --- contained tool environment -------------------------------------------- + +def test_tool_env_is_sanitized_and_contained(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + monkeypatch.setenv("PYTHONPATH", "/host/site-packages") + monkeypatch.setenv("PYTHONHOME", "/host/python") + monkeypatch.setenv("VIRTUAL_ENV", "/host/venv") + monkeypatch.setenv("PIP_TARGET", "/host/target") + monkeypatch.setenv("PIP_PREFIX", "/host/prefix") + monkeypatch.setenv("PIP_USER", "1") + monkeypatch.setenv("PIP_FIND_LINKS", "/host/wheels") + monkeypatch.setenv("PIP_INDEX_URL", "https://host.example/simple") + monkeypatch.setenv("PIP_EXTRA_INDEX_URL", "https://host.example/extra") + monkeypatch.setenv("PIP_CONSTRAINT", "/host/constraints.txt") + monkeypatch.setenv("UV_INDEX_URL", "https://host.example/simple") + monkeypatch.setenv("UV_DEFAULT_INDEX", "https://host.example/simple") + monkeypatch.setenv("UV_EXTRA_INDEX_URL", "https://host.example/extra") + monkeypatch.setenv("UV_INDEX", "https://host.example/extra") + monkeypatch.setenv("UV_FIND_LINKS", "/host/wheels") + monkeypatch.setenv("UV_CONSTRAINT", "/host/constraints.txt") + monkeypatch.delenv("PIP_CACHE_DIR", raising=False) + env = setup.tool_env() + prefix = str(tmp_path / "prefix") + assert "PYTHONPATH" not in env and "PYTHONHOME" not in env and "VIRTUAL_ENV" not in env + for name in ("PIP_TARGET", "PIP_PREFIX", "PIP_USER", "PIP_FIND_LINKS", + "PIP_INDEX_URL", "PIP_EXTRA_INDEX_URL", "PIP_CONSTRAINT", + "UV_INDEX_URL", "UV_DEFAULT_INDEX", "UV_EXTRA_INDEX_URL", + "UV_INDEX", "UV_FIND_LINKS", "UV_CONSTRAINT"): + assert name not in env, name + assert env["PYTHONNOUSERSITE"] == "1" + assert env["PIP_CONFIG_FILE"] == os.devnull + assert env["UV_NO_CONFIG"] == "1" + assert env["XDG_CACHE_HOME"] == f"{prefix}/cache" + assert env["PIP_CACHE_DIR"] == f"{prefix}/cache/pip" + assert env["UV_CACHE_DIR"] == f"{prefix}/cache/uv" + assert env["UV_PYTHON_INSTALL_DIR"] == f"{prefix}/uv/python" + assert env["TMPDIR"] == f"{prefix}/tmp" + assert (tmp_path / "prefix/tmp").is_dir() + + +def test_tool_env_tolerates_an_uncreatable_data_dir(monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", "/vf-prefix-unwritable") + env = setup.tool_env() + assert env["PIP_CACHE_DIR"] == "/vf-prefix-unwritable/cache/pip" + assert "TMPDIR" not in env + + +def test_tool_env_leaves_process_environment_untouched(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + monkeypatch.setenv("PYTHONPATH", "/host/site-packages") + setup.tool_env() + assert os.environ["PYTHONPATH"] == "/host/site-packages" + + +def test_ensure_uv_bootstraps_into_data_dir_with_contained_pip_cache(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + monkeypatch.setattr(setup.shutil, "which", lambda name: None) + calls = [] + uv_binary = tmp_path / "prefix/bootstrap/bin/uv" + + def fake_run(args, stage, progress=None, *, env=None): + calls.append((args, env)) + # Simulate pip actually producing the pinned uv. + if stage == "Installing uv from PyPI": + uv_binary.parent.mkdir(parents=True, exist_ok=True) + uv_binary.write_text("#!/bin/sh\n") + uv_binary.chmod(0o755) + + monkeypatch.setattr(setup, "run_process", fake_run) + uv = setup.ensure_uv() + assert uv == str(uv_binary) + venv_args, venv_env = calls[0] + pip_args, pip_env = calls[1] + assert venv_args[-1] == str(tmp_path / "prefix/bootstrap") + assert pip_env["PIP_CACHE_DIR"] == str(tmp_path / "prefix/cache/pip") + assert pip_env["UV_CACHE_DIR"] == str(tmp_path / "prefix/cache/uv") + assert pip_env["PIP_CONFIG_FILE"] == os.devnull + assert "PYTHONPATH" not in pip_env + assert "uv==0.8.17" in pip_args + + +def test_ensure_uv_reuses_contained_bootstrap(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + uv_binary = tmp_path / "prefix/bootstrap/bin/uv" + uv_binary.parent.mkdir(parents=True) + uv_binary.write_text("#!/bin/sh\n") + uv_binary.chmod(0o755) + monkeypatch.setattr(setup.shutil, "which", lambda name: None) + assert setup.ensure_uv() == str(uv_binary) + + +def test_ensure_uv_prefers_the_contained_bootstrap_over_a_host_uv(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + uv_binary = tmp_path / "prefix/bootstrap/bin/uv" + uv_binary.parent.mkdir(parents=True) + uv_binary.write_text("#!/bin/sh\n") + uv_binary.chmod(0o755) + monkeypatch.setattr(setup.shutil, "which", lambda name: "/host/bin/uv") + assert setup.ensure_uv() == str(uv_binary) + + +def test_ensure_uv_ignores_a_host_uv_without_required_capability(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + monkeypatch.setattr(setup.shutil, "which", lambda name: "/host/bin/uv") + monkeypatch.setattr(setup, "_uv_supports_relocatable", lambda uv: False) + stages = [] + uv_binary = tmp_path / "prefix/bootstrap/bin/uv" + + def fake_run(args, stage, progress=None, *, env=None): + stages.append(stage) + if stage == "Installing uv from PyPI": + uv_binary.parent.mkdir(parents=True, exist_ok=True) + uv_binary.write_text("#!/bin/sh\n") + uv_binary.chmod(0o755) + + monkeypatch.setattr(setup, "run_process", fake_run) + assert setup.ensure_uv() == str(uv_binary) + assert stages == ["Creating uv bootstrap", "Installing uv from PyPI"] + + +def test_ensure_uv_fails_when_bootstrap_does_not_produce_uv(tmp_path, monkeypatch): + monkeypatch.setenv("VOICEFORGE_HOME", str(tmp_path / "prefix")) + monkeypatch.setattr(setup.shutil, "which", lambda name: None) + monkeypatch.setattr(setup, "run_process", lambda *args, **kwargs: None) + with pytest.raises(RuntimeError, match="did not produce a usable uv"): + setup.ensure_uv() + + +# --- FFmpeg selection --------------------------------------------------------- + +def test_ensure_ffmpeg_prefers_the_bundled_binary_over_host(tmp_path, monkeypatch): + bundled = tmp_path / "bundled/ffmpeg" + bundled.parent.mkdir(parents=True) + bundled.write_text("#!/bin/sh\n") + monkeypatch.setitem(sys.modules, "imageio_ffmpeg", + types.SimpleNamespace(get_ffmpeg_exe=lambda: str(bundled))) + monkeypatch.setattr(setup.shutil, "which", lambda name: "/usr/bin/ffmpeg") + assert setup.ensure_ffmpeg() == str(bundled) + + +def test_ensure_ffmpeg_falls_back_to_system_without_the_bundle(monkeypatch): + monkeypatch.setitem(sys.modules, "imageio_ffmpeg", None) + monkeypatch.setattr(setup.shutil, "which", lambda name: "/usr/bin/ffmpeg") + assert setup.ensure_ffmpeg() == "/usr/bin/ffmpeg" + + +def test_ensure_ffmpeg_error_points_at_the_launcher(monkeypatch): + monkeypatch.setitem(sys.modules, "imageio_ffmpeg", None) + monkeypatch.setattr(setup.shutil, "which", lambda name: None) + with pytest.raises(RuntimeError, match=r"\./producer\.sh --rebuild") as error: + setup.ensure_ffmpeg() + assert "install.sh" not in str(error.value) + + +# --- producer.sh launcher ---------------------------------------------------- + +@pytest.mark.parametrize("argv", [["--help"], ["--rebuild", "--help"]]) +def test_launcher_forwards_help_without_bootstrapping(argv, shell_env, tmp_path, monkeypatch): + # A missing lib/ must not be created by a pure help request. + staging = tmp_path / "checkout" + staging.mkdir() + (staging / "producer.sh").write_text(LAUNCHER.read_text()) + (staging / "producer.sh").chmod(0o755) + monkeypatch.setenv("PATH", "/usr/bin:/bin") + result = subprocess.run(["./producer.sh", *argv], cwd=staging, env=shell_env, + capture_output=True, text=True, timeout=30) + assert result.returncode == 0 + assert "process" in result.stdout + assert not (staging / "lib").exists() + + +def test_launcher_script_parses(): + result = subprocess.run(["bash", "-n", str(LAUNCHER)], + capture_output=True, text=True, timeout=30) + assert result.returncode == 0, result.stderr |
