gabriel / musehub public
test_installer_profiles.py python
359 lines 16.8 KB
Raw
sha256:8d2339e3feaef5da2c7a3d6304444ec8d569fd4faf340cae62c47486d59fe8b6 feat(install): MUSE_PROFILE/MUSE_EXTRAS/MUSE_WHEELHOUSE and… Opus 5.5 patch 12 days ago
1 """Installation manifest, profiles and upgrades through the real installer (musehub#114, Phase 2b).
2
3 Each test drives the generated ``install.sh`` end to end -- real venv, real pip
4 (offline wheelhouse), real ``muse`` -- in a temporary HOME. Select with
5 ``pytest -m install_e2e tests/install_e2e``.
6 """
7
8 from __future__ import annotations
9
10 import json
11 import pathlib
12 import re
13 from collections.abc import Callable
14
15 import pytest
16
17 from musehub.api.routes.musehub.install import INSTALLER_REVISION
18 from tests.install_e2e import harness
19 from tests.install_e2e.harness import Sandbox
20
21 pytestmark = [pytest.mark.install_e2e, pytest.mark.slow]
22
23 GRAMMAR = "tree-sitter-go"
24
25
26 def _ok(proc: object) -> None:
27 assert getattr(proc, "returncode") == 0, f"{getattr(proc, 'stdout')}\n{getattr(proc, 'stderr')}"
28
29
30 def _validate_with_installed_muse(box: Sandbox) -> list[str]:
31 proc = box.run_python(
32 "import json;from muse.core.install_manifest import validate_manifest;"
33 f"print(json.dumps(validate_manifest(json.load(open({str(box.manifest_path)!r})))))"
34 )
35 assert proc.returncode == 0, proc.stderr
36 problems: list[str] = json.loads(proc.stdout)
37 return problems
38
39
40 @pytest.fixture(scope="module")
41 def module_sandbox(tmp_path_factory: pytest.TempPathFactory, wheelhouse: pathlib.Path) -> Callable[[str], Sandbox]:
42 def make(name: str) -> Sandbox:
43 return Sandbox(tmp_path_factory.mktemp(name), wheelhouse)
44
45 return make
46
47
48 @pytest.fixture(scope="module")
49 def full_install(module_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path) -> Sandbox:
50 box = module_sandbox("full")
51 _ok(box.install(candidate_sdist))
52 return box
53
54
55 @pytest.fixture(scope="module")
56 def slim_install(module_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path) -> Sandbox:
57 box = module_sandbox("slim")
58 _ok(box.install(candidate_sdist, MUSE_PROFILE="slim"))
59 return box
60
61
62 @pytest.fixture(scope="module")
63 def swift_install(module_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path) -> Sandbox:
64 box = module_sandbox("swift")
65 _ok(box.install(candidate_sdist, MUSE_EXTRAS="swift"))
66 return box
67
68
69 # ── Fresh installs write a valid manifest ──────────────────────────────────────
70
71
72 class TestFreshInstalls:
73 def test_default_profile_is_full(self, full_install: Sandbox, candidate_sdist: pathlib.Path) -> None:
74 m = full_install.manifest()
75 assert m["profile"] == "full" and m["extras"] == ["all"] and m["optional_languages"] == []
76 assert m["schema"] == 1 and m["installer_contract"] == 1
77 assert m["muse_version"] == harness.sdist_version(candidate_sdist)
78 assert _validate_with_installed_muse(full_install) == []
79
80 def test_full_installs_grammars_but_not_swift(self, full_install: Sandbox) -> None:
81 dists = full_install.installed_distributions()
82 assert GRAMMAR in dists and "tree-sitter-markdown" in dists
83 assert "py-tree-sitter-swift" not in dists
84
85 def test_full_doctor_is_verified_and_healthy(self, full_install: Sandbox) -> None:
86 code, payload = full_install.doctor()
87 doctor = payload["doctor"]
88 assert isinstance(doctor, dict)
89 assert code == 0 and doctor["ok"] is True
90 assert doctor["install"] == {
91 "profile": "full", "source": "manifest", "verified": True,
92 "optional_languages": [], "problems": [],
93 }
94
95 def test_slim_installs_core_only(self, slim_install: Sandbox) -> None:
96 m = slim_install.manifest()
97 assert m["profile"] == "slim" and m["extras"] == [] and m["optional_languages"] == []
98 dists = slim_install.installed_distributions()
99 assert "tree-sitter" in dists
100 assert not [d for d in dists if d.startswith("tree-sitter-")], dists
101 assert _validate_with_installed_muse(slim_install) == []
102
103 def test_slim_doctor_reports_slim_by_design_and_exits_zero(self, slim_install: Sandbox) -> None:
104 code, payload = slim_install.doctor()
105 doctor = payload["doctor"]
106 assert isinstance(doctor, dict)
107 assert code == 0 and doctor["ok"] is True and doctor["broken"] == []
108 assert doctor["install"]["profile"] == "slim" and doctor["install"]["verified"] is True
109 assert doctor["languages"]["go"]["health"] == "slim_by_design"
110 assert doctor["languages"]["python"]["health"] == "ok"
111
112 def test_swift_is_an_explicit_opt_in_layered_on_full(self, swift_install: Sandbox) -> None:
113 m = swift_install.manifest()
114 assert m["profile"] == "full"
115 assert m["extras"] == ["all", "swift"] and m["optional_languages"] == ["swift"]
116 assert "py-tree-sitter-swift" in swift_install.installed_distributions()
117 assert _validate_with_installed_muse(swift_install) == []
118
119 def test_swift_grammar_that_fails_to_load_is_broken(self, swift_install: Sandbox) -> None:
120 # The wheelhouse's Swift is a stand-in whose grammar cannot load.
121 code, payload = swift_install.doctor()
122 doctor = payload["doctor"]
123 assert isinstance(doctor, dict)
124 assert code == 1 and doctor["broken"] == ["swift"]
125 assert doctor["install"]["profile"] == "full+swift"
126
127 def test_swift_never_changes_the_default_install(self, full_install: Sandbox, swift_install: Sandbox) -> None:
128 assert full_install.manifest()["optional_languages"] == []
129 assert "py-tree-sitter-swift" not in full_install.installed_distributions()
130
131 def test_manifest_provenance_and_no_machine_paths(
132 self, full_install: Sandbox
133 ) -> None:
134 text = full_install.manifest_path.read_text()
135 m = json.loads(text)
136 assert m["installer"]["name"] == "musehub-install.sh"
137 assert m["installer"]["revision"] == INSTALLER_REVISION
138 assert re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", m["installed_at"])
139 for forbidden in (str(full_install.root), str(full_install.home), str(pathlib.Path.home()), "token", "secret"):
140 assert forbidden not in text
141
142 def test_manifest_written_atomically_leaves_no_temp_files(self, full_install: Sandbox) -> None:
143 assert sorted(p.name for p in full_install.venv.glob("muse-install*")) == ["muse-install.json"]
144
145 def test_binary_symlink_is_created_after_success(self, full_install: Sandbox) -> None:
146 assert (full_install.bin_dir / "muse").is_symlink()
147
148
149 # ── Invalid requests and failures leave nothing misleading ─────────────────────
150
151
152 class TestFailures:
153 def test_unknown_profile_is_rejected_before_anything_is_created(
154 self, sandbox: Sandbox, candidate_sdist: pathlib.Path
155 ) -> None:
156 proc = sandbox.install(candidate_sdist, MUSE_PROFILE="huge")
157 assert proc.returncode == 2 and "MUSE_PROFILE" in proc.stderr
158 assert not sandbox.venv.exists() and not sandbox.bin_dir.exists()
159
160 def test_unknown_extra_is_rejected(self, sandbox: Sandbox, candidate_sdist: pathlib.Path) -> None:
161 proc = sandbox.install(candidate_sdist, MUSE_EXTRAS="cobol")
162 assert proc.returncode == 2 and "MUSE_EXTRAS" in proc.stderr
163 assert not sandbox.venv.exists()
164
165 def test_failed_fresh_install_writes_no_manifest_and_no_symlink(
166 self, sandbox: Sandbox, candidate_sdist: pathlib.Path, tmp_path: pathlib.Path
167 ) -> None:
168 broken = harness.bad_sdist(tmp_path, "9.9.9", "unbuildable")
169 proc = sandbox.install(broken)
170 assert proc.returncode != 0
171 assert not sandbox.manifest_path.exists()
172 assert not (sandbox.bin_dir / "muse").exists()
173 assert "Muse 9.9.9 installed" not in proc.stdout
174
175 def test_unavailable_swift_build_fails_loudly_without_a_successful_manifest(
176 self, sandbox: Sandbox, candidate_sdist: pathlib.Path, tmp_path: pathlib.Path
177 ) -> None:
178 # No Swift wheel is reachable and no network: the opt-in install must fail, not degrade.
179 bare = tmp_path / "bare-wheelhouse"
180 bare.mkdir()
181 for wheel in sandbox.wheelhouse.iterdir():
182 if "swift" not in wheel.name:
183 (bare / wheel.name).symlink_to(wheel)
184 box = harness.Sandbox(sandbox.root, bare)
185 proc = box.install(candidate_sdist, MUSE_EXTRAS="swift")
186 assert proc.returncode != 0
187 assert not box.manifest_path.exists()
188 assert "installed." not in proc.stdout
189
190 def test_unbuildable_upgrade_keeps_the_previous_install_and_manifest_intact(
191 self, tmp_path: pathlib.Path, candidate_sdist: pathlib.Path, make_sandbox: Callable[[str], Sandbox]
192 ) -> None:
193 box = make_sandbox("failed-upgrade")
194 _ok(box.install(candidate_sdist))
195 before = box.manifest_path.read_bytes()
196 proc = box.install(harness.bad_sdist(tmp_path, "9.9.9", "unbuildable"))
197 assert proc.returncode != 0 and "NOT installed successfully" in proc.stderr
198 assert box.manifest_path.read_bytes() == before
199 code, payload = box.doctor()
200 assert code == 0 and payload["doctor"]["install"]["verified"] is True
201
202 def test_interrupted_upgrade_never_records_the_new_version(
203 self, tmp_path: pathlib.Path, candidate_sdist: pathlib.Path, make_sandbox: Callable[[str], Sandbox]
204 ) -> None:
205 # pip replaces muse with a hollow 9.9.9: the half-upgraded venv a crash would leave.
206 box = make_sandbox("interrupted-upgrade")
207 _ok(box.install(candidate_sdist))
208 before = box.manifest_path.read_bytes()
209 proc = box.install(harness.bad_sdist(tmp_path, "9.9.9", "hollow"))
210 assert proc.returncode != 0 and "NOT installed successfully" in proc.stderr
211 assert "Muse 9.9.9 installed" not in proc.stdout
212 assert box.manifest_path.read_bytes() == before
213 assert "9.9.9" not in box.manifest_path.read_text()
214 assert not (box.venv / "muse-install.json.new").exists()
215
216 def test_manifest_from_an_interrupted_upgrade_is_flagged_stale(self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path) -> None:
217 box = make_sandbox("stale")
218 _ok(box.install(candidate_sdist))
219 m = box.manifest()
220 m["muse_version"] = "0.0.1"
221 box.manifest_path.write_text(json.dumps(m))
222 code, payload = box.doctor()
223 assert code == 1
224 assert payload["doctor"]["install"]["verified"] is False and payload["doctor"]["install"]["problems"]
225
226
227 # ── Reinstall ──────────────────────────────────────────────────────────────────
228
229
230 class TestReinstall:
231 def test_rerun_preserves_the_recorded_slim_profile(
232 self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path
233 ) -> None:
234 box = make_sandbox("rerun-slim")
235 _ok(box.install(candidate_sdist, MUSE_PROFILE="slim"))
236 first = box.manifest()
237 _ok(box.install(candidate_sdist))
238 again = box.manifest()
239 assert again["profile"] == "slim" and again["extras"] == []
240 assert again["installed_at"] >= first["installed_at"]
241 assert not [d for d in box.installed_distributions() if d.startswith("tree-sitter-")]
242
243 def test_profile_changes_only_on_request(
244 self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path
245 ) -> None:
246 box = make_sandbox("switch")
247 _ok(box.install(candidate_sdist, MUSE_PROFILE="slim"))
248 _ok(box.install(candidate_sdist, MUSE_PROFILE="full"))
249 assert box.manifest()["profile"] == "full"
250 assert GRAMMAR in box.installed_distributions()
251 code, payload = box.doctor()
252 assert code == 0 and payload["doctor"]["install"]["profile"] == "full"
253
254 def test_rerun_preserves_swift_until_removed_on_request(
255 self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path
256 ) -> None:
257 box = make_sandbox("swift-rerun")
258 _ok(box.install(candidate_sdist, MUSE_EXTRAS="swift"))
259 _ok(box.install(candidate_sdist))
260 assert box.manifest()["optional_languages"] == ["swift"]
261 _ok(box.install(candidate_sdist, MUSE_EXTRAS=""))
262 m = box.manifest()
263 assert m["optional_languages"] == [] and m["extras"] == ["all"]
264
265 def test_corrupt_existing_manifest_is_not_trusted_and_is_replaced(
266 self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path
267 ) -> None:
268 box = make_sandbox("corrupt")
269 _ok(box.install(candidate_sdist, MUSE_PROFILE="slim"))
270 box.manifest_path.write_text("{not json")
271 proc = box.install(candidate_sdist)
272 _ok(proc)
273 assert "muse-install.json" in proc.stdout + proc.stderr # says it ignored the old manifest
274 assert box.manifest()["profile"] == "full"
275 assert _validate_with_installed_muse(box) == []
276
277
278 # ── Upgrades ───────────────────────────────────────────────────────────────────
279
280
281 class TestUpgrades:
282 def test_rc18_shaped_install_is_legacy_assumed_and_broken_before_upgrade(
283 self, make_sandbox: Callable[[str], Sandbox], previous_sdist: pathlib.Path
284 ) -> None:
285 box = make_sandbox("legacy-before")
286 box.legacy_install(previous_sdist)
287 assert not box.manifest_path.exists()
288 assert GRAMMAR not in box.installed_distributions()
289
290 def test_upgrade_from_legacy_rc18_installs_full_and_writes_manifest(
291 self, make_sandbox: Callable[[str], Sandbox], previous_sdist: pathlib.Path, candidate_sdist: pathlib.Path
292 ) -> None:
293 box = make_sandbox("legacy-upgrade")
294 box.legacy_install(previous_sdist)
295 _ok(box.install(candidate_sdist))
296 m = box.manifest()
297 assert m["profile"] == "full" and m["muse_version"] == harness.sdist_version(candidate_sdist)
298 assert GRAMMAR in box.installed_distributions()
299 code, payload = box.doctor()
300 assert code == 0 and payload["doctor"]["install"]["verified"] is True
301
302 @pytest.mark.parametrize(
303 "env, profile, optional",
304 [({}, "full", []), ({"MUSE_PROFILE": "slim"}, "slim", [])],
305 ids=["full", "slim"],
306 )
307 def test_upgrade_from_older_release_preserves_profile(
308 self, make_sandbox: Callable[[str], Sandbox], older_sdist: pathlib.Path,
309 candidate_sdist: pathlib.Path, env: dict[str, str], profile: str, optional: list[str],
310 ) -> None:
311 box = make_sandbox(f"upgrade-{profile}")
312 _ok(box.install(older_sdist, **env))
313 assert box.manifest()["muse_version"] == harness.sdist_version(older_sdist)
314 _ok(box.install(candidate_sdist))
315 new = box.manifest()
316 assert new["muse_version"] == harness.sdist_version(candidate_sdist)
317 assert new["profile"] == profile and new["optional_languages"] == optional
318 assert _validate_with_installed_muse(box) == []
319 code, payload = box.doctor()
320 assert code == 0 and payload["doctor"]["install"]["verified"] is True
321
322 def test_upgrade_preserves_a_swift_enabled_install(
323 self, make_sandbox: Callable[[str], Sandbox], candidate_sdist: pathlib.Path, tmp_path: pathlib.Path
324 ) -> None:
325 # No released engine has the swift extra yet, so upgrade candidate -> candidate-as-next-version.
326 box = make_sandbox("upgrade-swift")
327 _ok(box.install(candidate_sdist, MUSE_EXTRAS="swift"))
328 nxt = harness.retag_sdist(candidate_sdist, tmp_path, "9.9.9")
329 _ok(box.install(nxt))
330 m = box.manifest()
331 assert m["muse_version"] == "9.9.9" and m["profile"] == "full" and m["optional_languages"] == ["swift"]
332 assert _validate_with_installed_muse(box) == []
333
334 def test_new_installer_with_old_engine_skips_nothing_it_needs_and_old_engine_ignores_the_manifest(
335 self, make_sandbox: Callable[[str], Sandbox], older_sdist: pathlib.Path
336 ) -> None:
337 box = make_sandbox("old-engine")
338 _ok(box.install(older_sdist))
339 assert box.manifest()["profile"] == "full"
340 assert box.muse("--version").returncode == 0
341
342 def test_explicit_swift_on_an_engine_without_the_extra_fails_loudly(
343 self, make_sandbox: Callable[[str], Sandbox], older_sdist: pathlib.Path
344 ) -> None:
345 box = make_sandbox("old-engine-swift")
346 proc = box.install(older_sdist, MUSE_EXTRAS="swift")
347 assert proc.returncode != 0 and "swift" in proc.stderr
348 assert not box.manifest_path.exists()
349
350
351 # ── Installed-parser parity stays green ────────────────────────────────────────
352
353
354 class TestParityStillHolds:
355 def test_full_install_has_every_guaranteed_grammar(self, full_install: Sandbox) -> None:
356 code, payload = full_install.doctor()
357 langs = payload["doctor"]["languages"]
358 assert code == 0
359 assert {n for n, d in langs.items() if d["health"] != "ok" and d["profile"] == "full"} == set()
File History 2 commits
sha256:8d2339e3feaef5da2c7a3d6304444ec8d569fd4faf340cae62c47486d59fe8b6 feat(install): MUSE_PROFILE/MUSE_EXTRAS/MUSE_WHEELHOUSE and… Opus 5.5 patch 12 days ago
sha256:42b85c455874b92a57e82fafa5bcea5088a00aa5cb41493aadf125cdd9c08fc8 test(install): red e2e + contract tests for profiles, manif… Opus 5.5 patch 12 days ago