2023-12-05 09:03:18 +00:00
|
|
|
package tag
|
2023-12-05 08:51:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2024-03-11 08:23:17 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-12-05 08:51:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_stripTagPrefix(t *testing.T) {
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
2024-03-11 08:23:17 +00:00
|
|
|
before string
|
|
|
|
after string
|
2023-12-05 08:51:56 +00:00
|
|
|
}{
|
2024-03-11 10:52:52 +00:00
|
|
|
{name: "strip ref", before: "refs/tags/1.0.0", after: "1.0.0"},
|
|
|
|
{name: "strip ref and version prefix", before: "refs/tags/v1.0.0", after: "1.0.0"},
|
|
|
|
{name: "strip version prefix", before: "v1.0.0", after: "1.0.0"},
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 08:23:17 +00:00
|
|
|
for _, tt := range tests {
|
2024-03-11 10:52:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := stripTagPrefix(tt.before)
|
|
|
|
assert.Equal(t, got, tt.after)
|
|
|
|
})
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemverTagsStrict(t *testing.T) {
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
2024-03-11 08:23:17 +00:00
|
|
|
before string
|
|
|
|
after []string
|
2023-12-05 08:51:56 +00:00
|
|
|
}{
|
2024-03-11 10:52:52 +00:00
|
|
|
{name: "empty", before: "", after: []string{"latest"}},
|
|
|
|
{name: "main branch", before: "refs/heads/main", after: []string{"latest"}},
|
|
|
|
{name: "zero version", before: "refs/tags/0.9.0", after: []string{"0.9", "0.9.0"}},
|
|
|
|
{name: "regular version", before: "refs/tags/1.0.0", after: []string{"1", "1.0", "1.0.0"}},
|
|
|
|
{name: "regular version with version prefix", before: "refs/tags/v1.0.0", after: []string{"1", "1.0", "1.0.0"}},
|
|
|
|
{name: "regular version with meta", before: "refs/tags/v1.0.0+1", after: []string{"1", "1.0", "1.0.0"}},
|
|
|
|
{name: "regular pre-release version count", before: "refs/tags/v1.0.0-alpha.1", after: []string{"1.0.0-alpha.1"}},
|
|
|
|
{name: "regular pre-release version", before: "refs/tags/v1.0.0-alpha", after: []string{"1.0.0-alpha"}},
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 08:23:17 +00:00
|
|
|
for _, tt := range tests {
|
2024-03-11 10:52:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tags, err := SemverTags(tt.before, true)
|
|
|
|
assert.NoError(t, err)
|
2023-12-05 08:51:56 +00:00
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
got, want := tags, tt.after
|
|
|
|
assert.Equal(t, got, want)
|
|
|
|
})
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemverTags(t *testing.T) {
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
|
|
|
before string
|
|
|
|
after []string
|
|
|
|
strict bool
|
|
|
|
wantErr error
|
2023-12-05 08:51:56 +00:00
|
|
|
}{
|
2024-03-11 10:52:52 +00:00
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
before: "",
|
|
|
|
after: []string{"latest"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "main branch",
|
|
|
|
before: "refs/heads/main",
|
|
|
|
after: []string{"latest"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero version",
|
|
|
|
before: "refs/tags/0.9.0",
|
|
|
|
after: []string{"0.9", "0.9.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version",
|
|
|
|
before: "refs/tags/1.0.0",
|
|
|
|
after: []string{"1", "1.0", "1.0.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version with prefix",
|
|
|
|
before: "refs/tags/v1.0.0",
|
|
|
|
after: []string{"1", "1.0", "1.0.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version with meta",
|
|
|
|
before: "refs/tags/v1.0.0+1",
|
|
|
|
after: []string{"1", "1.0", "1.0.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular prerelease version count",
|
|
|
|
before: "refs/tags/v1.0.0-alpha.1",
|
|
|
|
after: []string{"1.0.0-alpha.1"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular prerelease version",
|
|
|
|
before: "refs/tags/v1.0.0-alpha",
|
|
|
|
after: []string{"1.0.0-alpha"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prerelease version",
|
|
|
|
before: "refs/tags/v1.0-alpha",
|
|
|
|
after: []string{"1.0.0-alpha"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version",
|
|
|
|
before: "refs/tags/22.04.0",
|
|
|
|
after: []string{"22", "22.4", "22.4.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version",
|
|
|
|
before: "refs/tags/22.04",
|
|
|
|
after: []string{"22", "22.4", "22.4.0"},
|
|
|
|
strict: false,
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid semver",
|
|
|
|
before: "refs/tags/x1.0.0",
|
|
|
|
strict: true,
|
|
|
|
wantErr: assert.AnError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "date tag",
|
|
|
|
before: "refs/tags/20190203",
|
|
|
|
strict: true,
|
|
|
|
wantErr: assert.AnError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version",
|
|
|
|
before: "refs/tags/22.04.0",
|
|
|
|
strict: true,
|
|
|
|
wantErr: assert.AnError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular version shorthand",
|
|
|
|
before: "refs/tags/22.04",
|
|
|
|
strict: true,
|
|
|
|
wantErr: assert.AnError,
|
|
|
|
},
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
tags, err := SemverTags(tt.before, tt.strict)
|
|
|
|
if tt.wantErr != nil {
|
|
|
|
assert.Error(t, err)
|
2023-12-05 08:51:56 +00:00
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-12-05 08:51:56 +00:00
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tags, tt.after)
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemverTagSuffix(t *testing.T) {
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
2024-03-11 08:23:17 +00:00
|
|
|
before string
|
|
|
|
suffix string
|
|
|
|
after []string
|
2023-12-05 08:51:56 +00:00
|
|
|
}{
|
|
|
|
// without suffix
|
|
|
|
{
|
2024-03-11 10:52:52 +00:00
|
|
|
name: "empty ref",
|
2024-03-11 08:23:17 +00:00
|
|
|
after: []string{"latest"},
|
2023-12-05 08:51:56 +00:00
|
|
|
},
|
|
|
|
{
|
2024-03-11 10:52:52 +00:00
|
|
|
name: "ref without suffix",
|
2024-03-11 08:23:17 +00:00
|
|
|
before: "refs/tags/v1.0.0",
|
|
|
|
after: []string{
|
2023-12-05 08:51:56 +00:00
|
|
|
"1",
|
|
|
|
"1.0",
|
|
|
|
"1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// with suffix
|
|
|
|
{
|
2024-03-11 10:52:52 +00:00
|
|
|
name: "empty ref with suffix",
|
2024-03-11 08:23:17 +00:00
|
|
|
suffix: "linux-amd64",
|
|
|
|
after: []string{"linux-amd64"},
|
2023-12-05 08:51:56 +00:00
|
|
|
},
|
|
|
|
{
|
2024-03-11 10:52:52 +00:00
|
|
|
name: "ref with suffix",
|
2024-03-11 08:23:17 +00:00
|
|
|
before: "refs/tags/v1.0.0",
|
|
|
|
suffix: "linux-amd64",
|
|
|
|
after: []string{
|
2023-12-05 08:51:56 +00:00
|
|
|
"1-linux-amd64",
|
|
|
|
"1.0-linux-amd64",
|
|
|
|
"1.0.0-linux-amd64",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-11 08:23:17 +00:00
|
|
|
for _, tt := range tests {
|
2024-03-11 10:52:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tag, err := SemverTagSuffix(tt.before, tt.suffix, true)
|
|
|
|
assert.NoError(t, err)
|
2023-12-05 08:51:56 +00:00
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
got, want := tag, tt.after
|
|
|
|
assert.Equal(t, got, want)
|
|
|
|
})
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_stripHeadPrefix(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
ref string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
2023-12-05 08:51:56 +00:00
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
2024-03-11 10:52:52 +00:00
|
|
|
name: "main branch",
|
2023-12-05 08:51:56 +00:00
|
|
|
args: args{
|
|
|
|
ref: "refs/heads/main",
|
|
|
|
},
|
|
|
|
want: "main",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2024-03-11 10:52:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := stripHeadPrefix(tt.args.ref)
|
|
|
|
assert.Equal(t, got, tt.want)
|
|
|
|
})
|
2023-12-05 08:51:56 +00:00
|
|
|
}
|
|
|
|
}
|