aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/repo_run.go
blob: aafcc92304e9cc9c741b55beff66b22925b1646a (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testgit

import (
	"bytes"
	"os/exec"
	"strings"
	"testing"
)

// Run executes git and returns trimmed textual output.
func (testRepo *TestRepo) Run(tb testing.TB, args ...string) string {
	tb.Helper()
	out := testRepo.runBytes(tb, nil, testRepo.dir, args...)
	return strings.TrimSpace(string(out))
}

// RunBytes executes git and returns raw output bytes.
func (testRepo *TestRepo) RunBytes(tb testing.TB, args ...string) []byte {
	tb.Helper()
	return testRepo.runBytes(tb, nil, testRepo.dir, args...)
}

// RunInput executes git with stdin and returns trimmed textual output.
func (testRepo *TestRepo) RunInput(tb testing.TB, stdin []byte, args ...string) string {
	tb.Helper()
	out := testRepo.runBytes(tb, stdin, testRepo.dir, args...)
	return strings.TrimSpace(string(out))
}

// RunInputBytes executes git with stdin and returns raw output bytes.
func (testRepo *TestRepo) RunInputBytes(tb testing.TB, stdin []byte, args ...string) []byte {
	tb.Helper()
	return testRepo.runBytes(tb, stdin, testRepo.dir, args...)
}

func (testRepo *TestRepo) runBytes(tb testing.TB, stdin []byte, dir string, args ...string) []byte {
	tb.Helper()
	cmd := exec.Command("git", args...)
	cmd.Dir = dir
	cmd.Env = testRepo.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
}