Skip to content

Commit

Permalink
testutil cleanup and reorg (cosmos#6658)
Browse files Browse the repository at this point in the history
Prepare migrating testing auxiliary functions from tests
to testutil.

Remove local duplicates on testutil.WriteToNewTempFile().

Always favor testutil.NewTestCaseDir() over ioutil.TempDir().

Add test cases for the testing auxiliary functions.
  • Loading branch information
Alessio Treglia authored Jul 9, 2020
1 parent f8df05f commit a940214
Show file tree
Hide file tree
Showing 44 changed files with 281 additions and 243 deletions.
10 changes: 5 additions & 5 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -37,7 +37,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

// Prepare a keybase
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
kbHome, kbCleanUp := testutil.NewTestCaseDir(t)
require.NotNil(t, kbHome)
t.Cleanup(kbCleanUp)

Expand All @@ -53,7 +53,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})

mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)
mockIn.Reset("test1234\ntest1234\n")
require.NoError(t, cmd.Execute())

Expand Down Expand Up @@ -85,10 +85,10 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
func Test_runAddCmdLedger(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

// Prepare a keybase
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
kbHome, kbCleanUp := testutil.NewTestCaseDir(t)
require.NotNil(t, kbHome)
t.Cleanup(kbCleanUp)

Expand Down
6 changes: 3 additions & 3 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runAddCmdBasic(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

kbHome, kbCleanUp := tests.NewTestCaseDir(t)
kbHome, kbCleanUp := testutil.NewTestCaseDir(t)
require.NotNil(t, kbHome)
t.Cleanup(kbCleanUp)

Expand Down
8 changes: 4 additions & 4 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runDeleteCmd(t *testing.T) {
cmd := DeleteKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

yesF, _ := cmd.Flags().GetBool(flagYes)
forceF, _ := cmd.Flags().GetBool(flagForce)
Expand All @@ -27,15 +27,15 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName1 := "runDeleteCmd_Key1"
fakeKeyName2 := "runDeleteCmd_Key2"
// Now add a temporary keybase
kbHome, cleanUp := tests.NewTestCaseDir(t)
kbHome, cleanUp := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp)

path := sdk.GetConfig().GetFullFundraiserPath()

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
require.NoError(t, err)

_, err = kb.NewAccount(fakeKeyName1, tests.TestMnemonic, "", path, hd.Secp256k1)
_, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

_, _, err = kb.NewMnemonic(fakeKeyName2, keyring.English, sdk.FullFundraiserPath, hd.Secp256k1)
Expand Down
8 changes: 4 additions & 4 deletions client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/testutil"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runExportCmd(t *testing.T) {
cmd := ExportKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

// Now add a temporary keybase
kbHome, cleanUp := tests.NewTestCaseDir(t)
kbHome, cleanUp := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp)

// create a key
Expand All @@ -31,7 +31,7 @@ func Test_runExportCmd(t *testing.T) {
})

path := sdk.GetConfig().GetFullFundraiserPath()
_, err = kb.NewAccount("keyname1", tests.TestMnemonic, "", path, hd.Secp256k1)
_, err = kb.NewAccount("keyname1", testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

// Now enter password
Expand Down
6 changes: 3 additions & 3 deletions client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runImportCmd(t *testing.T) {
cmd := ImportKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

// Now add a temporary keybase
kbHome, cleanUp := tests.NewTestCaseDir(t)
kbHome, cleanUp := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp)

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
Expand Down
10 changes: 5 additions & 5 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runListCmd(t *testing.T) {
cmd := ListKeysCmd()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

kbHome1, cleanUp1 := tests.NewTestCaseDir(t)
kbHome1, cleanUp1 := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp1)

kbHome2, cleanUp2 := tests.NewTestCaseDir(t)
kbHome2, cleanUp2 := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp2)

mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn)
require.NoError(t, err)

path := "" //sdk.GetConfig().GetFullFundraiserPath()
_, err = kb.NewAccount("something", tests.TestMnemonic, "", path, hd.Secp256k1)
_, err = kb.NewAccount("something", testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

t.Cleanup(func() {
Expand Down
6 changes: 3 additions & 3 deletions client/keys/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
)

func Test_runMigrateCmd(t *testing.T) {
Expand All @@ -20,7 +20,7 @@ func Test_runMigrateCmd(t *testing.T) {
cmd.SetOut(ioutil.Discard)
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

kbHome, kbCleanUp := tests.NewTestCaseDir(t)
kbHome, kbCleanUp := testutil.NewTestCaseDir(t)
copy.Copy("testdata", kbHome)
assert.NotNil(t, kbHome)
t.Cleanup(kbCleanUp)
Expand All @@ -35,7 +35,7 @@ func Test_runMigrateCmd(t *testing.T) {

cmd = MigrateCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)

cmd.SetArgs([]string{
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
Expand Down
4 changes: 2 additions & 2 deletions client/keys/mnemonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
)

func Test_RunMnemonicCmdNormal(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func Test_RunMnemonicCmdUser(t *testing.T) {
require.Equal(t, "EOF", err.Error())

// Try again
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(cmd)
mockIn.Reset("Hi!\n")
err = cmd.Execute()
require.Error(t, err)
Expand Down
10 changes: 5 additions & 5 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -38,15 +38,15 @@ func Test_showKeysCmd(t *testing.T) {
func Test_runShowCmd(t *testing.T) {
cmd := ShowKeysCmd()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn, _, _ := testutil.ApplyMockIO(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")

cmd.SetArgs([]string{"invalid1", "invalid2"})
require.EqualError(t, cmd.Execute(), "invalid1 is not a valid name or address: decoding bech32 failed: invalid index of 1")

kbHome, cleanUp := tests.NewTestCaseDir(t)
kbHome, cleanUp := testutil.NewTestCaseDir(t)
t.Cleanup(cleanUp)

fakeKeyName1 := "runShowCmd_Key1"
Expand All @@ -60,11 +60,11 @@ func Test_runShowCmd(t *testing.T) {
})

path := hd.NewFundraiserParams(1, sdk.CoinType, 0).String()
_, err = kb.NewAccount(fakeKeyName1, tests.TestMnemonic, "", path, hd.Secp256k1)
_, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

path2 := hd.NewFundraiserParams(1, sdk.CoinType, 1).String()
_, err = kb.NewAccount(fakeKeyName2, tests.TestMnemonic, "", path2, hd.Secp256k1)
_, err = kb.NewAccount(fakeKeyName2, testutil.TestMnemonic, "", path2, hd.Secp256k1)
require.NoError(t, err)

// Now try single key
Expand Down
8 changes: 4 additions & 4 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"errors"
"testing"

"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/x/auth/signing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestBuildUnsignedTx(t *testing.T) {
}

func TestSign(t *testing.T) {
dir, clean := tests.NewTestCaseDir(t)
dir, clean := testutil.NewTestCaseDir(t)
t.Cleanup(clean)

path := hd.CreateHDPath(118, 0, 0).String()
Expand Down
6 changes: 3 additions & 3 deletions client/verifier_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package client_test

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil"
)

func TestCreateVerifier(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "example")
require.NoError(t, err)
tmpDir, cleanup := testutil.NewTestCaseDir(t)
t.Cleanup(cleanup)

testCases := []struct {
name string
Expand Down
6 changes: 3 additions & 3 deletions crypto/keyring/keyring_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestInMemoryCreateLedger(t *testing.T) {
// TestSignVerify does some detailed checks on how we sign and validate
// signatures
func TestSignVerifyKeyRingWithLedger(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
dir, cleanup := testutil.NewTestCaseDir(t)
t.Cleanup(cleanup)
kb, err := New("keybasename", "test", dir, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestSignVerifyKeyRingWithLedger(t *testing.T) {
}

func TestAltKeyring_SaveLedgerKey(t *testing.T) {
dir, clean := tests.NewTestCaseDir(t)
dir, clean := testutil.NewTestCaseDir(t)
t.Cleanup(clean)

keyring, err := New(t.Name(), BackendTest, dir, nil)
Expand Down
Loading

0 comments on commit a940214

Please sign in to comment.