Files
alzeo/tests/test_get_sh.py
T
Hasan BasbunarandClaude Opus 5.5 e6e667fcec release check: a box updated by the one-liner knows which release it runs
The cockpit told the reference box, running v1.20.1, "Version v1.20.1 is
out; this box runs v1.19.0". The check named the installed release from
the checkout's tags (git describe), and get.sh fetched main and no tag,
so every box it updated described itself as the release it was first
cloned at, in the banner and in the Library's release line.

The check reads the release the checkout's own CHANGELOG.md names, the
one the badge already showed; the tags answer only when the file names
none. get.sh fetches the release tags too, so git describe agrees; a
local tag of the same name is kept and the update goes on. Tests: the
check against a checkout whose tags lag its files, get.sh against a
release tagged after the clone and a clashing local tag; they fail on
v1.20.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 11:31:47 +02:00

131 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""get.sh's guard logic, offline, against a real git fixture.
get.sh is what every user runs, and nothing exercised its behaviour: shellcheck
and `bash -n` only prove it parses. The fixture builds a bare repo whose path
contains this project's slug (so get.sh's origin check passes), clones it, and
replaces install.sh with a stub that records its arguments, so the whole script
runs to its exec without touching a real install."""
import os
import shutil
import subprocess
import sys
import tempfile
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GET = os.path.join(REPO_DIR, "get.sh")
STUB = '#!/usr/bin/env bash\necho "INSTALL_RAN args=$*"\n'
def git(*a, cwd, check=True):
return subprocess.run(["git", *a], cwd=cwd, capture_output=True, text=True, check=check)
def fixture(base):
"""A bare 'origin' whose URL carries the slug, plus a clone one commit behind."""
origin = os.path.join(base, "hasso5703", "dgx-spark-qwen38.git")
os.makedirs(origin)
git("init", "-q", "--bare", "-b", "main", ".", cwd=origin)
work = os.path.join(base, "work")
git("clone", "-q", origin, work, cwd=base)
for k, v in (("user.email", "t@t"), ("user.name", "t")):
git("config", k, v, cwd=work)
with open(os.path.join(work, "install.sh"), "w") as f:
f.write(STUB)
os.chmod(os.path.join(work, "install.sh"), 0o755)
git("add", "-A", cwd=work); git("commit", "-qm", "v1", cwd=work)
git("push", "-q", "origin", "main", cwd=work)
return origin, work
def run_get(work, env_extra=None, args=()):
env = {**os.environ, "DIR": work, **(env_extra or {})}
return subprocess.run(["bash", GET, *args], capture_output=True, text=True, env=env)
def main() -> None:
base = tempfile.mkdtemp()
try:
_, work = fixture(base)
# 1. Clean clone on main: get.sh runs through to the installer.
r = run_get(work)
assert r.returncode == 0, f"clean clone refused: {r.stderr}"
assert "INSTALL_RAN" in r.stdout, r.stdout
# 2. Arguments reach install.sh unchanged.
r = run_get(work, args=["--no-service", "--no-start"])
assert "INSTALL_RAN args=--no-service --no-start" in r.stdout, r.stdout
# 3. An untracked file must NOT block, must survive, and must be reported.
# It cannot make the checkout stale and `reset --hard` leaves it alone;
# blocking on it used to fail the install of anyone keeping notes there.
note = os.path.join(work, "my-notes.txt")
with open(note, "w") as f:
f.write("keep me")
r = run_get(work)
assert r.returncode == 0, f"an untracked file blocked the update: {r.stderr}"
assert os.path.exists(note), "the untracked file was removed"
assert open(note).read() == "keep me", "the untracked file was altered"
assert "Untracked files kept in place" in r.stdout, r.stdout
os.remove(note)
# 4. A modified tracked file DOES block, and says so.
with open(os.path.join(work, "install.sh"), "a") as f:
f.write("# local edit\n")
r = run_get(work)
assert r.returncode != 0, "a modified tracked file must block"
assert "modified tracked files" in r.stderr, r.stderr
# 5. FORCE_UPDATE=1 stashes the tracked change and proceeds.
r = run_get(work, env_extra={"FORCE_UPDATE": "1"})
assert r.returncode == 0, f"FORCE_UPDATE did not recover: {r.stderr}"
assert "INSTALL_RAN" in r.stdout, r.stdout
assert git("stash", "list", cwd=work).stdout.strip(), "the change was not stashed"
# 6. A clone that is not this repo is refused, whatever it contains.
other = os.path.join(base, "other")
git("clone", "-q", work, other, cwd=base)
git("remote", "set-url", "origin", "https://github.com/someone/else.git", cwd=other)
r = run_get(other)
assert r.returncode != 0 and "not a clone of this repo" in r.stderr, r.stderr
# 7. A side branch is refused without FORCE_UPDATE, accepted with it.
git("checkout", "-qb", "side", cwd=work)
r = run_get(work)
assert r.returncode != 0 and "not 'main'" in r.stderr, r.stderr
r = run_get(work, env_extra={"FORCE_UPDATE": "1"})
assert r.returncode == 0, f"FORCE_UPDATE did not switch back to main: {r.stderr}"
# 8. A release tagged after the clone reaches it: the cockpit's release check and
# `git describe` read the tags, and a fetch of main alone brought none, so a box
# updated by the one-liner kept naming the release it was cloned at (2026-10-01).
git("checkout", "-q", "main", cwd=work)
git("tag", "-a", "v9.9.9", "-m", "a release", cwd=work)
git("push", "-q", "origin", "v9.9.9", cwd=work)
git("tag", "-d", "v9.9.9", cwd=work)
r = run_get(work)
assert r.returncode == 0, f"the update failed: {r.stderr}"
assert git("tag", "-l", "v9.9.9", cwd=work).stdout.strip() == "v9.9.9", "the release tag did not come"
assert git("describe", "--tags", "--abbrev=0", cwd=work).stdout.strip() == "v9.9.9"
# 9. A tag of the same name made here, on another commit, is kept, and the update
# goes on rather than failing on it.
git("tag", "-a", "v9.9.8", "-m", "upstream", cwd=work)
git("push", "-q", "origin", "v9.9.8", cwd=work)
git("tag", "-d", "v9.9.8", cwd=work)
git("commit", "-q", "--allow-empty", "-m", "local", cwd=work)
git("tag", "v9.9.8", cwd=work)
git("reset", "-q", "--hard", "HEAD~1", cwd=work)
r = run_get(work)
assert r.returncode == 0, f"a local tag of the same name failed the update: {r.stderr}"
assert "INSTALL_RAN" in r.stdout, r.stdout
print("test_get_sh: OK")
finally:
shutil.rmtree(base, ignore_errors=True)
if __name__ == "__main__":
sys.exit(main())