Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env variable to cmd flags #9040

Merged
merged 12 commits into from
Apr 13, 2021
61 changes: 61 additions & 0 deletions client/config/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/flags"
)

var ErrWrongNumberOfArgs = fmt.Errorf("wrong number of arguments")

func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) {
// Prepare environment
t.Parallel()
configHome, cleanup := tmpDir(t)
defer cleanup()
_ = os.RemoveAll(filepath.Join(configHome, "config"))
viper.Set(flags.FlagHome, configHome)

// Init command config
cmd := Cmd()
assert.NotNil(t, cmd)

err := cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"})
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "--get"})
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "tcp://local:26657"})
assert.Nil(t, err)

err = cmd.RunE(cmd, []string{"node", "--get"})
assert.Nil(t, err)

err = cmd.RunE(cmd, nil)
assert.Nil(t, err)

//err = cmd.RunE(cmd, []string{"invalidKey", "--get"})
//require.Equal(t, err, errUnknownConfigKey("invalidKey"))

err = cmd.RunE(cmd, []string{"invalidArg1"})
require.Equal(t, err, ErrWrongNumberOfArgs)

//err = cmd.RunE(cmd, []string{"invalidKey", "invalidValue"})
//require.Equal(t, err, errUnknownConfigKey("invalidKey"))

}

func tmpDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
return dir, func() { _ = os.RemoveAll(dir) }
}
104 changes: 104 additions & 0 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package config_test

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
)

const (
nodeEnv = "NODE"
testNode1 = "http://localhost:1"
testNode2 = "http://localhost:2"
)

func initContext(t *testing.T) (context.Context, func()) {
home := t.TempDir()

clientCtx := client.Context{}.
WithHomeDir(home).
WithViper()

clientCtx.Viper.BindEnv(nodeEnv)

clientCtx, err := config.ReadFromClientConfig(clientCtx)
require.NoError(t, err)

ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
return ctx, func() { _ = os.RemoveAll(home) }
}

func TestConfigCmd(t *testing.T) {
os.Setenv(nodeEnv, testNode1)
ctx, cleanup := initContext(t)
defer func() {
os.Unsetenv(nodeEnv)
cleanup()
}()

cmd := config.Cmd()
// 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 //http://localhost:1
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"node"})
cmd.Execute()
out, err := ioutil.ReadAll(b)
require.NoError(t, err)
require.Equal(t, string(out), testNode1+"\n")
}

func TestConfigCmdEnvFlag(t *testing.T) {

const (
defaultNode = "http://localhost:26657"
)

tt := []struct {
name string
envVar string
args []string
expNode string
}{
{"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)
})
}

}
1 change: 1 addition & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceR
// client-side config from the config file.
func (ctx Context) WithViper() Context {
v := viper.New()
v.AutomaticEnv()
ctx.Viper = v
return ctx
}
Expand Down