-
Notifications
You must be signed in to change notification settings - Fork 12
/
command.go
59 lines (46 loc) · 1.26 KB
/
command.go
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
50
51
52
53
54
55
56
57
58
59
package testutil
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func RunSucceedCommand(dir, command string, args ...string) {
_, _ = RunCommandWithOptions(dir, command, args, RunCommandOptions{ShouldSucceed: true})
}
func SucceedCommandOutputString(dir, command string, args ...string) string {
res, _ := RunCommandWithOptions(dir, command, args, RunCommandOptions{ShouldSucceed: true})
return string(res)
}
type RunCommandOptions struct {
ExtraEnv []string
ToStdin string
ShouldSucceed bool
}
func RunCommandWithOptions(dir, command string, args []string, options RunCommandOptions) ([]byte, error) {
if isTrdlTestBinaryPath(command) {
args = TrdlBinArgs(args...)
}
cmd := exec.Command(command, args...)
if dir != "" {
cmd.Dir = dir
}
if len(options.ExtraEnv) != 0 {
cmd.Env = append(os.Environ(), options.ExtraEnv...)
}
if options.ToStdin != "" {
var b bytes.Buffer
b.Write([]byte(options.ToStdin))
cmd.Stdin = &b
}
res, err := cmd.CombinedOutput()
_, _ = GinkgoWriter.Write(res)
if options.ShouldSucceed {
errorDesc := fmt.Sprintf("%[2]s %[3]s (dir: %[1]s)", dir, command, strings.Join(args, " "))
Ω(err).ShouldNot(HaveOccurred(), errorDesc)
}
return res, err
}