Skip to content

Commit

Permalink
Go back to using exec to get a combined output and rename assertCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Sep 11, 2023
1 parent d4141f2 commit 7ee7980
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"strings"
"testing"

Expand All @@ -14,54 +15,54 @@ import (
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/require"
"gotest.tools/v3/icmd"
)

func TestCLIContractInstall(t *testing.T) {
NewCLITest(t)
output := assertCmd(t, "contract install --wasm "+helloWorldContractPath)
output := runSuccessfulCLICmd(t, "contract install --wasm "+helloWorldContractPath)
wasm := getHelloWorldContract(t)
contractHash := xdr.Hash(sha256.Sum256(wasm))
require.Contains(t, output, contractHash.HexString())
}

func TestCLIContractDeploy(t *testing.T) {
NewCLITest(t)
output := assertCmd(t, "contract deploy --salt 0 --wasm "+helloWorldContractPath)
output := runSuccessfulCLICmd(t, "contract deploy --salt 0 --wasm "+helloWorldContractPath)
require.Contains(t, output, "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM")
}

func TestCLIContractInvokeWithWasm(t *testing.T) {
NewCLITest(t)
output := assertCmd(t, fmt.Sprintf("contract invoke --salt=0 --wasm %s -- hello --world=world", helloWorldContractPath))
output := runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --salt=0 --wasm %s -- hello --world=world", helloWorldContractPath))
require.Contains(t, output, `["Hello","world"]`)
}

func TestCLIContractDeployAndInvoke(t *testing.T) {
NewCLITest(t)
output := assertCmd(t, "contract deploy --id 1 --wasm "+helloWorldContractPath)
output := runSuccessfulCLICmd(t, "contract deploy --id 1 --wasm "+helloWorldContractPath)
contractID := strings.TrimSpace(output)
output = assertCmd(t, fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
output = runSuccessfulCLICmd(t, fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
require.Contains(t, output, `["Hello","world"]`)
}

func assertCmd(t *testing.T, cmd string) string {
res := runCLICommand(t, cmd)
require.NoError(t, res.Error, res.Cmd.Stderr)
return res.Stdout()
func runSuccessfulCLICmd(t *testing.T, cmd string) string {
output, err := runCLICmd(t, cmd)
require.NoError(t, err, output)
return output
}

func runCLICommand(t *testing.T, cmd string) *icmd.Result {
func runCLICmd(t *testing.T, cmd string) (string, error) {
args := []string{"run", "-q", "--", "--vv"}
parsedArgs, err := shlex.Split(cmd)
require.NoError(t, err)
args = append(args, parsedArgs...)
c := icmd.Command("cargo", args...)
c := exec.Command("cargo", args...)
c.Env = append(os.Environ(),
fmt.Sprintf("SOROBAN_RPC_URL=http://localhost:%d/", sorobanRPCPort),
fmt.Sprintf("SOROBAN_NETWORK_PASSPHRASE=%s", StandaloneNetworkPassphrase),
)
return icmd.RunCmd(c)
output, err := c.CombinedOutput()
return string(output), err
}

func NewCLITest(t *testing.T) *Test {
Expand Down

0 comments on commit 7ee7980

Please sign in to comment.