2024-08-31 15:23:21 +02:00
|
|
|
package conventionalcommits
|
2024-07-12 15:32:35 +02:00
|
|
|
|
|
|
|
|
import (
|
2024-09-07 21:51:15 +02:00
|
|
|
"log/slog"
|
2024-07-12 15:32:35 +02:00
|
|
|
"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,
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-09-07 21:51:15 +02:00
|
|
|
name: "skips malformed commit message",
|
2024-08-31 15:23:21 +02:00
|
|
|
commits: []git.Commit{
|
2024-07-12 15:32:35 +02:00
|
|
|
{
|
|
|
|
|
Message: "aksdjaklsdjka",
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-09-07 21:51:15 +02:00
|
|
|
expectedCommits: []commitparser.AnalyzedCommit{},
|
|
|
|
|
wantErr: assert.NoError,
|
2024-07-12 15:32:35 +02:00
|
|
|
},
|
2024-09-08 21:05:18 +02:00
|
|
|
{
|
|
|
|
|
// GitLab seems to create commits with pattern "scope: message\n" if no body is added.
|
|
|
|
|
// This has previously caused a parser error "missing a blank line".
|
|
|
|
|
// We added a workaround with `strings.TrimSpace()` and this test make sure that it does not break again.
|
|
|
|
|
name: "handles title with new line",
|
|
|
|
|
commits: []git.Commit{
|
|
|
|
|
{
|
|
|
|
|
Message: "aksdjaklsdjka",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expectedCommits: []commitparser.AnalyzedCommit{},
|
|
|
|
|
wantErr: assert.NoError,
|
|
|
|
|
},
|
2024-07-12 15:32:35 +02:00
|
|
|
{
|
|
|
|
|
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
|
|
|
},
|
2024-11-15 17:25:15 +01:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
name: "success with body",
|
|
|
|
|
commits: []git.Commit{
|
|
|
|
|
{
|
|
|
|
|
Message: "feat: some thing (hz/fl!144)\n\nFixes #15\n\nDepends on !143",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expectedCommits: []commitparser.AnalyzedCommit{
|
|
|
|
|
{
|
|
|
|
|
Commit: git.Commit{Message: "feat: some thing (hz/fl!144)\n\nFixes #15\n\nDepends on !143"},
|
|
|
|
|
Type: "feat",
|
|
|
|
|
Description: "some thing (hz/fl!144)",
|
|
|
|
|
BreakingChange: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wantErr: assert.NoError,
|
|
|
|
|
},
|
2024-07-12 15:32:35 +02:00
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-09-07 21:51:15 +02:00
|
|
|
analyzedCommits, err := NewParser(slog.Default()).Analyze(tt.commits)
|
2024-07-12 15:32:35 +02:00
|
|
|
if !tt.wantErr(t, err) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, tt.expectedCommits, analyzedCommits)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|