Julian Strobl 487dc3b93c
[test] Check TxResponse on every ExecTestCLICmd() (#235)
Just a small improvement to not forget to check the error code of the
transaction and not only from the command itself.

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-12-15 11:32:29 +01:00

91 lines
2.3 KiB
Go

package cli
import (
"context"
"encoding/json"
"errors"
"regexp"
"github.com/planetmint/planetmint-go/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
// ExecTestCLICmd builds the client context, mocks the output and executes the command.
func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []string) (testutil.BufferWriter, error) {
cmd.SetArgs(extraArgs)
_, out := testutil.ApplyMockIO(cmd)
clientCtx = clientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
if err := cmd.ExecuteContext(ctx); err != nil {
return out, err
}
txResponse, err := GetTxResponseFromOut(out)
if err != nil {
return out, err
}
if txResponse.Code != 0 {
err = errors.New(txResponse.RawLog)
return out, err
}
return out, nil
}
// GetTxResponseFromOut converts strings to numbers and unmarshalles out into TxResponse struct
func GetTxResponseFromOut(out testutil.BufferWriter) (sdk.TxResponse, error) {
var txResponse sdk.TxResponse
m := regexp.MustCompile(`"([0-9]+?)"`)
str := m.ReplaceAllString(out.String(), "${1}")
// We might have YAML here, so we need to convert to JSON first, because TxResponse struct lacks `yaml:"height,omitempty"`, etc.
// Since JSON is a subset of YAML, passing JSON through YAMLToJSON is a no-op and the result is the byte array of the JSON again.
j, err := yaml.YAMLToJSON([]byte(str))
if err != nil {
return txResponse, err
}
err = json.Unmarshal(j, &txResponse)
if err != nil {
return txResponse, err
}
return txResponse, nil
}
// GetRawLogFromTxOut queries the TxHash of out from the chain and returns the RawLog from the answer.
func GetRawLogFromTxOut(val *network.Validator, out testutil.BufferWriter) (string, error) {
txResponse, err := GetTxResponseFromOut(out)
if err != nil {
return "", err
}
args := []string{
txResponse.TxHash,
}
out, err = ExecTestCLICmd(val.ClientCtx, authcmd.QueryTxCmd(), args)
if err != nil {
return "", err
}
txRes, err := GetTxResponseFromOut(out)
if err != nil {
return "", err
}
return txRes.RawLog, nil
}