Use Context in Command instead of Argument + Util (#6572)
* Use context * use PersistentPreRunE * undo * use init context * Update types * update tests * implement tests * Update simapp/cmd/simcli/main.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update simapp/cmd/simcli/main.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/bank/client/cli/tx.go Co-authored-by: Alessio Treglia <alessio@tendermint.com> * fix build Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
co-authored by
Federico Kunze
Alessio Treglia
parent
8ed09e5098
commit
14d1ee5437
@@ -1,12 +1,16 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
)
|
||||
|
||||
func TestValidateCmd(t *testing.T) {
|
||||
@@ -50,3 +54,66 @@ func TestValidateCmd(t *testing.T) {
|
||||
require.Equal(t, tt.wantErr, err != nil, tt.reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCmdClientContextHandler(t *testing.T) {
|
||||
initClientCtx := client.Context{}.WithHomeDir("/foo/bar").WithChainID("test-chain")
|
||||
|
||||
newCmd := func() *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return client.SetCmdClientContextHandler(initClientCtx, cmd)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
clientCtx := client.GetClientContextFromCmd(cmd)
|
||||
_, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
c.Flags().String(flags.FlagChainID, "", "network chain ID")
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectedContext client.Context
|
||||
args []string
|
||||
}{
|
||||
{
|
||||
"no flags set",
|
||||
initClientCtx,
|
||||
[]string{},
|
||||
},
|
||||
{
|
||||
"flags set",
|
||||
initClientCtx.WithChainID("new-chain-id"),
|
||||
[]string{
|
||||
fmt.Sprintf("--%s=new-chain-id", flags.FlagChainID),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
|
||||
|
||||
cmd := newCmd()
|
||||
cmd.SetOut(ioutil.Discard)
|
||||
cmd.SetErr(ioutil.Discard)
|
||||
cmd.SetArgs(tc.args)
|
||||
|
||||
require.NoError(t, cmd.ExecuteContext(ctx))
|
||||
|
||||
clientCtx := client.GetClientContextFromCmd(cmd)
|
||||
require.Equal(t, tc.expectedContext, clientCtx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user