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