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>
294 lines
9.3 KiB
Go
294 lines
9.3 KiB
Go
//go:build cgo && integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func setupGitRepoForIntegration(t *testing.T, dir string) {
|
|
t.Helper()
|
|
cmd := gitCommand("init", dir)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git init failed: %v\n%s", err, out)
|
|
}
|
|
for _, args := range [][]string{
|
|
{"-C", dir, "config", "user.email", "test@example.com"},
|
|
{"-C", dir, "config", "user.name", "Test User"},
|
|
} {
|
|
cmd = gitCommand(args...)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func isDoltBackendUnavailable(output string) bool {
|
|
return strings.Contains(output, "dolt backend") ||
|
|
strings.Contains(output, "server not available") ||
|
|
strings.Contains(output, "server not running") ||
|
|
strings.Contains(output, "connection refused")
|
|
}
|
|
|
|
func doltHeadCommit(t *testing.T, dir string, env []string) string {
|
|
t.Helper()
|
|
out, err := runBDExecAllowErrorWithEnv(t, dir, env, "--json", "vc", "status")
|
|
if err != nil {
|
|
t.Fatalf("bd vc status failed: %v\n%s", err, out)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
|
// Some commands can emit warnings; try from first '{'
|
|
if idx := strings.Index(out, "{"); idx >= 0 {
|
|
if err2 := json.Unmarshal([]byte(out[idx:]), &m); err2 != nil {
|
|
t.Fatalf("failed to parse vc status JSON: %v\n%s", err2, out)
|
|
}
|
|
} else {
|
|
t.Fatalf("failed to parse vc status JSON: %v\n%s", err, out)
|
|
}
|
|
}
|
|
commit, _ := m["commit"].(string)
|
|
if commit == "" {
|
|
t.Fatalf("missing commit in vc status output:\n%s", out)
|
|
}
|
|
return commit
|
|
}
|
|
|
|
func runCommandInDirCombinedOutput(dir string, name string, args ...string) (string, error) {
|
|
cmd := exec.Command(name, args...) // #nosec G204 -- test helper executes trusted binaries
|
|
cmd.Dir = dir
|
|
out, err := cmd.CombinedOutput()
|
|
return strings.TrimSpace(string(out)), err
|
|
}
|
|
|
|
func findDoltRepoDir(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
|
|
// bd init defaults to the embedded engine, whose data lives under
|
|
// .beads/embeddeddolt/<db>/; a server-mode workspace keeps it under
|
|
// .beads/dolt/ (a dolt repo there directly, or a db subdir with .dolt/).
|
|
var bases []string
|
|
for _, name := range []string{"embeddeddolt", "dolt"} {
|
|
base := filepath.Join(dir, ".beads", name)
|
|
bases = append(bases, base)
|
|
for _, c := range []string{base, filepath.Join(base, "beads")} {
|
|
if _, err := os.Stat(filepath.Join(c, ".dolt")); err == nil {
|
|
return c
|
|
}
|
|
}
|
|
}
|
|
|
|
var found string
|
|
for _, base := range bases {
|
|
_ = filepath.WalkDir(base, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil || found != "" {
|
|
return nil
|
|
}
|
|
if d.IsDir() && d.Name() == ".dolt" {
|
|
found = filepath.Dir(path)
|
|
return fs.SkipDir
|
|
}
|
|
return nil
|
|
})
|
|
if found != "" {
|
|
return found
|
|
}
|
|
}
|
|
t.Fatalf("could not find Dolt repo dir under %v", bases)
|
|
return ""
|
|
}
|
|
|
|
func doltHeadAuthor(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
|
|
doltDir := findDoltRepoDir(t, dir)
|
|
out, err := runCommandInDirCombinedOutput(doltDir, "dolt", "log", "-n", "1")
|
|
if err != nil {
|
|
t.Fatalf("dolt log failed: %v\n%s", err, out)
|
|
}
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if strings.HasPrefix(line, "Author:") {
|
|
return strings.TrimSpace(strings.TrimPrefix(line, "Author:"))
|
|
}
|
|
}
|
|
t.Fatalf("missing Author in dolt log output:\n%s", out)
|
|
return ""
|
|
}
|
|
|
|
func TestDoltAutoCommit_On_WritesAdvanceHead(t *testing.T) {
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("skipping: Dolt test container not available")
|
|
}
|
|
if runtime.GOOS == windowsOS {
|
|
t.Skip("dolt integration test not supported on windows")
|
|
}
|
|
|
|
tmpDir := createTempDirWithCleanup(t)
|
|
setupGitRepoForIntegration(t, tmpDir)
|
|
|
|
env := append(os.Environ(), "BEADS_TEST_MODE=1")
|
|
|
|
initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet")
|
|
if initErr != nil {
|
|
if isDoltBackendUnavailable(initOut) {
|
|
t.Skipf("dolt backend not available: %s", initOut)
|
|
}
|
|
t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut)
|
|
}
|
|
|
|
before := doltHeadCommit(t, tmpDir, env)
|
|
|
|
// Explicitly enable auto-commit=on (default is now "batch" in embedded mode).
|
|
out, err := runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "on", "create", "Auto-commit test", "--json")
|
|
if err != nil {
|
|
t.Fatalf("bd create failed: %v\n%s", err, out)
|
|
}
|
|
|
|
after := doltHeadCommit(t, tmpDir, env)
|
|
if after == before {
|
|
t.Fatalf("expected Dolt HEAD to change after write; before=%s after=%s", before, after)
|
|
}
|
|
|
|
// Commit author should be deterministic (not the authenticated SQL user like root@%).
|
|
expectedName := os.Getenv("GIT_AUTHOR_NAME")
|
|
if expectedName == "" {
|
|
expectedName = "beads"
|
|
}
|
|
expectedEmail := os.Getenv("GIT_AUTHOR_EMAIL")
|
|
if expectedEmail == "" {
|
|
expectedEmail = "beads@local"
|
|
}
|
|
expectedAuthor := fmt.Sprintf("%s <%s>", expectedName, expectedEmail)
|
|
if got := doltHeadAuthor(t, tmpDir); got != expectedAuthor {
|
|
t.Fatalf("expected Dolt commit author %q, got %q", expectedAuthor, got)
|
|
}
|
|
|
|
// A read-only command should not create another commit.
|
|
out, err = runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "on", "list")
|
|
if err != nil {
|
|
t.Fatalf("bd list failed: %v\n%s", err, out)
|
|
}
|
|
afterList := doltHeadCommit(t, tmpDir, env)
|
|
if afterList != after {
|
|
t.Fatalf("expected Dolt HEAD unchanged after read command; before=%s after=%s", after, afterList)
|
|
}
|
|
}
|
|
|
|
func TestDoltAutoCommit_Batch_DefersCommit(t *testing.T) {
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("skipping: Dolt test container not available")
|
|
}
|
|
if runtime.GOOS == windowsOS {
|
|
t.Skip("dolt integration test not supported on windows")
|
|
}
|
|
|
|
tmpDir := createTempDirWithCleanup(t)
|
|
setupGitRepoForIntegration(t, tmpDir)
|
|
|
|
env := append(os.Environ(), "BEADS_TEST_MODE=1")
|
|
|
|
initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet")
|
|
if initErr != nil {
|
|
if isDoltBackendUnavailable(initOut) {
|
|
t.Skipf("dolt backend not available: %s", initOut)
|
|
}
|
|
t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut)
|
|
}
|
|
|
|
before := doltHeadCommit(t, tmpDir, env)
|
|
|
|
// In batch mode (default for embedded), writes should NOT advance HEAD.
|
|
out, err := runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "batch", "create", "Batch test 1", "--json")
|
|
if err != nil {
|
|
t.Fatalf("bd create failed: %v\n%s", err, out)
|
|
}
|
|
|
|
afterCreate := doltHeadCommit(t, tmpDir, env)
|
|
if afterCreate != before {
|
|
t.Fatalf("expected Dolt HEAD unchanged in batch mode; before=%s after=%s", before, afterCreate)
|
|
}
|
|
|
|
// Create another issue — still deferred.
|
|
out, err = runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "batch", "create", "Batch test 2", "--json")
|
|
if err != nil {
|
|
t.Fatalf("bd create (2) failed: %v\n%s", err, out)
|
|
}
|
|
var created struct {
|
|
ID string `json:"id"`
|
|
}
|
|
if err := json.Unmarshal([]byte(out[strings.Index(out, "{"):]), &created); err != nil || created.ID == "" {
|
|
t.Fatalf("parse create JSON id: %v\n%s", err, out)
|
|
}
|
|
secondID := created.ID
|
|
|
|
afterCreate2 := doltHeadCommit(t, tmpDir, env)
|
|
if afterCreate2 != before {
|
|
t.Fatalf("expected Dolt HEAD still unchanged; before=%s after=%s", before, afterCreate2)
|
|
}
|
|
|
|
// Delete rides the issueops facade, not transactHonoringAutoCommit — it
|
|
// must defer too (the bd-4wamg review caught it passing a bare rootCtx).
|
|
out, err = runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "batch", "delete", secondID, "--force")
|
|
if err != nil {
|
|
t.Fatalf("bd delete failed: %v\n%s", err, out)
|
|
}
|
|
|
|
afterDelete := doltHeadCommit(t, tmpDir, env)
|
|
if afterDelete != before {
|
|
t.Fatalf("expected Dolt HEAD unchanged after batch-mode delete; before=%s after=%s", before, afterDelete)
|
|
}
|
|
|
|
// An explicit "bd dolt commit" should commit all accumulated changes.
|
|
out, err = runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "batch", "dolt", "commit")
|
|
if err != nil {
|
|
t.Fatalf("bd dolt commit failed: %v\n%s", err, out)
|
|
}
|
|
|
|
afterCommit := doltHeadCommit(t, tmpDir, env)
|
|
if afterCommit == before {
|
|
t.Fatalf("expected Dolt HEAD to advance after explicit commit; before=%s after=%s", before, afterCommit)
|
|
}
|
|
}
|
|
|
|
func TestDoltAutoCommit_Off_DoesNotAdvanceHead(t *testing.T) {
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("skipping: Dolt test container not available")
|
|
}
|
|
if runtime.GOOS == windowsOS {
|
|
t.Skip("dolt integration test not supported on windows")
|
|
}
|
|
|
|
tmpDir := createTempDirWithCleanup(t)
|
|
setupGitRepoForIntegration(t, tmpDir)
|
|
|
|
env := append(os.Environ(), "BEADS_TEST_MODE=1")
|
|
|
|
initOut, initErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet")
|
|
if initErr != nil {
|
|
if isDoltBackendUnavailable(initOut) {
|
|
t.Skipf("dolt backend not available: %s", initOut)
|
|
}
|
|
t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut)
|
|
}
|
|
|
|
before := doltHeadCommit(t, tmpDir, env)
|
|
|
|
// Disable auto-commit via persistent flag (must come before subcommand).
|
|
out, err := runBDExecAllowErrorWithEnv(t, tmpDir, env, "--dolt-auto-commit", "off", "create", "Auto-commit off", "--json")
|
|
if err != nil {
|
|
t.Fatalf("bd create failed: %v\n%s", err, out)
|
|
}
|
|
|
|
after := doltHeadCommit(t, tmpDir, env)
|
|
if after != before {
|
|
t.Fatalf("expected Dolt HEAD unchanged with auto-commit off; before=%s after=%s", before, after)
|
|
}
|
|
}
|