101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestParseVersion(t *testing.T) {
|
|
major, minor, patch, err := parseVersion("v0.0.0", "v")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if major != 0 || minor != 0 || patch != 0 {
|
|
t.Fatal("version does not match")
|
|
}
|
|
}
|
|
|
|
func TestParseTaggedVersion(t *testing.T) {
|
|
version, err := parseTaggedVersion("v0.10.50-meda.2", "v")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if version.major != 0 || version.minor != 10 || version.patch != 50 {
|
|
t.Fatalf("base version does not match: %+v", version)
|
|
}
|
|
if !version.hasSuffix || version.suffixLabel != "meda" || version.suffixRevision != 2 {
|
|
t.Fatalf("suffix does not match: %+v", version)
|
|
}
|
|
}
|
|
|
|
func TestParseVersionRejectsSuffix(t *testing.T) {
|
|
if _, _, _, err := parseVersion("v0.10.50-meda.2", "v"); err == nil {
|
|
t.Fatal("expected suffixed version to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestGetEnvActionInput(t *testing.T) {
|
|
t.Setenv("BUMP", "")
|
|
t.Setenv("SUFFIX", "")
|
|
t.Setenv("INPUT_BUMP", "suffix")
|
|
t.Setenv("INPUT_SUFFIX", "meda")
|
|
|
|
if value := getEnv("BUMP", ""); value != "suffix" {
|
|
t.Fatalf("expected suffix bump, got %s", value)
|
|
}
|
|
if value := getEnv("SUFFIX", ""); value != "meda" {
|
|
t.Fatalf("expected meda suffix, got %s", value)
|
|
}
|
|
}
|
|
|
|
func TestCalculateSuffixVersion(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
version taggedVersion
|
|
suffix string
|
|
expected string
|
|
expectsErr bool
|
|
}{
|
|
{
|
|
name: "first suffix",
|
|
version: taggedVersion{major: 0, minor: 10, patch: 50},
|
|
suffix: "meda",
|
|
expected: "v0.10.50-meda.1",
|
|
},
|
|
{
|
|
name: "increment suffix",
|
|
version: taggedVersion{major: 0, minor: 10, patch: 50, suffixLabel: "meda", suffixRevision: 1, hasSuffix: true},
|
|
suffix: "meda",
|
|
expected: "v0.10.50-meda.2",
|
|
},
|
|
{
|
|
name: "missing label",
|
|
version: taggedVersion{major: 0, minor: 10, patch: 50},
|
|
expectsErr: true,
|
|
},
|
|
{
|
|
name: "label mismatch",
|
|
version: taggedVersion{major: 0, minor: 10, patch: 50, suffixLabel: "meda", suffixRevision: 1, hasSuffix: true},
|
|
suffix: "other",
|
|
expectsErr: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
actual, err := calculateSuffixVersion(test.version, test.suffix, "v")
|
|
if test.expectsErr {
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual != test.expected {
|
|
t.Fatalf("expected %s, got %s", test.expected, actual)
|
|
}
|
|
})
|
|
}
|
|
}
|