From a940214a4923a3bf9a9161cd14bd3072299cd0c9 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 9 Jul 2020 14:21:20 +0200 Subject: [PATCH] testutil cleanup and reorg (#6658) 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. --- client/keys/add_ledger_test.go | 10 ++--- client/keys/add_test.go | 6 +-- client/keys/delete_test.go | 8 ++-- client/keys/export_test.go | 8 ++-- client/keys/import_test.go | 6 +-- client/keys/list_test.go | 10 ++--- client/keys/migrate_test.go | 6 +-- client/keys/mnemonic_test.go | 4 +- client/keys/show_test.go | 10 ++--- client/tx/tx_test.go | 8 ++-- client/verifier_test.go | 6 +-- crypto/keyring/keyring_ledger_test.go | 6 +-- crypto/keyring/keyring_test.go | 52 ++++++++++++------------ crypto/keyring/legacy_test.go | 6 +-- crypto/ledger_mock.go | 6 +-- crypto/ledger_test.go | 22 +++++------ server/constructors_test.go | 6 +-- server/export_test.go | 4 +- server/init_test.go | 6 +-- tests/io.go | 35 ---------------- tests/util.go | 13 ------ testutil/ioutil.go | 46 +++++++++++++++++++++ testutil/ioutil_test.go | 55 ++++++++++++++++++++++++++ {tests => testutil}/known_values.go | 2 +- testutil/{ => network}/doc.go | 10 ++--- testutil/{ => network}/network.go | 4 +- testutil/{ => network}/network_test.go | 8 ++-- testutil/{ => network}/util.go | 2 +- version/version_test.go | 4 +- x/auth/client/cli/broadcast_test.go | 4 +- x/auth/client/cli/cli_test.go | 39 +++++++++--------- x/auth/client/cli/encode_test.go | 4 +- x/auth/client/tx_test.go | 17 ++------ x/bank/client/cli/cli_test.go | 10 ++--- x/bank/client/rest/query_test.go | 10 ++--- x/bank/client/rest/tx_test.go | 6 +-- x/crisis/client/cli/cli_test.go | 10 ++--- x/distribution/client/cli/tx_test.go | 8 ++-- x/genutil/client/cli/init_test.go | 12 +++--- x/genutil/client/cli/migrate_test.go | 4 +- x/genutil/utils_test.go | 4 +- x/gov/client/cli/parse_test.go | 15 +++---- x/params/client/cli/tx_test.go | 8 ++-- x/upgrade/types/storeloader_test.go | 4 +- 44 files changed, 281 insertions(+), 243 deletions(-) delete mode 100644 tests/io.go create mode 100644 testutil/ioutil.go create mode 100644 testutil/ioutil_test.go rename {tests => testutil}/known_values.go (90%) rename testutil/{ => network}/doc.go (89%) rename testutil/{ => network}/network.go (99%) rename testutil/{ => network}/network_test.go (82%) rename testutil/{ => network}/util.go (99%) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 3820e418d..fae479bfe 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -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" ) @@ -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) @@ -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()) @@ -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) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 0b6af85b1..1a67f79b2 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -11,7 +11,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" ) @@ -19,9 +19,9 @@ 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) diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index ede5e72db..6bafefeef 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -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) @@ -27,7 +27,7 @@ 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() @@ -35,7 +35,7 @@ func Test_runDeleteCmd(t *testing.T) { 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) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 937582435..b64fb12af 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -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 @@ -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 diff --git a/client/keys/import_test.go b/client/keys/import_test.go index 88de08be6..835319bb5 100644 --- a/client/keys/import_test.go +++ b/client/keys/import_test.go @@ -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) diff --git a/client/keys/list_test.go b/client/keys/list_test.go index ad0bcafa9..bcde277aa 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -10,7 +10,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" ) @@ -18,18 +18,18 @@ 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() { diff --git a/client/keys/migrate_test.go b/client/keys/migrate_test.go index 6f36bfc9f..e779ffa03 100644 --- a/client/keys/migrate_test.go +++ b/client/keys/migrate_test.go @@ -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) { @@ -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) @@ -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), diff --git a/client/keys/mnemonic_test.go b/client/keys/mnemonic_test.go index c333657f9..359cd8fe9 100644 --- a/client/keys/mnemonic_test.go +++ b/client/keys/mnemonic_test.go @@ -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) { @@ -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) diff --git a/client/keys/show_test.go b/client/keys/show_test.go index abc6d1de5..024ab605d 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -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" ) @@ -38,7 +38,7 @@ 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") @@ -46,7 +46,7 @@ func Test_runShowCmd(t *testing.T) { 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" @@ -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 diff --git a/client/tx/tx_test.go b/client/tx/tx_test.go index dd95e2df4..b129e6187 100644 --- a/client/tx/tx_test.go +++ b/client/tx/tx_test.go @@ -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" @@ -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() diff --git a/client/verifier_test.go b/client/verifier_test.go index 029577f48..1088d6186 100644 --- a/client/verifier_test.go +++ b/client/verifier_test.go @@ -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 diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index 84ca7e572..e94ba395a 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -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" ) @@ -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) @@ -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) diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index 55646c1e3..1daf0164f 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "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" ) @@ -30,7 +30,7 @@ func init() { } func TestNewKeyring(t *testing.T) { - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) mockIn := strings.NewReader("") t.Cleanup(cleanup) kr, err := New("cosmos", BackendFile, dir, mockIn) @@ -48,7 +48,7 @@ func TestNewKeyring(t *testing.T) { } func TestKeyManagementKeyRing(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) @@ -130,7 +130,7 @@ func TestKeyManagementKeyRing(t *testing.T) { } func TestSignVerifyKeyRing(t *testing.T) { - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) kb, err := New("keybasename", "test", dir, nil) @@ -209,7 +209,7 @@ func TestSignVerifyKeyRing(t *testing.T) { } func TestExportImportKeyRing(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) @@ -243,7 +243,7 @@ func TestExportImportKeyRing(t *testing.T) { } func TestExportImportPubKeyKeyRing(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) @@ -283,7 +283,7 @@ func TestExportImportPubKeyKeyRing(t *testing.T) { } func TestAdvancedKeyManagementKeyRing(t *testing.T) { - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) kb, err := New("keybasename", "test", dir, nil) @@ -319,7 +319,7 @@ func TestAdvancedKeyManagementKeyRing(t *testing.T) { } func TestSeedPhraseKeyRing(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) @@ -350,7 +350,7 @@ func TestSeedPhraseKeyRing(t *testing.T) { } func TestKeyringKeybaseExportImportPrivKey(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) @@ -729,7 +729,7 @@ func TestInMemorySeedPhrase(t *testing.T) { } func TestKeyChain_ShouldFailWhenAddingSameGeneratedAccount(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) kr, err := New(t.Name(), BackendTest, dir, nil) @@ -801,7 +801,7 @@ func ExampleNew() { } func TestAltKeyring_List(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -835,7 +835,7 @@ func TestAltKeyring_List(t *testing.T) { } func TestAltKeyring_NewAccount(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -864,7 +864,7 @@ func TestAltKeyring_NewAccount(t *testing.T) { } func TestAltKeyring_Get(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -880,7 +880,7 @@ func TestAltKeyring_Get(t *testing.T) { } func TestAltKeyring_KeyByAddress(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -896,7 +896,7 @@ func TestAltKeyring_KeyByAddress(t *testing.T) { } func TestAltKeyring_Delete(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -919,7 +919,7 @@ func TestAltKeyring_Delete(t *testing.T) { } func TestAltKeyring_DeleteByAddress(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -942,7 +942,7 @@ func TestAltKeyring_DeleteByAddress(t *testing.T) { } func TestAltKeyring_SavePubKey(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -968,7 +968,7 @@ func TestAltKeyring_SavePubKey(t *testing.T) { } func TestAltKeyring_SaveMultisig(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -993,7 +993,7 @@ func TestAltKeyring_SaveMultisig(t *testing.T) { } func TestAltKeyring_Sign(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1012,7 +1012,7 @@ func TestAltKeyring_Sign(t *testing.T) { } func TestAltKeyring_SignByAddress(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1031,7 +1031,7 @@ func TestAltKeyring_SignByAddress(t *testing.T) { } func TestAltKeyring_ImportExportPrivKey(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1060,7 +1060,7 @@ func TestAltKeyring_ImportExportPrivKey(t *testing.T) { } func TestAltKeyring_ImportExportPrivKey_ByAddress(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1090,7 +1090,7 @@ func TestAltKeyring_ImportExportPrivKey_ByAddress(t *testing.T) { } func TestAltKeyring_ImportExportPubKey(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1115,7 +1115,7 @@ func TestAltKeyring_ImportExportPubKey(t *testing.T) { } func TestAltKeyring_ImportExportPubKey_ByAddress(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1140,7 +1140,7 @@ func TestAltKeyring_ImportExportPubKey_ByAddress(t *testing.T) { } func TestAltKeyring_ConstructorSupportedAlgos(t *testing.T) { - dir, clean := tests.NewTestCaseDir(t) + dir, clean := testutil.NewTestCaseDir(t) t.Cleanup(clean) keyring, err := New(t.Name(), BackendTest, dir, nil) @@ -1155,7 +1155,7 @@ func TestAltKeyring_ConstructorSupportedAlgos(t *testing.T) { require.NoError(t, err) // but we can create a new keybase with our provided algos. - dir2, clean2 := tests.NewTestCaseDir(t) + dir2, clean2 := testutil.NewTestCaseDir(t) t.Cleanup(clean2) keyring2, err := New(t.Name(), BackendTest, dir2, nil, func(options *Options) { diff --git a/crypto/keyring/legacy_test.go b/crypto/keyring/legacy_test.go index 11ea30bf0..38457a327 100644 --- a/crypto/keyring/legacy_test.go +++ b/crypto/keyring/legacy_test.go @@ -9,11 +9,11 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestNewLegacyKeyBase(t *testing.T) { - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) kb, err := keyring.NewLegacy("keybasename", dir) require.NoError(t, err) @@ -21,7 +21,7 @@ func TestNewLegacyKeyBase(t *testing.T) { } func TestLegacyKeybase(t *testing.T) { - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) // Backup testdata diff --git a/crypto/ledger_mock.go b/crypto/ledger_mock.go index 20a45c158..2acb3c43e 100644 --- a/crypto/ledger_mock.go +++ b/crypto/ledger_mock.go @@ -15,7 +15,7 @@ import ( bip39 "github.com/cosmos/go-bip39" "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -46,7 +46,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("Invalid derivation path") } - seed, err := bip39.NewSeedWithErrorChecking(tests.TestMnemonic, "") + seed, err := bip39.NewSeedWithErrorChecking(testutil.TestMnemonic, "") if err != nil { return nil, err } @@ -87,7 +87,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3 func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message []byte) ([]byte, error) { path := hd.NewParams(derivationPath[0], derivationPath[1], derivationPath[2], derivationPath[3] != 0, derivationPath[4]) - seed, err := bip39.NewSeedWithErrorChecking(tests.TestMnemonic, "") + seed, err := bip39.NewSeedWithErrorChecking(testutil.TestMnemonic, "") if err != nil { return nil, err } diff --git a/crypto/ledger_test.go b/crypto/ledger_test.go index 7408543b3..b22db9555 100644 --- a/crypto/ledger_test.go +++ b/crypto/ledger_test.go @@ -10,7 +10,7 @@ import ( cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -30,16 +30,16 @@ func TestPublicKeyUnsafe(t *testing.T) { require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", fmt.Sprintf("%x", priv.PubKey().Bytes()), - "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) addr := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", - addr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) } func TestPublicKeyUnsafeHDPath(t *testing.T) { @@ -78,7 +78,7 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedAnswers[i], pubKeyAddr, - "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) // Store and restore serializedPk := priv.Bytes() @@ -108,15 +108,15 @@ func TestPublicKeySafe(t *testing.T) { require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", fmt.Sprintf("%x", priv.PubKey().Bytes()), - "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", - addr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) addr2 := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, addr, addr2) @@ -167,7 +167,7 @@ func TestPublicKeyHDPath(t *testing.T) { require.Equal(t, addr2, addr) require.Equal(t, expectedAddrs[i], addr, - "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) // Check other methods require.NoError(t, priv.(PrivKeyLedgerSecp256k1).ValidateKey()) @@ -178,7 +178,7 @@ func TestPublicKeyHDPath(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedPubKeys[i], pubKeyAddr, - "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) // Store and restore serializedPk := priv.Bytes() @@ -220,7 +220,7 @@ func TestSignaturesHD(t *testing.T) { require.Nil(t, err) valid := pub.VerifyBytes(msg, sig) - require.True(t, valid, "Is your device using test mnemonic: %s ?", tests.TestMnemonic) + require.True(t, valid, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) } } diff --git a/server/constructors_test.go b/server/constructors_test.go index e11520fe5..694f9f538 100644 --- a/server/constructors_test.go +++ b/server/constructors_test.go @@ -6,12 +6,12 @@ import ( "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" ) func Test_openDB(t *testing.T) { t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) _, err := openDB(dir) require.NoError(t, err) @@ -19,7 +19,7 @@ func Test_openDB(t *testing.T) { func Test_openTraceWriter(t *testing.T) { t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) fname := filepath.Join(dir, "logfile") w, err := openTraceWriter(fname) diff --git a/server/export_test.go b/server/export_test.go index 96f14b139..5e600ef1c 100644 --- a/server/export_test.go +++ b/server/export_test.go @@ -21,13 +21,13 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/genutil" ) func TestExportCmd_ConsensusParams(t *testing.T) { - tempDir, clean := tests.NewTestCaseDir(t) + tempDir, clean := testutil.NewTestCaseDir(t) defer clean() err := createConfigFolder(tempDir) diff --git a/server/init_test.go b/server/init_test.go index c2b9b469d..2333d6d5a 100644 --- a/server/init_test.go +++ b/server/init_test.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/types" ) @@ -25,7 +25,7 @@ func TestGenerateCoinKey(t *testing.T) { func TestGenerateSaveCoinKey(t *testing.T) { t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) kb, err := keyring.New(t.Name(), "test", dir, nil) @@ -47,7 +47,7 @@ func TestGenerateSaveCoinKey(t *testing.T) { func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) { t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) kb, err := keyring.New(t.Name(), "test", dir, nil) diff --git a/tests/io.go b/tests/io.go deleted file mode 100644 index a51398571..000000000 --- a/tests/io.go +++ /dev/null @@ -1,35 +0,0 @@ -package tests - -import ( - "bytes" - "io/ioutil" - "os" - "strings" - - "github.com/spf13/cobra" - "github.com/stretchr/testify/require" -) - -// 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 -} - -// Write the given string to a new temporary file -func WriteToNewTempFile(t require.TestingT, s string) (*os.File, func()) { - fp, err := ioutil.TempFile(os.TempDir(), "cosmos_cli_test_") - require.Nil(t, err) - - _, err = fp.WriteString(s) - require.Nil(t, err) - - return fp, func() { os.Remove(fp.Name()) } -} - -// DONTCOVER diff --git a/tests/util.go b/tests/util.go index c588216e8..129c5d12a 100644 --- a/tests/util.go +++ b/tests/util.go @@ -2,13 +2,9 @@ package tests import ( "fmt" - "io/ioutil" "net/http" - "os" - "testing" "time" - "github.com/stretchr/testify/require" rpchttp "github.com/tendermint/tendermint/rpc/client/http" ctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -115,15 +111,6 @@ func WaitForStart(url string) { panic(err) } -// NewTestCaseDir creates a new temporary directory for a test case. -// Returns the directory path and a cleanup function. -// nolint: errcheck -func NewTestCaseDir(t testing.TB) (string, func()) { - dir, err := ioutil.TempDir("", t.Name()+"_") - require.NoError(t, err) - return dir, func() { os.RemoveAll(dir) } -} - var cdc = codec.New() func init() { diff --git a/testutil/ioutil.go b/testutil/ioutil.go new file mode 100644 index 000000000..ab0ccaec5 --- /dev/null +++ b/testutil/ioutil.go @@ -0,0 +1,46 @@ +package testutil + +import ( + "bytes" + "io/ioutil" + "os" + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/require" +) + +// NewTestCaseDir creates a new temporary directory for a test case. +// Returns the directory path and a cleanup function. +// nolint: errcheck +func NewTestCaseDir(t testing.TB) (string, func()) { + dir, err := ioutil.TempDir("", t.Name()+"_") + require.NoError(t, err) + return dir, func() { os.RemoveAll(dir) } +} + +// 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 +} + +// Write the given string to a new temporary file. +// Returns an open file and a clean up function that +// the caller must call to remove the file when it is +// no longer needed. +func WriteToNewTempFile(t testing.TB, s string) (*os.File, func()) { + fp, err := ioutil.TempFile("", t.Name()+"_") + require.Nil(t, err) + + _, err = fp.WriteString(s) + require.Nil(t, err) + + return fp, func() { os.Remove(fp.Name()) } +} diff --git a/testutil/ioutil_test.go b/testutil/ioutil_test.go new file mode 100644 index 000000000..d66f301bb --- /dev/null +++ b/testutil/ioutil_test.go @@ -0,0 +1,55 @@ +package testutil_test + +import ( + "io/ioutil" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/testutil" +) + +func TestNewTestCaseDir(t *testing.T) { + dir1, cleanup1 := testutil.NewTestCaseDir(t) + dir2, cleanup2 := testutil.NewTestCaseDir(t) + + require.NotEqual(t, dir1, dir2) + require.DirExists(t, dir1) + require.DirExists(t, dir2) + + cleanup1() + + require.NoDirExists(t, dir1) + require.DirExists(t, dir2) + + cleanup2() + require.NoDirExists(t, dir2) +} + +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) +} + +func TestWriteToNewTempFile(t *testing.T) { + tempfile, cleanup := testutil.WriteToNewTempFile(t, "test string") + tempfile.Close() + + bs, err := ioutil.ReadFile(tempfile.Name()) + require.NoError(t, err) + require.Equal(t, "test string", string(bs)) + + cleanup() + + require.NoFileExists(t, tempfile.Name()) +} diff --git a/tests/known_values.go b/testutil/known_values.go similarity index 90% rename from tests/known_values.go rename to testutil/known_values.go index eb2063fa1..a87ccd2af 100644 --- a/tests/known_values.go +++ b/testutil/known_values.go @@ -1,4 +1,4 @@ -package tests +package testutil const ( // Tests expect a ledger device initialized to the following mnemonic diff --git a/testutil/doc.go b/testutil/network/doc.go similarity index 89% rename from testutil/doc.go rename to testutil/network/doc.go index 3035d4e1a..9e44f371a 100644 --- a/testutil/doc.go +++ b/testutil/network/doc.go @@ -1,14 +1,14 @@ /* -Package testutil implements and exposes a fully operational in-process Tendermint +Package network implements and exposes a fully operational in-process Tendermint test network that consists of at least one or potentially many validators. This test network can be used primarily for integration tests or unit test suites. -The testnetwork utilizes SimApp as the ABCI application and uses all the modules +The test network utilizes SimApp as the ABCI application and uses all the modules defined in the Cosmos SDK. An in-process test network can be configured with any number of validators as well as account funds and even custom genesis state. When creating a test network, a series of Validator objects are returned. Each -Validator object has useful information such as their address and pubkey. A +Validator object has useful information such as their address and public key. A Validator will also provide its RPC, P2P, and API addresses that can be useful for integration testing. In addition, a Tendermint local RPC client is also provided which can be handy for making direct RPC calls to Tendermint. @@ -35,7 +35,7 @@ A typical testing flow might look like the following: cfg.NumValidators = 1 s.cfg = cfg - s.network = testutil.NewTestNetwork(s.T(), cfg) + s.network = testutil.New(s.T(), cfg) _, err := s.network.WaitForHeight(1) s.Require().NoError(err) @@ -62,4 +62,4 @@ A typical testing flow might look like the following: suite.Run(t, new(IntegrationTestSuite)) } */ -package testutil +package network diff --git a/testutil/network.go b/testutil/network/network.go similarity index 99% rename from testutil/network.go rename to testutil/network/network.go index d2e820257..e029878bc 100644 --- a/testutil/network.go +++ b/testutil/network/network.go @@ -1,4 +1,4 @@ -package testutil +package network import ( "bufio" @@ -144,7 +144,7 @@ type ( } ) -func NewTestNetwork(t *testing.T, cfg Config) *Network { +func New(t *testing.T, cfg Config) *Network { // only one caller/test can create and use a network at a time t.Log("acquiring test network lock") lock.Lock() diff --git a/testutil/network_test.go b/testutil/network/network_test.go similarity index 82% rename from testutil/network_test.go rename to testutil/network/network_test.go index 12f2338cd..e7b248e31 100644 --- a/testutil/network_test.go +++ b/testutil/network/network_test.go @@ -1,22 +1,24 @@ -package testutil +package network_test import ( "testing" "time" "github.com/stretchr/testify/suite" + + "github.com/cosmos/cosmos-sdk/testutil/network" ) type IntegrationTestSuite struct { suite.Suite - network *Network + network *network.Network } func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - s.network = NewTestNetwork(s.T(), DefaultConfig()) + s.network = network.New(s.T(), network.DefaultConfig()) s.Require().NotNil(s.network) _, err := s.network.WaitForHeight(1) diff --git a/testutil/util.go b/testutil/network/util.go similarity index 99% rename from testutil/util.go rename to testutil/network/util.go index c981e52cf..9a03a94fd 100644 --- a/testutil/util.go +++ b/testutil/network/util.go @@ -1,4 +1,4 @@ -package testutil +package network import ( "path/filepath" diff --git a/version/version_test.go b/version/version_test.go index 12e9deb0b..9eac0f3b5 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/cli" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestNewInfo(t *testing.T) { @@ -40,7 +40,7 @@ go version go1.14 linux/amd64` func Test_runVersionCmd(t *testing.T) { cmd := NewVersionCommand() - _, mockOut, _ := tests.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 db8a66421..7ddfde4a4 100644 --- a/x/auth/client/cli/broadcast_test.go +++ b/x/auth/client/cli/broadcast_test.go @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestGetBroadcastCommand_OfflineFlag(t *testing.T) { @@ -31,7 +31,7 @@ func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) { clientCtx = clientCtx.WithTxGenerator(simappparams.MakeEncodingConfig().TxGenerator) cmd := GetBroadcastCommand(clientCtx) - testDir, cleanFunc := tests.NewTestCaseDir(t) + testDir, cleanFunc := testutil.NewTestCaseDir(t) t.Cleanup(cleanFunc) // Create new file with tx diff --git a/x/auth/client/cli/cli_test.go b/x/auth/client/cli/cli_test.go index eb2403b4f..5b0ea986b 100644 --- a/x/auth/client/cli/cli_test.go +++ b/x/auth/client/cli/cli_test.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/tests" "github.com/cosmos/cosmos-sdk/tests/cli" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/client/testutil" bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/testutil" @@ -37,7 +38,7 @@ func TestCLIValidateSignatures(t *testing.T) { require.Empty(t, stderr) // write unsigned tx to file - unsignedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + unsignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // validate we can successfully sign @@ -51,7 +52,7 @@ func TestCLIValidateSignatures(t *testing.T) { require.Equal(t, fooAddr.String(), stdTx.GetSigners()[0].String()) // write signed tx to file - signedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + signedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // validate signatures @@ -61,7 +62,7 @@ func TestCLIValidateSignatures(t *testing.T) { // modify the transaction stdTx.Memo = "MODIFIED-ORIGINAL-TX-BAD" bz := cli.MarshalStdTx(t, f.Cdc, stdTx) - modSignedTxFile, cleanup := tests.WriteToNewTempFile(t, string(bz)) + modSignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, string(bz)) t.Cleanup(cleanup) // validate signature validation failure due to different transaction sig bytes @@ -87,7 +88,7 @@ func TestCLISignBatch(t *testing.T) { require.Empty(t, stderr) // Write the output to disk - batchfile, cleanup1 := tests.WriteToNewTempFile(t, strings.Repeat(generatedStdTx, 3)) + batchfile, cleanup1 := sdktestutil.WriteToNewTempFile(t, strings.Repeat(generatedStdTx, 3)) t.Cleanup(cleanup1) // sign-batch file - offline is set but account-number and sequence are not @@ -107,7 +108,7 @@ func TestCLISignBatch(t *testing.T) { require.Empty(t, stderr) require.Equal(t, 3, len(strings.Split(strings.Trim(stdout, "\n"), "\n"))) - malformedFile, cleanup2 := tests.WriteToNewTempFile(t, fmt.Sprintf("%smalformed", generatedStdTx)) + malformedFile, cleanup2 := sdktestutil.WriteToNewTempFile(t, fmt.Sprintf("%smalformed", generatedStdTx)) t.Cleanup(cleanup2) // sign-batch file @@ -165,7 +166,7 @@ func TestCLISendGenerateSignAndBroadcast(t *testing.T) { require.Equal(t, len(msg.Msgs), 1) // Write the output to disk - unsignedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + unsignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Test validate-signatures @@ -193,7 +194,7 @@ func TestCLISendGenerateSignAndBroadcast(t *testing.T) { require.Equal(t, fooAddr.String(), msg.GetSigners()[0].String()) // Write the output to disk - signedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + signedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Test validate-signatures @@ -247,7 +248,7 @@ func TestCLIMultisignInsufficientCosigners(t *testing.T) { require.True(t, success) // Write the output to disk - unsignedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + unsignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Sign with foo's key @@ -255,7 +256,7 @@ func TestCLIMultisignInsufficientCosigners(t *testing.T) { require.True(t, success) // Write the output to disk - fooSignatureFile, cleanup := tests.WriteToNewTempFile(t, stdout) + fooSignatureFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Multisign, not enough signatures @@ -263,7 +264,7 @@ func TestCLIMultisignInsufficientCosigners(t *testing.T) { require.True(t, success) // Write the output to disk - signedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + signedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Validate the multisignature @@ -298,7 +299,7 @@ func TestCLIEncode(t *testing.T) { require.Empty(t, stderr) // Write it to disk - jsonTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + jsonTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Run the encode command @@ -336,7 +337,7 @@ func TestCLIMultisignSortSignatures(t *testing.T) { require.True(t, success) // Write the output to disk - unsignedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + unsignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Sign with foo's key @@ -344,7 +345,7 @@ func TestCLIMultisignSortSignatures(t *testing.T) { require.True(t, success) // Write the output to disk - fooSignatureFile, cleanup := tests.WriteToNewTempFile(t, stdout) + fooSignatureFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Sign with baz's key @@ -352,7 +353,7 @@ func TestCLIMultisignSortSignatures(t *testing.T) { require.True(t, success) // Write the output to disk - bazSignatureFile, cleanup := tests.WriteToNewTempFile(t, stdout) + bazSignatureFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Multisign, keys in different order @@ -361,7 +362,7 @@ func TestCLIMultisignSortSignatures(t *testing.T) { require.True(t, success) // Write the output to disk - signedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + signedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Validate the multisignature @@ -402,7 +403,7 @@ func TestCLIMultisign(t *testing.T) { require.Empty(t, stderr) // Write the output to disk - unsignedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + unsignedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Sign with foo's key @@ -410,7 +411,7 @@ func TestCLIMultisign(t *testing.T) { require.True(t, success) // Write the output to disk - fooSignatureFile, cleanup := tests.WriteToNewTempFile(t, stdout) + fooSignatureFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Sign with bar's key @@ -418,7 +419,7 @@ func TestCLIMultisign(t *testing.T) { require.True(t, success) // Write the output to disk - barSignatureFile, cleanup := tests.WriteToNewTempFile(t, stdout) + barSignatureFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Multisign @@ -435,7 +436,7 @@ func TestCLIMultisign(t *testing.T) { require.True(t, success) // Write the output to disk - signedTxFile, cleanup := tests.WriteToNewTempFile(t, stdout) + signedTxFile, cleanup := sdktestutil.WriteToNewTempFile(t, stdout) t.Cleanup(cleanup) // Validate the multisignature diff --git a/x/auth/client/cli/encode_test.go b/x/auth/client/cli/encode_test.go index 65c569db0..870b4064c 100644 --- a/x/auth/client/cli/encode_test.go +++ b/x/auth/client/cli/encode_test.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -35,7 +35,7 @@ func TestGetCommandEncode(t *testing.T) { JSONEncoded, err := txGen.TxJSONEncoder()(stdTx) require.NoError(t, err) - txFile, cleanup := tests.WriteToNewTempFile(t, string(JSONEncoded)) + txFile, cleanup := testutil.WriteToNewTempFile(t, string(JSONEncoded)) txFileName := txFile.Name() t.Cleanup(cleanup) diff --git a/x/auth/client/tx_test.go b/x/auth/client/tx_test.go index 0bc4f2fbb..e5d7fd308 100644 --- a/x/auth/client/tx_test.go +++ b/x/auth/client/tx_test.go @@ -3,12 +3,11 @@ package client import ( "encoding/json" "errors" - "io/ioutil" - "os" "strings" "testing" "github.com/cosmos/cosmos-sdk/codec/testdata" + "github.com/cosmos/cosmos-sdk/testutil" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" @@ -139,8 +138,8 @@ func TestReadStdTxFromFile(t *testing.T) { // Write it to the file encodedTx, err := txGen.TxJSONEncoder()(stdTx) require.NoError(t, err) - jsonTxFile := writeToNewTempFile(t, string(encodedTx)) - defer os.Remove(jsonTxFile.Name()) + jsonTxFile, cleanup := testutil.WriteToNewTempFile(t, string(encodedTx)) + t.Cleanup(cleanup) // Read it back decodedTx, err := ReadTxFromFile(clientCtx, jsonTxFile.Name()) @@ -249,16 +248,6 @@ func TestPrepareTxBuilder(t *testing.T) { require.Equal(t, accSeq, bldr.Sequence()) } -func writeToNewTempFile(t *testing.T, data string) *os.File { - fp, err := ioutil.TempFile(os.TempDir(), "client_tx_test") - require.NoError(t, err) - - _, err = fp.WriteString(data) - require.NoError(t, err) - - return fp -} - func makeCodec() *codec.Codec { var cdc = codec.New() sdk.RegisterCodec(cdc) diff --git a/x/bank/client/cli/cli_test.go b/x/bank/client/cli/cli_test.go index 74b9de358..0666b3e75 100644 --- a/x/bank/client/cli/cli_test.go +++ b/x/bank/client/cli/cli_test.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/bank/client/cli" @@ -20,18 +20,18 @@ import ( type IntegrationTestSuite struct { suite.Suite - cfg testutil.Config - network *testutil.Network + cfg network.Config + network *network.Network } func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - cfg := testutil.DefaultConfig() + cfg := network.DefaultConfig() cfg.NumValidators = 1 s.cfg = cfg - s.network = testutil.NewTestNetwork(s.T(), cfg) + s.network = network.New(s.T(), cfg) _, err := s.network.WaitForHeight(1) s.Require().NoError(err) diff --git a/x/bank/client/rest/query_test.go b/x/bank/client/rest/query_test.go index d9206c413..8f9c1241b 100644 --- a/x/bank/client/rest/query_test.go +++ b/x/bank/client/rest/query_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" ) @@ -14,18 +14,18 @@ import ( type IntegrationTestSuite struct { suite.Suite - cfg testutil.Config - network *testutil.Network + cfg network.Config + network *network.Network } func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - cfg := testutil.DefaultConfig() + cfg := network.DefaultConfig() cfg.NumValidators = 1 s.cfg = cfg - s.network = testutil.NewTestNetwork(s.T(), cfg) + s.network = network.New(s.T(), cfg) _, err := s.network.WaitForHeight(1) s.Require().NoError(err) diff --git a/x/bank/client/rest/tx_test.go b/x/bank/client/rest/tx_test.go index 17b394bec..d15599d63 100644 --- a/x/bank/client/rest/tx_test.go +++ b/x/bank/client/rest/tx_test.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/network" "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/rest" @@ -41,7 +41,7 @@ func (s *IntegrationTestSuite) TestCoinSend() { }, stdTx.GetMsgs()) } -func submitSendReq(val *testutil.Validator, req bankrest.SendReq) (authtypes.StdTx, error) { +func submitSendReq(val *network.Validator, req bankrest.SendReq) (authtypes.StdTx, error) { url := fmt.Sprintf("%s/bank/accounts/%s/transfers", val.APIAddress, val.Address) bz, err := val.ClientCtx.JSONMarshaler.MarshalJSON(req) @@ -83,7 +83,7 @@ func generateSendReq(from authtypes.AccountI, amount types.Coins) bankrest.SendR } } -func getAccountInfo(val *testutil.Validator) (authtypes.AccountI, error) { +func getAccountInfo(val *network.Validator) (authtypes.AccountI, error) { url := fmt.Sprintf("%s/auth/accounts/%s", val.APIAddress, val.Address) resp, err := rest.GetRequest(url) diff --git a/x/crisis/client/cli/cli_test.go b/x/crisis/client/cli/cli_test.go index d3a7a2b9f..722ad65e7 100644 --- a/x/crisis/client/cli/cli_test.go +++ b/x/crisis/client/cli/cli_test.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/crisis/client/cli" ) @@ -18,18 +18,18 @@ import ( type IntegrationTestSuite struct { suite.Suite - cfg testutil.Config - network *testutil.Network + cfg network.Config + network *network.Network } func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - cfg := testutil.DefaultConfig() + cfg := network.DefaultConfig() cfg.NumValidators = 1 s.cfg = cfg - s.network = testutil.NewTestNetwork(s.T(), cfg) + s.network = network.New(s.T(), cfg) _, err := s.network.WaitForHeight(1) s.Require().NoError(err) diff --git a/x/distribution/client/cli/tx_test.go b/x/distribution/client/cli/tx_test.go index 7a00e9720..966eb73c6 100644 --- a/x/distribution/client/cli/tx_test.go +++ b/x/distribution/client/cli/tx_test.go @@ -1,10 +1,10 @@ package cli import ( - "io/ioutil" "testing" "github.com/cosmos/cosmos-sdk/codec/testdata" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/stretchr/testify/require" @@ -64,9 +64,7 @@ func Test_splitAndCall_Splitting(t *testing.T) { func TestParseProposal(t *testing.T) { cdc := codec.New() - okJSON, err := ioutil.TempFile("", "proposal") - require.Nil(t, err, "unexpected error") - _, err = okJSON.WriteString(` + okJSON, cleanup := testutil.WriteToNewTempFile(t, ` { "title": "Community Pool Spend", "description": "Pay me some Atoms!", @@ -75,7 +73,7 @@ func TestParseProposal(t *testing.T) { "deposit": "1000stake" } `) - require.NoError(t, err) + t.Cleanup(cleanup) proposal, err := ParseCommunityPoolSpendProposalJSON(cdc, okJSON.Name()) require.NoError(t, err) diff --git a/x/genutil/client/cli/init_test.go b/x/genutil/client/cli/init_test.go index d757d4fa5..79993c8c0 100644 --- a/x/genutil/client/cli/init_test.go +++ b/x/genutil/client/cli/init_test.go @@ -21,7 +21,7 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/mock" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/genutil" @@ -42,7 +42,7 @@ func createDefaultTendermintConfig(rootDir string) (*tmcfg.Config, error) { } func TestInitCmd(t *testing.T) { - home, cleanup := tests.NewTestCaseDir(t) + home, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) logger := log.NewNopLogger() @@ -63,7 +63,7 @@ func TestInitCmd(t *testing.T) { } func setupClientHome(t *testing.T) func() { - clientDir, cleanup := tests.NewTestCaseDir(t) + clientDir, cleanup := testutil.NewTestCaseDir(t) viper.Set(cli.HomeFlag, clientDir) return cleanup } @@ -71,7 +71,7 @@ func setupClientHome(t *testing.T) func() { func TestEmptyState(t *testing.T) { t.Cleanup(setupClientHome(t)) - home, cleanup := tests.NewTestCaseDir(t) + home, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) logger := log.NewNopLogger() @@ -117,7 +117,7 @@ func TestEmptyState(t *testing.T) { } func TestStartStandAlone(t *testing.T) { - home, cleanup := tests.NewTestCaseDir(t) + home, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) t.Cleanup(setupClientHome(t)) @@ -159,7 +159,7 @@ func TestStartStandAlone(t *testing.T) { } func TestInitNodeValidatorFiles(t *testing.T) { - home, cleanup := tests.NewTestCaseDir(t) + home, cleanup := testutil.NewTestCaseDir(t) cfg, err := createDefaultTendermintConfig(home) t.Cleanup(cleanup) nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(cfg) diff --git a/x/genutil/client/cli/migrate_test.go b/x/genutil/client/cli/migrate_test.go index 5602ddc88..5419a35ab 100644 --- a/x/genutil/client/cli/migrate_test.go +++ b/x/genutil/client/cli/migrate_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestGetMigrationCallback(t *testing.T) { @@ -19,7 +19,7 @@ func TestGetMigrationCallback(t *testing.T) { } func TestMigrateGenesis(t *testing.T) { - home, cleanup := tests.NewTestCaseDir(t) + home, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) cdc := makeCodec() diff --git a/x/genutil/utils_test.go b/x/genutil/utils_test.go index 4c93fbd92..bf8cf9f5d 100644 --- a/x/genutil/utils_test.go +++ b/x/genutil/utils_test.go @@ -6,14 +6,14 @@ import ( "testing" "time" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/stretchr/testify/require" ) func TestExportGenesisFileWithTime(t *testing.T) { t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) + dir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) fname := filepath.Join(dir, "genesis.json") diff --git a/x/gov/client/cli/parse_test.go b/x/gov/client/cli/parse_test.go index c1e3b108a..8ac3e70e2 100644 --- a/x/gov/client/cli/parse_test.go +++ b/x/gov/client/cli/parse_test.go @@ -1,18 +1,16 @@ package cli import ( - "io/ioutil" "testing" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/testutil" ) func TestParseSubmitProposalFlags(t *testing.T) { - okJSON, err := ioutil.TempFile("", "proposal") - require.Nil(t, err, "unexpected error") - _, err = okJSON.WriteString(` + okJSON, cleanup1 := testutil.WriteToNewTempFile(t, ` { "title": "Test Proposal", "description": "My awesome proposal", @@ -20,17 +18,16 @@ func TestParseSubmitProposalFlags(t *testing.T) { "deposit": "1000test" } `) - require.NoError(t, err) + t.Cleanup(cleanup1) - badJSON, err := ioutil.TempFile("", "proposal") - require.Nil(t, err, "unexpected error") - badJSON.WriteString("bad json") + badJSON, cleanup2 := testutil.WriteToNewTempFile(t, "bad json") + t.Cleanup(cleanup2) fs := NewCmdSubmitProposal(client.Context{}).Flags() // nonexistent json fs.Set(FlagProposal, "fileDoesNotExist") - _, err = parseSubmitProposalFlags(fs) + _, err := parseSubmitProposalFlags(fs) require.Error(t, err) // invalid json diff --git a/x/params/client/cli/tx_test.go b/x/params/client/cli/tx_test.go index e8973b6ea..9eb662bb2 100644 --- a/x/params/client/cli/tx_test.go +++ b/x/params/client/cli/tx_test.go @@ -1,20 +1,18 @@ package cli import ( - "io/ioutil" "testing" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/x/params/client/utils" ) func TestParseProposal(t *testing.T) { cdc := codec.New() - okJSON, err := ioutil.TempFile("", "proposal") - require.Nil(t, err, "unexpected error") - _, err = okJSON.WriteString(` + okJSON, cleanup := testutil.WriteToNewTempFile(t, ` { "title": "Staking Param Change", "description": "Update max validators", @@ -28,7 +26,7 @@ func TestParseProposal(t *testing.T) { "deposit": "1000stake" } `) - require.NoError(t, err) + t.Cleanup(cleanup) proposal, err := utils.ParseParamChangeProposalJSON(cdc, okJSON.Name()) require.NoError(t, err) diff --git a/x/upgrade/types/storeloader_test.go b/x/upgrade/types/storeloader_test.go index 3280b36e4..a77b7593e 100644 --- a/x/upgrade/types/storeloader_test.go +++ b/x/upgrade/types/storeloader_test.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/store/rootmulti" store "github.com/cosmos/cosmos-sdk/store/types" - "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -66,7 +66,7 @@ func checkStore(t *testing.T, db dbm.DB, ver int64, storeKey string, k, v []byte // Test that LoadLatestVersion actually does. func TestSetLoader(t *testing.T) { // set a temporary home dir - homeDir, cleanup := tests.NewTestCaseDir(t) + homeDir, cleanup := testutil.NewTestCaseDir(t) t.Cleanup(cleanup) upgradeInfoFilePath := filepath.Join(homeDir, "upgrade-info.json")