diff --git a/client/config/config_test.go b/client/config/config_test.go index 44b3d91af8c7..300ace39d75e 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -17,10 +17,12 @@ import ( ) const ( - nodeEnv = "NODE" + nodeEnv = "NODE" + testNode1 = "http://localhost:1" + testNode2 = "http://localhost:2" ) -func initContext(t *testing.T, testNode string) context.Context { +func initContext(t *testing.T) (context.Context, func()) { home := t.TempDir() clientCtx := client.Context{}. @@ -28,37 +30,28 @@ func initContext(t *testing.T, testNode string) context.Context { WithViper() clientCtx.Viper.BindEnv(nodeEnv) - os.Setenv(nodeEnv, testNode) clientCtx, err := config.ReadFromClientConfig(clientCtx) require.NoError(t, err) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - return ctx + return ctx, func() { _ = os.RemoveAll(home) } } -/* -First -env var > config -NODE=tcp://localhost:127 ./build/simd config node tcp://localhost:128 -./build/simd config node //tcp://localhost:127 -*/ -func TestConfigCmdFirst(t *testing.T) { - - const ( - testNode1 = "tcp://localhost:127" - testNode2 = "tcp://localhost:128" - ) - - ctx := initContext(t, testNode1) +func TestConfigCmd(t *testing.T) { + os.Setenv(nodeEnv, testNode1) + ctx, cleanup := initContext(t) + defer func() { + os.Unsetenv(nodeEnv) + cleanup() + }() cmd := config.Cmd() - - // NODE=tcp://localhost:127 ./build/simd config node tcp://localhost:128 + // NODE=http://localhost:1 ./build/simd config node http://localhost:2 cmd.SetArgs([]string{"node", testNode2}) require.NoError(t, cmd.ExecuteContext(ctx)) - //./build/simd config node //tcp://localhost:127 + //./build/simd config node //http://localhost:1 b := bytes.NewBufferString("") cmd.SetOut(b) cmd.SetArgs([]string{"node"}) @@ -66,51 +59,43 @@ func TestConfigCmdFirst(t *testing.T) { out, err := ioutil.ReadAll(b) require.NoError(t, err) require.Equal(t, string(out), testNode1+"\n") - } -/* -Second -env var > config WORKS -./build/simd config node // tcp://localhost:127 //done already -NODE=tcp://localhost:1 ./build/simd q staking validators -Error: post failed: Post "http://localhost:1": dial tcp 127.0.0.1:1: connect: connection refused - -Third -flags > env var > config WORKS -./build/simd config node // tcp://localhost:127 -NODE=tcp://localhost:1 ./build/simd q staking validators --node tcp://localhost:2 -Error: post failed: Post "http://localhost:2": dial tcp 127.0.0.1:2: connect: connection refused -*/ -func TestConfigCmdSecondThird(t *testing.T) { +func TestConfigCmdEnvFlag(t *testing.T) { const ( - testNode1 = "http://localhost:1" - testNode2 = "http://localhost:2" + defaultNode = "http://localhost:26657" ) - ctx := initContext(t, testNode1) - - /* - "no flag" Error: post failed: Post "http://localhost:1": dial tcp 127.0.0.1:1: connect: connection refused - "flag" Error: post failed: Post "http://localhost:2": dial tcp 127.0.0.1:2: connect: connection refused - */ - tt := []struct { name string + envVar string args []string expNode string }{ - {"no flag", []string{"validators"}, testNode1}, - {"flag", []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2}, + {"env var is set with no flag", testNode1, []string{"validators"}, testNode1}, + {"env var is set with a flag", testNode1, []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2}, + {"env var is not set with no flag", "", []string{"validators"}, defaultNode}, + {"env var is not set with a flag", "", []string{"validators", fmt.Sprintf("--%s=%s", flags.FlagNode, testNode2)}, testNode2}, } for _, tc := range tt { tc := tc t.Run(tc.name, func(t *testing.T) { + if tc.envVar != "" { + os.Setenv(nodeEnv, tc.envVar) + defer func() { + os.Unsetenv(nodeEnv) + }() + } + + ctx, cleanup := initContext(t) + defer cleanup() + cmd := cli.GetQueryCmd() cmd.SetArgs(tc.args) err := cmd.ExecuteContext(ctx) + require.Error(t, err) require.Contains(t, err.Error(), tc.expNode) })