Skip to content

Commit

Permalink
feat: add icmd
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Sep 8, 2023
1 parent 51eec32 commit b72fbd6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
40 changes: 20 additions & 20 deletions cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"strings"
"testing"

Expand All @@ -13,55 +12,56 @@ import (
"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gotest.tools/v3/icmd"
)

func TestCLIContractInstall(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand("contract install --wasm " + helloWorldContractPath)
assert.NoError(t, err)
output := assertCmd(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, err := runCLICommand("contract deploy --salt 0 --wasm " + helloWorldContractPath)
println(string(output))
assert.NoError(t, err)
output := assertCmd(t, "contract deploy --salt 0 --wasm "+helloWorldContractPath)
require.Contains(t, output, "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM")
}

func TestCLIContractInvokeWithWasm(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand(fmt.Sprintf("contract invoke --salt=0 --wasm %s -- hello --world=world", helloWorldContractPath))
assert.NoError(t, err)
output := assertCmd(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, err := runCLICommand("contract deploy --id 1 --wasm " + helloWorldContractPath)
assert.NoError(t, err)
output := assertCmd(t, "contract deploy --id 1 --wasm "+helloWorldContractPath)
contractID := strings.TrimSpace(output)
output, err = runCLICommand(fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
assert.NoError(t, err)
output = assertCmd(t, fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
require.Contains(t, output, `["Hello","world"]`)
}

func runCLICommand(cmd string) (string, error) {
baseCmdArgs := []string{"run", "-q", "--", "--vv"}
args := strings.Split(cmd, " ")
args = append(baseCmdArgs, args...)
c := exec.Command("cargo", args...)
func assertCmd(t *testing.T, cmd string) string {
res := runCLICommand(cmd)

stderr := res.Stderr()
println(stderr)
require.True(t, res.ExitCode == 0, stderr)
return res.Stdout()
}

func runCLICommand(cmd string) *icmd.Result {
args := []string{"run", "-q", "--", "--vv"}
args = append(args, strings.Split(cmd, " ")...)
c := icmd.Command("cargo", args...)
c.Env = append(os.Environ(),
fmt.Sprintf("SOROBAN_RPC_URL=http://localhost:%d/", sorobanRPCPort),
fmt.Sprintf("SOROBAN_NETWORK_PASSPHRASE=%s", StandaloneNetworkPassphrase),
)
bin, err := c.CombinedOutput()
return string(bin), err
return icmd.RunCmd(c)
}

func NewCLITest(t *testing.T) *Test {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ require (
github.com/mattn/go-sqlite3 v1.14.17
github.com/pelletier/go-toml v1.9.5
github.com/prometheus/client_golang v1.16.0
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00
github.com/rubenv/sql-migrate v1.5.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stellar/go v0.0.0-20230905170723-6e18a2f2f583
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.12.0
gotest.tools/v3 v3.5.0
)

require (
Expand All @@ -38,6 +40,7 @@ require (
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -57,7 +60,6 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00 // indirect
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 // indirect
github.com/segmentio/go-loggly v0.5.1-0.20171222203950-eb91657e62b2 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 h1:oERTZ1buOUYlpmKaqlO5fYmz8cZ1rYu5DieJzF4ZVmU=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -299,3 +300,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=

0 comments on commit b72fbd6

Please sign in to comment.