Skip to content

Commit

Permalink
started gaia go-bash cli testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Apr 18, 2018
1 parent 996cafe commit 8ab77e2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cmd/gaia/cmd/app_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions tests/gobash.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8ab77e2

Please sign in to comment.