aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/repo_run_extra_files.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-07 23:31:13 +0800
committerGravatar Runxi Yu2026-03-07 23:31:13 +0800
commitb8bd9e766c1940ea2568e94577a7c56f8ff0d2f3 (patch)
tree8541f0fadce0f2d0e91f4c86beaf03881b0809c6 /internal/testgit/repo_run_extra_files.go
parentREADME: Outdent the not planned section (diff)
signatureNo signature
internal/testgit: Add more execution helpers
Diffstat (limited to 'internal/testgit/repo_run_extra_files.go')
-rw-r--r--internal/testgit/repo_run_extra_files.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/testgit/repo_run_extra_files.go b/internal/testgit/repo_run_extra_files.go
new file mode 100644
index 00000000..4629c872
--- /dev/null
+++ b/internal/testgit/repo_run_extra_files.go
@@ -0,0 +1,55 @@
+package testgit
+
+import (
+ "bytes"
+ "context"
+ "os"
+ "os/exec"
+ "testing"
+)
+
+// RunWithExtraFilesE executes git with inherited extra files and returns split
+// stdout/stderr plus any command error.
+func (testRepo *TestRepo) RunWithExtraFilesE(
+ tb testing.TB,
+ extraFiles []*os.File,
+ args ...string,
+) ([]byte, []byte, error) {
+ tb.Helper()
+
+ return testRepo.RunWithExtraFilesEnvContextE(
+ tb,
+ context.Background(),
+ nil,
+ extraFiles,
+ args...,
+ )
+}
+
+// RunWithExtraFilesEnvContextE executes git with inherited extra files, extra
+// environment, and context cancellation, returning split stdout/stderr plus any
+// command error.
+func (testRepo *TestRepo) RunWithExtraFilesEnvContextE(
+ tb testing.TB,
+ ctx context.Context,
+ extraEnv []string,
+ extraFiles []*os.File,
+ args ...string,
+) ([]byte, []byte, error) {
+ tb.Helper()
+
+ cmd := exec.CommandContext(ctx, "git", args...) //#nosec G204
+ cmd.Dir = testRepo.dir
+ cmd.Env = testRepo.env
+ cmd.Env = append(cmd.Env, extraEnv...)
+ cmd.ExtraFiles = append([]*os.File(nil), extraFiles...)
+
+ var stdout, stderr bytes.Buffer
+
+ cmd.Stdout = &stdout
+ cmd.Stderr = &stderr
+
+ err := cmd.Run()
+
+ return stdout.Bytes(), stderr.Bytes(), err
+}