releaser-pleaser/internal/changelog/changelog_test.go

180 lines
3.8 KiB
Go
Raw Normal View History

package changelog
2024-07-12 15:56:50 +02:00
import (
"log/slog"
2024-07-12 15:56:50 +02:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/apricote/releaser-pleaser/internal/commitparser"
"github.com/apricote/releaser-pleaser/internal/git"
2024-07-12 15:56:50 +02:00
)
func ptr[T any](input T) *T {
return &input
}
2024-07-15 16:45:03 +02:00
func Test_NewChangelogEntry(t *testing.T) {
2024-07-12 15:56:50 +02:00
type args struct {
analyzedCommits []commitparser.AnalyzedCommit
version string
link string
prefix string
suffix string
2024-07-12 15:56:50 +02:00
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "empty",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{},
version: "1.0.0",
link: "https://example.com/1.0.0",
2024-07-12 15:56:50 +02:00
},
want: "## [1.0.0](https://example.com/1.0.0)\n",
2024-07-12 15:56:50 +02:00
wantErr: assert.NoError,
},
{
name: "single feature",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
2024-07-12 15:56:50 +02:00
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "feat",
Description: "Foobar!",
},
},
2024-07-12 15:56:50 +02:00
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: "## [1.0.0](https://example.com/1.0.0)\n\n### Features\n\n- Foobar!\n",
2024-07-12 15:56:50 +02:00
wantErr: assert.NoError,
},
{
name: "single fix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
2024-07-12 15:56:50 +02:00
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "fix",
Description: "Foobar!",
},
},
2024-07-12 15:56:50 +02:00
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: "## [1.0.0](https://example.com/1.0.0)\n\n### Bug Fixes\n\n- Foobar!\n",
2024-07-12 15:56:50 +02:00
wantErr: assert.NoError,
},
{
name: "multiple commits with scopes",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
2024-07-12 15:56:50 +02:00
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "feat",
Description: "Blabla!",
},
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "feat",
Description: "So awesome!",
Scope: ptr("awesome"),
},
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "fix",
Description: "Foobar!",
},
{
Commit: git.Commit{},
2024-07-12 15:56:50 +02:00
Type: "fix",
Description: "So sad!",
Scope: ptr("sad"),
},
},
2024-07-12 15:56:50 +02:00
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: `## [1.0.0](https://example.com/1.0.0)
2024-07-12 15:56:50 +02:00
### Features
- Blabla!
- **awesome**: So awesome!
### Bug Fixes
- Foobar!
- **sad**: So sad!
`,
wantErr: assert.NoError,
},
{
name: "prefix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
prefix: "### Breaking Changes",
},
want: `## [1.0.0](https://example.com/1.0.0)
### Breaking Changes
### Bug Fixes
- Foobar!
`,
wantErr: assert.NoError,
},
{
name: "suffix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
suffix: "### Compatibility\n\nThis version is compatible with flux-compensator v2.2 - v2.9.",
},
want: `## [1.0.0](https://example.com/1.0.0)
### Bug Fixes
- Foobar!
### Compatibility
This version is compatible with flux-compensator v2.2 - v2.9.
2024-07-12 15:56:50 +02:00
`,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := New(commitparser.ByType(tt.args.analyzedCommits), tt.args.version, tt.args.link, tt.args.prefix, tt.args.suffix)
got, err := Entry(slog.Default(), DefaultTemplate(), data, Formatting{})
2024-07-12 15:56:50 +02:00
if !tt.wantErr(t, err) {
return
}
assert.Equal(t, tt.want, got)
})
}
}