cosmos-sdk/tests/gobash.go

52 lines
1.2 KiB
Go
Raw Normal View History

2018-04-12 19:51:14 -07:00
package tests
import (
"io"
"os/exec"
"strings"
"testing"
2018-04-13 16:45:26 -07:00
"time"
2018-04-12 19:51:14 -07:00
"github.com/stretchr/testify/require"
)
func getCmd(t *testing.T, command string) *exec.Cmd {
//split command into command and args
split := strings.Split(command, " ")
require.True(t, len(split) > 0, "no command provided")
var cmd *exec.Cmd
if len(split) == 1 {
cmd = exec.Command(split[0])
} else {
cmd = exec.Command(split[0], split[1:]...)
}
return cmd
}
// Execute the command, return standard output and error, try a few times if requested
2018-04-25 13:38:13 -07:00
func ExecuteT(t *testing.T, command string) (out string) {
2018-04-12 19:51:14 -07:00
cmd := getCmd(t, command)
bz, err := cmd.CombinedOutput()
2018-04-25 13:38:13 -07:00
if err != nil {
panic(err)
}
2018-04-13 13:08:06 -07:00
require.NoError(t, err, string(bz))
2018-04-12 19:51:14 -07:00
out = strings.Trim(string(bz), "\n") //trim any new lines
2018-04-13 16:45:26 -07:00
time.Sleep(time.Second)
2018-04-13 13:08:06 -07:00
return out
2018-04-12 19:51:14 -07:00
}
// Asynchronously execute the command, return standard output and error
2018-04-13 16:45:26 -07:00
func GoExecuteT(t *testing.T, command string) (cmd *exec.Cmd, pipeIn io.WriteCloser, pipeOut io.ReadCloser) {
cmd = getCmd(t, command)
2018-04-13 13:08:06 -07:00
pipeIn, err := cmd.StdinPipe()
2018-04-12 19:51:14 -07:00
require.NoError(t, err)
2018-04-13 13:08:06 -07:00
pipeOut, err = cmd.StdoutPipe()
require.NoError(t, err)
2018-04-25 13:38:13 -07:00
cmd.Start()
2018-04-13 16:45:26 -07:00
time.Sleep(time.Second)
return cmd, pipeIn, pipeOut
2018-04-12 19:51:14 -07:00
}