仰望星辰工作室

gitcat

gitcat/ internal/gitx/helper_test.go 758 B · 28 行 原始文件
1package gitx
2
3import (
4 "context"
5 "os"
6 "os/exec"
7 "strings"
8 "testing"
9 "time"
10)
11
12// runStin 跑一条带 stdin 的 git 命令并返回去空格后的输出。
13func runStdin(t *testing.T, dir, stdin string, args ...string) string {
14 t.Helper()
15 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
16 defer cancel()
17 cmd := exec.CommandContext(ctx, "git", args...)
18 cmd.Dir = dir
19 cmd.Stdin = strings.NewReader(stdin)
20 cmd.Env = append(os.Environ(),
21 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@gitcat.local",
22 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@gitcat.local")
23 out, err := cmd.CombinedOutput()
24 if err != nil {
25 t.Fatalf("git %s 失败: %v\n%s", strings.Join(args, " "), err, out)
26 }
27 return strings.TrimSpace(string(out))
28}