From 99f0663a3256e815328fde2dcae33f5a12e038b3 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 6 Jul 2022 14:58:51 +0800 Subject: [PATCH] test: use `T.TempDir` to create temporary test directory (#3500) This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun Co-authored-by: huofei <68298506@qq.com> Co-authored-by: Haaai <55118568+Liuhaai@users.noreply.github.com> --- ioctl/client_test.go | 29 +++----------- ioctl/cmd/account/account_test.go | 10 +---- ioctl/cmd/alias/alias_test.go | 12 ++---- ioctl/newcmd/account/account_test.go | 40 ++++++------------- ioctl/newcmd/account/accountcreateadd_test.go | 4 +- ioctl/newcmd/account/accountdelete_test.go | 6 +-- ioctl/newcmd/account/accountexport_test.go | 6 +-- .../account/accountexportpublic_test.go | 6 +-- ioctl/newcmd/account/accountimport_test.go | 10 ++--- ioctl/newcmd/account/accountlist_test.go | 6 +-- ioctl/newcmd/account/accountsign_test.go | 6 +-- ioctl/newcmd/account/accountupdate_test.go | 10 +---- ioctl/newcmd/config/config_test.go | 9 +---- pkg/recovery/recovery_test.go | 7 +--- 14 files changed, 37 insertions(+), 124 deletions(-) diff --git a/ioctl/client_test.go b/ioctl/client_test.go index 671b61b132..b5e8b59d7a 100644 --- a/ioctl/client_test.go +++ b/ioctl/client_test.go @@ -9,7 +9,6 @@ package ioctl import ( "context" "os" - "path" "strings" "testing" @@ -17,7 +16,6 @@ import ( "gopkg.in/yaml.v2" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/testutil" ) func TestStop(t *testing.T) { @@ -134,7 +132,6 @@ func TestGetAddress(t *testing.T) { for _, test := range tests { r := require.New(t) configFilePath := writeTempConfig(t, &test.cfg) - defer testutil.CleanupPath(path.Dir(configFilePath)) cfgload := loadTempConfig(t, configFilePath) r.Equal(test.cfg, cfgload) @@ -149,9 +146,7 @@ func TestGetAddress(t *testing.T) { func TestNewKeyStore(t *testing.T) { r := require.New(t) - testWallet, err := os.MkdirTemp(os.TempDir(), "testKeyStore") - r.NoError(err) - defer testutil.CleanupPath(testWallet) + testWallet := t.TempDir() c := NewClient(config.Config{ Wallet: testWallet, @@ -178,7 +173,6 @@ func TestAliasMap(t *testing.T) { } configFilePath := writeTempConfig(t, &cfg) - defer testutil.CleanupPath(path.Dir(configFilePath)) cfgload := loadTempConfig(t, configFilePath) r.Equal(cfg, cfgload) @@ -202,7 +196,6 @@ func TestAlias(t *testing.T) { }, } configFilePath := writeTempConfig(t, &cfg) - defer testutil.CleanupPath(path.Dir(configFilePath)) cfgload := loadTempConfig(t, configFilePath) r.Equal(cfg, cfgload) @@ -283,9 +276,7 @@ func TestSetAlias(t *testing.T) { } r := require.New(t) - testPathd, err := os.MkdirTemp(os.TempDir(), "cfgtest") - r.NoError(err) - defer testutil.CleanupPath(testPathd) + testPathd := t.TempDir() for _, test := range tests { configFilePath := testPathd + "/config.default" @@ -349,9 +340,7 @@ func TestDeleteAlias(t *testing.T) { } r := require.New(t) - testPathd, err := os.MkdirTemp(os.TempDir(), "cfgtest") - r.NoError(err) - defer testutil.CleanupPath(testPathd) + testPathd := t.TempDir() for _, test := range tests { configFilePath := testPathd + "/config.default" @@ -367,9 +356,7 @@ func TestDeleteAlias(t *testing.T) { func TestHdwalletMnemonic(t *testing.T) { r := require.New(t) - testPathWallet, err := os.MkdirTemp(os.TempDir(), "cfgWallet") - r.NoError(err) - defer testutil.CleanupPath(testPathWallet) + testPathWallet := t.TempDir() c := NewClient(config.Config{ Wallet: testPathWallet, }, testPathWallet+"/config.default") @@ -383,9 +370,7 @@ func TestHdwalletMnemonic(t *testing.T) { func TestWriteHdWalletConfigFile(t *testing.T) { r := require.New(t) - testPathWallet, err := os.MkdirTemp(os.TempDir(), "cfgWallet") - r.NoError(err) - defer testutil.CleanupPath(testPathWallet) + testPathWallet := t.TempDir() c := NewClient(config.Config{ Wallet: testPathWallet, @@ -397,9 +382,7 @@ func TestWriteHdWalletConfigFile(t *testing.T) { func writeTempConfig(t *testing.T, cfg *config.Config) string { r := require.New(t) - testPathd, err := os.MkdirTemp(os.TempDir(), "testConfig") - r.NoError(err) - configFilePath := testPathd + "/config.default" + configFilePath := t.TempDir() + "/config.default" out, err := yaml.Marshal(cfg) r.NoError(err) r.NoError(os.WriteFile(configFilePath, out, 0600)) diff --git a/ioctl/cmd/account/account_test.go b/ioctl/cmd/account/account_test.go index 5834070280..9eaebf3c66 100644 --- a/ioctl/cmd/account/account_test.go +++ b/ioctl/cmd/account/account_test.go @@ -9,7 +9,6 @@ package account import ( "crypto/ecdsa" "math/rand" - "os" "strconv" "testing" @@ -21,19 +20,12 @@ import ( "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/testutil" -) - -const ( - _testPath = "ksTest" ) func TestAccount(t *testing.T) { r := require.New(t) - testWallet, err := os.MkdirTemp(os.TempDir(), _testPath) - r.NoError(err) - defer testutil.CleanupPath(testWallet) + testWallet := t.TempDir() config.ReadConfig.Wallet = testWallet ks := keystore.NewKeyStore(config.ReadConfig.Wallet, keystore.StandardScryptN, keystore.StandardScryptP) diff --git a/ioctl/cmd/alias/alias_test.go b/ioctl/cmd/alias/alias_test.go index 766ddb86e0..5ec6261293 100644 --- a/ioctl/cmd/alias/alias_test.go +++ b/ioctl/cmd/alias/alias_test.go @@ -15,15 +15,13 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/validator" - "github.com/iotexproject/iotex-core/testutil" ) func TestAlias(t *testing.T) { require := require.New(t) - testPathd, err := testInit() + _, err := testInit(t) require.NoError(err) - defer testutil.CleanupPath(testPathd) raullen := "raullen" qevan := "qevan" @@ -65,11 +63,9 @@ func TestAlias(t *testing.T) { require.Equal(jing, aliases["io1kmpejl35lys5pxcpk74g8am0kwmzwwuvsvqrp8"]) } -func testInit() (string, error) { - testPathd, err := os.MkdirTemp(os.TempDir(), "kstest") - if err != nil { - return testPathd, err - } +func testInit(t *testing.T) (string, error) { + var err error + testPathd := t.TempDir() config.ConfigDir = testPathd config.DefaultConfigFile = config.ConfigDir + "/config.default" config.ReadConfig, err = config.LoadConfig() diff --git a/ioctl/newcmd/account/account_test.go b/ioctl/newcmd/account/account_test.go index 99bc966303..5969736f4f 100644 --- a/ioctl/newcmd/account/account_test.go +++ b/ioctl/newcmd/account/account_test.go @@ -31,7 +31,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/identityset" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) const ( @@ -85,9 +84,8 @@ func TestNewAccountCmd(t *testing.T) { func TestSign(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(keystore.StandardScryptN, keystore.StandardScryptP) + _, ks, passwd, _, err := newTestAccountWithKeyStore(t, keystore.StandardScryptN, keystore.StandardScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -151,9 +149,8 @@ func TestSign(t *testing.T) { func TestAccount(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, nonce, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + testWallet, ks, passwd, nonce, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -269,9 +266,7 @@ func TestMeta(t *testing.T) { func TestAccountError(t *testing.T) { require := require.New(t) - testFilePath, err := os.MkdirTemp(os.TempDir(), _testPath) - require.NoError(err) - defer testutil.CleanupPath(testFilePath) + testFilePath := t.TempDir() alias := "aaa" passwordOfKeyStore := "123456" keyStorePath := testFilePath @@ -279,9 +274,7 @@ func TestAccountError(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mock_ioctlclient.NewMockClient(ctrl) - testWallet, err := os.MkdirTemp(os.TempDir(), _testPath) - require.NoError(err) - defer testutil.CleanupPath(testWallet) + testWallet := t.TempDir() client.EXPECT().DecryptPrivateKey(gomock.Any(), gomock.Any()).DoAndReturn( func(passwordOfKeyStore, keyStorePath string) (*ecdsa.PrivateKey, error) { @@ -291,7 +284,7 @@ func TestAccountError(t *testing.T) { }) cmd := &cobra.Command{} cmd.SetOut(new(bytes.Buffer)) - _, err = newAccountByKeyStore(client, cmd, alias, passwordOfKeyStore, keyStorePath) + _, err := newAccountByKeyStore(client, cmd, alias, passwordOfKeyStore, keyStorePath) require.Error(err) require.Contains(err.Error(), fmt.Sprintf("keystore file \"%s\" read error", keyStorePath)) @@ -318,9 +311,8 @@ func TestAccountError(t *testing.T) { func TestStoreKey(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -386,9 +378,8 @@ func TestStoreKey(t *testing.T) { func TestNewAccount(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + _, ks, passwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -403,9 +394,8 @@ func TestNewAccount(t *testing.T) { func TestNewAccountSm2(t *testing.T) { require := require.New(t) - testWallet, passwd, _, err := newTestAccount() + testWallet, passwd, _, err := newTestAccount(t) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -420,9 +410,8 @@ func TestNewAccountSm2(t *testing.T) { func TestNewAccountByKey(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + _, ks, passwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -439,18 +428,15 @@ func TestNewAccountByKey(t *testing.T) { require.Equal(prvKey.PublicKey().Address().String(), result) } -func newTestAccount() (string, string, string, error) { - testWallet, err := os.MkdirTemp(os.TempDir(), _testPath) - if err != nil { - return testWallet, "", "", err - } +func newTestAccount(t *testing.T) (string, string, string, error) { + testWallet := t.TempDir() nonce := strconv.FormatInt(rand.Int63(), 10) passwd := "3dj,<>@@SF{}rj0ZF#" + nonce return testWallet, passwd, nonce, nil } -func newTestAccountWithKeyStore(scryptN, scryptP int) (string, *keystore.KeyStore, string, string, error) { - testWallet, passwd, nonce, err := newTestAccount() +func newTestAccountWithKeyStore(t *testing.T, scryptN, scryptP int) (string, *keystore.KeyStore, string, string, error) { + testWallet, passwd, nonce, err := newTestAccount(t) if err != nil { return testWallet, nil, "", "", err } diff --git a/ioctl/newcmd/account/accountcreateadd_test.go b/ioctl/newcmd/account/accountcreateadd_test.go index 7f16e962b4..7d6c8bc679 100644 --- a/ioctl/newcmd/account/accountcreateadd_test.go +++ b/ioctl/newcmd/account/accountcreateadd_test.go @@ -17,7 +17,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountCreateAdd(t *testing.T) { @@ -26,9 +25,8 @@ func TestNewAccountCreateAdd(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testWallet, ks, pwd, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + testWallet, ks, pwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) client.EXPECT().ReadSecret().Return(pwd, nil).Times(4) client.EXPECT().AskToConfirm(gomock.Any()).Return(true).Times(2) diff --git a/ioctl/newcmd/account/accountdelete_test.go b/ioctl/newcmd/account/accountdelete_test.go index 7a1ad4b2d8..c25e8a448b 100644 --- a/ioctl/newcmd/account/accountdelete_test.go +++ b/ioctl/newcmd/account/accountdelete_test.go @@ -7,7 +7,6 @@ package account import ( - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -19,7 +18,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountDelete(t *testing.T) { @@ -30,9 +28,7 @@ func TestNewAccountDelete(t *testing.T) { client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(30) - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountDelete") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() t.Run("CryptoSm2 is false", func(t *testing.T) { client.EXPECT().IsCryptoSm2().Return(false).Times(2) diff --git a/ioctl/newcmd/account/accountexport_test.go b/ioctl/newcmd/account/accountexport_test.go index a616525644..c2713e9d87 100644 --- a/ioctl/newcmd/account/accountexport_test.go +++ b/ioctl/newcmd/account/accountexport_test.go @@ -7,7 +7,6 @@ package account import ( - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -19,7 +18,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountExport(t *testing.T) { @@ -28,9 +26,7 @@ func TestNewAccountExport(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountExport") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) client.EXPECT().NewKeyStore().Return(ks).AnyTimes() diff --git a/ioctl/newcmd/account/accountexportpublic_test.go b/ioctl/newcmd/account/accountexportpublic_test.go index 4ddf36bee2..69f0bff492 100644 --- a/ioctl/newcmd/account/accountexportpublic_test.go +++ b/ioctl/newcmd/account/accountexportpublic_test.go @@ -7,7 +7,6 @@ package account import ( - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -19,7 +18,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountExportPublic(t *testing.T) { @@ -28,9 +26,7 @@ func TestNewAccountExportPublic(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountExportPublic") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) client.EXPECT().NewKeyStore().Return(ks).AnyTimes() diff --git a/ioctl/newcmd/account/accountimport_test.go b/ioctl/newcmd/account/accountimport_test.go index 05f674da27..30dfcaf967 100644 --- a/ioctl/newcmd/account/accountimport_test.go +++ b/ioctl/newcmd/account/accountimport_test.go @@ -18,7 +18,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountImportCmd(t *testing.T) { @@ -38,9 +37,8 @@ func TestNewAccountImportCmd(t *testing.T) { func TestNewAccountImportKeyCmd(t *testing.T) { require := require.New(t) - testWallet, ks, _, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + testWallet, ks, _, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -63,9 +61,8 @@ func TestNewAccountImportKeyCmd(t *testing.T) { func TestNewAccountImportKeyStoreCmd(t *testing.T) { require := require.New(t) - testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(veryLightScryptN, veryLightScryptP) + testWallet, ks, passwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -93,9 +90,8 @@ func TestNewAccountImportKeyStoreCmd(t *testing.T) { func TestNewAccountImportPemCmd(t *testing.T) { require := require.New(t) - testWallet, passwd, _, err := newTestAccount() + testWallet, passwd, _, err := newTestAccount(t) require.NoError(err) - defer testutil.CleanupPath(testWallet) ctrl := gomock.NewController(t) defer ctrl.Finish() diff --git a/ioctl/newcmd/account/accountlist_test.go b/ioctl/newcmd/account/accountlist_test.go index 3dc531adc0..1794a486aa 100644 --- a/ioctl/newcmd/account/accountlist_test.go +++ b/ioctl/newcmd/account/accountlist_test.go @@ -8,7 +8,6 @@ package account import ( "errors" - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -19,7 +18,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountList(t *testing.T) { @@ -30,9 +28,7 @@ func TestNewAccountList(t *testing.T) { t.Run("When NewAccountList returns no error", func(t *testing.T) { client.EXPECT().IsCryptoSm2().Return(false) - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountList") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) genAccount := func(passwd string) string { diff --git a/ioctl/newcmd/account/accountsign_test.go b/ioctl/newcmd/account/accountsign_test.go index 7685bacca2..0c3d6fb66a 100644 --- a/ioctl/newcmd/account/accountsign_test.go +++ b/ioctl/newcmd/account/accountsign_test.go @@ -7,7 +7,6 @@ package account import ( - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -18,7 +17,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountSign(t *testing.T) { @@ -27,9 +25,7 @@ func TestNewAccountSign(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountSign") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) client.EXPECT().NewKeyStore().Return(ks).AnyTimes() diff --git a/ioctl/newcmd/account/accountupdate_test.go b/ioctl/newcmd/account/accountupdate_test.go index 788391b346..75e4eb04ab 100644 --- a/ioctl/newcmd/account/accountupdate_test.go +++ b/ioctl/newcmd/account/accountupdate_test.go @@ -8,7 +8,6 @@ package account import ( "fmt" - "os" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -20,7 +19,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" - "github.com/iotexproject/iotex-core/testutil" ) func TestNewAccountUpdate_FindKeystore(t *testing.T) { @@ -29,9 +27,7 @@ func TestNewAccountUpdate_FindKeystore(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountUpdate") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) client.EXPECT().NewKeyStore().Return(ks).AnyTimes() const pwd = "test" @@ -77,9 +73,7 @@ func TestNewAccountUpdate_FindPemFile(t *testing.T) { client := mock_ioctlclient.NewMockClient(ctrl) client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() - testAccountFolder, err := os.MkdirTemp(os.TempDir(), "testNewAccountUpdate_FindPemFile") - require.NoError(err) - defer testutil.CleanupPath(testAccountFolder) + testAccountFolder := t.TempDir() ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) client.EXPECT().NewKeyStore().Return(ks).AnyTimes() const pwd = "test" diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index a3bf3a2e22..ef28f8d919 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -7,22 +7,15 @@ package config import ( - "os" "path/filepath" "testing" "github.com/stretchr/testify/require" - - "github.com/iotexproject/iotex-core/testutil" ) func TestInitConfig(t *testing.T) { require := require.New(t) - testPath, err := os.MkdirTemp(os.TempDir(), "testCfg") - require.NoError(err) - defer func() { - testutil.CleanupPath(testPath) - }() + testPath := t.TempDir() _configDir = testPath cfg, cfgFilePath, err := InitConfig() require.NoError(err) diff --git a/pkg/recovery/recovery_test.go b/pkg/recovery/recovery_test.go index 9351bd0dec..11fb16a2ff 100644 --- a/pkg/recovery/recovery_test.go +++ b/pkg/recovery/recovery_test.go @@ -2,19 +2,14 @@ package recovery import ( "errors" - "os" "testing" "github.com/stretchr/testify/require" - - "github.com/iotexproject/iotex-core/testutil" ) func TestCrashLog(t *testing.T) { require := require.New(t) - heapdumpDir, err := os.MkdirTemp(os.TempDir(), "heapdump") - require.NoError(err) - defer testutil.CleanupPath(heapdumpDir) + heapdumpDir := t.TempDir() require.NoError(SetCrashlogDir(heapdumpDir)) t.Run("index out of range", func(t *testing.T) {