Add self-update, release 1.0.2

nscan update installs the newest release from git.micw.org, nscan
update -c only checks; --update and --check-update work as well. The
download lands next to the running binary, is started once with
--version and only then replaces it.

Once a day an ordinary run asks in the background and mentions a newer
release in the title bar and on stderr, only on a terminal and not with
NSCAN_NO_UPDATE_CHECK set. selfupdate.go is goca's copyable template.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mikeandClaude Opus 5.5 committed 2026-10-26 16:25:36 +01:00
1 parent 60257239d7
commit c91dbe9f7d
11 files changed
+761 -16

No files matched your search

+155
View File
@@ -0,0 +1,155 @@
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/nscan" {
t.Errorf("apiBase = %q, %v", got, err)
}
if _, err := (selfUpdater{repo: "https://git.micw.org/nscan"}).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 nscan does.
func fakeForge(t *testing.T, size int64) (*httptest.Server, []byte) {
t.Helper()
bin := []byte("#!/bin/sh\necho nscan 9.9.9\n")
if size == 0 {
size = int64(len(bin))
}
mux := http.NewServeMux()
var srv *httptest.Server
mux.HandleFunc("/api/v1/repos/mike/nscan/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/nscan/releases/tag/9.9.9",
"assets": []map[string]any{{
"name": fmt.Sprintf("nscan-%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/nscan", asset: "nscan", current: "1.0.1"}
var out bytes.Buffer
if err := u.check(&out); err != nil {
t.Fatal(err)
}
if !strings.Contains(out.String(), "nscan 9.9.9 is available, running 1.0.1") ||
!strings.Contains(out.String(), "run 'nscan 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/nscan", asset: "nscan", current: "1.0.1", verify: []string{"--version"}}
rel, err := u.latest()
if err != nil {
t.Fatal(err)
}
exe := filepath.Join(t.TempDir(), "nscan")
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/nscan", asset: "nscan", 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, "nscan"), 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)
}
}