2019-11-17 17:03:30 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-13 18:40:09 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2019-11-17 17:03:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSemVerCommitsProcessorImpl_NextVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-02-14 22:42:22 +00:00
|
|
|
name string
|
|
|
|
ignoreUnknown bool
|
2021-12-29 21:02:50 +00:00
|
|
|
version *semver.Version
|
2021-02-14 22:42:22 +00:00
|
|
|
commits []GitCommitLog
|
2021-12-29 21:02:50 +00:00
|
|
|
want *semver.Version
|
2021-04-11 01:59:30 +00:00
|
|
|
wantUpdated bool
|
2019-11-17 17:03:30 +00:00
|
|
|
}{
|
2021-04-11 01:59:30 +00:00
|
|
|
{"no update", true, version("0.0.0"), []GitCommitLog{}, version("0.0.0"), false},
|
2021-12-29 21:07:52 +00:00
|
|
|
{"no update without version", true, nil, []GitCommitLog{}, nil, false},
|
2022-02-07 01:06:46 +00:00
|
|
|
{"no update on unknown type", true, version("0.0.0"), []GitCommitLog{commitlog("a", map[string]string{}, "a")}, version("0.0.0"), false},
|
|
|
|
{"no update on unmapped known type", false, version("0.0.0"), []GitCommitLog{commitlog("none", map[string]string{}, "a")}, version("0.0.0"), false},
|
|
|
|
{"update patch on unknown type", false, version("0.0.0"), []GitCommitLog{commitlog("a", map[string]string{}, "a")}, version("0.0.1"), true},
|
|
|
|
{"patch update", false, version("0.0.0"), []GitCommitLog{commitlog("patch", map[string]string{}, "a")}, version("0.0.1"), true},
|
|
|
|
{"patch update without version", false, nil, []GitCommitLog{commitlog("patch", map[string]string{}, "a")}, nil, true},
|
|
|
|
{"minor update", false, version("0.0.0"), []GitCommitLog{commitlog("patch", map[string]string{}, "a"), commitlog("minor", map[string]string{}, "a")}, version("0.1.0"), true},
|
|
|
|
{"major update", false, version("0.0.0"), []GitCommitLog{commitlog("patch", map[string]string{}, "a"), commitlog("major", map[string]string{}, "a")}, version("1.0.0"), true},
|
|
|
|
{"breaking change update", false, version("0.0.0"), []GitCommitLog{commitlog("patch", map[string]string{}, "a"), commitlog("patch", map[string]string{"breaking-change": "break"}, "a")}, version("1.0.0"), true},
|
2019-11-17 17:03:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-02-15 05:41:43 +00:00
|
|
|
p := NewSemVerCommitsProcessor(VersioningConfig{UpdateMajor: []string{"major"}, UpdateMinor: []string{"minor"}, UpdatePatch: []string{"patch"}, IgnoreUnknown: tt.ignoreUnknown}, CommitMessageConfig{Types: []string{"major", "minor", "patch", "none"}})
|
2021-04-11 01:59:30 +00:00
|
|
|
got, gotUpdated := p.NextVersion(tt.version, tt.commits)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("SemVerCommitsProcessorImpl.NextVersion() Version = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
if tt.wantUpdated != gotUpdated {
|
|
|
|
t.Errorf("SemVerCommitsProcessorImpl.NextVersion() Updated = %v, want %v", gotUpdated, tt.wantUpdated)
|
2019-11-17 17:03:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
2021-12-29 21:02:50 +00:00
|
|
|
want *semver.Version
|
2019-11-17 17:03:30 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"empty version", "", version("0.0.0"), false},
|
2021-12-29 21:02:50 +00:00
|
|
|
{"invalid version", "abc", nil, true},
|
2019-11-17 17:03:30 +00:00
|
|
|
{"valid version", "1.2.3", version("1.2.3"), false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := ToVersion(tt.input)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ToVersion() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("ToVersion() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 22:25:52 +00:00
|
|
|
|
|
|
|
func TestIsValidVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"simple version", "1.0.0", true},
|
|
|
|
{"with v prefix version", "v1.0.0", true},
|
|
|
|
{"prerelease version", "1.0.0-alpha", true},
|
|
|
|
{"prerelease version", "1.0.0-alpha.1", true},
|
|
|
|
{"prerelease version", "1.0.0-0.3.7", true},
|
|
|
|
{"prerelease version", "1.0.0-x.7.z.92", true},
|
|
|
|
{"prerelease version", "1.0.0-x-y-z.-", true},
|
|
|
|
{"metadata version", "1.0.0-alpha+001", true},
|
|
|
|
{"metadata version", "1.0.0+20130313144700", true},
|
|
|
|
{"metadata version", "1.0.0-beta+exp.sha.5114f85", true},
|
|
|
|
{"metadata version", "1.0.0+21AF26D3-117B344092BD", true},
|
|
|
|
{"incomplete version", "1", true},
|
|
|
|
{"invalid version", "invalid", false},
|
|
|
|
{"invalid prefix version", "random1.0.0", false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := IsValidVersion(tt.value); got != tt.want {
|
|
|
|
t.Errorf("IsValidVersion(%s) = %v, want %v", tt.value, got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|