aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/repo_run.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-20 21:19:47 +0800
committerGravatar Runxi Yu2026-02-20 21:34:42 +0800
commit05e07f6c6aca1662c33359f41c66e6f9b6eb935a (patch)
tree6928a76783b2230a010c36d53b8da48ff24d611f /internal/testgit/repo_run.go
parentoid: Add ObjectID (diff)
signatureNo signature
testgit: Add test harnesses
Diffstat (limited to 'internal/testgit/repo_run.go')
-rw-r--r--internal/testgit/repo_run.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/testgit/repo_run.go b/internal/testgit/repo_run.go
new file mode 100644
index 00000000..66222569
--- /dev/null
+++ b/internal/testgit/repo_run.go
@@ -0,0 +1,49 @@
+package testgit
+
+import (
+ "bytes"
+ "os/exec"
+ "strings"
+ "testing"
+)
+
+// Run executes git and returns trimmed textual output.
+func (repo *TestRepo) Run(tb testing.TB, args ...string) string {
+ tb.Helper()
+ out := repo.runBytes(tb, nil, repo.dir, args...)
+ return strings.TrimSpace(string(out))
+}
+
+// RunBytes executes git and returns raw output bytes.
+func (repo *TestRepo) RunBytes(tb testing.TB, args ...string) []byte {
+ tb.Helper()
+ return repo.runBytes(tb, nil, repo.dir, args...)
+}
+
+// RunInput executes git with stdin and returns trimmed textual output.
+func (repo *TestRepo) RunInput(tb testing.TB, stdin []byte, args ...string) string {
+ tb.Helper()
+ out := repo.runBytes(tb, stdin, repo.dir, args...)
+ return strings.TrimSpace(string(out))
+}
+
+// RunInputBytes executes git with stdin and returns raw output bytes.
+func (repo *TestRepo) RunInputBytes(tb testing.TB, stdin []byte, args ...string) []byte {
+ tb.Helper()
+ return repo.runBytes(tb, stdin, repo.dir, args...)
+}
+
+func (repo *TestRepo) runBytes(tb testing.TB, stdin []byte, dir string, args ...string) []byte {
+ tb.Helper()
+ cmd := exec.Command("git", args...)
+ cmd.Dir = dir
+ cmd.Env = repo.env
+ if stdin != nil {
+ cmd.Stdin = bytes.NewReader(stdin)
+ }
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ tb.Fatalf("git %v failed: %v\n%s", args, err, out)
+ }
+ return out
+}