|
| 1 | +"""Unit tests for agent_assembly._install — install-time runtime resolution.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from agent_assembly import _install |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def isolate_runtime(monkeypatch, tmp_path: Path) -> Path: |
| 14 | + """Isolate ensure_runtime() from the host environment. |
| 15 | +
|
| 16 | + Yields a context where: |
| 17 | + - PATH is empty (so ``shutil.which`` cannot find any system binary). |
| 18 | + - ``WHEEL_BUNDLED_BIN`` points at ``tmp_path/bin/aasm`` (missing by |
| 19 | + default; tests opt in by creating the file). |
| 20 | + """ |
| 21 | + fake_bin_dir = tmp_path / "bin" |
| 22 | + fake_bin_dir.mkdir() |
| 23 | + fake_binary = fake_bin_dir / _install.BINARY_NAME |
| 24 | + monkeypatch.setattr(_install, "WHEEL_BUNDLED_BIN", fake_binary) |
| 25 | + monkeypatch.setenv("PATH", "") |
| 26 | + return fake_binary |
| 27 | + |
| 28 | + |
| 29 | +def test_ensure_runtime_returns_path_match_first(monkeypatch, tmp_path: Path) -> None: |
| 30 | + """When `aasm` is on PATH, ensure_runtime returns that resolved path.""" |
| 31 | + import stat |
| 32 | + |
| 33 | + bin_dir = tmp_path / "system-bin" |
| 34 | + bin_dir.mkdir() |
| 35 | + on_path = bin_dir / _install.BINARY_NAME |
| 36 | + on_path.write_text("#!/bin/sh\nexit 0\n") |
| 37 | + on_path.chmod(on_path.stat().st_mode | stat.S_IXUSR) |
| 38 | + monkeypatch.setenv("PATH", str(bin_dir)) |
| 39 | + |
| 40 | + resolved = _install.ensure_runtime() |
| 41 | + |
| 42 | + assert resolved == on_path |
| 43 | + |
| 44 | + |
| 45 | +def test_ensure_runtime_falls_back_to_wheel_bundled(isolate_runtime: Path) -> None: |
| 46 | + """When PATH has no aasm, ensure_runtime returns the wheel-bundled path.""" |
| 47 | + import stat |
| 48 | + |
| 49 | + fake_binary = isolate_runtime |
| 50 | + fake_binary.write_text("#!/bin/sh\nexit 0\n") |
| 51 | + fake_binary.chmod(fake_binary.stat().st_mode | stat.S_IXUSR) |
| 52 | + |
| 53 | + resolved = _install.ensure_runtime() |
| 54 | + |
| 55 | + assert resolved == fake_binary |
| 56 | + |
| 57 | + |
| 58 | +def test_ensure_runtime_raises_with_install_hint(isolate_runtime: Path) -> None: |
| 59 | + """When no binary exists, raise RuntimeError carrying INSTALL_HINT.""" |
| 60 | + # Sanity: isolate_runtime points at a path that doesn't exist yet. |
| 61 | + assert not isolate_runtime.exists() |
| 62 | + |
| 63 | + with pytest.raises(RuntimeError) as exc_info: |
| 64 | + _install.ensure_runtime() |
| 65 | + |
| 66 | + assert _install.INSTALL_HINT in str(exc_info.value) |
0 commit comments