Files
vociferate/internal/vociferate/vociferate_test.go
Micheal Wilkinson 71c1b81426
Some checks failed
Push Validation / validate (push) Has been cancelled
test: harden changelog link tests for CI env vars
2026-03-20 22:30:46 +00:00

267 lines
9.4 KiB
Go

package vociferate_test
import (
"os"
"path/filepath"
"testing"
"git.hrafn.xyz/aether/vociferate/internal/vociferate"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type PrepareSuite struct {
suite.Suite
rootDir string
}
func TestPrepareSuite(t *testing.T) {
suite.Run(t, new(PrepareSuite))
}
func (s *PrepareSuite) SetupTest() {
s.rootDir = s.T().TempDir()
s.T().Setenv("GITHUB_SERVER_URL", "")
s.T().Setenv("GITHUB_REPOSITORY", "")
require.NoError(s.T(), os.MkdirAll(filepath.Join(s.rootDir, ".git"), 0o755))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, ".git", "config"),
[]byte("[remote \"origin\"]\n\turl = git@git.hrafn.xyz:aether/vociferate.git\n"),
0o644,
))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "release-version"),
[]byte("1.1.6\n"),
0o644,
))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Breaking\n\n### Added\n\n- New thing.\n\n### Fixed\n\n- Old thing.\n\n## [1.1.6] - 2017-12-20\n\n### Fixed\n\n- Historical note.\n"),
0o644,
))
}
func (s *PrepareSuite) TestPrepare_UpdatesVersionAndPromotesUnreleasedNotes() {
err := vociferate.Prepare(s.rootDir, "v1.1.7", "2026-03-20", vociferate.Options{})
require.NoError(s.T(), err)
versionBytes, err := os.ReadFile(filepath.Join(s.rootDir, "release-version"))
require.NoError(s.T(), err)
require.Equal(s.T(), "1.1.7\n", string(versionBytes))
changelogBytes, err := os.ReadFile(filepath.Join(s.rootDir, "changelog.md"))
require.NoError(s.T(), err)
require.Equal(s.T(), "# Changelog\n\n## [Unreleased](https://git.hrafn.xyz/aether/vociferate/src/branch/main)\n\n## [1.1.7](https://git.hrafn.xyz/aether/vociferate/releases/tag/v1.1.7) - 2026-03-20\n\n### Breaking\n\n### Added\n\n- New thing.\n\n### Fixed\n\n- Old thing.\n\n## [1.1.6](https://git.hrafn.xyz/aether/vociferate/releases/tag/v1.1.6) - 2017-12-20\n\n### Fixed\n\n- Historical note.\n", string(changelogBytes))
}
func (s *PrepareSuite) TestPrepare_ReturnsErrorWhenUnreleasedSectionMissing() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [1.1.6] - 2017-12-20\n"),
0o644,
))
err := vociferate.Prepare(s.rootDir, "1.1.7", "2026-03-20", vociferate.Options{})
require.ErrorContains(s.T(), err, "unreleased section")
}
func (s *PrepareSuite) TestPrepare_ReturnsErrorWhenUnreleasedSectionIsEmpty() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n## [1.1.6] - 2017-12-20\n"),
0o644,
))
err := vociferate.Prepare(s.rootDir, "1.1.7", "2026-03-20", vociferate.Options{})
require.ErrorContains(s.T(), err, "unreleased section is empty")
}
func (s *PrepareSuite) TestRecommendedTag_UsesMajorBumpWhenBreakingHeadingExists() {
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v2.0.0", tag)
}
func (s *PrepareSuite) TestRecommendedTag_UsesPatchBumpForFixOnlyChanges() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Fixed\n\n- Patch note.\n\n## [1.1.6] - 2017-12-20\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v1.1.7", tag)
}
func (s *PrepareSuite) TestRecommendedTag_UsesMajorBumpWhenRemovedEntriesExist() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Removed\n\n- Breaking removal.\n\n## [1.1.6] - 2017-12-20\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v2.0.0", tag)
}
func (s *PrepareSuite) TestRecommendedTag_UsesMajorBumpWhenBreakingEntriesExist() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Breaking\n\n- Changed API contract.\n\n### Changed\n\n- Updated defaults.\n\n## [1.1.6] - 2017-12-20\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v2.0.0", tag)
}
func (s *PrepareSuite) TestPrepare_UsesCustomVersionFileAndPattern() {
customVersionFile := filepath.Join("custom", "VERSION.txt")
require.NoError(s.T(), os.MkdirAll(filepath.Join(s.rootDir, "custom"), 0o755))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, customVersionFile),
[]byte("VERSION=1.1.6\n"),
0o644,
))
err := vociferate.Prepare(s.rootDir, "1.1.8", "2026-03-20", vociferate.Options{
VersionFile: customVersionFile,
VersionPattern: `VERSION=([^\n]+)`,
})
require.NoError(s.T(), err)
versionBytes, err := os.ReadFile(filepath.Join(s.rootDir, customVersionFile))
require.NoError(s.T(), err)
require.Equal(s.T(), "VERSION=1.1.8\n", string(versionBytes))
}
func (s *PrepareSuite) TestPrepare_AllowsUnchangedVersionValue() {
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "release-version"),
[]byte("1.1.6\n"),
0o644,
))
err := vociferate.Prepare(s.rootDir, "1.1.6", "2026-03-20", vociferate.Options{})
require.NoError(s.T(), err)
versionBytes, readErr := os.ReadFile(filepath.Join(s.rootDir, "release-version"))
require.NoError(s.T(), readErr)
require.Equal(s.T(), "1.1.6\n", string(versionBytes))
}
func (s *PrepareSuite) TestRecommendedTag_UsesChangelogVersionWhenNoVersionFileConfigured() {
// The default release-version file is present from SetupTest but should be ignored;
// the current version must be read from the changelog, not the file.
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "release-version"),
[]byte("99.99.99\n"), // deliberately wrong value
0o644,
))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Fixed\n\n- A fix.\n\n## [3.0.0] - 2026-01-01\n\n### Fixed\n\n- Historical.\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v3.0.1", tag)
}
func (s *PrepareSuite) TestRecommendedTag_DefaultsToV1WhenNoPriorReleasesInChangelog() {
require.NoError(s.T(), os.Remove(filepath.Join(s.rootDir, "release-version")))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Breaking\n\n### Added\n\n- First feature.\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{})
require.NoError(s.T(), err)
require.Equal(s.T(), "v1.0.0", tag)
}
func (s *PrepareSuite) TestPrepare_CreatesVersionFileWhenNotPresent() {
require.NoError(s.T(), os.Remove(filepath.Join(s.rootDir, "release-version")))
err := vociferate.Prepare(s.rootDir, "2.0.0", "2026-03-20", vociferate.Options{})
require.NoError(s.T(), err)
versionBytes, readErr := os.ReadFile(filepath.Join(s.rootDir, "release-version"))
require.NoError(s.T(), readErr)
require.Equal(s.T(), "2.0.0\n", string(versionBytes))
}
func (s *PrepareSuite) TestRecommendedTag_UsesCustomVersionFileAndPattern() {
customVersionFile := filepath.Join("custom", "VERSION.txt")
require.NoError(s.T(), os.MkdirAll(filepath.Join(s.rootDir, "custom"), 0o755))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, customVersionFile),
[]byte("VERSION=2.3.4\n"),
0o644,
))
require.NoError(s.T(), os.WriteFile(
filepath.Join(s.rootDir, "changelog.md"),
[]byte("# Changelog\n\n## [Unreleased]\n\n### Added\n\n- Feature.\n\n## [2.3.4] - 2026-03-10\n"),
0o644,
))
tag, err := vociferate.RecommendedTag(s.rootDir, vociferate.Options{
VersionFile: customVersionFile,
VersionPattern: `VERSION=([^\n]+)`,
})
require.NoError(s.T(), err)
require.Equal(s.T(), "v2.4.0", tag)
}
func (s *PrepareSuite) TestPrepare_UsesGitHrafnXYZEnvironmentForChangelogLinks() {
s.T().Setenv("GITHUB_SERVER_URL", "https://git.hrafn.xyz")
s.T().Setenv("GITHUB_REPOSITORY", "aether/vociferate")
err := vociferate.Prepare(s.rootDir, "1.1.7", "2026-03-20", vociferate.Options{})
require.NoError(s.T(), err)
changelogBytes, readErr := os.ReadFile(filepath.Join(s.rootDir, "changelog.md"))
require.NoError(s.T(), readErr)
changelog := string(changelogBytes)
require.Contains(s.T(), changelog, "## [Unreleased](https://git.hrafn.xyz/aether/vociferate/src/branch/main)")
require.Contains(s.T(), changelog, "## [1.1.7](https://git.hrafn.xyz/aether/vociferate/releases/tag/v1.1.7) - 2026-03-20")
require.Contains(s.T(), changelog, "## [1.1.6](https://git.hrafn.xyz/aether/vociferate/releases/tag/v1.1.6) - 2017-12-20")
}
func (s *PrepareSuite) TestPrepare_UsesGitHubEnvironmentForChangelogLinks() {
s.T().Setenv("GITHUB_SERVER_URL", "https://github.com")
s.T().Setenv("GITHUB_REPOSITORY", "aether/vociferate")
err := vociferate.Prepare(s.rootDir, "1.1.7", "2026-03-20", vociferate.Options{})
require.NoError(s.T(), err)
changelogBytes, readErr := os.ReadFile(filepath.Join(s.rootDir, "changelog.md"))
require.NoError(s.T(), readErr)
changelog := string(changelogBytes)
require.Contains(s.T(), changelog, "## [Unreleased](https://github.com/aether/vociferate/src/branch/main)")
require.Contains(s.T(), changelog, "## [1.1.7](https://github.com/aether/vociferate/releases/tag/v1.1.7) - 2026-03-20")
require.Contains(s.T(), changelog, "## [1.1.6](https://github.com/aether/vociferate/releases/tag/v1.1.6) - 2017-12-20")
}