forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobash.go
113 lines (93 loc) · 2.67 KB
/
gobash.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package tests
import (
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
)
// ExecuteT executes the command, pipes any input to STDIN and return STDOUT,
// logging STDOUT/STDERR to t.
// nolint: errcheck
func ExecuteT(t *testing.T, cmd, input string) (stdout, stderr string) {
t.Log("Running", cmn.Cyan(cmd))
// split cmd to name and args
split := strings.Split(cmd, " ")
require.True(t, len(split) > 0, "no command provided")
name, args := split[0], []string(nil)
if len(split) > 1 {
args = split[1:]
}
proc, err := StartProcess("", name, args)
require.NoError(t, err)
// if input is provided, pass it to STDIN and close the pipe
if input != "" {
_, err = io.WriteString(proc.StdinPipe, input)
require.NoError(t, err)
proc.StdinPipe.Close()
}
outbz, errbz, err := proc.ReadAll()
if err != nil {
fmt.Println("Err on proc.ReadAll()", err, args)
}
proc.Wait()
if len(outbz) > 0 {
t.Log("Stdout:", cmn.Green(string(outbz)))
}
if len(errbz) > 0 {
t.Log("Stderr:", cmn.Red(string(errbz)))
}
stdout = strings.Trim(string(outbz), "\n")
stderr = strings.Trim(string(errbz), "\n")
return
}
// Execute the command, launch goroutines to log stdout/err to t.
// Caller should wait for .Wait() or .Stop() to terminate.
func GoExecuteT(t *testing.T, cmd string) (proc *Process) {
t.Log("Running", cmn.Cyan(cmd))
// Split cmd to name and args.
split := strings.Split(cmd, " ")
require.True(t, len(split) > 0, "no command provided")
name, args := split[0], []string(nil)
if len(split) > 1 {
args = split[1:]
}
// Start process.
proc, err := StartProcess("", name, args)
require.NoError(t, err)
return proc
}
// Same as GoExecuteT but spawns a go routine to ReadAll off stdout.
func GoExecuteTWithStdout(t *testing.T, cmd string) (proc *Process) {
t.Log("Running", cmn.Cyan(cmd))
// Split cmd to name and args.
split := strings.Split(cmd, " ")
require.True(t, len(split) > 0, "no command provided")
name, args := split[0], []string(nil)
if len(split) > 1 {
args = split[1:]
}
// Start process.
proc, err := CreateProcess("", name, args)
require.NoError(t, err)
// Without this, the test halts ?!
// (theory: because stdout and/or err aren't connected to anything the process halts)
go func() {
_, err := ioutil.ReadAll(proc.StdoutPipe)
if err != nil {
fmt.Println("-------------ERR-----------------------", err)
return
}
_, err = ioutil.ReadAll(proc.StderrPipe)
if err != nil {
fmt.Println("-------------ERR-----------------------", err)
return
}
}()
err = proc.Cmd.Start()
require.NoError(t, err)
proc.Pid = proc.Cmd.Process.Pid
return proc
}