Files
stamp/internal/config/config_test.go
Thomas 8049c505a0 Add TUI add and preview commands
Use charmbracelet/huh for an interactive add flow. Add a preview
subcommand to enter/exit per-project pre-release tags. Move stamp.toml
into .stamp/ and add ConfigPath helper. Prefer --snapshot then project
PreTag when computing versions and promote bumps to pre-release when
appropriate. Export CurrentVersion and add required TUI deps to go.mod.
2026-03-12 22:59:20 +01:00

133 lines
2.8 KiB
Go

package config_test
import (
"os"
"path/filepath"
"testing"
"git.thokra.dev/thokra/stamp/internal/config"
)
const validTOML = `
[config]
base_branch = "main"
[[projects]]
name = "my-app"
path = "apps/my-app"
version = "1.2.3"
[[projects]]
name = "my-lib"
path = "libs/my-lib"
version = "0.1.0"
`
func writeConfig(t *testing.T, dir, content string) {
t.Helper()
stampDir := filepath.Join(dir, ".stamp")
if err := os.MkdirAll(stampDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(stampDir, "stamp.toml"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestLoad_Valid(t *testing.T) {
dir := t.TempDir()
writeConfig(t, dir, validTOML)
cfg, err := config.Load(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(cfg.Projects) != 2 {
t.Fatalf("expected 2 projects, got %d", len(cfg.Projects))
}
if cfg.BaseBranch() != "main" {
t.Errorf("expected base_branch=main, got %s", cfg.BaseBranch())
}
if cfg.ChangesetDir() != ".stamp" {
t.Errorf("expected ChangesetDir=.stamp, got %s", cfg.ChangesetDir())
}
}
func TestLoad_Defaults(t *testing.T) {
dir := t.TempDir()
minimal := "[[projects]]\nname = \"app\"\npath = \".\"\nversion = \"1.0.0\"\n"
writeConfig(t, dir, minimal)
cfg, err := config.Load(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.BaseBranch() != "main" {
t.Errorf("default base_branch should be main, got %s", cfg.BaseBranch())
}
if cfg.ChangesetDir() != ".stamp" {
t.Errorf("default changeset_dir should be .stamp, got %s", cfg.ChangesetDir())
}
}
func TestLoad_MissingFile(t *testing.T) {
_, err := config.Load(t.TempDir())
if err == nil {
t.Fatal("expected error for missing stamp.toml")
}
}
func TestLoad_NoProjects(t *testing.T) {
dir := t.TempDir()
writeConfig(t, dir, "[config]\n")
_, err := config.Load(dir)
if err == nil {
t.Fatal("expected error for config with no projects")
}
}
func TestLoad_DuplicateProjectName(t *testing.T) {
dir := t.TempDir()
dup := `
[[projects]]
name = "app"
path = "."
version = "1.0.0"
[[projects]]
name = "app"
path = "other"
version = "2.0.0"
`
writeConfig(t, dir, dup)
_, err := config.Load(dir)
if err == nil {
t.Fatal("expected error for duplicate project name")
}
}
func TestFindProject(t *testing.T) {
dir := t.TempDir()
writeConfig(t, dir, validTOML)
cfg, _ := config.Load(dir)
if p := cfg.FindProject("my-app"); p == nil {
t.Error("expected to find my-app")
}
if p := cfg.FindProject("nonexistent"); p != nil {
t.Error("expected nil for nonexistent project")
}
}
func TestPublishDefaults(t *testing.T) {
var pc config.PublishConfig
if !pc.PublishTags() {
t.Error("tags should default to true")
}
if !pc.PublishReleases() {
t.Error("releases should default to true")
}
}