test: add update version and checksum tests

This commit is contained in:
Mathis Maquenne
2026-08-05 11:50:58 +02:00
parent 3fe050ea15
commit 66fda7424a
2 changed files with 108 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package update
import (
"strings"
"testing"
)
func TestParseChecksums(t *testing.T) {
checksums := `ABCDEF0123 godeez_1.0.0_darwin_arm64
deadbeef *godeez_1.0.0_linux_amd64
malformed-line
one two three
cafebabe godeez_1.0.0_windows_amd64.exe
`
tests := []struct {
name string
asset string
want string
}{
{"plain name lowercased", "godeez_1.0.0_darwin_arm64", "abcdef0123"},
{"star-prefixed name", "godeez_1.0.0_linux_amd64", "deadbeef"},
{"windows asset", "godeez_1.0.0_windows_amd64.exe", "cafebabe"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseChecksums(strings.NewReader(checksums), tt.asset)
if err != nil {
t.Fatalf("parseChecksums: %v", err)
}
if got != tt.want {
t.Errorf("parseChecksums() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseChecksumsMissing(t *testing.T) {
if _, err := parseChecksums(strings.NewReader("abc other_asset\n"), "godeez_1.0.0_darwin_arm64"); err == nil {
t.Error("expected error for missing asset name")
}
}
+65
View File
@@ -0,0 +1,65 @@
package update
import "testing"
func TestIsNewer(t *testing.T) {
tests := []struct {
current string
latest string
want bool
}{
{"1.0.0", "1.0.1", true},
{"v1.0.0", "v1.1.0", true},
{"1.0.0", "v2.0.0", true},
{"1.0.0", "1.0.0", false},
{"1.1.0", "1.0.0", false},
{" 1.0.0 ", "1.0.1", true},
{"1.0.0", "1.0.1-rc.1", true},
{"1.0.0-rc.1", "1.0.0", true},
{"dev", "1.0.0", false},
{"1.0.0", "not-a-version", false},
{"", "1.0.0", false},
{"1.0.0", "", false},
}
for _, tt := range tests {
if got := IsNewer(tt.current, tt.latest); got != tt.want {
t.Errorf("IsNewer(%q, %q) = %v, want %v", tt.current, tt.latest, got, tt.want)
}
}
}
func TestTrimV(t *testing.T) {
tests := []struct {
in string
want string
}{
{"v1.2.3", "1.2.3"},
{"1.2.3", "1.2.3"},
{" v1.2.3 ", "1.2.3"},
}
for _, tt := range tests {
if got := trimV(tt.in); got != tt.want {
t.Errorf("trimV(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestCanonical(t *testing.T) {
tests := []struct {
in string
want string
}{
{"1.2.3", "v1.2.3"},
{"v1.2.3", "v1.2.3"},
{"", ""},
{"garbage", ""},
}
for _, tt := range tests {
if got := canonical(tt.in); got != tt.want {
t.Errorf("canonical(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}