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>
223 lines
6.5 KiB
Go
223 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsBareGitRepo(t *testing.T) {
|
|
t.Run("returns true in bare repository", func(t *testing.T) {
|
|
repoDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", repoDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if !isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = false, want true")
|
|
}
|
|
})
|
|
|
|
t.Run("returns false in non-bare repository", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = true, want false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns false outside git repository", func(t *testing.T) {
|
|
nonRepoDir := t.TempDir()
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(nonRepoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = true, want false")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitOriginGetURL(t *testing.T) {
|
|
t.Run("returns origin URL", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
url, err := gitOriginGetURL()
|
|
if err != nil {
|
|
t.Fatalf("gitOriginGetURL failed: %v", err)
|
|
}
|
|
if url != bareDir {
|
|
t.Errorf("gitOriginGetURL = %q, want %q", url, bareDir)
|
|
}
|
|
})
|
|
|
|
t.Run("returns error for nonexistent remote", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
_, err = gitOriginGetURL()
|
|
if err == nil {
|
|
t.Fatal("expected error for nonexistent remote")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitOriginHasDoltDataRef(t *testing.T) {
|
|
t.Run("returns false for nonexistent ref", func(t *testing.T) {
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if gitOriginHasDoltDataRef() {
|
|
t.Fatal("expected false for nonexistent ref")
|
|
}
|
|
})
|
|
|
|
t.Run("returns true for existing ref", func(t *testing.T) {
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
|
|
// Create a repo, commit, push to create refs/dolt/data
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init", "-b", "main")
|
|
runGitForSyncTest(t, repoDir, "config", "user.email", "test@test.com")
|
|
runGitForSyncTest(t, repoDir, "config", "user.name", "Test User")
|
|
runGitForSyncTest(t, repoDir, "commit", "--allow-empty", "-m", "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
runGitForSyncTest(t, repoDir, "push", "origin", "HEAD:refs/dolt/data")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if !gitOriginHasDoltDataRef() {
|
|
t.Fatal("expected true for existing ref")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitURLToDoltRemote(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want string
|
|
}{
|
|
{"https://github.com/org/repo.git", "git+https://github.com/org/repo.git"},
|
|
{"http://github.com/org/repo.git", "git+http://github.com/org/repo.git"},
|
|
{"ssh://git@github.com/org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
{"git@github.com:org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
{"git+https://github.com/org/repo.git", "git+https://github.com/org/repo.git"},
|
|
{"git+ssh://git@github.com/org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
{"C:/Users/alice/repos/beads.git", "git+C:/Users/alice/repos/beads.git"},
|
|
{`D:\repos\beads.git`, `git+D:\repos\beads.git`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
got := gitURLToDoltRemote(tt.input)
|
|
if got != tt.want {
|
|
t.Errorf("gitURLToDoltRemote(%q) = %q, want %q", tt.input, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func runGitForSyncTest(t *testing.T, dir string, args ...string) {
|
|
t.Helper()
|
|
|
|
cmd := gitCommand(args...)
|
|
if dir != "" {
|
|
cmd.Dir = dir
|
|
}
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, string(output))
|
|
}
|
|
}
|
|
|
|
// TestGitRemoteHasDoltDataRefStatusSurfacesGitStderr proves the probe failure a
|
|
// user actually sees carries git's own diagnosis. exec.ExitError.Error() is
|
|
// only "exit status 128"; git's complaint sits unread in ExitError.Stderr, and
|
|
// bootstrap's blocked message is built from this error (#5743).
|
|
func TestGitRemoteHasDoltDataRefStatusSurfacesGitStderr(t *testing.T) {
|
|
missing := filepath.Join(t.TempDir(), "not-a-repo")
|
|
|
|
hasData, err := gitRemoteHasDoltDataRefStatus(missing)
|
|
|
|
if err == nil {
|
|
t.Fatalf("probe of %q returned hasData=%v and no error; want a failure", missing, hasData)
|
|
}
|
|
msg := err.Error()
|
|
if !strings.Contains(msg, "ls-remote") {
|
|
t.Errorf("error %q does not name the command that failed", msg)
|
|
}
|
|
// Whatever git's exact wording, the message must be more than the bare
|
|
// exit status — that is the whole point of reading ExitError.Stderr.
|
|
if msg == "git ls-remote: exit status 128" {
|
|
t.Errorf("error %q carries no stderr detail; the ExitError.Stderr thread-through regressed", msg)
|
|
}
|
|
if !strings.Contains(msg, "repository") && !strings.Contains(msg, "fatal") {
|
|
t.Errorf("error %q does not look like it carries git's stderr", msg)
|
|
}
|
|
}
|