cli/pkg/release/config_test.go
Snider b6fbb781d5 ci(workflows): use host-uk/build@dev for releases (#264)
* ci(workflows): use host-uk/build@dev for releases

- Replace manual Go bootstrap with host-uk/build@dev action
- Add matrix builds for linux/amd64, linux/arm64, darwin/universal, windows/amd64
- Update README URLs from Snider/Core to host-uk/core
- Simplify artifact handling with merge-multiple

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs(mkdocs): update repo references to host-uk/core

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore(coderabbit): disable auto-review, manual trigger only

Trigger with @coderabbitai review to control costs.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat(ci): implement semver release channels

- Rename dev-release.yml → alpha-release.yml
- Alpha builds: v0.0.4-alpha.{run_number} (prerelease)
- Add pr-build.yml for draft releases
- PR builds: v0.0.4-pr.{num}.bid.{id} (draft, not published)
- Add attestation permissions for SLSA compliance
- No more deleting/recreating dev tag

Versioning strategy:
- Draft: +pr.{NUM}.bid.{ID} (testable, not published)
- Alpha: -alpha.{N} (canary channel)
- Beta: -beta (quality scored)
- RC: -rc.{N} (release candidate)
- Stable: no suffix

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(ci): use build@v3 and dot notation for versions

- Switch from host-uk/build@dev to host-uk/build@v3
- Use dots instead of + for build metadata (GitHub tag compatible)
- v0.0.4.pr.{num}.bid.{id} format for PR drafts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci(workflows): update all actions to latest versions

- actions/setup-go: v5 → v6 (Go 1.25+ support)
- actions/upload-artifact: v4 → v6 (immutable artifacts)
- actions/download-artifact: v4 → v7 (attestations support)
- actions/github-script: v7 → v8 (Node 20)
- actions/checkout: standardized on v6

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: temporarily use self-hosted runners

Testing build on local runners while GitHub hosted runners are backed up.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: fix webkit dep for Ubuntu 20.04 runners

Fall back to libwebkit2gtk-4.0-dev on older Ubuntu.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: skip unwritable dir test when running as root

Docker self-hosted runners run as root, which can write anywhere.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: revert to ubuntu-latest runners

Self-hosted runners need environment parity work (ARM64, root user, SDK tools).
Keep self-hosted for future local-llm integration tasks.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 08:54:24 +00:00

360 lines
9.2 KiB
Go

package release
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupConfigTestDir creates a temp directory with optional .core/release.yaml content.
func setupConfigTestDir(t *testing.T, configContent string) string {
t.Helper()
dir := t.TempDir()
if configContent != "" {
coreDir := filepath.Join(dir, ConfigDir)
err := os.MkdirAll(coreDir, 0755)
require.NoError(t, err)
configPath := filepath.Join(coreDir, ConfigFileName)
err = os.WriteFile(configPath, []byte(configContent), 0644)
require.NoError(t, err)
}
return dir
}
func TestLoadConfig_Good(t *testing.T) {
t.Run("loads valid config", func(t *testing.T) {
content := `
version: 1
project:
name: myapp
repository: owner/repo
build:
targets:
- os: linux
arch: amd64
- os: darwin
arch: arm64
publishers:
- type: github
prerelease: true
draft: false
changelog:
include:
- feat
- fix
exclude:
- chore
`
dir := setupConfigTestDir(t, content)
cfg, err := LoadConfig(dir)
require.NoError(t, err)
require.NotNil(t, cfg)
assert.Equal(t, 1, cfg.Version)
assert.Equal(t, "myapp", cfg.Project.Name)
assert.Equal(t, "owner/repo", cfg.Project.Repository)
assert.Len(t, cfg.Build.Targets, 2)
assert.Equal(t, "linux", cfg.Build.Targets[0].OS)
assert.Equal(t, "amd64", cfg.Build.Targets[0].Arch)
assert.Equal(t, "darwin", cfg.Build.Targets[1].OS)
assert.Equal(t, "arm64", cfg.Build.Targets[1].Arch)
assert.Len(t, cfg.Publishers, 1)
assert.Equal(t, "github", cfg.Publishers[0].Type)
assert.True(t, cfg.Publishers[0].Prerelease)
assert.False(t, cfg.Publishers[0].Draft)
assert.Equal(t, []string{"feat", "fix"}, cfg.Changelog.Include)
assert.Equal(t, []string{"chore"}, cfg.Changelog.Exclude)
})
t.Run("returns defaults when config file missing", func(t *testing.T) {
dir := t.TempDir()
cfg, err := LoadConfig(dir)
require.NoError(t, err)
require.NotNil(t, cfg)
defaults := DefaultConfig()
assert.Equal(t, defaults.Version, cfg.Version)
assert.Equal(t, defaults.Build.Targets, cfg.Build.Targets)
assert.Equal(t, defaults.Publishers, cfg.Publishers)
assert.Equal(t, defaults.Changelog.Include, cfg.Changelog.Include)
assert.Equal(t, defaults.Changelog.Exclude, cfg.Changelog.Exclude)
})
t.Run("applies defaults for missing fields", func(t *testing.T) {
content := `
version: 2
project:
name: partial
`
dir := setupConfigTestDir(t, content)
cfg, err := LoadConfig(dir)
require.NoError(t, err)
require.NotNil(t, cfg)
// Explicit values preserved
assert.Equal(t, 2, cfg.Version)
assert.Equal(t, "partial", cfg.Project.Name)
// Defaults applied
defaults := DefaultConfig()
assert.Equal(t, defaults.Build.Targets, cfg.Build.Targets)
assert.Equal(t, defaults.Publishers, cfg.Publishers)
})
t.Run("sets project directory on load", func(t *testing.T) {
dir := setupConfigTestDir(t, "version: 1")
cfg, err := LoadConfig(dir)
require.NoError(t, err)
assert.Equal(t, dir, cfg.projectDir)
})
}
func TestLoadConfig_Bad(t *testing.T) {
t.Run("returns error for invalid YAML", func(t *testing.T) {
content := `
version: 1
project:
name: [invalid yaml
`
dir := setupConfigTestDir(t, content)
cfg, err := LoadConfig(dir)
assert.Error(t, err)
assert.Nil(t, cfg)
assert.Contains(t, err.Error(), "failed to parse config file")
})
t.Run("returns error for unreadable file", func(t *testing.T) {
dir := t.TempDir()
coreDir := filepath.Join(dir, ConfigDir)
err := os.MkdirAll(coreDir, 0755)
require.NoError(t, err)
// Create config as a directory instead of file
configPath := filepath.Join(coreDir, ConfigFileName)
err = os.Mkdir(configPath, 0755)
require.NoError(t, err)
cfg, err := LoadConfig(dir)
assert.Error(t, err)
assert.Nil(t, cfg)
assert.Contains(t, err.Error(), "failed to read config file")
})
}
func TestDefaultConfig_Good(t *testing.T) {
t.Run("returns sensible defaults", func(t *testing.T) {
cfg := DefaultConfig()
assert.Equal(t, 1, cfg.Version)
assert.Empty(t, cfg.Project.Name)
assert.Empty(t, cfg.Project.Repository)
// Default targets
assert.Len(t, cfg.Build.Targets, 4)
hasLinuxAmd64 := false
hasDarwinArm64 := false
hasWindowsAmd64 := false
for _, target := range cfg.Build.Targets {
if target.OS == "linux" && target.Arch == "amd64" {
hasLinuxAmd64 = true
}
if target.OS == "darwin" && target.Arch == "arm64" {
hasDarwinArm64 = true
}
if target.OS == "windows" && target.Arch == "amd64" {
hasWindowsAmd64 = true
}
}
assert.True(t, hasLinuxAmd64)
assert.True(t, hasDarwinArm64)
assert.True(t, hasWindowsAmd64)
// Default publisher
assert.Len(t, cfg.Publishers, 1)
assert.Equal(t, "github", cfg.Publishers[0].Type)
assert.False(t, cfg.Publishers[0].Prerelease)
assert.False(t, cfg.Publishers[0].Draft)
// Default changelog settings
assert.Contains(t, cfg.Changelog.Include, "feat")
assert.Contains(t, cfg.Changelog.Include, "fix")
assert.Contains(t, cfg.Changelog.Exclude, "chore")
assert.Contains(t, cfg.Changelog.Exclude, "docs")
})
}
func TestConfigPath_Good(t *testing.T) {
t.Run("returns correct path", func(t *testing.T) {
path := ConfigPath("/project/root")
assert.Equal(t, "/project/root/.core/release.yaml", path)
})
}
func TestConfigExists_Good(t *testing.T) {
t.Run("returns true when config exists", func(t *testing.T) {
dir := setupConfigTestDir(t, "version: 1")
assert.True(t, ConfigExists(dir))
})
t.Run("returns false when config missing", func(t *testing.T) {
dir := t.TempDir()
assert.False(t, ConfigExists(dir))
})
t.Run("returns false when .core dir missing", func(t *testing.T) {
dir := t.TempDir()
assert.False(t, ConfigExists(dir))
})
}
func TestWriteConfig_Good(t *testing.T) {
t.Run("writes config to file", func(t *testing.T) {
dir := t.TempDir()
cfg := DefaultConfig()
cfg.Project.Name = "testapp"
cfg.Project.Repository = "owner/testapp"
err := WriteConfig(cfg, dir)
require.NoError(t, err)
// Verify file exists
assert.True(t, ConfigExists(dir))
// Reload and verify
loaded, err := LoadConfig(dir)
require.NoError(t, err)
assert.Equal(t, "testapp", loaded.Project.Name)
assert.Equal(t, "owner/testapp", loaded.Project.Repository)
})
t.Run("creates .core directory if missing", func(t *testing.T) {
dir := t.TempDir()
cfg := DefaultConfig()
err := WriteConfig(cfg, dir)
require.NoError(t, err)
// Check directory was created
coreDir := filepath.Join(dir, ConfigDir)
info, err := os.Stat(coreDir)
require.NoError(t, err)
assert.True(t, info.IsDir())
})
}
func TestConfig_GetRepository_Good(t *testing.T) {
t.Run("returns repository", func(t *testing.T) {
cfg := &Config{
Project: ProjectConfig{
Repository: "owner/repo",
},
}
assert.Equal(t, "owner/repo", cfg.GetRepository())
})
t.Run("returns empty string when not set", func(t *testing.T) {
cfg := &Config{}
assert.Empty(t, cfg.GetRepository())
})
}
func TestConfig_GetProjectName_Good(t *testing.T) {
t.Run("returns project name", func(t *testing.T) {
cfg := &Config{
Project: ProjectConfig{
Name: "myapp",
},
}
assert.Equal(t, "myapp", cfg.GetProjectName())
})
t.Run("returns empty string when not set", func(t *testing.T) {
cfg := &Config{}
assert.Empty(t, cfg.GetProjectName())
})
}
func TestConfig_SetVersion_Good(t *testing.T) {
t.Run("sets version override", func(t *testing.T) {
cfg := &Config{}
cfg.SetVersion("v1.2.3")
assert.Equal(t, "v1.2.3", cfg.version)
})
}
func TestConfig_SetProjectDir_Good(t *testing.T) {
t.Run("sets project directory", func(t *testing.T) {
cfg := &Config{}
cfg.SetProjectDir("/path/to/project")
assert.Equal(t, "/path/to/project", cfg.projectDir)
})
}
func TestWriteConfig_Bad(t *testing.T) {
t.Run("returns error for unwritable directory", func(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root can write to any directory")
}
dir := t.TempDir()
// Create .core directory and make it unwritable
coreDir := filepath.Join(dir, ConfigDir)
err := os.MkdirAll(coreDir, 0755)
require.NoError(t, err)
// Make directory read-only
err = os.Chmod(coreDir, 0555)
require.NoError(t, err)
defer func() { _ = os.Chmod(coreDir, 0755) }()
cfg := DefaultConfig()
err = WriteConfig(cfg, dir)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to write config file")
})
t.Run("returns error when directory creation fails", func(t *testing.T) {
// Use a path that doesn't exist and can't be created
cfg := DefaultConfig()
err := WriteConfig(cfg, "/nonexistent/path/that/cannot/be/created")
assert.Error(t, err)
})
}
func TestApplyDefaults_Good(t *testing.T) {
t.Run("applies version default when zero", func(t *testing.T) {
cfg := &Config{Version: 0}
applyDefaults(cfg)
assert.Equal(t, 1, cfg.Version)
})
t.Run("preserves existing version", func(t *testing.T) {
cfg := &Config{Version: 2}
applyDefaults(cfg)
assert.Equal(t, 2, cfg.Version)
})
t.Run("applies changelog defaults only when both empty", func(t *testing.T) {
cfg := &Config{
Changelog: ChangelogConfig{
Include: []string{"feat"},
},
}
applyDefaults(cfg)
// Should not apply defaults because Include is set
assert.Equal(t, []string{"feat"}, cfg.Changelog.Include)
assert.Empty(t, cfg.Changelog.Exclude)
})
}