github.com/cosmos/cosmos-sdk@v0.50.10/client/cmd_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/cosmos/cosmos-sdk/client"
    12  	"github.com/cosmos/cosmos-sdk/client/flags"
    13  	"github.com/cosmos/cosmos-sdk/testutil"
    14  )
    15  
    16  func TestValidateCmd(t *testing.T) {
    17  	// setup root and subcommands
    18  	rootCmd := &cobra.Command{
    19  		Use: "root",
    20  	}
    21  	queryCmd := &cobra.Command{
    22  		Use: "query",
    23  	}
    24  	rootCmd.AddCommand(queryCmd)
    25  
    26  	// command being tested
    27  	distCmd := &cobra.Command{
    28  		Use:                        "distr",
    29  		DisableFlagParsing:         true,
    30  		SuggestionsMinimumDistance: 2,
    31  	}
    32  	queryCmd.AddCommand(distCmd)
    33  
    34  	commissionCmd := &cobra.Command{
    35  		Use: "commission",
    36  	}
    37  	distCmd.AddCommand(commissionCmd)
    38  
    39  	tests := []struct {
    40  		reason  string
    41  		args    []string
    42  		wantErr bool
    43  	}{
    44  		{"misspelled command", []string{"COMMISSION"}, true},
    45  		{"no command provided", []string{}, false},
    46  		{"help flag", []string{"COMMISSION", "--help"}, false},
    47  		{"shorthand help flag", []string{"COMMISSION", "-h"}, false},
    48  		{"flag only, no command provided", []string{"--gas", "1000atom"}, false},
    49  		{"flag and misspelled command", []string{"--gas", "1000atom", "COMMISSION"}, true},
    50  	}
    51  
    52  	for _, tt := range tests {
    53  		err := client.ValidateCmd(distCmd, tt.args)
    54  		require.Equal(t, tt.wantErr, err != nil, tt.reason)
    55  	}
    56  }
    57  
    58  func TestSetCmdClientContextHandler(t *testing.T) {
    59  	initClientCtx := client.Context{}.WithHomeDir("/foo/bar").WithChainID("test-chain").WithKeyringDir("/foo/bar")
    60  
    61  	newCmd := func() *cobra.Command {
    62  		c := &cobra.Command{
    63  			PreRunE: func(cmd *cobra.Command, args []string) error {
    64  				return client.SetCmdClientContextHandler(initClientCtx, cmd)
    65  			},
    66  			RunE: func(cmd *cobra.Command, _ []string) error {
    67  				_, err := client.GetClientTxContext(cmd)
    68  				return err
    69  			},
    70  		}
    71  
    72  		c.Flags().String(flags.FlagChainID, "", "network chain ID")
    73  		c.Flags().String(flags.FlagHome, "", "home dir")
    74  
    75  		return c
    76  	}
    77  
    78  	testCases := []struct {
    79  		name            string
    80  		expectedContext client.Context
    81  		args            []string
    82  		ctx             context.Context
    83  	}{
    84  		{
    85  			"no flags set",
    86  			initClientCtx,
    87  			[]string{},
    88  			context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}),
    89  		},
    90  		{
    91  			"flags set",
    92  			initClientCtx.WithChainID("new-chain-id"),
    93  			[]string{
    94  				fmt.Sprintf("--%s=new-chain-id", flags.FlagChainID),
    95  			},
    96  			context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}),
    97  		},
    98  		{
    99  			"flags set with space",
   100  			initClientCtx.WithHomeDir("/tmp/dir"),
   101  			[]string{
   102  				fmt.Sprintf("--%s", flags.FlagHome),
   103  				"/tmp/dir",
   104  			},
   105  			context.Background(),
   106  		},
   107  		{
   108  			"no context provided",
   109  			initClientCtx.WithHomeDir("/tmp/noctx"),
   110  			[]string{
   111  				fmt.Sprintf("--%s", flags.FlagHome),
   112  				"/tmp/noctx",
   113  			},
   114  			nil,
   115  		},
   116  		{
   117  			"with invalid client value in the context",
   118  			initClientCtx.WithHomeDir("/tmp/invalid"),
   119  			[]string{
   120  				fmt.Sprintf("--%s", flags.FlagHome),
   121  				"/tmp/invalid",
   122  			},
   123  			context.WithValue(context.Background(), client.ClientContextKey, "invalid"),
   124  		},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		tc := tc
   129  
   130  		t.Run(tc.name, func(t *testing.T) {
   131  			cmd := newCmd()
   132  			_ = testutil.ApplyMockIODiscardOutErr(cmd)
   133  			cmd.SetArgs(tc.args)
   134  
   135  			require.NoError(t, cmd.ExecuteContext(tc.ctx))
   136  
   137  			clientCtx := client.GetClientContextFromCmd(cmd)
   138  			require.Equal(t, tc.expectedContext, clientCtx)
   139  		})
   140  	}
   141  }