Files
Reims-Go/internal/selfupdate/selfupdate_test.go
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

167 lines
5.2 KiB
Go

package selfupdate
import (
"os"
"path/filepath"
"testing"
)
func write(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o755); err != nil {
t.Fatal(err)
}
}
func read(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return string(b)
}
func TestSwapAndCleanup(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "reims-go.exe")
newExe := filepath.Join(t.TempDir(), "reims-go-v9.9.9-windows-amd64.exe")
write(t, exe, "old build")
write(t, newExe, "new build")
write(t, exe+".old", "left over from an earlier update")
if err := Swap(exe, exe, newExe, false); err != nil {
t.Fatal(err)
}
if got := read(t, exe); got != "new build" {
t.Fatalf("exe = %q, want the new build at the original path", got)
}
if got := read(t, exe+".old"); got != "old build" {
t.Fatalf(".old = %q, want the replaced build", got)
}
if _, err := os.Stat(exe + ".new"); !os.IsNotExist(err) {
t.Fatal("staged .new must not remain")
}
Cleanup(exe)
if _, err := os.Stat(exe + ".old"); !os.IsNotExist(err) {
t.Fatal("Cleanup must remove .old")
}
if got := read(t, exe); got != "new build" {
t.Fatal("Cleanup must not touch the executable")
}
}
func TestSwapLeavesExeOnFailure(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "reims-go.exe")
write(t, exe, "old build")
if err := Swap(exe, exe, filepath.Join(dir, "missing.exe"), false); err == nil {
t.Fatal("a missing download must fail")
}
if got := read(t, exe); got != "old build" {
t.Fatalf("exe changed after a failed swap: %q", got)
}
for _, p := range []string{exe + ".new", exe + ".old"} {
if _, err := os.Stat(p); !os.IsNotExist(err) {
t.Errorf("%s left behind after a failed swap", filepath.Base(p))
}
}
}
func TestWritable(t *testing.T) {
if err := Writable(t.TempDir()); err != nil {
t.Fatal(err)
}
if err := Writable(filepath.Join(t.TempDir(), "missing")); err == nil {
t.Fatal("a missing folder is not writable")
}
}
func TestTarget(t *testing.T) {
dir := t.TempDir()
asset := "reims-go-v0.2.6-windows-amd64.exe"
cases := []struct{ running, want string }{
{"reims-go-v0.2.5-windows-amd64.exe", asset}, // release name: follow the version
{"reims-go-v0.2.6-alpha.1-windows-amd64.exe", asset}, // alpha release upgrades to stable name
{"reims-go-v0.2.6-dev-66da194-windows-amd64.exe", asset},
{"Reims-Go-V0.2.5-Windows-AMD64.exe", asset}, // case does not matter
{"reims-go.exe", "reims-go.exe"}, // own name: keep it for shortcuts
{"reims-go-v0.2.5-windows-amd64 (1).exe", "reims-go-v0.2.5-windows-amd64 (1).exe"},
}
for _, c := range cases {
if got := Target(filepath.Join(dir, c.running), asset); got != filepath.Join(dir, c.want) {
t.Errorf("Target(%s) = %s, want %s", c.running, filepath.Base(got), c.want)
}
}
if got := Target(filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe"), "odd.exe"); filepath.Base(got) != "reims-go-v0.2.5-windows-amd64.exe" {
t.Errorf("an asset without a release name must not rename, got %s", filepath.Base(got))
}
}
func TestSwapRenamesReleaseFile(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe")
newExe := filepath.Join(t.TempDir(), "reims-go-v0.2.6-windows-amd64.exe")
write(t, exe, "v0.2.5")
write(t, newExe, "v0.2.6")
target := Target(exe, filepath.Base(newExe))
if err := Swap(exe, target, newExe, false); err != nil {
t.Fatal(err)
}
if got := read(t, target); got != "v0.2.6" {
t.Fatalf("new name holds %q", got)
}
if _, err := os.Stat(exe); !os.IsNotExist(err) {
t.Fatal("the old release name must be gone, not left beside the new one")
}
if got := read(t, exe+".old"); got != "v0.2.5" {
t.Fatalf("old build moved aside = %q", got)
}
// The new version's first start removes the old release's .old file.
Cleanup(target)
if _, err := os.Stat(exe + ".old"); !os.IsNotExist(err) {
t.Fatal("Cleanup must remove the earlier release's .old file")
}
if got := read(t, target); got != "v0.2.6" {
t.Fatal("Cleanup must not touch the running executable")
}
}
func TestSwapKeepOldAndCleanupStaging(t *testing.T) {
dir := t.TempDir()
exe := filepath.Join(dir, "reims-go.exe")
newExe := filepath.Join(t.TempDir(), "reims-go-v0.2.9-dev-abc1234-windows-amd64.exe")
write(t, exe, "current dev-1")
write(t, exe+".old", "stable backup")
write(t, newExe, "new dev-2")
if err := Swap(exe, exe, newExe, true); err != nil {
t.Fatal(err)
}
if got := read(t, exe); got != "new dev-2" {
t.Fatalf("exe = %q, want new dev-2", got)
}
if got := read(t, exe+".old"); got != "stable backup" {
t.Fatalf("existing .old backup must be preserved when keepOld=true, got %q", got)
}
matches, err := filepath.Glob(exe + ".old-*")
if err != nil || len(matches) != 1 || read(t, matches[0]) != "current dev-1" {
t.Fatalf("expected timestamped backup of current dev-1, got %v", matches)
}
write(t, exe+".new", "aborted stage")
CleanupStaging(exe)
if _, err := os.Stat(exe + ".new"); !os.IsNotExist(err) {
t.Fatal("CleanupStaging must remove .new")
}
if _, err := os.Stat(exe + ".old"); err != nil {
t.Fatal("CleanupStaging must not remove .old")
}
if _, err := os.Stat(matches[0]); err != nil {
t.Fatal("CleanupStaging must not remove timestamped .old-*")
}
}