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
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

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) {
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
// Prepare environment
t.Parallel()
configHome, cleanup := tmpDir(t)
defer cleanup()
_ = os.RemoveAll(filepath.Join(configHome, "config"))
viper.Set(flags.FlagHome, configHome)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

// Init command config
cmd := Cmd()
assert.NotNil(t, cmd)
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved

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

err = cmd.RunE(cmd, []string{"node", "--get"})
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
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) }
}
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()
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
ctx.Viper = v
return ctx
}
Expand Down