Add unit tests for ParseVersion function

Tests basic version parsing, v prefix stripping, prerelease,
build metadata, and error cases for invalid input.
This commit is contained in:
rcourtman 2025-11-29 16:00:44 +00:00
parent 3ed190e8bc
commit a4ef95ef9e

View file

@ -53,6 +53,88 @@ func TestNormalizeVersionString(t *testing.T) {
}
}
func TestParseVersion(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
wantErr bool
wantMajor int
wantMinor int
wantPatch int
wantPrerel string
wantBuild string
}{
{
name: "basic version",
input: "4.24.0",
wantMajor: 4, wantMinor: 24, wantPatch: 0,
},
{
name: "with v prefix",
input: "v4.24.0",
wantMajor: 4, wantMinor: 24, wantPatch: 0,
},
{
name: "with prerelease",
input: "4.24.0-rc.3",
wantMajor: 4, wantMinor: 24, wantPatch: 0,
wantPrerel: "rc.3",
},
{
name: "with build metadata",
input: "4.24.0+build.123",
wantMajor: 4, wantMinor: 24, wantPatch: 0,
wantBuild: "build.123",
},
{
name: "with prerelease and build",
input: "4.24.0-rc.3+build.123",
wantMajor: 4, wantMinor: 24, wantPatch: 0,
wantPrerel: "rc.3",
wantBuild: "build.123",
},
{
name: "invalid format",
input: "not-a-version",
wantErr: true,
},
{
name: "empty string",
input: "",
wantErr: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := ParseVersion(tc.input)
if tc.wantErr {
if err == nil {
t.Fatalf("ParseVersion(%q) expected error, got nil", tc.input)
}
return
}
if err != nil {
t.Fatalf("ParseVersion(%q) unexpected error: %v", tc.input, err)
}
if got.Major != tc.wantMajor || got.Minor != tc.wantMinor || got.Patch != tc.wantPatch {
t.Fatalf("ParseVersion(%q) = %d.%d.%d, want %d.%d.%d",
tc.input, got.Major, got.Minor, got.Patch, tc.wantMajor, tc.wantMinor, tc.wantPatch)
}
if got.Prerelease != tc.wantPrerel {
t.Fatalf("ParseVersion(%q).Prerelease = %q, want %q", tc.input, got.Prerelease, tc.wantPrerel)
}
if got.Build != tc.wantBuild {
t.Fatalf("ParseVersion(%q).Build = %q, want %q", tc.input, got.Build, tc.wantBuild)
}
})
}
}
func TestDetectChannelFromVersion(t *testing.T) {
t.Parallel()