Files
alzeo/tests/test_get_sh_force_update.py
Hasan Basbunar d7da3801cc get.sh: install and update the stable channel, the latest published release; --edge follows main
The one command no longer resets the clone to origin/main: it installs the
latest published release, on a branch of its own (stable), and main becomes
the edge channel, opted into with --edge (back with --stable). The choice is
kept in ~/.config/qwen38/channel; a box installed before, on main, migrates at
its next update without downloading anything the tags do not already need.
The release is found from GitHub's release API, then the remote's tags, then
the tags the update itself fetched; nothing is touched when none answers.

The channels are exercised by a real git fixture with two tagged releases and
a curl shim standing in for the API: the update from N-1 to N, the switch to
edge and back, the fallbacks, the refusals. The older get.sh tests run on the
edge channel, which behaves exactly as get.sh always did.

Generated by Mistral Vibe.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 05:05:16 +01:00

171 lines
8.9 KiB
Python

#!/usr/bin/env python3
"""get.sh brings a clone that is behind up to date, refuses to lose local commits, and
FORCE_UPDATE=1 keeps what it promises to keep, off main too.
The one test of get.sh before this file (test_get_sh.py) used a clone that was never
behind origin, so neither the update, the reason the one-liner exists, nor the refusal of a
clone with local commits ran: a get.sh that installed without updating, or reset local
commits away, passed it (found in review, 2026-09-24).
Off main, get.sh switched to main before it stashed the tracked changes: a change that
conflicted with main ended the run (git's rc 128), and a commit made on a detached HEAD
was left on no branch (found in review, 2026-09-24). Its advice to discard changes also
included `git clean -fd`, which deletes the untracked files it keeps on purpose. This runs
the real get.sh against throwaway git repos, with a throwaway HOME and git limited to
file:// so nothing can reach GitHub, and a stub install.sh."""
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
STUB = '#!/usr/bin/env bash\necho "INSTALL_RAN"\n'
def git(*a, cwd, check=True):
return subprocess.run(["git", *a], cwd=cwd, capture_output=True, text=True, check=check,
env={**os.environ, "GIT_ALLOW_PROTOCOL": "file"})
class Fixture(unittest.TestCase):
def setUp(self):
self.base = pathlib.Path(tempfile.mkdtemp(prefix="get-force-"))
self.addCleanup(shutil.rmtree, self.base, True)
origin = self.base / "hasso5703" / "dgx-spark-qwen38.git"
origin.mkdir(parents=True)
git("init", "-q", "--bare", "-b", "main", ".", cwd=origin)
up = self.base / "up"
git("clone", "-q", str(origin), str(up), cwd=self.base)
for k, v in (("user.email", "t@t"), ("user.name", "t")):
git("config", k, v, cwd=up)
(up / "install.sh").write_text(STUB)
(up / "install.sh").chmod(0o755)
(up / "file.txt").write_text("a\n")
git("add", "-A", cwd=up); git("commit", "-qm", "v1", cwd=up); git("push", "-q", "origin", "main", cwd=up)
self.work = self.base / "work"
git("clone", "-q", str(origin), str(self.work), cwd=self.base)
for k, v in (("user.email", "t@t"), ("user.name", "t")):
git("config", k, v, cwd=self.work)
(up / "file.txt").write_text("b\n") # origin moves on, installer included
(up / "install.sh").write_text(STUB.replace("INSTALL_RAN", "INSTALL_RAN v2"))
git("commit", "-qam", "v2", cwd=up); git("push", "-q", "origin", "main", cwd=up)
self.v2 = git("rev-parse", "HEAD", cwd=up).stdout.strip()
def run_get(self, force=True):
home = self.base / "home"
home.mkdir(exist_ok=True)
# the edge channel: these tests exercise the update machinery (the stash, the copies
# aside, the backup branches), which edge runs exactly as get.sh always did on main.
# The stable channel, the default, has its own file: test_get_sh_channels.py
(home / ".config" / "qwen38").mkdir(parents=True, exist_ok=True)
(home / ".config" / "qwen38" / "channel").write_text("edge\n")
env = {"PATH": os.environ["PATH"], "HOME": str(home), "DIR": str(self.work), "GIT_ALLOW_PROTOCOL": "file"}
if force:
env["FORCE_UPDATE"] = "1"
return subprocess.run(["bash", str(REPO / "get.sh")], capture_output=True, text=True, timeout=60,
cwd=str(self.base), env=env)
def commit_locally(self):
(self.work / "mine.txt").write_text("my work\n")
git("add", "mine.txt", cwd=self.work); git("commit", "-qm", "local work", cwd=self.work)
return git("rev-parse", "HEAD", cwd=self.work).stdout.strip()
def at_origin(self):
return git("rev-parse", "HEAD", cwd=self.work).stdout.strip() == self.v2
class OnMain(Fixture):
def test_a_clone_behind_origin_is_updated_before_the_installer_runs(self):
r = self.run_get(force=False)
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertTrue(self.at_origin(), "the clone was left behind origin")
self.assertIn("INSTALL_RAN v2", r.stdout, "the installer that ran is not origin's")
def test_local_commits_are_refused_and_left_alone(self):
local = self.commit_locally()
r = self.run_get(force=False)
self.assertNotEqual(r.returncode, 0, r.stdout)
self.assertIn("has local commits not on origin/main", r.stderr)
self.assertNotIn("INSTALL_RAN", r.stdout)
self.assertEqual(git("rev-parse", "HEAD", cwd=self.work).stdout.strip(), local, "the local commit was reset away")
def test_local_commits_under_force_are_kept_on_a_branch(self):
local = self.commit_locally()
r = self.run_get()
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertTrue(self.at_origin())
self.assertIn("INSTALL_RAN v2", r.stdout)
holders = git("branch", "--contains", local, cwd=self.work).stdout
self.assertIn("backup-", holders, "the local commit is on no branch")
class OffMain(Fixture):
def test_a_side_branch_with_a_conflicting_change(self):
git("checkout", "-qb", "feature", cwd=self.work)
(self.work / "file.txt").write_text("feat\n")
git("commit", "-qam", "feature work", cwd=self.work)
(self.work / "file.txt").write_text("mine, not committed\n")
r = self.run_get()
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertIn("INSTALL_RAN", r.stdout)
self.assertTrue(self.at_origin())
self.assertIn("get.sh auto-stash", git("stash", "list", cwd=self.work).stdout)
self.assertEqual(git("show", "feature:file.txt", cwd=self.work).stdout, "feat\n", "the branch was lost")
def test_a_commit_on_a_detached_head_is_kept_on_a_branch(self):
git("checkout", "-q", "--detach", cwd=self.work)
(self.work / "notes.txt").write_text("x\n")
git("add", "notes.txt", cwd=self.work); git("commit", "-qm", "local on detached", cwd=self.work)
local = git("rev-parse", "HEAD", cwd=self.work).stdout.strip()
r = self.run_get()
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertTrue(self.at_origin())
holders = git("branch", "--contains", local, cwd=self.work).stdout
self.assertTrue(holders.strip(), "the detached commit is on no branch")
def test_an_ignored_file_the_switch_to_main_writes_over_is_copied_aside_first(self):
# code review of v1.45.0: off main, FORCE_UPDATE switches to main before its reset, and git's checkout writes
# over an ignored file at a path main tracks, without a word: the copy-aside looked only after the switch
git("checkout", "-qb", "side", cwd=self.work)
(self.work / ".gitignore").write_text("notes.md\n")
git("add", ".gitignore", cwd=self.work); git("commit", "-qm", "side ignores notes.md", cwd=self.work)
git("checkout", "-q", "main", cwd=self.work)
(self.work / "notes.md").write_text("main's notes\n")
git("add", "notes.md", cwd=self.work); git("commit", "-qm", "local main tracks notes.md", cwd=self.work)
git("checkout", "-q", "side", cwd=self.work)
(self.work / "notes.md").write_text("my notes\n") # ignored on side
r = self.run_get()
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertTrue(self.at_origin())
kept = sorted((self.base / "home" / ".local/share/alzeo").glob("kept-before-update-*/notes.md"))
self.assertEqual([k.read_text() for k in kept], ["my notes\n"], r.stdout)
def test_an_untracked_file_where_main_has_one_does_not_stop_the_switch(self):
# third review of v1.45.0: git's checkout refused an untracked file at a path main tracks, then its fallback
# (-b main) said a branch named main already exists, rc 128, the copy aside never said
git("checkout", "-q", "main", cwd=self.work)
(self.work / "plan.md").write_text("main's plan\n")
git("add", "plan.md", cwd=self.work); git("commit", "-qm", "local main tracks plan.md", cwd=self.work)
git("checkout", "-q", "HEAD~1", cwd=self.work) # detached, before plan.md
git("checkout", "-qb", "side", cwd=self.work)
(self.work / "plan.md").write_text("my plan\n") # untracked here, not ignored
r = self.run_get()
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertTrue(self.at_origin())
kept = sorted((self.base / "home" / ".local/share/alzeo").glob("kept-before-update-*/plan.md"))
self.assertEqual([k.read_text() for k in kept], ["my plan\n"], r.stdout)
class TheAdviceKeepsUntrackedFiles(unittest.TestCase):
def test_no_git_clean_in_the_advice(self):
text = (REPO / "get.sh").read_text()
self.assertNotIn("git -C $DIR clean", text)
if __name__ == "__main__":
unittest.main(verbosity=2)