2021-01-17 13:06:23 +01:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_stripTagPrefix(t *testing.T) {
|
2022-04-24 14:37:52 +02:00
|
|
|
tests := []struct {
|
2021-01-17 13:06:23 +01:00
|
|
|
Before string
|
|
|
|
After string
|
|
|
|
}{
|
|
|
|
{"refs/tags/1.0.0", "1.0.0"},
|
|
|
|
{"refs/tags/v1.0.0", "1.0.0"},
|
|
|
|
{"v1.0.0", "1.0.0"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got, want := stripTagPrefix(test.Before), test.After
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Got tag %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultTags(t *testing.T) {
|
2022-04-24 14:37:52 +02:00
|
|
|
tests := []struct {
|
2021-01-17 13:06:23 +01:00
|
|
|
Before string
|
|
|
|
After []string
|
|
|
|
}{
|
|
|
|
{"", []string{"latest"}},
|
2022-01-12 09:09:57 +01:00
|
|
|
{"refs/heads/main", []string{"latest"}},
|
2021-01-17 13:06:23 +01:00
|
|
|
{"refs/tags/0.9.0", []string{"0.9", "0.9.0"}},
|
|
|
|
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}},
|
|
|
|
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}},
|
|
|
|
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
tags, err := DefaultTags(test.Before)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, want := tags, test.After
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Got tag %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultTagsError(t *testing.T) {
|
2022-04-24 14:37:52 +02:00
|
|
|
tests := []string{
|
2021-01-17 13:06:23 +01:00
|
|
|
"refs/tags/x1.0.0",
|
|
|
|
"refs/tags/20190203",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
_, err := DefaultTags(test)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expect tag error for %s", test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultTagSuffix(t *testing.T) {
|
2022-04-24 14:37:52 +02:00
|
|
|
tests := []struct {
|
2021-01-17 13:06:23 +01:00
|
|
|
Before string
|
|
|
|
Suffix string
|
|
|
|
After []string
|
|
|
|
}{
|
|
|
|
// without suffix
|
|
|
|
{
|
|
|
|
After: []string{"latest"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Before: "refs/tags/v1.0.0",
|
|
|
|
After: []string{
|
|
|
|
"1",
|
|
|
|
"1.0",
|
|
|
|
"1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// with suffix
|
|
|
|
{
|
|
|
|
Suffix: "linux-amd64",
|
|
|
|
After: []string{"linux-amd64"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Before: "refs/tags/v1.0.0",
|
|
|
|
Suffix: "linux-amd64",
|
|
|
|
After: []string{
|
|
|
|
"1-linux-amd64",
|
|
|
|
"1.0-linux-amd64",
|
|
|
|
"1.0.0-linux-amd64",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Suffix: "nanoserver",
|
|
|
|
After: []string{"nanoserver"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Before: "refs/tags/v1.9.2",
|
|
|
|
Suffix: "nanoserver",
|
|
|
|
After: []string{
|
|
|
|
"1-nanoserver",
|
|
|
|
"1.9-nanoserver",
|
|
|
|
"1.9.2-nanoserver",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Before: "refs/tags/v18.06.0",
|
|
|
|
Suffix: "nanoserver",
|
|
|
|
After: []string{
|
|
|
|
"18-nanoserver",
|
|
|
|
"18.06-nanoserver",
|
|
|
|
"18.06.0-nanoserver",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
tag, err := DefaultTagSuffix(test.Before, test.Suffix)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, want := tag, test.After
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Got tag %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_stripHeadPrefix(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
ref string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: args{
|
2022-01-12 09:09:57 +01:00
|
|
|
ref: "refs/heads/main",
|
2021-01-17 13:06:23 +01:00
|
|
|
},
|
2022-01-12 09:09:57 +01:00
|
|
|
want: "main",
|
2021-01-17 13:06:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if got := stripHeadPrefix(tt.args.ref); got != tt.want {
|
|
|
|
t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUseDefaultTag(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
ref string
|
|
|
|
defaultBranch string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "latest tag for default branch",
|
|
|
|
args: args{
|
2022-01-12 09:09:57 +01:00
|
|
|
ref: "refs/heads/main",
|
|
|
|
defaultBranch: "main",
|
2021-01-17 13:06:23 +01:00
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "build from tags",
|
|
|
|
args: args{
|
|
|
|
ref: "refs/tags/v1.0.0",
|
2022-01-12 09:09:57 +01:00
|
|
|
defaultBranch: "main",
|
2021-01-17 13:06:23 +01:00
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "skip build for not default branch",
|
|
|
|
args: args{
|
|
|
|
ref: "refs/heads/develop",
|
2022-01-12 09:09:57 +01:00
|
|
|
defaultBranch: "main",
|
2021-01-17 13:06:23 +01:00
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if got := UseDefaultTag(tt.args.ref, tt.args.defaultBranch); got != tt.want {
|
|
|
|
t.Errorf("%q. UseDefaultTag() = %v, want %v", tt.name, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|