testutil: add ApplyMockIODiscardOutErr, simplify existing functions (#6665)
This commit is contained in:
parent
d269eaf74c
commit
255ed06eb4
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue