From 8ab77e2ab5cea35f144103ef1c343d9183be450a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 12 Apr 2018 22:51:14 -0400 Subject: [PATCH] started gaia go-bash cli testing --- cmd/gaia/cmd/app_test.go | 42 ++++++++++++++++++++++++++++++++++ tests/gobash.go | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 cmd/gaia/cmd/app_test.go create mode 100644 tests/gobash.go diff --git a/cmd/gaia/cmd/app_test.go b/cmd/gaia/cmd/app_test.go new file mode 100644 index 000000000000..98cf2a0d2d83 --- /dev/null +++ b/cmd/gaia/cmd/app_test.go @@ -0,0 +1,42 @@ +package common + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/tests" +) + +func TestGaiaCLI(t *testing.T) { + + _, out := tests.ExecuteT(t, "gaiad init") + var initRes map[string]interface{} + outCut := "{" + strings.SplitN(out, "{", 2)[1] + err := json.Unmarshal([]byte(outCut), &initRes) + require.NoError(t, err, "out %v outCut %v err %v", out, outCut, err) + masterKey := (initRes["secret"]).(string) + _ = masterKey + + //wc1, _ := tests.GoExecuteT(t, "gaiacli keys add foo --recover") + //time.Sleep(time.Second) + //_, err = wc1.Write([]byte("1234567890\n")) + //time.Sleep(time.Second) + //_, err = wc1.Write([]byte(masterKey + "\n")) + //time.Sleep(time.Second) + //out = <-outChan + //wc1.Close() + //fmt.Println(out) + + //_, out = tests.ExecuteT(t, "gaiacli keys show foo") + //fooAddr := strings.TrimLeft(out, "foo\t") + + //wc2, _ := tests.GoExecuteT(t, "gaiad start") + //defer wc2.Close() + //time.Sleep(time.Second) + + //_, out = tests.ExecuteT(t, fmt.Sprintf("gaiacli account %v", fooAddr)) + //fmt.Println(fooAddr) +} diff --git a/tests/gobash.go b/tests/gobash.go new file mode 100644 index 000000000000..edef7ccd504c --- /dev/null +++ b/tests/gobash.go @@ -0,0 +1,49 @@ +package tests + +import ( + "io" + "os/exec" + "strings" + "testing" + + "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 +func ExecuteT(t *testing.T, command string) (pipe io.WriteCloser, out string) { + cmd := getCmd(t, command) + pipe, err := cmd.StdinPipe() + require.NoError(t, err) + bz, err := cmd.CombinedOutput() + require.NoError(t, err) + out = strings.Trim(string(bz), "\n") //trim any new lines + return pipe, out +} + +// Asynchronously execute the command, return standard output and error +func GoExecuteT(t *testing.T, command string) (pipe io.WriteCloser, outChan chan string) { + cmd := getCmd(t, command) + pipe, err := cmd.StdinPipe() + require.NoError(t, err) + go func() { + bz, err := cmd.CombinedOutput() + require.NoError(t, err) + outChan <- strings.Trim(string(bz), "\n") //trim any new lines + }() + return pipe, outChan +}