2019-02-08 12:45:23 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-10-08 10:41:35 -07:00
|
|
|
"context"
|
2020-07-06 12:50:09 -07:00
|
|
|
"fmt"
|
2019-02-08 12:45:23 -08:00
|
|
|
"testing"
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/spf13/cobra"
|
2019-11-14 06:17:21 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-07-06 12:50:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2020-01-22 09:54:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-08 12:45:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_runListCmd(t *testing.T) {
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd := ListKeysCmd()
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-09-18 04:08:24 -07:00
|
|
|
kbHome1 := t.TempDir()
|
|
|
|
kbHome2 := t.TempDir()
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2020-07-06 12:50:09 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
clientCtx := client.Context{}.WithKeyring(kb)
|
|
|
|
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
|
|
|
|
2021-02-22 07:14:09 -08:00
|
|
|
path := "" //sdk.GetConfig().GetFullBIP44Path()
|
2020-07-09 05:21:20 -07:00
|
|
|
_, err = kb.NewAccount("something", testutil.TestMnemonic, "", path, hd.Secp256k1)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(func() {
|
2020-04-08 02:38:28 -07:00
|
|
|
kb.Delete("something") // nolint:errcheck
|
2020-03-01 16:16:23 -08:00
|
|
|
})
|
2020-07-06 12:50:09 -07:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
cmd *cobra.Command
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
testData := []struct {
|
|
|
|
name string
|
|
|
|
kbDir string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2020-07-06 12:50:09 -07:00
|
|
|
{"keybase: empty", kbHome1, false},
|
|
|
|
{"keybase: w/key", kbHome2, false},
|
2019-02-08 12:45:23 -08:00
|
|
|
}
|
|
|
|
for _, tt := range testData {
|
2019-10-17 06:47:35 -07:00
|
|
|
tt := tt
|
2019-02-08 12:45:23 -08:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, tt.kbDir),
|
|
|
|
fmt.Sprintf("--%s=false", flagListNames),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
if err := cmd.ExecuteContext(ctx); (err != nil) != tt.wantErr {
|
2019-02-08 12:45:23 -08:00
|
|
|
t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
2019-12-05 07:07:29 -08:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, tt.kbDir),
|
|
|
|
fmt.Sprintf("--%s=true", flagListNames),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
if err := cmd.ExecuteContext(ctx); (err != nil) != tt.wantErr {
|
2019-12-05 07:07:29 -08:00
|
|
|
t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
2019-02-08 12:45:23 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|