mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-07 02:07:04 +00:00
feat: update changelog file
This commit is contained in:
parent
75fe39fe30
commit
d7136c1f64
3 changed files with 179 additions and 21 deletions
|
|
@ -1,16 +1,108 @@
|
|||
package rp
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/apricote/releaser-pleaser/internal/testutils"
|
||||
)
|
||||
|
||||
func ptr[T any](input T) *T {
|
||||
return &input
|
||||
}
|
||||
|
||||
func Test_formatChangelog(t *testing.T) {
|
||||
func TestUpdateChangelogFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repoFn testutils.Repo
|
||||
entry string
|
||||
expectedContent string
|
||||
newFile bool
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "empty repo",
|
||||
repoFn: testutils.WithTestRepo(),
|
||||
entry: "## v1.0.0\n",
|
||||
expectedContent: "# Changelog\n\n## v1.0.0\n",
|
||||
newFile: true,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "repo with well-formatted changelog",
|
||||
repoFn: testutils.WithTestRepo(testutils.WithCommit("feat: add changelog", testutils.WithFile(ChangelogFile, `# Changelog
|
||||
|
||||
## v0.0.1
|
||||
|
||||
- Bazzle
|
||||
|
||||
## v0.1.0
|
||||
|
||||
### Bazuuum
|
||||
`))),
|
||||
entry: "## v1.0.0\n\n- Version 1, juhu.\n",
|
||||
expectedContent: `# Changelog
|
||||
|
||||
## v1.0.0
|
||||
|
||||
- Version 1, juhu.
|
||||
|
||||
## v0.0.1
|
||||
|
||||
- Bazzle
|
||||
|
||||
## v0.1.0
|
||||
|
||||
### Bazuuum
|
||||
`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
repo := tt.repoFn(t)
|
||||
wt, err := repo.Worktree()
|
||||
require.NoError(t, err, "failed to get worktree")
|
||||
|
||||
err = UpdateChangelogFile(wt, tt.entry)
|
||||
if !tt.wantErr(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
wtStatus, err := wt.Status()
|
||||
require.NoError(t, err, "failed to get worktree status")
|
||||
|
||||
assert.Len(t, wtStatus, 1, "worktree status does not have the expected entry number")
|
||||
|
||||
changelogFileStatus := wtStatus.File(ChangelogFile)
|
||||
|
||||
if tt.newFile {
|
||||
assert.Equal(t, git.Unmodified, changelogFileStatus.Worktree, "unexpected file status in worktree")
|
||||
assert.Equal(t, git.Added, changelogFileStatus.Staging, "unexpected file status in staging")
|
||||
} else {
|
||||
assert.Equal(t, git.Modified, changelogFileStatus.Worktree, "unexpected file status in worktree")
|
||||
assert.Equal(t, git.Modified, changelogFileStatus.Staging, "unexpected file status in staging")
|
||||
}
|
||||
|
||||
changelogFile, err := wt.Filesystem.Open(ChangelogFile)
|
||||
require.NoError(t, err)
|
||||
defer changelogFile.Close()
|
||||
|
||||
changelogFileContent, err := io.ReadAll(changelogFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tt.expectedContent, string(changelogFileContent))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_NewChangelogEntry(t *testing.T) {
|
||||
type args struct {
|
||||
commits []AnalyzedCommit
|
||||
version string
|
||||
|
|
@ -111,7 +203,7 @@ func Test_formatChangelog(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := formatChangelog(tt.args.commits, tt.args.version, tt.args.link)
|
||||
got, err := NewChangelogEntry(tt.args.commits, tt.args.version, tt.args.link)
|
||||
if !tt.wantErr(t, err) {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue