chore(go): inject release service dependencies and mirror local validation

This commit is contained in:
Micheal Wilkinson
2026-03-21 14:12:15 +00:00
parent f31141702d
commit 383aad48be
4 changed files with 361 additions and 40 deletions

View File

@@ -3,9 +3,54 @@ package vociferate
import (
"os"
"path/filepath"
"strings"
"testing"
)
type stubFileSystem struct {
files map[string][]byte
}
func newStubFileSystem(files map[string]string) *stubFileSystem {
backing := make(map[string][]byte, len(files))
for path, contents := range files {
backing[path] = []byte(contents)
}
return &stubFileSystem{files: backing}
}
func (fs *stubFileSystem) ReadFile(path string) ([]byte, error) {
contents, ok := fs.files[path]
if !ok {
return nil, os.ErrNotExist
}
clone := make([]byte, len(contents))
copy(clone, contents)
return clone, nil
}
func (fs *stubFileSystem) WriteFile(path string, data []byte, _ os.FileMode) error {
clone := make([]byte, len(data))
copy(clone, data)
fs.files[path] = clone
return nil
}
type stubEnvironment map[string]string
func (env stubEnvironment) Getenv(key string) string {
return env[key]
}
type stubGitRunner struct {
commit string
ok bool
}
func (runner stubGitRunner) FirstCommitShortHash(_ string) (string, bool) {
return runner.commit, runner.ok
}
func TestNormalizeRepoURL(t *testing.T) {
t.Parallel()
@@ -159,3 +204,110 @@ func TestResolveOptions_UsesUppercaseChangelogDefault(t *testing.T) {
t.Fatalf("resolved changelog = %q, want %q", resolved.Changelog, "CHANGELOG.md")
}
}
func TestNewService_ValidatesDependencies(t *testing.T) {
t.Parallel()
validFS := newStubFileSystem(nil)
validEnv := stubEnvironment{}
validGit := stubGitRunner{}
tests := []struct {
name string
deps Dependencies
wantErr string
}{
{
name: "missing file system",
deps: Dependencies{Environment: validEnv, Git: validGit},
wantErr: "file system dependency must not be nil",
},
{
name: "missing environment",
deps: Dependencies{FileSystem: validFS, Git: validGit},
wantErr: "environment dependency must not be nil",
},
{
name: "missing git runner",
deps: Dependencies{FileSystem: validFS, Environment: validEnv},
wantErr: "git runner dependency must not be nil",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := NewService(tt.deps)
if err == nil || err.Error() != tt.wantErr {
t.Fatalf("NewService() error = %v, want %q", err, tt.wantErr)
}
})
}
}
func TestServicePrepare_UsesInjectedEnvironmentForRepositoryLinks(t *testing.T) {
t.Parallel()
rootDir := "/repo"
fs := newStubFileSystem(map[string]string{
filepath.Join(rootDir, "release-version"): "1.1.6\n",
filepath.Join(rootDir, "CHANGELOG.md"): "# Changelog\n\n## [Unreleased]\n\n### Added\n\n- New thing.\n\n## [1.1.6] - 2017-12-20\n\n### Fixed\n\n- Historical note.\n",
})
svc, err := NewService(Dependencies{
FileSystem: fs,
Environment: stubEnvironment{"GITHUB_SERVER_URL": "https://git.hrafn.xyz", "GITHUB_REPOSITORY": "aether/vociferate"},
Git: stubGitRunner{commit: "deadbee", ok: true},
})
if err != nil {
t.Fatalf("NewService() unexpected error: %v", err)
}
err = svc.Prepare(rootDir, "1.1.7", "2026-03-21", Options{})
if err != nil {
t.Fatalf("Prepare() unexpected error: %v", err)
}
updated := string(fs.files[filepath.Join(rootDir, "CHANGELOG.md")])
if !contains(updated, "[Unreleased]: https://git.hrafn.xyz/aether/vociferate/compare/v1.1.7...main") {
t.Fatalf("Prepare() changelog missing injected environment link:\n%s", updated)
}
if !contains(updated, "[1.1.7]: https://git.hrafn.xyz/aether/vociferate/compare/v1.1.6...v1.1.7") {
t.Fatalf("Prepare() changelog missing injected release link:\n%s", updated)
}
}
func TestServicePrepare_UsesInjectedGitRunnerForFirstCommitLink(t *testing.T) {
t.Parallel()
rootDir := "/repo"
fs := newStubFileSystem(map[string]string{
filepath.Join(rootDir, "release-version"): "1.1.6\n",
filepath.Join(rootDir, "CHANGELOG.md"): "# Changelog\n\n## [Unreleased]\n\n### Added\n\n- New thing.\n\n## [1.1.6] - 2017-12-20\n\n### Fixed\n\n- Historical note.\n",
filepath.Join(rootDir, ".git", "config"): "[remote \"origin\"]\n\turl = git@git.hrafn.xyz:aether/vociferate.git\n",
})
svc, err := NewService(Dependencies{
FileSystem: fs,
Environment: stubEnvironment{},
Git: stubGitRunner{commit: "abc1234", ok: true},
})
if err != nil {
t.Fatalf("NewService() unexpected error: %v", err)
}
err = svc.Prepare(rootDir, "1.1.7", "2026-03-21", Options{})
if err != nil {
t.Fatalf("Prepare() unexpected error: %v", err)
}
updated := string(fs.files[filepath.Join(rootDir, "CHANGELOG.md")])
if !contains(updated, "[1.1.6]: https://git.hrafn.xyz/aether/vociferate/compare/abc1234...v1.1.6") {
t.Fatalf("Prepare() changelog missing injected git link:\n%s", updated)
}
}
func contains(text, fragment string) bool {
return len(fragment) > 0 && strings.Contains(text, fragment)
}