Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 8m21s
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package apiversion
|
|
|
|
import "testing"
|
|
|
|
func TestParse(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
raw string
|
|
valid bool
|
|
version Version
|
|
}{
|
|
{name: "empty", raw: "", valid: false},
|
|
{name: "invalid text", raw: "abc", valid: false},
|
|
{name: "missing patch", raw: "1.0", valid: false},
|
|
{name: "exact", raw: "1.0.0", valid: true, version: Version{Major: 1, Minor: 0, Patch: 0}},
|
|
{name: "with prefix", raw: "v1.2.3", valid: true, version: Version{Major: 1, Minor: 2, Patch: 3}},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
version, ok := Parse(tt.raw)
|
|
if ok != tt.valid {
|
|
t.Fatalf("expected valid=%v, got %v", tt.valid, ok)
|
|
}
|
|
if tt.valid && version != tt.version {
|
|
t.Fatalf("expected version=%+v, got %+v", tt.version, version)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUseLatest(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
header string
|
|
threshold string
|
|
expect bool
|
|
}{
|
|
{name: "missing header", header: "", threshold: "1.0.0", expect: false},
|
|
{name: "invalid header", header: "invalid", threshold: "1.0.0", expect: false},
|
|
{name: "equal threshold", header: "1.0.0", threshold: "1.0.0", expect: false},
|
|
{name: "greater threshold", header: "1.0.1", threshold: "1.0.0", expect: true},
|
|
{name: "greater with v prefix", header: "v1.2.3", threshold: "1.0.0", expect: true},
|
|
{name: "less than threshold", header: "0.9.9", threshold: "1.0.0", expect: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := UseLatest(tt.header, tt.threshold)
|
|
if result != tt.expect {
|
|
t.Fatalf("expected %v, got %v", tt.expect, result)
|
|
}
|
|
})
|
|
}
|
|
}
|