From 255ed06eb402e2e66208d858508e5b89e8d5c0fc Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 10 Jul 2020 07:55:48 +0100 Subject: [PATCH] testutil: add ApplyMockIODiscardOutErr, simplify existing functions (#6665) --- client/cmd_test.go | 5 ++-- client/keys/add_ledger_test.go | 4 ++-- client/keys/add_test.go | 2 +- client/keys/delete_test.go | 2 +- client/keys/export_test.go | 2 +- client/keys/import_test.go | 2 +- client/keys/list_test.go | 2 +- client/keys/migrate_test.go | 6 ++--- client/keys/mnemonic_test.go | 9 +++---- client/keys/show_test.go | 2 +- testutil/ioutil.go | 36 ++++++++++++++++++++++++---- testutil/ioutil_test.go | 12 +++++++++- version/version_test.go | 2 +- x/auth/client/cli/broadcast_test.go | 3 +-- x/auth/client/cli/encode_test.go | 7 ++---- x/genutil/client/cli/migrate_test.go | 3 +-- 16 files changed, 63 insertions(+), 36 deletions(-) diff --git a/client/cmd_test.go b/client/cmd_test.go index 3f15ebf621..039b239062 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -3,7 +3,6 @@ package client_test import ( "context" "fmt" - "io/ioutil" "testing" "github.com/spf13/cobra" @@ -11,6 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestValidateCmd(t *testing.T) { @@ -106,8 +106,7 @@ func TestSetCmdClientContextHandler(t *testing.T) { ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{}) cmd := newCmd() - cmd.SetOut(ioutil.Discard) - cmd.SetErr(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs(tc.args) require.NoError(t, cmd.ExecuteContext(ctx)) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index fae479bfe2..60ac6b4f3c 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -53,7 +53,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) mockIn.Reset("test1234\ntest1234\n") require.NoError(t, cmd.Execute()) @@ -85,7 +85,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { func Test_runAddCmdLedger(t *testing.T) { cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) // Prepare a keybase kbHome, kbCleanUp := testutil.NewTestCaseDir(t) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 1a67f79b2e..4bbed32e2e 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -19,7 +19,7 @@ func Test_runAddCmdBasic(t *testing.T) { cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) kbHome, kbCleanUp := testutil.NewTestCaseDir(t) require.NotNil(t, kbHome) diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 6bafefeef7..f7a5017cb3 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -16,7 +16,7 @@ import ( func Test_runDeleteCmd(t *testing.T) { cmd := DeleteKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) yesF, _ := cmd.Flags().GetBool(flagYes) forceF, _ := cmd.Flags().GetBool(flagForce) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index b64fb12afa..36361dffbb 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -17,7 +17,7 @@ import ( func Test_runExportCmd(t *testing.T) { cmd := ExportKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) // Now add a temporary keybase kbHome, cleanUp := testutil.NewTestCaseDir(t) diff --git a/client/keys/import_test.go b/client/keys/import_test.go index 835319bb59..16fb46b862 100644 --- a/client/keys/import_test.go +++ b/client/keys/import_test.go @@ -17,7 +17,7 @@ import ( func Test_runImportCmd(t *testing.T) { cmd := ImportKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) // Now add a temporary keybase kbHome, cleanUp := testutil.NewTestCaseDir(t) diff --git a/client/keys/list_test.go b/client/keys/list_test.go index bcde277aa6..0f84924f58 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -24,7 +24,7 @@ func Test_runListCmd(t *testing.T) { kbHome2, cleanUp2 := testutil.NewTestCaseDir(t) t.Cleanup(cleanUp2) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn) require.NoError(t, err) diff --git a/client/keys/migrate_test.go b/client/keys/migrate_test.go index e779ffa039..fcd826eb30 100644 --- a/client/keys/migrate_test.go +++ b/client/keys/migrate_test.go @@ -2,7 +2,6 @@ package keys import ( "fmt" - "io/ioutil" "testing" "github.com/otiai10/copy" @@ -16,8 +15,7 @@ import ( func Test_runMigrateCmd(t *testing.T) { cmd := AddKeyCommand() - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) kbHome, kbCleanUp := testutil.NewTestCaseDir(t) @@ -35,7 +33,7 @@ func Test_runMigrateCmd(t *testing.T) { cmd = MigrateCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs([]string{ fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), diff --git a/client/keys/mnemonic_test.go b/client/keys/mnemonic_test.go index 359cd8fe93..c986e6230b 100644 --- a/client/keys/mnemonic_test.go +++ b/client/keys/mnemonic_test.go @@ -2,7 +2,6 @@ package keys import ( "fmt" - "io/ioutil" "strings" "testing" @@ -13,16 +12,14 @@ import ( func Test_RunMnemonicCmdNormal(t *testing.T) { cmd := MnemonicKeyCommand() - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs([]string{}) require.NoError(t, cmd.Execute()) } func Test_RunMnemonicCmdUser(t *testing.T) { cmd := MnemonicKeyCommand() - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs([]string{fmt.Sprintf("--%s=1", flagUserEntropy)}) err := cmd.Execute() @@ -30,7 +27,7 @@ func Test_RunMnemonicCmdUser(t *testing.T) { require.Equal(t, "EOF", err.Error()) // Try again - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) mockIn.Reset("Hi!\n") err = cmd.Execute() require.Error(t, err) diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 024ab605de..b7550fd566 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -38,7 +38,7 @@ func Test_showKeysCmd(t *testing.T) { func Test_runShowCmd(t *testing.T) { cmd := ShowKeysCmd() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - mockIn, _, _ := testutil.ApplyMockIO(cmd) + mockIn := testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs([]string{"invalid"}) require.EqualError(t, cmd.Execute(), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7") diff --git a/testutil/ioutil.go b/testutil/ioutil.go index ab0ccaec58..c70d50ad6b 100644 --- a/testutil/ioutil.go +++ b/testutil/ioutil.go @@ -2,6 +2,7 @@ package testutil import ( "bytes" + "io" "io/ioutil" "os" "strings" @@ -20,15 +21,42 @@ func NewTestCaseDir(t testing.TB) (string, func()) { return dir, func() { os.RemoveAll(dir) } } +// BufferReader is implemented by types that read from a string buffer. +type BufferReader interface { + io.Reader + Reset(string) +} + +// BufferWriter is implemented by types that write to a buffer. +type BufferWriter interface { + io.Writer + Reset() + String() string +} + // ApplyMockIO replaces stdin/out/err with buffers that can be used during testing. -func ApplyMockIO(c *cobra.Command) (*strings.Reader, *bytes.Buffer, *bytes.Buffer) { +// Returns an input BufferReader and an output BufferWriter. +func ApplyMockIO(c *cobra.Command) (BufferReader, BufferWriter) { mockIn := strings.NewReader("") mockOut := bytes.NewBufferString("") - mockErr := bytes.NewBufferString("") + c.SetIn(mockIn) c.SetOut(mockOut) - c.SetErr(mockErr) - return mockIn, mockOut, mockErr + c.SetErr(mockOut) + + return mockIn, mockOut +} + +// ApplyMockIODiscardOutputs replaces a cobra.Command output and error streams with a dummy io.Writer. +// Replaces and returns the io.Reader associated to the cobra.Command input stream. +func ApplyMockIODiscardOutErr(c *cobra.Command) BufferReader { + mockIn := strings.NewReader("") + + c.SetIn(mockIn) + c.SetOut(ioutil.Discard) + c.SetErr(ioutil.Discard) + + return mockIn } // Write the given string to a new temporary file. diff --git a/testutil/ioutil_test.go b/testutil/ioutil_test.go index d66f301bbb..272ff323b3 100644 --- a/testutil/ioutil_test.go +++ b/testutil/ioutil_test.go @@ -29,7 +29,6 @@ func TestNewTestCaseDir(t *testing.T) { func TestApplyMockIO(t *testing.T) { cmd := &cobra.Command{} - oldStdin := cmd.InOrStdin() oldStdout := cmd.OutOrStdout() oldStderr := cmd.ErrOrStderr() @@ -39,6 +38,7 @@ func TestApplyMockIO(t *testing.T) { require.NotEqual(t, cmd.InOrStdin(), oldStdin) require.NotEqual(t, cmd.OutOrStdout(), oldStdout) require.NotEqual(t, cmd.ErrOrStderr(), oldStderr) + require.Equal(t, cmd.ErrOrStderr(), cmd.OutOrStdout()) } func TestWriteToNewTempFile(t *testing.T) { @@ -53,3 +53,13 @@ func TestWriteToNewTempFile(t *testing.T) { require.NoFileExists(t, tempfile.Name()) } + +func TestApplyMockIODiscardOutErr(t *testing.T) { + cmd := &cobra.Command{} + oldStdin := cmd.InOrStdin() + + testutil.ApplyMockIODiscardOutErr(cmd) + require.NotEqual(t, cmd.InOrStdin(), oldStdin) + require.Equal(t, cmd.OutOrStdout(), ioutil.Discard) + require.Equal(t, cmd.ErrOrStderr(), ioutil.Discard) +} diff --git a/version/version_test.go b/version/version_test.go index 9eac0f3b53..ef44f8d317 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -40,7 +40,7 @@ go version go1.14 linux/amd64` func Test_runVersionCmd(t *testing.T) { cmd := NewVersionCommand() - _, mockOut, _ := testutil.ApplyMockIO(cmd) + _, mockOut := testutil.ApplyMockIO(cmd) cmd.SetArgs([]string{ fmt.Sprintf("--%s=''", cli.OutputFlag), diff --git a/x/auth/client/cli/broadcast_test.go b/x/auth/client/cli/broadcast_test.go index 7ddfde4a4a..fee6de1c2a 100644 --- a/x/auth/client/cli/broadcast_test.go +++ b/x/auth/client/cli/broadcast_test.go @@ -19,8 +19,7 @@ func TestGetBroadcastCommand_OfflineFlag(t *testing.T) { clientCtx = clientCtx.WithTxGenerator(simappparams.MakeEncodingConfig().TxGenerator) cmd := GetBroadcastCommand(clientCtx) - cmd.SetOut(ioutil.Discard) - cmd.SetErr(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs([]string{fmt.Sprintf("--%s=true", flags.FlagOffline), ""}) require.EqualError(t, cmd.Execute(), "cannot broadcast tx during offline mode") diff --git a/x/auth/client/cli/encode_test.go b/x/auth/client/cli/encode_test.go index 870b4064cb..550e1133cc 100644 --- a/x/auth/client/cli/encode_test.go +++ b/x/auth/client/cli/encode_test.go @@ -2,7 +2,6 @@ package cli import ( "encoding/base64" - "io/ioutil" "testing" "github.com/stretchr/testify/require" @@ -21,8 +20,7 @@ func TestGetCommandEncode(t *testing.T) { WithJSONMarshaler(encodingConfig.Marshaler) cmd := GetEncodeCommand(clientCtx) - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) authtypes.RegisterCodec(encodingConfig.Amino) sdk.RegisterCodec(encodingConfig.Amino) @@ -51,8 +49,7 @@ func TestGetCommandDecode(t *testing.T) { WithJSONMarshaler(encodingConfig.Marshaler) cmd := GetDecodeCommand(clientCtx) - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) sdk.RegisterCodec(encodingConfig.Amino) diff --git a/x/genutil/client/cli/migrate_test.go b/x/genutil/client/cli/migrate_test.go index 5419a35abe..d5d35754ee 100644 --- a/x/genutil/client/cli/migrate_test.go +++ b/x/genutil/client/cli/migrate_test.go @@ -28,8 +28,7 @@ func TestMigrateGenesis(t *testing.T) { target := "v0.36" cmd := MigrateGenesisCmd() - cmd.SetErr(ioutil.Discard) - cmd.SetOut(ioutil.Discard) + _ = testutil.ApplyMockIODiscardOutErr(cmd) clientCtx := client.Context{}.WithJSONMarshaler(cdc) ctx := context.Background()