* Fix worktree-aware config.yaml resolution * fix: honor shared worktree config resolution * fix(doctor-fix): resolve shared-worktree beads paths intent(doctor-fix): review-fix-loop found that doctor --fix should keep working when a worktree uses a shared .beads directory with no local path constraint(doctor-fix): permission fixes must still skip chmod on local symlinked .beads directories when they exist decision(doctor-fix): centralize resolved workspace lookup in a helper and route fix handlers through it instead of rebuilding path/.beads ad hoc * test(doctor-fix): cover shared-worktree path resolution intent(doctor-fix): review-fix-loop needed regressions that prove fix handlers operate on the shared workspace when a worktree has no local .beads decision(doctor-fix): cover one config-regeneration path and two file-mutation paths so future regressions fail close to the real worktree behavior learned(doctor-fix): metadata regeneration, stale lock cleanup, and permission repair all exercised separate path assumptions inside the fix package * test(doctor-fix): strengthen workspace validation coverage Why: - shared-worktree fix coverage still left validateBeadsWorkspace edge cases loosely asserted - fix package had no direct test for resolvedWorkspaceBeadsDir local vs shared precedence What: - add focused tests for resolvedWorkspaceBeadsDir local/shared selection - make validateBeadsWorkspace reject file-backed and file-symlinked .beads explicitly Refs: - beads-xc4 * refactor(doctor-fix): share workspace beads dir resolution Why: - fix package resolved local and shared .beads paths in multiple steps even though they come from the same workspace root - permissions handling was correct but harder to follow because local/resolved values were stitched together at the call site What: - extract a package-local helper that returns both local and resolved .beads paths from one workspace lookup - make Permissions reuse that helper and add direct tests for local path behavior Refs: - beads-rim * fix(config): remove shared-worktree import cycle Why: - shared-worktree config fallback in internal/config imported internal/beads - that pulled internal/utils back into internal/utils test setup through storage/dolt and caused PR #37 CI import cycle failures What: - keep worktree fallback resolution inside internal/config - reuse the repo-scoped git-dir fallback for config.yaml discovery without importing internal/beads Refs: - beads-3ix * fix(doctor): resolve database-integrity against shared worktrees intent(doctor): keep integrity recovery from reading a nonexistent worktree-local .beads during shared-worktree repairs decision(database-integrity): resolve config and dolt paths from resolvedWorkspaceBeadsDir while keeping bd init --force anchored to the worktree repo root learned(shared-worktree): the server-mode guard must inspect shared config or it can silently bypass the configured database safety check * fix(doctor): honor shared worktrees in remote-consistency intent(doctor): keep remote reconciliation pointed at the active shared workspace state during worktree repairs decision(remote-consistency): resolve shared-worktree config and dolt paths once before comparing SQL and CLI remotes constraint(remote-consistency): the fixer still runs from the worktree repo path but cannot derive paths from repoPath/.beads when that directory is absent * fix(doctor): align shared worktree resolution (beads-u8z) * test(config): cover shared worktree config precedence (beads-7kw) intent(config): lock down shared worktree config resolution so future fallback fixes do not silently write or read the wrong file decision(config-tests): cover both write precedence and Initialize() local override behavior in worktree-specific tests constraint(shared-worktree): runtime resolution can legitimately target either worktree-local or shared .beads, so the regression coverage must assert both paths explicitly * Add worktree path coverage for beads dir resolution * Avoid creating .beads on normal Dolt store opens * Prefer loaded worktree config when finding project config * test(worktree): add shared config regression coverage (beads-z65b) intent(worktree-config): keep shared-worktree sync.remote persistence covered at both the YAML helper and CLI command layers decision(regression-tests): pair a loaded-state config test with a bd subprocess test because the original regression crossed initialization and command execution constraint(cmd-bd): use the repo CGO test wrapper for command coverage because raw go test ./cmd/bd requires local ICU setup outside the supported path * fix(doctor): sweep shared-worktree resolver into remaining checks (beads-3y9p) intent: keep this PR's worktree bugfix coherent by removing the last redirect-only doctor resolution paths decision: route standalone doctor checks through ResolveBeadsDirForRepo and add shared-worktree regressions for locks, mq cleanup, hook migrations, and remote consistency constraint: leave local-only semantics alone and validate with targeted doctor tests instead of broad environment-sensitive full-suite expectations * test(worktree): strengthen resolver regression coverage (beads-i9x1) intent(worktree-coverage): raise patch coverage for the shared-worktree resolver PR on branches introduced by this change set decision(config-tests): cover worktree config helpers directly in internal/config so fallback and loaded-state branches stay deterministic decision(storage-tests): exercise lazy federation credential-key initialization with one server-backed peer lifecycle instead of broader end-to-end sync flows constraint(doctor-tests): verify tracked runtime cleanup from the resolved main repo because shared .beads lives outside the worktree root * fix(bootstrap): persist sync remote in resolved config intent(bootstrap): fresh worktree bootstrap should finalize the shared workspace config instead of failing on a local .beads stub decision(config-resolution): add a targeted yaml writer for an explicit beads directory so bootstrap can reuse the resolved workspace path constraint(worktree-config): bare-parent and shared worktree setups can expose a local .beads directory that must not shadow the canonical config.yaml during finalize * test(config): cover worktree resolution edge paths (beads-adan) intent(codecov): PR #3265 needed patch coverage above the Codecov complaint on config/worktree resolution changes decision(tests): add unit coverage in existing config, doctor/fix, and credentials suites instead of broader e2e coverage constraint(tests): keep sanitizeDBName available to nocgo typecheck because init.go calls it outside cgo-only files * test(embedded): allow concurrent dolt status --------- Co-authored-by: matt wilkie <maphew@gmail.com>
341 lines
11 KiB
Go
341 lines
11 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/git"
|
|
)
|
|
|
|
func setupConfigWorktree(t *testing.T) (mainRepoDir, worktreeDir, mainConfigPath string) {
|
|
t.Helper()
|
|
|
|
tmpDir := t.TempDir()
|
|
mainRepoDir = filepath.Join(tmpDir, "main-repo")
|
|
if err := os.MkdirAll(mainRepoDir, 0o755); err != nil {
|
|
t.Fatalf("failed to create main repo dir: %v", err)
|
|
}
|
|
|
|
run := func(dir string, args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
|
|
run(mainRepoDir, "init")
|
|
run(mainRepoDir, "config", "user.email", "test@example.com")
|
|
run(mainRepoDir, "config", "user.name", "Test User")
|
|
|
|
readmePath := filepath.Join(mainRepoDir, "README.md")
|
|
if err := os.WriteFile(readmePath, []byte("# Test\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write README.md: %v", err)
|
|
}
|
|
run(mainRepoDir, "add", "README.md")
|
|
run(mainRepoDir, "commit", "-m", "Initial commit")
|
|
|
|
worktreeDir = filepath.Join(tmpDir, "worktree")
|
|
addWorktree := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
|
|
addWorktree.Dir = mainRepoDir
|
|
if out, err := addWorktree.CombinedOutput(); err != nil {
|
|
t.Fatalf("git worktree add failed: %v\n%s", err, out)
|
|
}
|
|
t.Cleanup(func() {
|
|
removeWorktree := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
|
|
removeWorktree.Dir = mainRepoDir
|
|
_ = removeWorktree.Run()
|
|
})
|
|
|
|
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
|
|
if err := os.MkdirAll(mainBeadsDir, 0o755); err != nil {
|
|
t.Fatalf("failed to create main .beads dir: %v", err)
|
|
}
|
|
mainConfigPath = filepath.Join(mainBeadsDir, "config.yaml")
|
|
if err := os.WriteFile(mainConfigPath, []byte("no-git-ops: false\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write main config.yaml: %v", err)
|
|
}
|
|
|
|
if err := os.RemoveAll(filepath.Join(worktreeDir, ".beads")); err != nil {
|
|
t.Fatalf("failed to remove worktree .beads: %v", err)
|
|
}
|
|
|
|
t.Setenv("BEADS_DIR", "")
|
|
t.Chdir(worktreeDir)
|
|
git.ResetCaches()
|
|
t.Cleanup(git.ResetCaches)
|
|
|
|
return mainRepoDir, worktreeDir, mainConfigPath
|
|
}
|
|
|
|
func TestSetYamlConfig_WorktreeFallbackUsesMainRepoConfig(t *testing.T) {
|
|
_, worktreeDir, mainConfigPath := setupConfigWorktree(t)
|
|
|
|
if err := SetYamlConfig("no-git-ops", "true"); err != nil {
|
|
t.Fatalf("SetYamlConfig() error = %v", err)
|
|
}
|
|
|
|
content, err := os.ReadFile(mainConfigPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to read main config.yaml: %v", err)
|
|
}
|
|
if !strings.Contains(string(content), "no-git-ops: true") {
|
|
t.Fatalf("expected shared config.yaml to be updated, got:\n%s", string(content))
|
|
}
|
|
|
|
if _, err := os.Stat(filepath.Join(worktreeDir, ".beads", "config.yaml")); !os.IsNotExist(err) {
|
|
t.Fatalf("expected no worktree-local config.yaml, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestSetYamlConfig_PrefersWorktreeLocalConfigWhenPresent(t *testing.T) {
|
|
_, worktreeDir, mainConfigPath := setupConfigWorktree(t)
|
|
|
|
worktreeBeadsDir := filepath.Join(worktreeDir, ".beads")
|
|
if err := os.MkdirAll(worktreeBeadsDir, 0o755); err != nil {
|
|
t.Fatalf("failed to create worktree .beads dir: %v", err)
|
|
}
|
|
worktreeConfigPath := filepath.Join(worktreeBeadsDir, "config.yaml")
|
|
if err := os.WriteFile(worktreeConfigPath, []byte("no-git-ops: false\nactor: worktree-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write worktree config.yaml: %v", err)
|
|
}
|
|
|
|
if err := SetYamlConfig("no-git-ops", "true"); err != nil {
|
|
t.Fatalf("SetYamlConfig() error = %v", err)
|
|
}
|
|
|
|
worktreeContent, err := os.ReadFile(worktreeConfigPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to read worktree config.yaml: %v", err)
|
|
}
|
|
if !strings.Contains(string(worktreeContent), "no-git-ops: true") {
|
|
t.Fatalf("expected worktree config.yaml to be updated, got:\n%s", string(worktreeContent))
|
|
}
|
|
|
|
mainContent, err := os.ReadFile(mainConfigPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to read main config.yaml: %v", err)
|
|
}
|
|
if strings.Contains(string(mainContent), "no-git-ops: true") {
|
|
t.Fatalf("expected shared config.yaml to remain unchanged, got:\n%s", string(mainContent))
|
|
}
|
|
}
|
|
|
|
func TestFindConfigYAMLPath_WorktreeFallbackUsesMainRepoConfig(t *testing.T) {
|
|
_, _, mainConfigPath := setupConfigWorktree(t)
|
|
|
|
got, err := FindConfigYAMLPath()
|
|
if err != nil {
|
|
t.Fatalf("FindConfigYAMLPath() error = %v", err)
|
|
}
|
|
|
|
gotResolved, err := filepath.EvalSymlinks(filepath.Clean(got))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", got, err)
|
|
}
|
|
wantResolved, err := filepath.EvalSymlinks(filepath.Clean(mainConfigPath))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", mainConfigPath, err)
|
|
}
|
|
|
|
if gotResolved != wantResolved {
|
|
t.Fatalf("FindConfigYAMLPath() = %q, want %q", gotResolved, wantResolved)
|
|
}
|
|
}
|
|
|
|
func TestInitialize_WorktreeFallbackUsesMainRepoConfig(t *testing.T) {
|
|
restore := envSnapshot(t)
|
|
defer restore()
|
|
|
|
_, _, mainConfigPath := setupConfigWorktree(t)
|
|
if err := os.WriteFile(mainConfigPath, []byte("json: true\nactor: shared-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write main config.yaml: %v", err)
|
|
}
|
|
|
|
ResetForTesting()
|
|
if err := Initialize(); err != nil {
|
|
t.Fatalf("Initialize() error = %v", err)
|
|
}
|
|
|
|
if got := GetBool("json"); !got {
|
|
t.Fatalf("GetBool(json) = %v, want true", got)
|
|
}
|
|
if got := GetString("actor"); got != "shared-user" {
|
|
t.Fatalf("GetString(actor) = %q, want %q", got, "shared-user")
|
|
}
|
|
}
|
|
|
|
func TestInitialize_WorktreeFallbackMergesSharedLocalOverride(t *testing.T) {
|
|
restore := envSnapshot(t)
|
|
defer restore()
|
|
|
|
_, _, mainConfigPath := setupConfigWorktree(t)
|
|
if err := os.WriteFile(mainConfigPath, []byte("json: false\nactor: project-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write main config.yaml: %v", err)
|
|
}
|
|
|
|
localConfigPath := filepath.Join(filepath.Dir(mainConfigPath), "config.local.yaml")
|
|
if err := os.WriteFile(localConfigPath, []byte("actor: local-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write config.local.yaml: %v", err)
|
|
}
|
|
|
|
ResetForTesting()
|
|
if err := Initialize(); err != nil {
|
|
t.Fatalf("Initialize() error = %v", err)
|
|
}
|
|
|
|
if got := GetBool("json"); got {
|
|
t.Fatalf("GetBool(json) = %v, want false", got)
|
|
}
|
|
if got := GetString("actor"); got != "local-user" {
|
|
t.Fatalf("GetString(actor) = %q, want %q", got, "local-user")
|
|
}
|
|
}
|
|
|
|
func TestWorktreeFallbackConfigPath(t *testing.T) {
|
|
mainRepoDir, worktreeDir, mainConfigPath := setupConfigWorktree(t)
|
|
|
|
t.Run("shared worktree resolves main repo config", func(t *testing.T) {
|
|
got := worktreeFallbackConfigPath(worktreeDir)
|
|
gotResolved, err := filepath.EvalSymlinks(filepath.Clean(got))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", got, err)
|
|
}
|
|
wantResolved, err := filepath.EvalSymlinks(filepath.Clean(mainConfigPath))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", mainConfigPath, err)
|
|
}
|
|
if gotResolved != wantResolved {
|
|
t.Fatalf("worktreeFallbackConfigPath() = %q, want %q", gotResolved, wantResolved)
|
|
}
|
|
})
|
|
|
|
t.Run("primary repo returns empty", func(t *testing.T) {
|
|
if got := worktreeFallbackConfigPath(mainRepoDir); got != "" {
|
|
t.Fatalf("worktreeFallbackConfigPath(mainRepoDir) = %q, want empty", got)
|
|
}
|
|
})
|
|
|
|
t.Run("non git repo returns empty", func(t *testing.T) {
|
|
if got := worktreeFallbackConfigPath(t.TempDir()); got != "" {
|
|
t.Fatalf("worktreeFallbackConfigPath(non-git) = %q, want empty", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitDirsForRepo_NonGitRepo(t *testing.T) {
|
|
if gitDir, commonDir, ok := gitDirsForRepo(t.TempDir()); ok || gitDir != "" || commonDir != "" {
|
|
t.Fatalf("gitDirsForRepo(non-git) = (%q, %q, %v), want empty paths and false", gitDir, commonDir, ok)
|
|
}
|
|
}
|
|
|
|
func TestWorktreePathHelpers_EdgeCases(t *testing.T) {
|
|
t.Run("empty git path", func(t *testing.T) {
|
|
if got := gitPathForRepo(t.TempDir(), ""); got != "" {
|
|
t.Fatalf("gitPathForRepo(empty) = %q, want empty", got)
|
|
}
|
|
})
|
|
|
|
t.Run("missing relative git path falls back to clean path", func(t *testing.T) {
|
|
repoPath := t.TempDir()
|
|
want := filepath.Join(repoPath, "missing-git-dir")
|
|
if got := gitPathForRepo(repoPath, "missing-git-dir"); got != want {
|
|
t.Fatalf("gitPathForRepo(missing) = %q, want %q", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("same path handles empty inputs", func(t *testing.T) {
|
|
if !samePath("", "") {
|
|
t.Fatal("samePath(empty, empty) = false, want true")
|
|
}
|
|
if samePath("", t.TempDir()) {
|
|
t.Fatal("samePath(empty, dir) = true, want false")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFindProjectConfigYamlWithFinder_UsesLoadedWorktreeConfig(t *testing.T) {
|
|
restore := envSnapshot(t)
|
|
defer restore()
|
|
|
|
_, _, mainConfigPath := setupConfigWorktree(t)
|
|
if err := os.WriteFile(mainConfigPath, []byte("sync.remote: git+ssh://git@example.com/org/repo.git\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write main config.yaml: %v", err)
|
|
}
|
|
|
|
ResetForTesting()
|
|
if err := Initialize(); err != nil {
|
|
t.Fatalf("Initialize() error = %v", err)
|
|
}
|
|
|
|
got, err := findProjectConfigYamlWithFinder(func() string { return "" })
|
|
if err != nil {
|
|
t.Fatalf("findProjectConfigYamlWithFinder() error = %v", err)
|
|
}
|
|
|
|
gotResolved, err := filepath.EvalSymlinks(filepath.Clean(got))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", got, err)
|
|
}
|
|
wantResolved, err := filepath.EvalSymlinks(filepath.Clean(mainConfigPath))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", mainConfigPath, err)
|
|
}
|
|
|
|
if gotResolved != wantResolved {
|
|
t.Fatalf("findProjectConfigYamlWithFinder() = %q, want %q", gotResolved, wantResolved)
|
|
}
|
|
}
|
|
|
|
func TestFindProjectConfigYamlWithFinder_UsesSharedConfigAfterLocalOverrideMerge(t *testing.T) {
|
|
restore := envSnapshot(t)
|
|
defer restore()
|
|
|
|
_, _, mainConfigPath := setupConfigWorktree(t)
|
|
if err := os.WriteFile(mainConfigPath, []byte("json: false\nactor: shared-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write main config.yaml: %v", err)
|
|
}
|
|
|
|
localConfigPath := filepath.Join(filepath.Dir(mainConfigPath), "config.local.yaml")
|
|
if err := os.WriteFile(localConfigPath, []byte("actor: local-user\n"), 0o644); err != nil {
|
|
t.Fatalf("failed to write config.local.yaml: %v", err)
|
|
}
|
|
|
|
ResetForTesting()
|
|
if err := Initialize(); err != nil {
|
|
t.Fatalf("Initialize() error = %v", err)
|
|
}
|
|
|
|
usedConfig := ConfigFileUsed()
|
|
usedResolved, err := filepath.EvalSymlinks(filepath.Clean(usedConfig))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", usedConfig, err)
|
|
}
|
|
|
|
wantResolved, err := filepath.EvalSymlinks(filepath.Clean(mainConfigPath))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", mainConfigPath, err)
|
|
}
|
|
|
|
if usedResolved != wantResolved {
|
|
t.Fatalf("ConfigFileUsed() = %q, want %q", usedResolved, wantResolved)
|
|
}
|
|
|
|
got, err := findProjectConfigYamlWithFinder(func() string { return "" })
|
|
if err != nil {
|
|
t.Fatalf("findProjectConfigYamlWithFinder() error = %v", err)
|
|
}
|
|
|
|
gotResolved, err := filepath.EvalSymlinks(filepath.Clean(got))
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", got, err)
|
|
}
|
|
|
|
if gotResolved != wantResolved {
|
|
t.Fatalf("findProjectConfigYamlWithFinder() = %q, want %q", gotResolved, wantResolved)
|
|
}
|
|
}
|