grokkit/cmd/changelog_test.go
Greg Gauthier f0322a84bd
Some checks failed
CI / Test (push) Successful in 33s
CI / Lint (push) Failing after 17s
CI / Build (push) Successful in 21s
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

111 lines
2.9 KiB
Go

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) {
t.Parallel()
// nolint:tparallel // os.Chdir affects the entire process
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) {
t.Parallel()
// nolint:tparallel // os.Chdir affects the entire process
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
`
require.NoError(t, os.WriteFile("CHANGELOG.md", []byte(existing), 0600))
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)
assert.Equal(t, "V", versionFlag.Shorthand)
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)
}