Skip to content

Commit

Permalink
refactored, all tests pass, r4r
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Apr 6, 2021
1 parent b471bcd commit 3434789
Showing 1 changed file with 32 additions and 47 deletions.
79 changes: 32 additions & 47 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,100 +17,85 @@ 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{}.
WithHomeDir(home).
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"})
cmd.Execute()
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)
})
Expand Down

0 comments on commit 3434789

Please sign in to comment.