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

     1  package config_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     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/config"
    13  	"github.com/cosmos/cosmos-sdk/client/flags"
    14  	"github.com/cosmos/cosmos-sdk/codec"
    15  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    16  	clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
    17  )
    18  
    19  const (
    20  	nodeEnv   = "NODE"
    21  	testNode1 = "http://localhost:1"
    22  	testNode2 = "http://localhost:2"
    23  )
    24  
    25  // initClientContext initiates client Context for tests
    26  func initClientContext(t *testing.T, envVar string) (client.Context, func()) {
    27  	home := t.TempDir()
    28  	chainID := "test-chain"
    29  	clientCtx := client.Context{}.
    30  		WithHomeDir(home).
    31  		WithViper("").
    32  		WithCodec(codec.NewProtoCodec(codectypes.NewInterfaceRegistry())).
    33  		WithChainID(chainID)
    34  
    35  	require.NoError(t, clientCtx.Viper.BindEnv(nodeEnv))
    36  	if envVar != "" {
    37  		require.NoError(t, os.Setenv(nodeEnv, envVar))
    38  	}
    39  
    40  	clientCtx, err := config.ReadFromClientConfig(clientCtx)
    41  	require.NoError(t, err)
    42  	require.Equal(t, clientCtx.ChainID, chainID)
    43  
    44  	return clientCtx, func() { _ = os.RemoveAll(home) }
    45  }
    46  
    47  func TestConfigCmdEnvFlag(t *testing.T) {
    48  	tt := []struct {
    49  		name    string
    50  		envVar  string
    51  		args    []string
    52  		expNode string
    53  	}{
    54  		{"env var is set with no flag", testNode1, []string{}, testNode1},
    55  		{"env var is set with a flag", testNode1, []string{fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2},
    56  		{"env var is not set with no flag", "", []string{}, "tcp://localhost:26657"},
    57  		{"env var is not set with a flag", "", []string{fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2},
    58  	}
    59  
    60  	for _, tc := range tt {
    61  		tc := tc
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			testCmd := &cobra.Command{
    64  				Use: "test",
    65  				RunE: func(cmd *cobra.Command, args []string) error {
    66  					clientCtx, err := client.GetClientQueryContext(cmd)
    67  					if err != nil {
    68  						return err
    69  					}
    70  
    71  					return fmt.Errorf("%s", clientCtx.NodeURI)
    72  				},
    73  			}
    74  			flags.AddQueryFlagsToCmd(testCmd)
    75  
    76  			clientCtx, cleanup := initClientContext(t, tc.envVar)
    77  			defer func() {
    78  				cleanup()
    79  				_ = os.Unsetenv(nodeEnv)
    80  			}()
    81  			/*
    82  				env var is set with a flag
    83  
    84  				NODE=http://localhost:1 test-cmd --node http://localhost:2
    85  				Prints "http://localhost:2"
    86  
    87  				It prints http://localhost:2 cause a flag has the higher priority than env variable.
    88  			*/
    89  
    90  			_, err := clitestutil.ExecTestCLICmd(clientCtx, testCmd, tc.args)
    91  			require.Error(t, err)
    92  			require.Contains(t, err.Error(), tc.expNode)
    93  		})
    94  	}
    95  }