Add tests/: four suites, a runner, and a sandbox that can't escape
These grew one bug at a time in a scratch directory and were about to be thrown away, having caught four real regressions between them. Move them in as they are -- plain scripts, stdlib only, no test dependency -- behind a shared harness and `python3 tests/run.py`. The harness exists mostly for its guard. A suite testing the purge path once sandboxed LLMHOST_CONFIG but not LLMHOST_HOME and deleted a live install's data directory; BIN_DIR and SHIM have no env override at all, coming straight off Path.home(). So the harness repoints those into the sandbox and then asserts that every path llmhost could write or delete is inside it, aborting the run if not -- verified by pointing it at a real path and watching it refuse. The model plan the opencode suite needs is now a fixture rather than a file read out of someone's home directory; it carries no key (the key lives in its own file) and its machine-specific fields are pinned. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ba7bd76af6
commit
51c2880a2e
8 changed files with 852 additions and 0 deletions
76
tests/harness.py
Normal file
76
tests/harness.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
"""Shared bits for the suites: repo-relative imports, a sandbox, and a check() counter.
|
||||
|
||||
Each suite is a plain script: run it directly, or run them all with `python3 tests/run.py`.
|
||||
Stdlib only, like the rest of llm-host.
|
||||
|
||||
Every suite sandboxes LLMHOST_HOME / LLMHOST_CONFIG / XDG_* before importing llmhost, so
|
||||
nothing touches a real install. setup() asserts that, because a suite that forgot
|
||||
LLMHOST_HOME once deleted a live install's data directory.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
FIXTURES = Path(__file__).resolve().parent / "fixtures"
|
||||
|
||||
|
||||
def setup():
|
||||
"""Point every path llmhost writes to at a fresh temp dir. Returns its root."""
|
||||
root = tempfile.mkdtemp(prefix="llmhost-test-")
|
||||
os.environ["LLMHOST_HOME"] = root + "/.local/share/llm-host"
|
||||
os.environ["LLMHOST_CONFIG"] = root + "/.config/llm-host"
|
||||
os.environ["XDG_CONFIG_HOME"] = root + "/.config"
|
||||
os.environ["XDG_DATA_HOME"] = root + "/.local/share"
|
||||
sys.path.insert(0, str(REPO))
|
||||
return root
|
||||
|
||||
|
||||
def guard(paths, root):
|
||||
"""Move the paths no env var covers into the sandbox, then abort unless everything
|
||||
llmhost could write or delete is inside it.
|
||||
|
||||
LLMHOST_HOME and LLMHOST_CONFIG redirect most of it, but BIN_DIR/SHIM come off
|
||||
Path.home() with no override, so they are repointed here -- once, for every suite,
|
||||
rather than per test. The assertion is the real safety net: a suite that forgot
|
||||
LLMHOST_HOME once deleted a live install's data directory."""
|
||||
paths.BIN_DIR = Path(root) / ".local/bin"
|
||||
paths.SHIM = paths.BIN_DIR / "llmhost"
|
||||
paths.LOG_DIR = Path(root) / "logs"
|
||||
for name in ("DATA", "CONFIG_DIR", "CONFIG_FILE", "KEY_FILE", "BIN_DIR", "SHIM", "LOG_DIR"):
|
||||
value = str(getattr(paths, name))
|
||||
if not value.startswith(root):
|
||||
sys.exit(f"ABORT: paths.{name} = {value} is outside the sandbox {root}")
|
||||
|
||||
|
||||
def config():
|
||||
"""The planned-models fixture: a real `llmhost setup` result, machine bits pinned."""
|
||||
return json.loads((FIXTURES / "config.json").read_text())
|
||||
|
||||
|
||||
class Checks:
|
||||
def __init__(self):
|
||||
self.failed = []
|
||||
self.passed = 0
|
||||
|
||||
def __call__(self, label, cond, extra=""):
|
||||
if cond:
|
||||
self.passed += 1
|
||||
print(" ok " + label)
|
||||
else:
|
||||
self.failed.append(label)
|
||||
print(" FAIL " + label + ((" <- " + str(extra)) if extra else ""))
|
||||
return bool(cond)
|
||||
|
||||
def section(self, title):
|
||||
print("\n" + title)
|
||||
|
||||
def done(self, root=None):
|
||||
if root:
|
||||
shutil.rmtree(root, ignore_errors=True)
|
||||
print("\n" + (f"ALL PASS ({self.passed} checks)" if not self.failed
|
||||
else f"{len(self.failed)} FAILURES of {self.passed + len(self.failed)}: {self.failed}"))
|
||||
sys.exit(1 if self.failed else 0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue