2026-03-03 21:59:09 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestBuildChangelogMessages(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
log := `feat: add changelog command
|
|
|
|
|
|
|
|
|
|
Implements #1
|
|
|
|
|
---
|
|
|
|
|
fix: typo in docs
|
|
|
|
|
---`
|
|
|
|
|
version := "v0.2.0"
|
|
|
|
|
|
|
|
|
|
messages := buildChangelogMessages(log, version)
|
|
|
|
|
|
|
|
|
|
require.Len(t, messages, 2)
|
|
|
|
|
assert.Equal(t, "system", messages[0]["role"])
|
|
|
|
|
assert.Contains(t, messages[0]["content"], "Generate a changelog section")
|
|
|
|
|
assert.Contains(t, messages[0]["content"], "### Added")
|
|
|
|
|
assert.Contains(t, messages[0]["content"], "### Changed")
|
|
|
|
|
assert.Contains(t, messages[0]["content"], "### Fixed")
|
|
|
|
|
assert.Equal(t, "user", messages[1]["role"])
|
|
|
|
|
assert.Contains(t, messages[1]["content"], "Version: v0.2.0")
|
|
|
|
|
assert.Contains(t, messages[1]["content"], log)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildFullChangelog(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
newSection := "## [v0.2.0] - 2026-03-03\n\n### Added\n- changelog command\n"
|
|
|
|
|
|
|
|
|
|
t.Run("creates new file with header", func(t *testing.T) {
|
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
// nolint:tparallel // os.Chdir affects the entire process
|
2026-03-03 21:59:09 +00:00
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
|
|
originalWd, err := os.Getwd()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
require.NoError(t, os.Chdir(originalWd))
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, os.Chdir(tmpDir))
|
|
|
|
|
|
|
|
|
|
result := buildFullChangelog(newSection)
|
|
|
|
|
|
|
|
|
|
assert.Contains(t, result, "# Changelog")
|
|
|
|
|
assert.Contains(t, result, "All notable changes to this project will be documented in this file.")
|
|
|
|
|
assert.Contains(t, result, newSection)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("prepends to existing file", func(t *testing.T) {
|
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
// nolint:tparallel // os.Chdir affects the entire process
|
2026-03-03 21:59:09 +00:00
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
|
|
originalWd, err := os.Getwd()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
require.NoError(t, os.Chdir(originalWd))
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, os.Chdir(tmpDir))
|
|
|
|
|
|
|
|
|
|
// Simulate existing changelog
|
|
|
|
|
existing := `# Changelog
|
|
|
|
|
|
|
|
|
|
All notable changes to this project will be documented in this file.
|
|
|
|
|
|
|
|
|
|
## [v0.1.0] - 2025-01-01
|
|
|
|
|
|
|
|
|
|
### Fixed
|
|
|
|
|
- old bug
|
|
|
|
|
`
|
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
|
|
|
require.NoError(t, os.WriteFile("CHANGELOG.md", []byte(existing), 0600))
|
2026-03-03 21:59:09 +00:00
|
|
|
|
|
|
|
|
result := buildFullChangelog(newSection)
|
|
|
|
|
|
|
|
|
|
assert.True(t, strings.HasPrefix(result, newSection), "new version section must be prepended at the top")
|
|
|
|
|
assert.Contains(t, result, "old bug")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestChangelogCmd_Flags(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
require.NotNil(t, changelogCmd)
|
|
|
|
|
|
|
|
|
|
stdoutFlag := changelogCmd.Flags().Lookup("stdout")
|
|
|
|
|
require.NotNil(t, stdoutFlag)
|
|
|
|
|
assert.Equal(t, "Print ONLY the new section (ideal for Gitea release notes)", stdoutFlag.Usage)
|
|
|
|
|
|
|
|
|
|
versionFlag := changelogCmd.Flags().Lookup("version")
|
|
|
|
|
require.NotNil(t, versionFlag)
|
2026-03-03 22:27:49 +00:00
|
|
|
assert.Equal(t, "V", versionFlag.Shorthand)
|
2026-03-03 21:59:09 +00:00
|
|
|
|
|
|
|
|
commitFlag := changelogCmd.Flags().Lookup("commit")
|
|
|
|
|
require.NotNil(t, commitFlag)
|
|
|
|
|
assert.Equal(t, "After writing, remind to run grokkit commit", commitFlag.Usage)
|
|
|
|
|
|
|
|
|
|
sinceFlag := changelogCmd.Flags().Lookup("since")
|
|
|
|
|
require.NotNil(t, sinceFlag)
|
|
|
|
|
}
|