2020-07-09 05:21:20 -07:00
|
|
|
package testutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyMockIO(t *testing.T) {
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
oldStdin := cmd.InOrStdin()
|
|
|
|
oldStdout := cmd.OutOrStdout()
|
|
|
|
oldStderr := cmd.ErrOrStderr()
|
|
|
|
|
|
|
|
testutil.ApplyMockIO(cmd)
|
|
|
|
|
|
|
|
require.NotEqual(t, cmd.InOrStdin(), oldStdin)
|
|
|
|
require.NotEqual(t, cmd.OutOrStdout(), oldStdout)
|
|
|
|
require.NotEqual(t, cmd.ErrOrStderr(), oldStderr)
|
2020-07-09 23:55:48 -07:00
|
|
|
require.Equal(t, cmd.ErrOrStderr(), cmd.OutOrStdout())
|
2020-07-09 05:21:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteToNewTempFile(t *testing.T) {
|
2020-12-09 10:27:20 -08:00
|
|
|
tempfile := testutil.WriteToNewTempFile(t, "test string")
|
2020-07-09 05:21:20 -07:00
|
|
|
tempfile.Close()
|
|
|
|
|
|
|
|
bs, err := ioutil.ReadFile(tempfile.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "test string", string(bs))
|
|
|
|
}
|
2020-07-09 23:55:48 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|