aboutsummaryrefslogtreecommitdiff
path: root/lib/project/tests/test_install.py
blob: f1dfcf2f08704b50e586b49679716c6f04995272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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