* feat: add -C <path> flag to change directory before running commands Adds a persistent -C/--directory flag (matching git -C) that calls os.Chdir before .beads/ discovery or database initialisation. Allows running bd commands against a different project without changing directory first. Example: bd -C ~/projects/myapp ready Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Avoid cwd mutation for directory flag Resolve -C by binding command selection to the target project's .beads directory instead of calling os.Chdir. Add focused coverage for invalid targets and no process cwd mutation.\n\nCo-authored-by: Robin Carswell <2803445+rjc123@users.noreply.github.com> * test: resolve symlinks in tempdir for macOS compatibility t.TempDir() returns paths under /var/folders on macOS, but resolveChangeDirBeadsDir canonicalizes via EvalSymlinks to /private/var/folders. Resolve projectDir up front so the expected and actual paths match on every OS. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: matt wilkie <maphew@gmail.com>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveChangeDirBeadsDirDoesNotChangeCWD(t *testing.T) {
|
|
origWD, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("Getwd: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = os.Chdir(origWD)
|
|
})
|
|
|
|
startDir := t.TempDir()
|
|
t.Chdir(startDir)
|
|
|
|
projectDir := t.TempDir()
|
|
if resolved, err := filepath.EvalSymlinks(projectDir); err == nil {
|
|
projectDir = resolved
|
|
}
|
|
beadsDir := filepath.Join(projectDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o644); err != nil {
|
|
t.Fatalf("WriteFile: %v", err)
|
|
}
|
|
|
|
got, err := resolveChangeDirBeadsDir(projectDir)
|
|
if err != nil {
|
|
t.Fatalf("resolveChangeDirBeadsDir: %v", err)
|
|
}
|
|
if got != beadsDir {
|
|
t.Fatalf("resolveChangeDirBeadsDir() = %q, want %q", got, beadsDir)
|
|
}
|
|
|
|
afterWD, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("Getwd after resolve: %v", err)
|
|
}
|
|
if afterWD != startDir {
|
|
t.Fatalf("working directory changed to %q, want %q", afterWD, startDir)
|
|
}
|
|
}
|
|
|
|
func TestResolveChangeDirBeadsDirRejectsFile(t *testing.T) {
|
|
filePath := filepath.Join(t.TempDir(), "not-a-directory")
|
|
if err := os.WriteFile(filePath, []byte("x"), 0o644); err != nil {
|
|
t.Fatalf("WriteFile: %v", err)
|
|
}
|
|
|
|
if _, err := resolveChangeDirBeadsDir(filePath); err == nil {
|
|
t.Fatal("expected non-directory -C target to fail")
|
|
}
|
|
}
|
|
|
|
func TestResolveChangeDirBeadsDirRejectsDirectoryWithoutProject(t *testing.T) {
|
|
if _, err := resolveChangeDirBeadsDir(t.TempDir()); err == nil {
|
|
t.Fatal("expected -C target without a beads project to fail")
|
|
}
|
|
}
|