From 2f09daa7c3daed04305acd8f830e8af5e572843c Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 20 Jun 2019 14:37:22 +0200 Subject: [PATCH] Remove client/input.{Buffer,Override}Stdin() functions Cobra's new release made them redundant. Thanks: Juan Leni for the original patch. --- .pending/breaking/sdk/4602-client-input-Bu | 1 + client/alias.go | 2 -- client/input/input.go | 22 ------------ client/keys/add.go | 41 +++++++++++----------- client/keys/add_ledger_test.go | 12 +++---- client/keys/add_test.go | 29 +++++---------- client/keys/delete.go | 8 ++--- client/keys/delete_test.go | 8 ++--- client/keys/export.go | 8 ++--- client/keys/export_test.go | 7 ++-- client/keys/import.go | 5 +-- client/keys/import_test.go | 7 ++-- client/keys/mnemonic.go | 6 ++-- client/keys/mnemonic_test.go | 17 ++++----- client/keys/update.go | 17 +++++---- client/keys/update_test.go | 19 +++------- client/keys/utils.go | 4 ++- go.mod | 9 ++--- go.sum | 20 ++++++++--- tests/io.go | 21 +++++++++++ x/auth/client/utils/tx.go | 3 +- 21 files changed, 118 insertions(+), 148 deletions(-) create mode 100644 .pending/breaking/sdk/4602-client-input-Bu create mode 100644 tests/io.go diff --git a/.pending/breaking/sdk/4602-client-input-Bu b/.pending/breaking/sdk/4602-client-input-Bu new file mode 100644 index 000000000000..0472e404ab11 --- /dev/null +++ b/.pending/breaking/sdk/4602-client-input-Bu @@ -0,0 +1 @@ +#4602 client/input.{Buffer,Override}Stdin() functions are removed. Thanks to cobra's new release they are now redundant. diff --git a/client/alias.go b/client/alias.go index f6e11aa6305d..7361cf33a2d8 100644 --- a/client/alias.go +++ b/client/alias.go @@ -103,8 +103,6 @@ var ( GetValidators = rpc.GetValidators ValidatorSetRequestHandlerFn = rpc.ValidatorSetRequestHandlerFn LatestValidatorSetRequestHandlerFn = rpc.LatestValidatorSetRequestHandlerFn - BufferStdin = input.BufferStdin - OverrideStdin = input.OverrideStdin GetPassword = input.GetPassword GetCheckPassword = input.GetCheckPassword GetConfirmation = input.GetConfirmation diff --git a/client/input/input.go b/client/input/input.go index 2ff691432c1b..f10decad8262 100644 --- a/client/input/input.go +++ b/client/input/input.go @@ -14,28 +14,6 @@ import ( // MinPassLength is the minimum acceptable password length const MinPassLength = 8 -var currentStdin *bufio.Reader - -func init() { - currentStdin = bufio.NewReader(os.Stdin) -} - -// BufferStdin is used to allow reading prompts for stdin -// multiple times, when we read from non-tty -func BufferStdin() *bufio.Reader { - return currentStdin -} - -// OverrideStdin allows to temporarily override stdin -func OverrideStdin(newStdin *bufio.Reader) (cleanUp func()) { - prevStdin := currentStdin - currentStdin = newStdin - cleanUp = func() { - currentStdin = prevStdin - } - return cleanUp -} - // GetPassword will prompt for a password one-time (to sign a tx) // It enforces the password length func GetPassword(prompt string, buf *bufio.Reader) (pass string, err error) { diff --git a/client/keys/add.go b/client/keys/add.go index c0752c406944..f702c6b90d4f 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -1,9 +1,10 @@ package keys import ( + "bufio" "bytes" + "errors" "fmt" - "os" "sort" "github.com/cosmos/cosmos-sdk/client/flags" @@ -11,12 +12,10 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" - "errors" - "github.com/spf13/cobra" "github.com/spf13/viper" - bip39 "github.com/cosmos/go-bip39" + "github.com/cosmos/go-bip39" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/multisig" @@ -85,12 +84,12 @@ input output - armor encrypted private key (saved to file) */ -func runAddCmd(_ *cobra.Command, args []string) error { +func runAddCmd(cmd *cobra.Command, args []string) error { var kb keys.Keybase var err error var encryptPassword string - buf := input.BufferStdin() + inBuf := bufio.NewReader(cmd.InOrStdin()) name := args[0] interactive := viper.GetBool(flagInteractive) @@ -110,7 +109,7 @@ func runAddCmd(_ *cobra.Command, args []string) error { _, err = kb.Get(name) if err == nil { // account exists, ask for user confirmation - response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), buf) + response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), inBuf) if err2 != nil { return err2 } @@ -148,7 +147,7 @@ func runAddCmd(_ *cobra.Command, args []string) error { return err } - fmt.Fprintf(os.Stderr, "Key %q saved to disk.\n", name) + cmd.PrintErrf("Key %q saved to disk.\n", name) return nil } @@ -156,7 +155,7 @@ func runAddCmd(_ *cobra.Command, args []string) error { if viper.GetString(FlagPublicKey) == "" && !viper.GetBool(flags.FlagUseLedger) { encryptPassword, err = input.GetCheckPassword( "Enter a passphrase to encrypt your key to disk:", - "Repeat the passphrase:", buf) + "Repeat the passphrase:", inBuf) if err != nil { return err } @@ -186,7 +185,7 @@ func runAddCmd(_ *cobra.Command, args []string) error { return err } - return printCreate(info, false, "") + return printCreate(cmd, info, false, "") } // Get bip39 mnemonic @@ -199,7 +198,7 @@ func runAddCmd(_ *cobra.Command, args []string) error { bip39Message = "Enter your bip39 mnemonic, or hit enter to generate one." } - mnemonic, err = input.GetString(bip39Message, buf) + mnemonic, err = input.GetString(bip39Message, inBuf) if err != nil { return err } @@ -226,14 +225,14 @@ func runAddCmd(_ *cobra.Command, args []string) error { if interactive { bip39Passphrase, err = input.GetString( "Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed. "+ - "Most users should just hit enter to use the default, \"\"", buf) + "Most users should just hit enter to use the default, \"\"", inBuf) if err != nil { return err } // if they use one, make them re-enter it if len(bip39Passphrase) != 0 { - p2, err := input.GetString("Repeat the passphrase:", buf) + p2, err := input.GetString("Repeat the passphrase:", inBuf) if err != nil { return err } @@ -256,23 +255,23 @@ func runAddCmd(_ *cobra.Command, args []string) error { mnemonic = "" } - return printCreate(info, showMnemonic, mnemonic) + return printCreate(cmd, info, showMnemonic, mnemonic) } -func printCreate(info keys.Info, showMnemonic bool, mnemonic string) error { +func printCreate(cmd *cobra.Command, info keys.Info, showMnemonic bool, mnemonic string) error { output := viper.Get(cli.OutputFlag) switch output { case OutputFormatText: - fmt.Fprintln(os.Stderr) + cmd.PrintErrln() printKeyInfo(info, keys.Bech32KeyOutput) // print mnemonic unless requested not to. if showMnemonic { - fmt.Fprintln(os.Stderr, "\n**Important** write this mnemonic phrase in a safe place.") - fmt.Fprintln(os.Stderr, "It is the only way to recover your account if you ever forget your password.") - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, mnemonic) + cmd.PrintErrln("\n**Important** write this mnemonic phrase in a safe place.") + cmd.PrintErrln("It is the only way to recover your account if you ever forget your password.") + cmd.PrintErrln("") + cmd.PrintErrln(mnemonic) } case OutputFormatJSON: out, err := keys.Bech32KeyOutput(info) @@ -294,7 +293,7 @@ func printCreate(info keys.Info, showMnemonic bool, mnemonic string) error { if err != nil { return err } - fmt.Fprintln(os.Stderr, string(jsonString)) + cmd.PrintErrln(string(jsonString)) default: return fmt.Errorf("I can't speak: %s", output) } diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 67afe9d67505..7174b0ecb8ed 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -1,10 +1,8 @@ -//+build ledger,test_ledger_mock +//+build ledger test_ledger_mock package keys import ( - "bufio" - "strings" "testing" "github.com/spf13/viper" @@ -13,7 +11,6 @@ import ( "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/tests" sdk "github.com/cosmos/cosmos-sdk/types" @@ -33,10 +30,9 @@ func Test_runAddCmdLedger(t *testing.T) { /// Test Text viper.Set(cli.OutputFlag, OutputFormatText) // Now enter password - cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) - defer cleanUp1() - err := runAddCmd(cmd, []string{"keyname1"}) - assert.NoError(t, err) + mockIn, _, _ := tests.ApplyMockIO(cmd) + mockIn.Reset("test1234\ntest1234\n") + assert.NoError(t, runAddCmd(cmd, []string{"keyname1"})) // Now check that it has been stored properly kb, err := NewKeyBaseFromHomeFlag() diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 25e24e517175..86f93f2bc50a 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -1,8 +1,6 @@ package keys import ( - "bufio" - "strings" "testing" "github.com/spf13/viper" @@ -11,49 +9,40 @@ import ( "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/tests" ) func Test_runAddCmdBasic(t *testing.T) { cmd := addKeyCommand() assert.NotNil(t, cmd) + mockIn, _, _ := tests.ApplyMockIO(cmd) - // Prepare a keybase kbHome, kbCleanUp := tests.NewTestCaseDir(t) assert.NotNil(t, kbHome) defer kbCleanUp() viper.Set(flags.FlagHome, kbHome) - /// Test Text viper.Set(cli.OutputFlag, OutputFormatText) - // Now enter password - cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) - defer cleanUp1() + + mockIn.Reset("test1234\ntest1234\n") err := runAddCmd(cmd, []string{"keyname1"}) assert.NoError(t, err) - /// Test Text - Replace? >> FAIL viper.Set(cli.OutputFlag, OutputFormatText) - // Now enter password - cleanUp2 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) - defer cleanUp2() + + mockIn.Reset("test1234\ntest1234\n") err = runAddCmd(cmd, []string{"keyname1"}) assert.Error(t, err) - /// Test Text - Replace? Answer >> PASS viper.Set(cli.OutputFlag, OutputFormatText) - // Now enter password - cleanUp3 := input.OverrideStdin(bufio.NewReader(strings.NewReader("y\ntest1234\ntest1234\n"))) - defer cleanUp3() + + mockIn.Reset("y\ntest1234\ntest1234\n") err = runAddCmd(cmd, []string{"keyname1"}) assert.NoError(t, err) - // Check JSON viper.Set(cli.OutputFlag, OutputFormatJSON) - // Now enter password - cleanUp4 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) - defer cleanUp4() + + mockIn.Reset("test1234\ntest1234\n") err = runAddCmd(cmd, []string{"keyname2"}) assert.NoError(t, err) } diff --git a/client/keys/delete.go b/client/keys/delete.go index 7cc0a6dc14f6..6f8d2837fa8e 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -3,8 +3,6 @@ package keys import ( "bufio" "errors" - "fmt" - "os" "github.com/spf13/viper" @@ -53,7 +51,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error { return err } - buf := input.BufferStdin() + buf := bufio.NewReader(cmd.InOrStdin()) if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline { if !viper.GetBool(flagYes) { if err := confirmDeletion(buf); err != nil { @@ -63,7 +61,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error { if err := kb.Delete(name, "", true); err != nil { return err } - fmt.Fprintln(os.Stderr, "Public key reference deleted") + cmd.PrintErrln("Public key reference deleted") return nil } @@ -81,7 +79,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error { if err != nil { return err } - fmt.Fprintln(os.Stderr, "Key deleted forever (uh oh!)") + cmd.PrintErrln("Key deleted forever (uh oh!)") return nil } diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 1303ae1214cf..22d173021756 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/tests" ) @@ -52,10 +51,9 @@ func Test_runDeleteCmd(t *testing.T) { require.NoError(t, err) // Now there is a confirmation - cleanUp := input.OverrideStdin(bufio.NewReader(strings.NewReader("y\n"))) - defer cleanUp() - err = runDeleteCmd(deleteKeyCommand, []string{fakeKeyName1}) - require.NoError(t, err) + mockIn, _, _ := tests.ApplyMockIO(deleteKeyCommand) + mockIn.Reset("y\n") + require.NoError(t, runDeleteCmd(deleteKeyCommand, []string{fakeKeyName1})) _, err = kb.Get(fakeKeyName1) require.Error(t, err) // Key1 is gone diff --git a/client/keys/export.go b/client/keys/export.go index fac21e0dae11..93e1085f97f1 100644 --- a/client/keys/export.go +++ b/client/keys/export.go @@ -1,7 +1,7 @@ package keys import ( - "fmt" + "bufio" "github.com/spf13/cobra" @@ -19,13 +19,13 @@ func exportKeyCommand() *cobra.Command { return cmd } -func runExportCmd(_ *cobra.Command, args []string) error { +func runExportCmd(cmd *cobra.Command, args []string) error { kb, err := NewKeyBaseFromHomeFlag() if err != nil { return err } - buf := input.BufferStdin() + buf := bufio.NewReader(cmd.InOrStdin()) decryptPassword, err := input.GetPassword("Enter passphrase to decrypt your key:", buf) if err != nil { return err @@ -40,6 +40,6 @@ func runExportCmd(_ *cobra.Command, args []string) error { return err } - fmt.Println(armored) + cmd.Println(armored) return nil } diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 18a6cf0da056..bfb04dc62446 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -1,15 +1,12 @@ package keys import ( - "bufio" - "strings" "testing" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/tests" ) @@ -27,8 +24,8 @@ func Test_runExportCmd(t *testing.T) { _, err = kb.CreateAccount("keyname1", tests.TestMnemonic, "", "123456789", 0, 0) assert.NoError(t, err) + mockIn, _, _ := tests.ApplyMockIO(exportKeyCommand) + mockIn.Reset("123456789\n123456789\n") // Now enter password - cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("123456789\n123456789\n"))) - defer cleanUp1() assert.NoError(t, runExportCmd(exportKeyCommand, []string{"keyname1"})) } diff --git a/client/keys/import.go b/client/keys/import.go index 433c703a60f9..dd24b44d720a 100644 --- a/client/keys/import.go +++ b/client/keys/import.go @@ -1,6 +1,7 @@ package keys import ( + "bufio" "io/ioutil" "github.com/cosmos/cosmos-sdk/client/input" @@ -18,7 +19,7 @@ func importKeyCommand() *cobra.Command { return cmd } -func runImportCmd(_ *cobra.Command, args []string) error { +func runImportCmd(cmd *cobra.Command, args []string) error { kb, err := NewKeyBaseFromHomeFlag() if err != nil { return err @@ -29,7 +30,7 @@ func runImportCmd(_ *cobra.Command, args []string) error { return err } - buf := input.BufferStdin() + buf := bufio.NewReader(cmd.InOrStdin()) passphrase, err := input.GetPassword("Enter passphrase to decrypt your key:", buf) if err != nil { return err diff --git a/client/keys/import_test.go b/client/keys/import_test.go index c608728cee1d..95d9abfe46e5 100644 --- a/client/keys/import_test.go +++ b/client/keys/import_test.go @@ -1,10 +1,8 @@ package keys import ( - "bufio" "io/ioutil" "path/filepath" - "strings" "testing" "github.com/spf13/viper" @@ -12,7 +10,6 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/tests" ) @@ -37,7 +34,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO require.NoError(t, ioutil.WriteFile(keyfile, []byte(armoredKey), 0644)) // Now enter password - cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("123456789\n"))) - defer cleanUp1() + mockIn, _, _ := tests.ApplyMockIO(importKeyCommand) + mockIn.Reset("123456789\n") assert.NoError(t, runImportCmd(importKeyCommand, []string{"keyname1", keyfile})) } diff --git a/client/keys/mnemonic.go b/client/keys/mnemonic.go index 48b30f48d37f..fcc73a98052f 100644 --- a/client/keys/mnemonic.go +++ b/client/keys/mnemonic.go @@ -1,6 +1,7 @@ package keys import ( + "bufio" "crypto/sha256" "fmt" @@ -36,7 +37,7 @@ func runMnemonicCmd(cmd *cobra.Command, args []string) error { if userEntropy { // prompt the user to enter some entropy - buf := input.BufferStdin() + buf := bufio.NewReader(cmd.InOrStdin()) inputEntropy, err := input.GetString("> WARNING: Generate at least 256-bits of entropy and enter the results here:", buf) if err != nil { return err @@ -68,8 +69,7 @@ func runMnemonicCmd(cmd *cobra.Command, args []string) error { if err != nil { return err } - - fmt.Println(mnemonic) + cmd.Println(mnemonic) return nil } diff --git a/client/keys/mnemonic_test.go b/client/keys/mnemonic_test.go index 617a2ecc7574..e532c42ddccb 100644 --- a/client/keys/mnemonic_test.go +++ b/client/keys/mnemonic_test.go @@ -1,11 +1,11 @@ package keys import ( - "bufio" "strings" "testing" - "github.com/cosmos/cosmos-sdk/client/input" + "github.com/cosmos/cosmos-sdk/tests" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -26,8 +26,8 @@ func Test_RunMnemonicCmdUser(t *testing.T) { require.Equal(t, "EOF", err.Error()) // Try again - cleanUp := input.OverrideStdin(bufio.NewReader(strings.NewReader("Hi!\n"))) - defer cleanUp() + mockIn, _, _ := tests.ApplyMockIO(cmdUser) + mockIn.Reset("Hi!\n") err = runMnemonicCmd(cmdUser, []string{}) require.Error(t, err) require.Equal(t, @@ -36,22 +36,19 @@ func Test_RunMnemonicCmdUser(t *testing.T) { // Now provide "good" entropy :) fakeEntropy := strings.Repeat(":)", 40) + "\ny\n" // entropy + accept count - cleanUp2 := input.OverrideStdin(bufio.NewReader(strings.NewReader(fakeEntropy))) - defer cleanUp2() + mockIn.Reset(fakeEntropy) err = runMnemonicCmd(cmdUser, []string{}) require.NoError(t, err) // Now provide "good" entropy but no answer fakeEntropy = strings.Repeat(":)", 40) + "\n" // entropy + accept count - cleanUp3 := input.OverrideStdin(bufio.NewReader(strings.NewReader(fakeEntropy))) - defer cleanUp3() + mockIn.Reset(fakeEntropy) err = runMnemonicCmd(cmdUser, []string{}) require.Error(t, err) // Now provide "good" entropy but say no fakeEntropy = strings.Repeat(":)", 40) + "\nn\n" // entropy + accept count - cleanUp4 := input.OverrideStdin(bufio.NewReader(strings.NewReader(fakeEntropy))) - defer cleanUp4() + mockIn.Reset(fakeEntropy) err = runMnemonicCmd(cmdUser, []string{}) require.NoError(t, err) } diff --git a/client/keys/update.go b/client/keys/update.go index 3e1160fe8ebe..5e220ef2f1e3 100644 --- a/client/keys/update.go +++ b/client/keys/update.go @@ -1,10 +1,11 @@ package keys import ( - "fmt" + "bufio" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client/input" ) func updateKeyCommand() *cobra.Command { @@ -20,13 +21,12 @@ func updateKeyCommand() *cobra.Command { func runUpdateCmd(cmd *cobra.Command, args []string) error { name := args[0] - buf := input.BufferStdin() + buf := bufio.NewReader(cmd.InOrStdin()) kb, err := NewKeyBaseFromHomeFlag() if err != nil { return err } - oldpass, err := input.GetPassword( - "Enter the current passphrase:", buf) + oldpass, err := input.GetPassword("Enter the current passphrase:", buf) if err != nil { return err } @@ -36,11 +36,10 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error { "Enter the new passphrase:", "Repeat the new passphrase:", buf) } - - err = kb.Update(name, oldpass, getNewpass) - if err != nil { + if err := kb.Update(name, oldpass, getNewpass); err != nil { return err } - fmt.Println("Password successfully updated!") + + cmd.PrintErrln("Password successfully updated!") return nil } diff --git a/client/keys/update_test.go b/client/keys/update_test.go index c322785da294..43e5dc98f0e2 100644 --- a/client/keys/update_test.go +++ b/client/keys/update_test.go @@ -1,15 +1,12 @@ package keys import ( - "bufio" - "strings" "testing" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/tests" ) @@ -26,15 +23,12 @@ func Test_runUpdateCmd(t *testing.T) { cmd := updateKeyCommand() // fails because it requests a password - err := runUpdateCmd(cmd, []string{fakeKeyName1}) - assert.EqualError(t, err, "EOF") - - cleanUp := input.OverrideStdin(bufio.NewReader(strings.NewReader("pass1234\n"))) - defer cleanUp() + assert.EqualError(t, runUpdateCmd(cmd, []string{fakeKeyName1}), "EOF") // try again - err = runUpdateCmd(cmd, []string{fakeKeyName1}) - assert.EqualError(t, err, "Key runUpdateCmd_Key1 not found") + mockIn, _, _ := tests.ApplyMockIO(cmd) + mockIn.Reset("pass1234\n") + assert.EqualError(t, runUpdateCmd(cmd, []string{fakeKeyName1}), "Key runUpdateCmd_Key1 not found") // Prepare a key base // Now add a temporary keybase @@ -50,13 +44,10 @@ func Test_runUpdateCmd(t *testing.T) { assert.NoError(t, err) // Try again now that we have keys - cleanUp2 := input.OverrideStdin(bufio.NewReader(strings.NewReader("pass1234\nNew1234\nNew1234"))) - defer cleanUp2() - // Incorrect key type + mockIn.Reset("pass1234\nNew1234\nNew1234") err = runUpdateCmd(cmd, []string{fakeKeyName1}) assert.EqualError(t, err, "locally stored key required. Received: keys.offlineInfo") // TODO: Check for other type types? - } diff --git a/client/keys/utils.go b/client/keys/utils.go index e7442dec9bd9..2c4fcc879ff5 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -1,7 +1,9 @@ package keys import ( + "bufio" "fmt" + "os" "path/filepath" "github.com/spf13/viper" @@ -62,7 +64,7 @@ func GetPassphrase(name string) (string, error) { // ReadPassphraseFromStdin attempts to read a passphrase from STDIN return an // error upon failure. func ReadPassphraseFromStdin(name string) (string, error) { - buf := input.BufferStdin() + buf := bufio.NewReader(os.Stdin) prompt := fmt.Sprintf("Password to sign with '%s':", name) passphrase, err := input.GetPassword(prompt, buf) diff --git a/go.mod b/go.mod index 7324ba3df867..1913459cd98e 100644 --- a/go.mod +++ b/go.mod @@ -15,12 +15,8 @@ require ( github.com/golang/snappy v0.0.1 // indirect github.com/gorilla/mux v1.7.0 github.com/gorilla/websocket v1.4.0 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jmhodges/levigo v1.0.0 // indirect - github.com/magiconair/properties v1.8.0 // indirect github.com/mattn/go-isatty v0.0.6 - github.com/mitchellh/mapstructure v1.1.2 // indirect github.com/pelletier/go-toml v1.2.0 github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v0.9.2 // indirect @@ -31,11 +27,10 @@ require ( github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 // indirect github.com/rs/cors v1.6.0 // indirect github.com/spf13/afero v1.2.1 // indirect - github.com/spf13/cast v1.3.0 // indirect - github.com/spf13/cobra v0.0.3 + github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.3 - github.com/spf13/viper v1.0.3 + github.com/spf13/viper v1.3.2 github.com/stretchr/testify v1.3.0 github.com/syndtr/goleveldb v0.0.0-20180708030551-c4c61651e9e3 // indirect github.com/tendermint/btcd v0.1.1 diff --git a/go.sum b/go.sum index 5ae57a4c34ad..c016a3fb8bc6 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,7 @@ github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= @@ -24,12 +25,16 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 h1:Iwin12wRQtyZhH6FV3ykFcdGNlYEzoeR0jN8Vn+JWsI= github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/ledger-cosmos-go v0.10.3 h1:Qhi5yTR5Pg1CaTpd00pxlGwNl4sFRdtK1J96OTjeFFc= github.com/cosmos/ledger-cosmos-go v0.10.3/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI= github.com/cosmos/ledger-go v0.9.2/go.mod h1:oZJ2hHAZROdlHiwTg4t7kP+GKIIkBT+o6c9QWFanOyI= +github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -87,6 +92,7 @@ github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= @@ -122,19 +128,22 @@ github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFE github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= +github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.1 h1:qgMbHoJbPbw579P+1zVY+6n4nIFuIchaIjzZ/I/Yq8M= github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= +github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/viper v1.0.3 h1:z5LPUc2iz8VLT5Cw1UyrESG6FUUnOGecYGY08BLKSuc= -github.com/spf13/viper v1.0.3/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM= +github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= +github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= @@ -153,6 +162,8 @@ github.com/tendermint/iavl v0.12.2 h1:Ls5p5VINCM1HRT9g5Vvs2zmDOCU/CCIvIHzd/pZ8P0 github.com/tendermint/iavl v0.12.2/go.mod h1:EoKMMv++tDOL5qKKVnoIqtVPshRrEPeJ0WsgDOLAauM= github.com/tendermint/tendermint v0.31.5 h1:vTet8tCq3B9/J9Yo11dNZ8pOB7NtSy++bVSfkP4KzR4= github.com/tendermint/tendermint v0.31.5/go.mod h1:ymcPyWblXCplCPQjbOYbrF1fWnpslATMVqiGgWbZrlc= +github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/zondax/hid v0.9.0 h1:eiT3P6vNxAEVxXMw66eZUAAnU2zD33JBkfG/EnfAKl8= github.com/zondax/hid v0.9.0/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -176,6 +187,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/tests/io.go b/tests/io.go new file mode 100644 index 000000000000..b1aa30634a67 --- /dev/null +++ b/tests/io.go @@ -0,0 +1,21 @@ +package tests + +import ( + "bytes" + "strings" + + "github.com/spf13/cobra" +) + +// ApplyMockIO replaces stdin/out/err with buffers that can be used during testing. +func ApplyMockIO(c *cobra.Command) (*strings.Reader, *bytes.Buffer, *bytes.Buffer) { + mockIn := strings.NewReader("") + mockOut := bytes.NewBufferString("") + mockErr := bytes.NewBufferString("") + c.SetIn(mockIn) + c.SetOut(mockOut) + c.SetErr(mockErr) + return mockIn, mockOut, mockErr +} + +// DONTCOVER diff --git a/x/auth/client/utils/tx.go b/x/auth/client/utils/tx.go index 3353f25980dd..8445b0212acb 100644 --- a/x/auth/client/utils/tx.go +++ b/x/auth/client/utils/tx.go @@ -1,6 +1,7 @@ package utils import ( + "bufio" "bytes" "fmt" "io/ioutil" @@ -84,7 +85,7 @@ func CompleteAndBroadcastTxCLI(txBldr authtypes.TxBuilder, cliCtx context.CLICon _, _ = fmt.Fprintf(os.Stderr, "%s\n\n", json) - buf := input.BufferStdin() + buf := bufio.NewReader(os.Stdin) ok, err := input.GetConfirmation("confirm transaction before signing and broadcasting", buf) if err != nil || !ok { _, _ = fmt.Fprintf(os.Stderr, "%s\n", "cancelled transaction")