package git import ( "os" "os/exec" "path/filepath" "testing" "github.com/stretchr/testify/require" ) // gitAvailable 跳过测试如果 git 不在 PATH。 func gitAvailable(t *testing.T) { t.Helper() if _, err := exec.LookPath("git"); err != nil { t.Skip("git not found in PATH") } } // makeBareRepo 在 tmpdir 下 init --bare 一个仓库并塞一个初始 commit。 // 返回 bare repo 绝对路径(可作为 remote URL 的 file:// 路径)。 func makeBareRepo(t *testing.T) string { t.Helper() gitAvailable(t) dir := t.TempDir() bare := filepath.Join(dir, "remote.git") require.NoError(t, exec.Command("git", "init", "--bare", "-b", "main", bare).Run()) // 用一个 working repo 推一次 commit 进 bare,避免空 remote 导致 clone 报错 work := filepath.Join(dir, "seed") require.NoError(t, os.MkdirAll(work, 0o755)) mustGit(t, work, "init", "-b", "main") mustGit(t, work, "config", "user.email", "test@example.com") mustGit(t, work, "config", "user.name", "Test") require.NoError(t, os.WriteFile(filepath.Join(work, "README.md"), []byte("hello\n"), 0o644)) mustGit(t, work, "add", ".") mustGit(t, work, "commit", "-m", "initial") mustGit(t, work, "remote", "add", "origin", bare) mustGit(t, work, "push", "origin", "main") return bare } func mustGit(t *testing.T, dir string, args ...string) { t.Helper() c := exec.Command("git", args...) c.Dir = dir out, err := c.CombinedOutput() require.NoError(t, err, "git %v: %s", args, string(out)) } // newTestRunner 返回挂在 t.TempDir() 的 Runner。 func newTestRunner(t *testing.T) *DefaultRunner { t.Helper() return NewDefaultRunner(Config{Binary: "git", TmpDir: t.TempDir()}) }