releaser-pleaser/internal/commitparser/conventionalcommits/conventionalcommits_test.go

126 lines
2.5 KiB
Go
Raw Normal View History

2024-08-30 22:47:50 +02:00
package conventionalcommits
import (
"testing"
"github.com/stretchr/testify/assert"
2024-08-30 22:47:50 +02:00
"github.com/apricote/releaser-pleaser/internal/commitparser"
"github.com/apricote/releaser-pleaser/internal/git"
)
func TestAnalyzeCommits(t *testing.T) {
tests := []struct {
name string
2024-08-30 22:47:50 +02:00
commits []git.Commit
expectedCommits []commitparser.AnalyzedCommit
wantErr assert.ErrorAssertionFunc
}{
{
name: "empty commits",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{},
expectedCommits: []commitparser.AnalyzedCommit{},
wantErr: assert.NoError,
},
{
name: "malformed commit message",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{
{
Message: "aksdjaklsdjka",
},
},
expectedCommits: nil,
wantErr: assert.Error,
},
{
name: "drops unreleasable",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{
{
Message: "chore: foobar",
},
},
2024-08-30 22:47:50 +02:00
expectedCommits: []commitparser.AnalyzedCommit{},
wantErr: assert.NoError,
},
{
name: "highest bump (patch)",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{
{
Message: "chore: foobar",
},
{
Message: "fix: blabla",
},
},
2024-08-30 22:47:50 +02:00
expectedCommits: []commitparser.AnalyzedCommit{
{
2024-08-30 22:47:50 +02:00
Commit: git.Commit{Message: "fix: blabla"},
Type: "fix",
Description: "blabla",
},
},
wantErr: assert.NoError,
},
{
name: "highest bump (minor)",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{
{
Message: "fix: blabla",
},
{
Message: "feat: foobar",
},
},
2024-08-30 22:47:50 +02:00
expectedCommits: []commitparser.AnalyzedCommit{
{
2024-08-30 22:47:50 +02:00
Commit: git.Commit{Message: "fix: blabla"},
Type: "fix",
Description: "blabla",
},
{
2024-08-30 22:47:50 +02:00
Commit: git.Commit{Message: "feat: foobar"},
Type: "feat",
Description: "foobar",
},
},
wantErr: assert.NoError,
},
{
name: "highest bump (major)",
2024-08-30 22:47:50 +02:00
commits: []git.Commit{
{
Message: "fix: blabla",
},
{
Message: "feat!: foobar",
},
},
2024-08-30 22:47:50 +02:00
expectedCommits: []commitparser.AnalyzedCommit{
{
2024-08-30 22:47:50 +02:00
Commit: git.Commit{Message: "fix: blabla"},
Type: "fix",
Description: "blabla",
},
{
2024-08-30 22:47:50 +02:00
Commit: git.Commit{Message: "feat!: foobar"},
2024-08-02 19:06:32 +02:00
Type: "feat",
Description: "foobar",
BreakingChange: true,
},
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2024-08-30 22:47:50 +02:00
analyzedCommits, err := NewParser().Analyze(tt.commits)
if !tt.wantErr(t, err) {
return
}
assert.Equal(t, tt.expectedCommits, analyzedCommits)
})
}
}