0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-28 06:30:35 +00:00
git-sv/sv/commit_test.go

183 lines
4.3 KiB
Go
Raw Normal View History

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 TestSemVerCommitProcessor_NextVersion(t *testing.T) {
2019-11-17 17:03:30 +00:00
tests := []struct {
name string
ignoreUnknown bool
version *semver.Version
commits []CommitLog
want *semver.Version
wantUpdated bool
2019-11-17 17:03:30 +00:00
}{
2023-10-12 14:18:25 +00:00
{
"no update",
true,
TestVersion("0.0.0"),
[]CommitLog{},
TestVersion("0.0.0"),
2023-10-12 14:18:25 +00:00
false,
},
{
"no update without version",
true,
nil,
[]CommitLog{},
2023-10-12 14:18:25 +00:00
nil,
false,
},
{
"no update on unknown type",
true,
TestVersion("0.0.0"),
[]CommitLog{TestCommitlog("a", map[string]string{}, "a")},
TestVersion("0.0.0"),
2023-10-12 14:18:25 +00:00
false,
},
{
"no update on unmapped known type",
false,
TestVersion("0.0.0"),
[]CommitLog{TestCommitlog("none", map[string]string{}, "a")},
TestVersion("0.0.0"),
2023-10-12 14:18:25 +00:00
false,
},
{
"update patch on unknown type",
false,
TestVersion("0.0.0"),
[]CommitLog{TestCommitlog("a", map[string]string{}, "a")},
TestVersion("0.0.1"),
2023-10-12 14:18:25 +00:00
true,
},
{
"patch update",
false, TestVersion("0.0.0"),
[]CommitLog{TestCommitlog("patch", map[string]string{}, "a")},
TestVersion("0.0.1"), true,
2023-10-12 14:18:25 +00:00
},
{
"patch update without version",
false,
nil,
[]CommitLog{TestCommitlog("patch", map[string]string{}, "a")},
2023-10-12 14:18:25 +00:00
nil,
true,
},
{
"minor update",
false,
TestVersion("0.0.0"),
[]CommitLog{
TestCommitlog("patch", map[string]string{}, "a"),
TestCommitlog("minor", map[string]string{}, "a"),
2023-10-12 14:18:25 +00:00
},
TestVersion("0.1.0"),
2023-10-12 14:18:25 +00:00
true,
},
{
"major update",
false,
TestVersion("0.0.0"),
[]CommitLog{
TestCommitlog("patch", map[string]string{}, "a"),
TestCommitlog("major", map[string]string{}, "a"),
2023-10-12 14:18:25 +00:00
},
TestVersion("1.0.0"),
2023-10-12 14:18:25 +00:00
true,
},
{
"breaking change update",
false,
TestVersion("0.0.0"),
[]CommitLog{
TestCommitlog("patch", map[string]string{}, "a"),
TestCommitlog("patch", map[string]string{"breaking-change": "break"}, "a"),
2023-10-12 14:18:25 +00:00
},
TestVersion("1.0.0"),
2023-10-12 14:18:25 +00:00
true,
},
2019-11-17 17:03:30 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewSemVerCommitProcessor(
2023-10-12 14:18:25 +00:00
VersioningConfig{
UpdateMajor: []string{"major"},
UpdateMinor: []string{"minor"},
UpdatePatch: []string{"patch"},
IgnoreUnknown: tt.ignoreUnknown,
},
CommitMessageConfig{Types: []string{"major", "minor", "patch", "none"}})
got, gotUpdated := p.NextVersion(tt.version, tt.commits)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SemVerCommitProcessor.NextVersion() Version = %v, want %v", got, tt.want)
}
if tt.wantUpdated != gotUpdated {
t.Errorf("SemVerCommitProcessor.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
want *semver.Version
2019-11-17 17:03:30 +00:00
wantErr bool
}{
{"empty version", "", TestVersion("0.0.0"), false},
{"invalid version", "abc", nil, true},
{"valid version", "1.2.3", TestVersion("1.2.3"), false},
2019-11-17 17:03:30 +00:00
}
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)
2023-10-12 14:18:25 +00:00
2019-11-17 17:03:30 +00:00
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToVersion() = %v, want %v", got, tt.want)
}
})
}
}
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)
}
})
}
}