Files
ghosttyconf/selfupdate_test.go
T
mikeandClaude Opus 5.5 fb5602452a Add self-update, version 1.0.1
ghosttyconf --update installs the newest release from git.micw.org,
--check-update only checks. The download lands next to the running
binary, is started once with --version and only then replaces it.

Once a day a normal start asks in the background and mentions a newer
release in the header, at the top of the help and on stderr after
quitting; only on a terminal and not with GHOSTTYCONF_NO_UPDATE_CHECK
set. selfupdate.go is nscan's copyable template, with goca's --update
spelling in the hints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:22:51 +01:00

156 lines
4.4 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestUpdateCompare(t *testing.T) {
tests := []struct {
a, b string
want int
}{
{"1.0.1", "1.0.0", 1},
{"1.0.10", "1.0.9", 1},
{"v1.0.2", "1.0.2", 0},
{"1.1", "1.1.0", 0},
{"1.0.2-rc1", "1.0.2", -1},
{"0.9.9", "1.0.0", -1},
}
for _, tt := range tests {
if got := updateCompare(tt.a, tt.b); got != tt.want {
t.Errorf("updateCompare(%q, %q) = %d, want %d", tt.a, tt.b, got, tt.want)
}
}
}
func TestUpdateAPIBase(t *testing.T) {
got, err := selfUpdate.apiBase()
if err != nil || got != "https://git.micw.org/api/v1/repos/mike/ghosttyconf" {
t.Errorf("apiBase = %q, %v", got, err)
}
if _, err := (selfUpdater{repo: "https://git.micw.org/ghosttyconf"}).apiBase(); err == nil {
t.Error("repo without owner accepted")
}
}
// fakeForge serves a Gitea release with tag 9.9.9 whose asset is a shell
// script answering --version like ghosttyconf does.
func fakeForge(t *testing.T, size int64) (*httptest.Server, []byte) {
t.Helper()
bin := []byte("#!/bin/sh\necho ghosttyconf 9.9.9\n")
if size == 0 {
size = int64(len(bin))
}
mux := http.NewServeMux()
var srv *httptest.Server
mux.HandleFunc("/api/v1/repos/mike/ghosttyconf/releases/latest", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{
"tag_name": "9.9.9",
"html_url": srv.URL + "/mike/ghosttyconf/releases/tag/9.9.9",
"assets": []map[string]any{{
"name": fmt.Sprintf("ghosttyconf-%s-%s", runtime.GOOS, runtime.GOARCH),
"size": size,
"browser_download_url": srv.URL + "/download",
}},
})
})
mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) { w.Write(bin) })
srv = httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv, bin
}
// isolateCache keeps the update note out of the real cache directory.
func isolateCache(t *testing.T) {
dir := t.TempDir()
t.Setenv("HOME", dir)
t.Setenv("XDG_CACHE_HOME", dir)
}
func TestUpdateCheck(t *testing.T) {
isolateCache(t)
srv, _ := fakeForge(t, 0)
u := selfUpdater{repo: srv.URL + "/mike/ghosttyconf", asset: "ghosttyconf", current: "1.0.1"}
var out bytes.Buffer
if err := u.check(&out); err != nil {
t.Fatal(err)
}
if !strings.Contains(out.String(), "ghosttyconf 9.9.9 is available, running 1.0.1") ||
!strings.Contains(out.String(), "run 'ghosttyconf --update'") {
t.Errorf("check said %q", out.String())
}
if st := u.loadState(); st.Latest != "9.9.9" || st.Checked.IsZero() {
t.Errorf("note after check: %+v", st)
}
out.Reset()
u.current = "9.9.9"
if err := u.check(&out); err != nil || !strings.Contains(out.String(), "up to date") {
t.Errorf("check on the newest version said %q, %v", out.String(), err)
}
}
func TestUpdateDownloadProbeReplace(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake binary is a shell script")
}
isolateCache(t)
srv, bin := fakeForge(t, 0)
u := selfUpdater{repo: srv.URL + "/mike/ghosttyconf", asset: "ghosttyconf", current: "1.0.1", verify: []string{"--version"}}
rel, err := u.latest()
if err != nil {
t.Fatal(err)
}
exe := filepath.Join(t.TempDir(), "ghosttyconf")
if err := os.WriteFile(exe, []byte("old"), 0o755); err != nil {
t.Fatal(err)
}
tmp, err := u.download(&rel.Assets[0], exe, 0o755)
if err != nil {
t.Fatal(err)
}
if filepath.Dir(tmp) != filepath.Dir(exe) {
t.Errorf("download landed in %s, not next to the binary", tmp)
}
if err := u.probe(tmp, "9.9.8"); err == nil {
t.Error("probe accepted a binary reporting another version")
}
if err := u.probe(tmp, rel.TagName); err != nil {
t.Fatal(err)
}
if err := updateReplace(tmp, exe); err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(exe); !bytes.Equal(got, bin) {
t.Errorf("binary after replace: %q", got)
}
}
func TestUpdateRejectsShortDownload(t *testing.T) {
isolateCache(t)
srv, _ := fakeForge(t, 1<<20) // the release claims more than is served
u := selfUpdater{repo: srv.URL + "/mike/ghosttyconf", asset: "ghosttyconf", current: "1.0.1"}
rel, err := u.latest()
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
if _, err := u.download(&rel.Assets[0], filepath.Join(dir, "ghosttyconf"), 0o755); err == nil {
t.Fatal("short download accepted")
}
if left, _ := os.ReadDir(dir); len(left) != 0 {
t.Errorf("leftovers after a failed download: %v", left)
}
}