From b0bf51d28b2d3f93e2ce0e0da3995d56ccbf6680 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Fri, 8 Sep 2023 15:54:28 -0400 Subject: [PATCH] feat: add icmd --- cmd/soroban-rpc/internal/test/cli_test.go | 40 +++++++++++------------ go.mod | 4 ++- go.sum | 3 ++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/cmd/soroban-rpc/internal/test/cli_test.go b/cmd/soroban-rpc/internal/test/cli_test.go index 6bd9a5a850..5f16dc5390 100644 --- a/cmd/soroban-rpc/internal/test/cli_test.go +++ b/cmd/soroban-rpc/internal/test/cli_test.go @@ -4,7 +4,6 @@ import ( "crypto/sha256" "fmt" "os" - "os/exec" "strings" "testing" @@ -13,14 +12,13 @@ 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()) @@ -28,40 +26,42 @@ func TestCLIContractInstall(t *testing.T) { 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 { diff --git a/go.mod b/go.mod index 73a1e6fb74..77b21daea7 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ 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 @@ -18,6 +19,7 @@ require ( 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 ( @@ -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 @@ -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 diff --git a/go.sum b/go.sum index cb4a932ffd..15fab6fb87 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=