Fixes #7349. The same race #7314 closed in internal/beads and
internal/config, in the fixtures it did not cover: a fixture's plain
`git commit` spawns the detached `git maintenance run --auto` child, whose
worktree-prune races the fixture's next `git worktree add` and deletes
.git/worktrees/<name>/locked under it (seen on PR #7312 run 37585997441,
cmd/bd Dolt-server tier shard 4/16, TestBootstrap_WorktreeFallbackDir).
One noAutoMaintenance + gitCommand(args...) helper per test package, each
in an existing untagged shared test file (cmd/bd/helpers_test.go,
doctor/git_test_helpers_test.go, doctor/fix/common_test.go,
formula/parser_test.go, git/gitdir_test.go, storage/domain/git/git_test.go;
cmd/bd/test_helpers_test.go is //go:build cgo so unusable for the pure
lane), and every inline exec.Command("git", ...) in those six packages
swapped for it. The flags ride the command line, never env config: the
routing-key scrub drops env config. No new files, so no BUILD.bazel rows.
Proof: GIT_TRACE on `go test ./cmd/bd -run TestBootstrap_WorktreeFallbackDir`
counts 3 `maintenance run --auto` spawns at d8d8bf587b and 0 with this
change.
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1187 lines
38 KiB
Go
1187 lines
38 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/git"
|
|
"github.com/steveyegge/beads/internal/gitenv"
|
|
"github.com/steveyegge/beads/internal/storage/dolt"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConfigCommands(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
// Test SetConfig
|
|
err := store.SetConfig(ctx, "test.key", "test-value")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
|
|
// Test GetConfig
|
|
value, err := store.GetConfig(ctx, "test.key")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig failed: %v", err)
|
|
}
|
|
if value != "test-value" {
|
|
t.Errorf("Expected 'test-value', got '%s'", value)
|
|
}
|
|
|
|
// Test GetConfig for non-existent key
|
|
value, err = store.GetConfig(ctx, "nonexistent.key")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig for nonexistent key failed: %v", err)
|
|
}
|
|
if value != "" {
|
|
t.Errorf("Expected empty string for nonexistent key, got '%s'", value)
|
|
}
|
|
|
|
// Test SetConfig update
|
|
err = store.SetConfig(ctx, "test.key", "updated-value")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig update failed: %v", err)
|
|
}
|
|
value, err = store.GetConfig(ctx, "test.key")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig after update failed: %v", err)
|
|
}
|
|
if value != "updated-value" {
|
|
t.Errorf("Expected 'updated-value', got '%s'", value)
|
|
}
|
|
|
|
// Test GetAllConfig
|
|
err = store.SetConfig(ctx, "jira.url", "https://example.atlassian.net")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig for jira.url failed: %v", err)
|
|
}
|
|
err = store.SetConfig(ctx, "jira.project", "PROJ")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig for jira.project failed: %v", err)
|
|
}
|
|
|
|
config, err := store.GetAllConfig(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetAllConfig failed: %v", err)
|
|
}
|
|
|
|
// Should have at least our test keys (may have default compaction config too)
|
|
if len(config) < 3 {
|
|
t.Errorf("Expected at least 3 config entries, got %d", len(config))
|
|
}
|
|
|
|
if config["test.key"] != "updated-value" {
|
|
t.Errorf("Expected 'updated-value' for test.key, got '%s'", config["test.key"])
|
|
}
|
|
if config["jira.url"] != "https://example.atlassian.net" {
|
|
t.Errorf("Expected jira.url in config, got '%s'", config["jira.url"])
|
|
}
|
|
if config["jira.project"] != "PROJ" {
|
|
t.Errorf("Expected jira.project in config, got '%s'", config["jira.project"])
|
|
}
|
|
|
|
// Test DeleteConfig
|
|
err = store.DeleteConfig(ctx, "test.key")
|
|
if err != nil {
|
|
t.Fatalf("DeleteConfig failed: %v", err)
|
|
}
|
|
|
|
value, err = store.GetConfig(ctx, "test.key")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig after delete failed: %v", err)
|
|
}
|
|
if value != "" {
|
|
t.Errorf("Expected empty string after delete, got '%s'", value)
|
|
}
|
|
|
|
// Test DeleteConfig for non-existent key (should not error)
|
|
err = store.DeleteConfig(ctx, "nonexistent.key")
|
|
if err != nil {
|
|
t.Fatalf("DeleteConfig for nonexistent key failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConfigNamespaces(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
// Test various namespace conventions
|
|
namespaces := map[string]string{
|
|
"jira.url": "https://example.atlassian.net",
|
|
"jira.project": "PROJ",
|
|
"jira.status_map.todo": "open",
|
|
"linear.team_id": "team-123",
|
|
"github.org": "myorg",
|
|
"custom.my_integration.field": "value",
|
|
}
|
|
|
|
for key, val := range namespaces {
|
|
err := store.SetConfig(ctx, key, val)
|
|
if err != nil {
|
|
t.Fatalf("SetConfig for %s failed: %v", key, err)
|
|
}
|
|
}
|
|
|
|
// Verify all set correctly
|
|
for key, expected := range namespaces {
|
|
value, err := store.GetConfig(ctx, key)
|
|
if err != nil {
|
|
t.Fatalf("GetConfig for %s failed: %v", key, err)
|
|
}
|
|
if value != expected {
|
|
t.Errorf("Expected '%s' for %s, got '%s'", expected, key, value)
|
|
}
|
|
}
|
|
|
|
// Test GetAllConfig returns all
|
|
config, err := store.GetAllConfig(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetAllConfig failed: %v", err)
|
|
}
|
|
|
|
for key, expected := range namespaces {
|
|
if config[key] != expected {
|
|
t.Errorf("Expected '%s' for %s in GetAllConfig, got '%s'", expected, key, config[key])
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestYamlOnlyConfigWithoutDatabase verifies that yaml-only config keys
|
|
// (like no-db) can be set/get without requiring a SQLite database.
|
|
// This is the fix for GH#536 - the chicken-and-egg problem where you couldn't
|
|
// run `bd config set no-db true` without first having a database.
|
|
func TestYamlOnlyConfigWithoutDatabase(t *testing.T) {
|
|
// Create a temp directory with only config.yaml (no database)
|
|
tmpDir, err := os.MkdirTemp("", "bd-test-yaml-config-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create .beads dir: %v", err)
|
|
}
|
|
|
|
// Create config.yaml with a prefix but NO database
|
|
configPath := filepath.Join(beadsDir, "config.yaml")
|
|
if err := os.WriteFile(configPath, []byte("prefix: test\n"), 0644); err != nil {
|
|
t.Fatalf("Failed to create config.yaml: %v", err)
|
|
}
|
|
|
|
// Create empty issues.jsonl (simulates fresh clone)
|
|
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
|
|
if err := os.WriteFile(jsonlPath, []byte(""), 0644); err != nil {
|
|
t.Fatalf("Failed to create issues.jsonl: %v", err)
|
|
}
|
|
|
|
// Test that IsYamlOnlyKey correctly identifies yaml-only keys
|
|
yamlOnlyKeys := []string{"no-db", "json", "routing.mode"}
|
|
for _, key := range yamlOnlyKeys {
|
|
if !config.IsYamlOnlyKey(key) {
|
|
t.Errorf("Expected %q to be a yaml-only key", key)
|
|
}
|
|
}
|
|
|
|
// Test that non-yaml-only keys are correctly identified
|
|
nonYamlKeys := []string{"jira.url", "linear.team_id", "status.custom"}
|
|
for _, key := range nonYamlKeys {
|
|
if config.IsYamlOnlyKey(key) {
|
|
t.Errorf("Expected %q to NOT be a yaml-only key", key)
|
|
}
|
|
}
|
|
}
|
|
|
|
// setupTestDB creates a test store on the suite's Dolt test server
|
|
// (skipped when it is not running). The store is closed by t.Cleanup; the
|
|
// returned cleanup is kept for the callers' defer and does nothing more.
|
|
func setupTestDB(t *testing.T) (*dolt.DoltStore, func()) {
|
|
t.Helper()
|
|
store := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "bd")
|
|
return store, func() {}
|
|
}
|
|
|
|
// TestBeadsRoleGitConfig verifies that beads.role is stored in git config,
|
|
// not SQLite, so that bd doctor can find it (GH#1531).
|
|
func TestBeadsRoleGitConfig(t *testing.T) {
|
|
tmpDir := newGitRepo(t)
|
|
|
|
t.Run("set contributor role writes to git config", func(t *testing.T) {
|
|
cmd := gitCommand("config", "beads.role", "contributor")
|
|
cmd.Dir = tmpDir
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("git config set failed: %v", err)
|
|
}
|
|
|
|
// Verify it's readable from git config
|
|
cmd = gitCommand("config", "--get", "beads.role")
|
|
cmd.Dir = tmpDir
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("git config get failed: %v", err)
|
|
}
|
|
if got := strings.TrimSpace(string(output)); got != "contributor" {
|
|
t.Errorf("expected 'contributor', got %q", got)
|
|
}
|
|
})
|
|
|
|
t.Run("set maintainer role writes to git config", func(t *testing.T) {
|
|
cmd := gitCommand("config", "beads.role", "maintainer")
|
|
cmd.Dir = tmpDir
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("git config set failed: %v", err)
|
|
}
|
|
|
|
cmd = gitCommand("config", "--get", "beads.role")
|
|
cmd.Dir = tmpDir
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("git config get failed: %v", err)
|
|
}
|
|
if got := strings.TrimSpace(string(output)); got != "maintainer" {
|
|
t.Errorf("expected 'maintainer', got %q", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestBeadsRoleCommandsIgnoreInheritedGitRouting(t *testing.T) {
|
|
for _, entry := range os.Environ() {
|
|
key := gitenv.EntryKey(entry)
|
|
if gitenv.IsRoutingKeyForOS(key, runtime.GOOS) {
|
|
t.Setenv(key, "")
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
home := t.TempDir()
|
|
for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME"} {
|
|
t.Setenv(key, home)
|
|
}
|
|
pinJSONOutput(t, false)
|
|
runGit := func(t *testing.T, repo string, args ...string) string {
|
|
t.Helper()
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir = repo
|
|
cmd.Env = gitenv.ScrubRouting(os.Environ())
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("fixture git %v: %v: %s", args, err, out)
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
target, decoy := t.TempDir(), t.TempDir()
|
|
for _, repo := range []string{target, decoy} {
|
|
runGit(t, repo, "init", "--quiet")
|
|
runGit(t, repo, "config", "--local", "core.hooksPath", ".git/hooks")
|
|
}
|
|
t.Chdir(target)
|
|
localRole := func(t *testing.T, repo string) string {
|
|
t.Helper()
|
|
for _, line := range strings.Split(runGit(t, repo, "config", "--local", "--list"), "\n") {
|
|
if value, ok := strings.CutPrefix(line, "beads.role="); ok {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
commands := []struct {
|
|
name string
|
|
cmd *cobra.Command
|
|
args []string
|
|
wantRole string
|
|
wantOutput string
|
|
}{
|
|
{"set", configSetCmd, []string{"beads.role", "contributor"}, "contributor", "Set beads.role = contributor (in git config)"},
|
|
{"get", configGetCmd, []string{"beads.role"}, "maintainer", "maintainer"},
|
|
{"unset", configUnsetCmd, []string{"beads.role"}, "", "Unset beads.role (in git config)"},
|
|
{"set-many", configSetManyCmd, []string{"beads.role=contributor"}, "contributor", "Set beads.role = contributor (in git config)"},
|
|
}
|
|
for _, command := range commands {
|
|
for _, poison := range []struct {
|
|
name string
|
|
env map[string]string
|
|
}{
|
|
{"repository", map[string]string{"GIT_DIR": filepath.Join(decoy, ".git"), "GIT_WORK_TREE": decoy}},
|
|
{"inline_config", map[string]string{"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "beads.role", "GIT_CONFIG_VALUE_0": "injected-role"}},
|
|
} {
|
|
t.Run(command.name+"/"+poison.name, func(t *testing.T) {
|
|
runGit(t, target, "config", "--local", "beads.role", "maintainer")
|
|
runGit(t, decoy, "config", "--local", "beads.role", "decoy-role")
|
|
for key, value := range poison.env {
|
|
t.Setenv(key, value)
|
|
}
|
|
out := captureStdout(t, func() error { return command.cmd.RunE(command.cmd, command.args) })
|
|
if strings.TrimSpace(out) != command.wantOutput {
|
|
t.Errorf("handler output = %q, want %q", out, command.wantOutput)
|
|
}
|
|
if got := localRole(t, target); got != command.wantRole {
|
|
t.Errorf("target role = %q, want %q", got, command.wantRole)
|
|
}
|
|
if got := localRole(t, decoy); got != "decoy-role" {
|
|
t.Errorf("decoy role changed to %q", got)
|
|
}
|
|
for key, value := range poison.env {
|
|
if os.Getenv(key) != value {
|
|
t.Errorf("handler changed parent environment %s", key)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
for _, command := range []struct {
|
|
name string
|
|
cmd *cobra.Command
|
|
args []string
|
|
}{
|
|
{"invalid set", configSetCmd, []string{"beads.role", "admin"}},
|
|
{"invalid batch", configSetManyCmd, []string{"beads.role=contributor", "beads.role=admin"}},
|
|
} {
|
|
t.Run(command.name, func(t *testing.T) {
|
|
runGit(t, target, "config", "--local", "beads.role", "maintainer")
|
|
var err error
|
|
out := captureStderr(t, func() { err = command.cmd.RunE(command.cmd, command.args) })
|
|
if code, ok := exitCodeFromError(err); !ok || code != 1 || !strings.Contains(out, "invalid role") {
|
|
t.Errorf("invalid role refusal = %v, stderr %q", err, out)
|
|
}
|
|
if got := localRole(t, target); got != "maintainer" {
|
|
t.Errorf("invalid role changed target to %q", got)
|
|
}
|
|
})
|
|
}
|
|
t.Run("absent role", func(t *testing.T) {
|
|
runGit(t, target, "config", "--local", "beads.role", "maintainer")
|
|
runGit(t, target, "config", "--local", "--unset", "beads.role")
|
|
out := captureStdout(t, func() error { return configGetCmd.RunE(configGetCmd, []string{"beads.role"}) })
|
|
if strings.TrimSpace(out) != "beads.role (not set in git config)" {
|
|
t.Errorf("absent role output = %q", out)
|
|
}
|
|
var err error
|
|
stderr := captureStderr(t, func() { err = configUnsetCmd.RunE(configUnsetCmd, []string{"beads.role"}) })
|
|
if code, ok := exitCodeFromError(err); !ok || code != 1 || !strings.Contains(stderr, "unsetting beads.role in git config") {
|
|
t.Errorf("absent unset = %v, stderr %q", err, stderr)
|
|
}
|
|
})
|
|
t.Run("default global role", func(t *testing.T) {
|
|
runGit(t, target, "config", "--local", "beads.role", "maintainer")
|
|
runGit(t, target, "config", "--local", "--unset", "beads.role")
|
|
if err := os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[beads]\n\trole = contributor\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := captureStdout(t, func() error { return configGetCmd.RunE(configGetCmd, []string{"beads.role"}) })
|
|
if strings.TrimSpace(out) != "contributor" {
|
|
t.Errorf("default global role output = %q", out)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestBeadsRoleWritesUseFreshCommonConfig(t *testing.T) {
|
|
for _, entry := range os.Environ() {
|
|
key := gitenv.EntryKey(entry)
|
|
if gitenv.IsRoutingKeyForOS(key, runtime.GOOS) {
|
|
t.Setenv(key, "")
|
|
require.NoError(t, os.Unsetenv(key))
|
|
}
|
|
}
|
|
pinJSONOutput(t, false)
|
|
t.Cleanup(git.ResetCaches)
|
|
for _, kind := range []string{"linked", "bare", "separate", "nonrepo"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME"} {
|
|
t.Setenv(key, home)
|
|
}
|
|
run := func(dir string, args ...string) string {
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir, cmd.Env = dir, gitenv.ScrubRouting(os.Environ())
|
|
out, err := cmd.CombinedOutput()
|
|
require.NoError(t, err, "fixture git %v: %s", args, out)
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
main, decoy, target := t.TempDir(), t.TempDir(), t.TempDir()
|
|
for _, dir := range []string{main, decoy} {
|
|
run(dir, "init", "--quiet")
|
|
run(dir, "config", "--local", "core.hooksPath", ".git/hooks")
|
|
}
|
|
switch kind {
|
|
case "linked":
|
|
run(main, "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test", "-c", "commit.gpgSign=false", "commit", "--allow-empty", "-m", "seed")
|
|
run(main, "config", "extensions.worktreeConfig", "true")
|
|
run(main, "worktree", "add", "--detach", target)
|
|
run(target, "config", "--worktree", "beads.role", "private-role")
|
|
case "bare":
|
|
run(home, "init", "--bare", target)
|
|
case "separate":
|
|
run(home, "init", "--separate-git-dir", filepath.Join(home, "external git"), target)
|
|
}
|
|
t.Chdir(decoy)
|
|
git.ResetCaches()
|
|
cached, err := git.GetGitCommonDir()
|
|
require.NoError(t, err)
|
|
t.Chdir(target)
|
|
t.Setenv("GIT_DIR", filepath.Join(decoy, ".git"))
|
|
t.Setenv("GIT_CONFIG", filepath.Join(decoy, ".git", "config"))
|
|
before, err := os.ReadFile(filepath.Join(decoy, ".git", "config"))
|
|
require.NoError(t, err)
|
|
for _, command := range []struct {
|
|
cmd *cobra.Command
|
|
args []string
|
|
}{
|
|
{configSetCmd, []string{"beads.role", "contributor"}},
|
|
{configSetManyCmd, []string{"beads.role=maintainer", "beads.role=contributor"}},
|
|
{configUnsetCmd, []string{"beads.role"}},
|
|
} {
|
|
if kind == "nonrepo" {
|
|
captureStderr(t, func() { require.Error(t, command.cmd.RunE(command.cmd, command.args)) })
|
|
continue
|
|
}
|
|
captureStdout(t, func() error { return command.cmd.RunE(command.cmd, command.args) })
|
|
role := run(target, "config", "--local", "--list")
|
|
require.Equal(t, command.cmd != configUnsetCmd, strings.Contains(role, "beads.role=contributor"))
|
|
if kind == "linked" {
|
|
require.Equal(t, "private-role", run(target, "config", "--worktree", "--get", "beads.role"))
|
|
}
|
|
}
|
|
after, err := os.ReadFile(filepath.Join(decoy, ".git", "config"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, before, after)
|
|
stillCached, err := git.GetGitCommonDir()
|
|
require.NoError(t, err)
|
|
require.Equal(t, cached, stillCached)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestIsValidRemoteURL tests the remote URL validation function
|
|
func TestIsValidRemoteURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
url string
|
|
expected bool
|
|
}{
|
|
// Valid URLs
|
|
{"dolthub scheme", "dolthub://org/repo", true},
|
|
{"gs scheme", "gs://bucket/path", true},
|
|
{"s3 scheme", "s3://bucket/path", true},
|
|
{"file scheme", "file:///path/to/repo", true},
|
|
{"https scheme", "https://github.com/user/repo", true},
|
|
{"http scheme", "http://github.com/user/repo", true},
|
|
{"ssh scheme", "ssh://git@github.com/user/repo", true},
|
|
{"git ssh format", "git@github.com:user/repo.git", true},
|
|
{"git ssh with underscore", "git@gitlab.example_host.com:user/repo.git", true},
|
|
|
|
// Invalid URLs
|
|
{"empty string", "", false},
|
|
{"no scheme", "github.com/user/repo", false},
|
|
{"invalid scheme", "ftp://server/path", false},
|
|
{"malformed git ssh", "git@:repo", false},
|
|
{"just path", "/path/to/repo", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := isValidRemoteURL(tt.url)
|
|
if got != tt.expected {
|
|
t.Errorf("isValidRemoteURL(%q) = %v, want %v", tt.url, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestValidateSyncConfig tests the sync config validation function
|
|
func TestValidateSyncConfig(t *testing.T) {
|
|
// Create a temp directory for testing
|
|
tmpDir := t.TempDir()
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create .beads dir: %v", err)
|
|
}
|
|
|
|
t.Run("valid empty config", func(t *testing.T) {
|
|
// Create minimal config.yaml
|
|
configContent := `prefix: test
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
// After JSONL removal, Dolt sync requires federation.remote
|
|
if len(issues) != 1 {
|
|
t.Errorf("Expected 1 issue (missing federation.remote) for empty config, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid federation.sovereignty", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
federation:
|
|
sovereignty: "invalid-value"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
found := false
|
|
for _, issue := range issues {
|
|
if strings.Contains(issue, "federation.sovereignty") {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Expected issue about federation.sovereignty, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("dolt-native mode without remote", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
sync:
|
|
mode: "dolt-native"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
found := false
|
|
for _, issue := range issues {
|
|
if strings.Contains(issue, "federation.remote") && strings.Contains(issue, "required") {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Expected issue about federation.remote being required, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid remote URL", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
federation:
|
|
remote: "invalid-url"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
found := false
|
|
for _, issue := range issues {
|
|
if strings.Contains(issue, "federation.remote") && (strings.Contains(issue, "not a valid remote URL") || strings.Contains(issue, "no scheme") || strings.Contains(issue, "not allowed")) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Expected issue about invalid remote URL, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("valid sync config", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
sync:
|
|
mode: "dolt-native"
|
|
conflict:
|
|
strategy: "newest"
|
|
federation:
|
|
sovereignty: "T2"
|
|
remote: "https://github.com/user/beads-data.git"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
if len(issues) != 0 {
|
|
t.Errorf("Expected no issues for valid config, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("remote URL with null byte", func(t *testing.T) {
|
|
configContent := "prefix: test\nfederation:\n remote: \"dolthub://org/repo\\x00evil\"\n"
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
found := false
|
|
for _, issue := range issues {
|
|
if strings.Contains(issue, "federation.remote") {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Expected issue about invalid remote URL with null byte, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("allowed-remote-patterns enforcement", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
federation:
|
|
remote: "https://github.com/user/repo"
|
|
allowed-remote-patterns:
|
|
- "dolthub://myorg/*"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
found := false
|
|
for _, issue := range issues {
|
|
if strings.Contains(issue, "does not match") {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Expected issue about remote not matching allowed patterns, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("allowed-remote-patterns passes when matching", func(t *testing.T) {
|
|
configContent := `prefix: test
|
|
federation:
|
|
remote: "dolthub://myorg/myrepo"
|
|
allowed-remote-patterns:
|
|
- "dolthub://myorg/*"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(tmpDir)
|
|
if len(issues) != 0 {
|
|
t.Errorf("Expected no issues when remote matches allowed pattern, got: %v", issues)
|
|
}
|
|
})
|
|
|
|
t.Run("uses shared worktree config when local .beads is absent", func(t *testing.T) {
|
|
bareDir, worktreeDir := setupBareParentInitWorktree(t)
|
|
bareBeadsDir := filepath.Join(bareDir, ".beads")
|
|
if err := os.MkdirAll(bareBeadsDir, 0o755); err != nil {
|
|
t.Fatalf("Failed to create bare .beads dir: %v", err)
|
|
}
|
|
|
|
configContent := `federation:
|
|
remote: "dolthub://myorg/myrepo"
|
|
allowed-remote-patterns:
|
|
- "dolthub://myorg/*"
|
|
`
|
|
if err := os.WriteFile(filepath.Join(bareBeadsDir, "config.yaml"), []byte(configContent), 0o644); err != nil {
|
|
t.Fatalf("Failed to write config.yaml: %v", err)
|
|
}
|
|
|
|
issues := validateSyncConfig(worktreeDir)
|
|
if len(issues) != 0 {
|
|
t.Errorf("Expected no issues when shared worktree config is valid, got: %v", issues)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestResolvedConfigRepoRoot(t *testing.T) {
|
|
resetResolutionCaches := func(t *testing.T) {
|
|
t.Helper()
|
|
beads.ResetCaches()
|
|
git.ResetCaches()
|
|
t.Cleanup(func() {
|
|
beads.ResetCaches()
|
|
git.ResetCaches()
|
|
})
|
|
}
|
|
|
|
assertSameResolvedPath := func(t *testing.T, got, want string) {
|
|
t.Helper()
|
|
|
|
gotResolved, err := filepath.EvalSymlinks(got)
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q): %v", got, err)
|
|
}
|
|
wantResolved, err := filepath.EvalSymlinks(want)
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q): %v", want, err)
|
|
}
|
|
if gotResolved != wantResolved {
|
|
t.Errorf("resolvedConfigRepoRoot() = %q (resolved %q), want %q (resolved %q)", got, gotResolved, want, wantResolved)
|
|
}
|
|
}
|
|
|
|
t.Run("uses local workspace from subdirectory", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
subDir := filepath.Join(tmpDir, "sub", "dir")
|
|
|
|
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
|
t.Fatalf("Failed to create .beads dir: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o644); err != nil {
|
|
t.Fatalf("Failed to create metadata.json: %v", err)
|
|
}
|
|
if err := os.MkdirAll(subDir, 0o755); err != nil {
|
|
t.Fatalf("Failed to create sub dir: %v", err)
|
|
}
|
|
|
|
// CWD discovery is what is under test: an inherited BEADS_DIR
|
|
// (a developer's shell, or one an earlier in-process command
|
|
// exported) would take precedence and fail the lookup.
|
|
t.Setenv("BEADS_DIR", "")
|
|
t.Chdir(subDir)
|
|
resetResolutionCaches(t)
|
|
|
|
got, err := resolvedConfigRepoRoot()
|
|
if err != nil {
|
|
t.Fatalf("resolvedConfigRepoRoot returned error: %v", err)
|
|
}
|
|
assertSameResolvedPath(t, got, tmpDir)
|
|
})
|
|
|
|
t.Run("uses BEADS_DIR target", func(t *testing.T) {
|
|
cwdDir := t.TempDir()
|
|
targetRepo := t.TempDir()
|
|
targetBeadsDir := filepath.Join(targetRepo, ".beads")
|
|
|
|
if err := os.MkdirAll(targetBeadsDir, 0o755); err != nil {
|
|
t.Fatalf("Failed to create target .beads dir: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(targetBeadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o644); err != nil {
|
|
t.Fatalf("Failed to create target metadata.json: %v", err)
|
|
}
|
|
|
|
t.Setenv("BEADS_DIR", targetBeadsDir)
|
|
t.Chdir(cwdDir)
|
|
resetResolutionCaches(t)
|
|
|
|
got, err := resolvedConfigRepoRoot()
|
|
if err != nil {
|
|
t.Fatalf("resolvedConfigRepoRoot returned error: %v", err)
|
|
}
|
|
assertSameResolvedPath(t, got, targetRepo)
|
|
})
|
|
|
|
t.Run("uses worktree fallback when local .beads is absent", func(t *testing.T) {
|
|
bareDir, worktreeDir := setupBareParentInitWorktree(t)
|
|
bareBeadsDir := filepath.Join(bareDir, ".beads")
|
|
|
|
if err := os.MkdirAll(bareBeadsDir, 0o755); err != nil {
|
|
t.Fatalf("Failed to create bare .beads dir: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(bareBeadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o644); err != nil {
|
|
t.Fatalf("Failed to create bare metadata.json: %v", err)
|
|
}
|
|
|
|
t.Setenv("BEADS_DIR", "") // as above: discovery, not BEADS_DIR, is under test
|
|
t.Chdir(worktreeDir)
|
|
resetResolutionCaches(t)
|
|
|
|
got, err := resolvedConfigRepoRoot()
|
|
if err != nil {
|
|
t.Fatalf("resolvedConfigRepoRoot returned error: %v", err)
|
|
}
|
|
assertSameResolvedPath(t, got, bareDir)
|
|
})
|
|
}
|
|
|
|
func TestCustomStatusConfig(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
t.Run("categorized format round-trips", func(t *testing.T) {
|
|
err := store.SetConfig(ctx, "status.custom", "review:active,testing:wip")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
detailed, err := store.GetCustomStatusesDetailed(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatusesDetailed failed: %v", err)
|
|
}
|
|
if len(detailed) != 2 {
|
|
t.Fatalf("expected 2 statuses, got %d", len(detailed))
|
|
}
|
|
if detailed[0].Name != "review" || detailed[0].Category != types.CategoryActive {
|
|
t.Errorf("status[0] = {%q, %q}, want {review, active}", detailed[0].Name, detailed[0].Category)
|
|
}
|
|
if detailed[1].Name != "testing" || detailed[1].Category != types.CategoryWIP {
|
|
t.Errorf("status[1] = {%q, %q}, want {testing, wip}", detailed[1].Name, detailed[1].Category)
|
|
}
|
|
})
|
|
|
|
t.Run("flat format returns CategoryUnspecified", func(t *testing.T) {
|
|
err := store.SetConfig(ctx, "status.custom", "review,testing")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
detailed, err := store.GetCustomStatusesDetailed(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatusesDetailed failed: %v", err)
|
|
}
|
|
if len(detailed) != 2 {
|
|
t.Fatalf("expected 2 statuses, got %d", len(detailed))
|
|
}
|
|
for _, s := range detailed {
|
|
if s.Category != types.CategoryUnspecified {
|
|
t.Errorf("status %q has category %q, want unspecified", s.Name, s.Category)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("mixed format returns both categorized and uncategorized", func(t *testing.T) {
|
|
err := store.SetConfig(ctx, "status.custom", "review:active,legacy")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
detailed, err := store.GetCustomStatusesDetailed(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatusesDetailed failed: %v", err)
|
|
}
|
|
if len(detailed) != 2 {
|
|
t.Fatalf("expected 2 statuses, got %d", len(detailed))
|
|
}
|
|
// The custom_statuses table is read back in name order, not
|
|
// config order: look each status up by name.
|
|
categories := map[string]types.StatusCategory{}
|
|
for _, d := range detailed {
|
|
categories[d.Name] = d.Category
|
|
}
|
|
if got := categories["review"]; got != types.CategoryActive {
|
|
t.Errorf("review should be active, got %q", got)
|
|
}
|
|
if got, ok := categories["legacy"]; !ok || got != types.CategoryUnspecified {
|
|
t.Errorf("legacy should be unspecified, got %q (present=%v)", got, ok)
|
|
}
|
|
})
|
|
|
|
t.Run("GetCustomStatuses returns just names (backward compat)", func(t *testing.T) {
|
|
err := store.SetConfig(ctx, "status.custom", "review:active,testing:wip,qa:done")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
names, err := store.GetCustomStatuses(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatuses failed: %v", err)
|
|
}
|
|
if len(names) != 3 {
|
|
t.Fatalf("expected 3 names, got %d", len(names))
|
|
}
|
|
// Name order: since the normalized custom_statuses table (#2961,
|
|
// 8537d943b) statuses are read back ORDER BY name; the table has
|
|
// no ordinal column, and custom_types reads the same way.
|
|
want := []string{"qa", "review", "testing"}
|
|
for i, name := range names {
|
|
if name != want[i] {
|
|
t.Errorf("name[%d] = %q, want %q", i, name, want[i])
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("cache invalidation on SetConfig", func(t *testing.T) {
|
|
// Set first value
|
|
err := store.SetConfig(ctx, "status.custom", "alpha:active")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
detailed1, err := store.GetCustomStatusesDetailed(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatusesDetailed failed: %v", err)
|
|
}
|
|
if len(detailed1) != 1 || detailed1[0].Name != "alpha" {
|
|
t.Fatalf("expected [alpha], got %+v", detailed1)
|
|
}
|
|
|
|
// Set different value — cache should be invalidated
|
|
err = store.SetConfig(ctx, "status.custom", "beta:wip,gamma:done")
|
|
if err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
detailed2, err := store.GetCustomStatusesDetailed(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetCustomStatusesDetailed failed: %v", err)
|
|
}
|
|
if len(detailed2) != 2 {
|
|
t.Fatalf("expected 2 statuses after cache invalidation, got %d", len(detailed2))
|
|
}
|
|
if detailed2[0].Name != "beta" || detailed2[0].Category != types.CategoryWIP {
|
|
t.Errorf("status[0] = {%q, %q}, want {beta, wip}", detailed2[0].Name, detailed2[0].Category)
|
|
}
|
|
if detailed2[1].Name != "gamma" || detailed2[1].Category != types.CategoryDone {
|
|
t.Errorf("status[1] = {%q, %q}, want {gamma, done}", detailed2[1].Name, detailed2[1].Category)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestConfigSetMany tests the batch config set functionality used by 'bd config set-many'.
|
|
func TestConfigSetMany(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
t.Run("batch set multiple DB keys", func(t *testing.T) {
|
|
pairs := map[string]string{
|
|
"ado.state_map.open": "New",
|
|
"ado.state_map.in_progress": "Active",
|
|
"ado.state_map.closed": "Closed",
|
|
}
|
|
for k, v := range pairs {
|
|
if err := store.SetConfig(ctx, k, v); err != nil {
|
|
t.Fatalf("SetConfig(%s) failed: %v", k, err)
|
|
}
|
|
}
|
|
|
|
// Verify all values were set correctly
|
|
for k, expected := range pairs {
|
|
got, err := store.GetConfig(ctx, k)
|
|
if err != nil {
|
|
t.Fatalf("GetConfig(%s) failed: %v", k, err)
|
|
}
|
|
if got != expected {
|
|
t.Errorf("GetConfig(%s) = %q, want %q", k, got, expected)
|
|
}
|
|
}
|
|
|
|
// Verify they appear in GetAllConfig
|
|
all, err := store.GetAllConfig(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetAllConfig failed: %v", err)
|
|
}
|
|
for k, expected := range pairs {
|
|
if all[k] != expected {
|
|
t.Errorf("GetAllConfig[%s] = %q, want %q", k, all[k], expected)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("batch set overwrites existing values", func(t *testing.T) {
|
|
// Set initial values
|
|
if err := store.SetConfig(ctx, "test.batch.a", "old-a"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
if err := store.SetConfig(ctx, "test.batch.b", "old-b"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
|
|
// Overwrite with batch
|
|
updates := map[string]string{
|
|
"test.batch.a": "new-a",
|
|
"test.batch.b": "new-b",
|
|
"test.batch.c": "new-c",
|
|
}
|
|
for k, v := range updates {
|
|
if err := store.SetConfig(ctx, k, v); err != nil {
|
|
t.Fatalf("SetConfig(%s) failed: %v", k, err)
|
|
}
|
|
}
|
|
|
|
for k, expected := range updates {
|
|
got, err := store.GetConfig(ctx, k)
|
|
if err != nil {
|
|
t.Fatalf("GetConfig(%s) failed: %v", k, err)
|
|
}
|
|
if got != expected {
|
|
t.Errorf("GetConfig(%s) = %q, want %q", k, got, expected)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("batch set with empty value", func(t *testing.T) {
|
|
if err := store.SetConfig(ctx, "test.empty", ""); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
got, err := store.GetConfig(ctx, "test.empty")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig failed: %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Errorf("expected empty string, got %q", got)
|
|
}
|
|
})
|
|
|
|
t.Run("batch set mixed namespaces in single operation", func(t *testing.T) {
|
|
// Simulates what set-many does: multiple keys from different
|
|
// namespaces all written to the DB in one logical batch.
|
|
mixed := map[string]string{
|
|
"jira.url": "https://jira.example.com",
|
|
"jira.project": "BEADS",
|
|
"ado.state_map.open": "New",
|
|
"ado.state_map.closed": "Done",
|
|
"custom.pipeline": "review,qa,deploy",
|
|
"status.custom": "awaiting_review,awaiting_testing",
|
|
}
|
|
for k, v := range mixed {
|
|
if err := store.SetConfig(ctx, k, v); err != nil {
|
|
t.Fatalf("SetConfig(%s) failed: %v", k, err)
|
|
}
|
|
}
|
|
|
|
// Verify every key was persisted
|
|
for k, expected := range mixed {
|
|
got, err := store.GetConfig(ctx, k)
|
|
if err != nil {
|
|
t.Fatalf("GetConfig(%s) failed: %v", k, err)
|
|
}
|
|
if got != expected {
|
|
t.Errorf("GetConfig(%s) = %q, want %q", k, got, expected)
|
|
}
|
|
}
|
|
|
|
// Verify all appear in GetAllConfig
|
|
all, err := store.GetAllConfig(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetAllConfig failed: %v", err)
|
|
}
|
|
for k, expected := range mixed {
|
|
if all[k] != expected {
|
|
t.Errorf("GetAllConfig[%s] = %q, want %q", k, all[k], expected)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("batch set preserves previously written keys", func(t *testing.T) {
|
|
// Write batch 1
|
|
if err := store.SetConfig(ctx, "retain.alpha", "aaa"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
if err := store.SetConfig(ctx, "retain.beta", "bbb"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
|
|
// Write batch 2 (different keys)
|
|
if err := store.SetConfig(ctx, "retain.gamma", "ggg"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
|
|
// Verify batch 1 keys are still intact after batch 2
|
|
got, err := store.GetConfig(ctx, "retain.alpha")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig failed: %v", err)
|
|
}
|
|
if got != "aaa" {
|
|
t.Errorf("retain.alpha = %q, want %q", got, "aaa")
|
|
}
|
|
|
|
got, err = store.GetConfig(ctx, "retain.beta")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig failed: %v", err)
|
|
}
|
|
if got != "bbb" {
|
|
t.Errorf("retain.beta = %q, want %q", got, "bbb")
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestConfigSetManyValidationIntegration tests that the validation logic
|
|
// in set-many correctly rejects invalid values for known constrained keys
|
|
// before any DB writes would occur.
|
|
func TestConfigSetManyValidationIntegration(t *testing.T) {
|
|
t.Run("beads.role only accepts maintainer or contributor", func(t *testing.T) {
|
|
validRoles := map[string]bool{"maintainer": true, "contributor": true}
|
|
for _, role := range []string{"maintainer", "contributor"} {
|
|
if !validRoles[role] {
|
|
t.Errorf("expected %q to be valid", role)
|
|
}
|
|
}
|
|
for _, role := range []string{"admin", "superadmin", "owner", "", "MAINTAINER"} {
|
|
if validRoles[role] {
|
|
t.Errorf("expected %q to be invalid", role)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("status.custom validation catches invalid formats", func(t *testing.T) {
|
|
valid := []string{
|
|
"awaiting_review,awaiting_testing",
|
|
"review,qa,deploy",
|
|
"single_status",
|
|
}
|
|
for _, v := range valid {
|
|
if _, err := types.ParseCustomStatusConfig(v); err != nil {
|
|
t.Errorf("expected %q to be valid: %v", v, err)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("empty status.custom is allowed", func(t *testing.T) {
|
|
// Empty value skips validation in the command handler
|
|
result, err := types.ParseCustomStatusConfig("")
|
|
if err != nil {
|
|
t.Errorf("expected empty status.custom to be valid: %v", err)
|
|
}
|
|
if result != nil {
|
|
t.Errorf("expected nil result for empty input, got %v", result)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestBeadsRoleWriteErrorsRetainGitDiagnostics(t *testing.T) {
|
|
t.Setenv("LC_ALL", "C")
|
|
pinJSONOutput(t, false)
|
|
for _, entry := range os.Environ() {
|
|
key := gitenv.EntryKey(entry)
|
|
if gitenv.IsRoutingKeyForOS(key, runtime.GOOS) {
|
|
t.Setenv(key, "")
|
|
require.NoError(t, os.Unsetenv(key))
|
|
}
|
|
}
|
|
for _, operation := range []struct {
|
|
name string
|
|
cmd *cobra.Command
|
|
args []string
|
|
}{
|
|
{"set", configSetCmd, []string{"beads.role", "contributor"}},
|
|
{"set-many", configSetManyCmd, []string{"beads.role=contributor"}},
|
|
{"unset", configUnsetCmd, []string{"beads.role"}},
|
|
} {
|
|
for _, failure := range []string{"nonrepo", "config_lock", "duplicate"} {
|
|
t.Run(operation.name+"/"+failure, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME"} {
|
|
t.Setenv(key, home)
|
|
}
|
|
repo := newGitRepo(t)
|
|
t.Chdir(repo)
|
|
run := func(args ...string) {
|
|
cmd := gitCommand(args...)
|
|
out, err := cmd.CombinedOutput()
|
|
require.NoError(t, err, "%s", out)
|
|
}
|
|
run("config", "--local", "beads.role", "maintainer")
|
|
want := "not a git repository"
|
|
switch failure {
|
|
case "nonrepo":
|
|
t.Chdir(t.TempDir())
|
|
case "config_lock":
|
|
require.NoError(t, os.WriteFile(filepath.Join(repo, ".git", "config.lock"), []byte("owned lock"), 0600))
|
|
want = "could not lock config file"
|
|
case "duplicate":
|
|
run("config", "--local", "--add", "beads.role", "contributor")
|
|
want = "multiple values"
|
|
}
|
|
configPath := filepath.Join(repo, ".git", "config")
|
|
before, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
stderr := captureStderr(t, func() { err = operation.cmd.RunE(operation.cmd, operation.args) })
|
|
code, ok := exitCodeFromError(err)
|
|
require.True(t, ok)
|
|
require.Equal(t, 1, code)
|
|
require.Contains(t, stderr, want)
|
|
after, readErr := os.ReadFile(configPath)
|
|
require.NoError(t, readErr)
|
|
require.Equal(t, before, after, "failed role write changed config")
|
|
})
|
|
}
|
|
}
|
|
}
|