banger/internal/cli/commands_update_test.go
Thales Maciel d867d61eb3
update: refresh install.toml commit + built_at from new binary
After `banger update` swaps binaries, install.toml needs to reflect
the just-installed identity. The previous code passed
buildinfo.Current().{Commit,BuiltAt} into installmeta.UpdateBuildInfo
— but buildinfo.Current() in the running CLI is the OLD pre-swap
binary's identity (we're it), not the staged one. install.toml's
version field got refreshed to target.Version while commit and
built_at stayed pinned at the previous release. `banger doctor`
compares the running CLI's three fields against install.toml's
three fields and so raised a false-positive drift warning on
every update.

Fix: after the swap, exec /usr/local/bin/banger version, parse the
three-line output, and write all three fields to install.toml. If
the exec fails for any reason we fall back to the old behaviour
(version + stale commit/built_at) with a warning, since install.toml
drift is a doctor warning not a broken host — same posture as
before for the failure path.

The parser is split out (parseVersionOutput) and table-tested:
happy path, whitespace-tolerance, missing-field rejection, empty
input rejection, ignoring unrelated lines.

Caught by running v0.1.0 → v0.1.1 live as the first end-to-end
smoke test of the self-update flow, which was the whole point of
that exercise.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 14:38:59 -03:00

79 lines
1.8 KiB
Go

package cli
import "testing"
func TestParseVersionOutput(t *testing.T) {
cases := []struct {
name string
in string
wantVersion string
wantCommit string
wantBuilt string
wantErr bool
}{
{
name: "happy path — three-line shape from banger version",
in: `version: v0.1.2
commit: a0b5c7fa3ca95a37ba99b35280fc75e5647b59e8
built_at: 2026-04-29T17:34:45Z
`,
wantVersion: "v0.1.2",
wantCommit: "a0b5c7fa3ca95a37ba99b35280fc75e5647b59e8",
wantBuilt: "2026-04-29T17:34:45Z",
},
{
name: "tolerates extra whitespace around the values",
in: ` version : v0.1.2
commit : abc123
built_at : 2026-01-01T00:00:00Z`,
wantVersion: "v0.1.2",
wantCommit: "abc123",
wantBuilt: "2026-01-01T00:00:00Z",
},
{
name: "missing commit field is rejected",
in: "version: v0.1.2\nbuilt_at: 2026-01-01T00:00:00Z\n",
wantErr: true,
},
{
name: "empty input is rejected",
in: "",
wantErr: true,
},
{
name: "unrelated lines are ignored",
in: `banger v0.1.2
some other diagnostic line: with a colon
version: v0.1.2
commit: abc
built_at: 2026-01-01T00:00:00Z
`,
wantVersion: "v0.1.2",
wantCommit: "abc",
wantBuilt: "2026-01-01T00:00:00Z",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := parseVersionOutput(tc.in)
if tc.wantErr {
if err == nil {
t.Fatalf("want error, got nil; parsed=%+v", got)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Version != tc.wantVersion {
t.Errorf("Version: got %q, want %q", got.Version, tc.wantVersion)
}
if got.Commit != tc.wantCommit {
t.Errorf("Commit: got %q, want %q", got.Commit, tc.wantCommit)
}
if got.BuiltAt != tc.wantBuilt {
t.Errorf("BuiltAt: got %q, want %q", got.BuiltAt, tc.wantBuilt)
}
})
}
}