Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.TempDir to create temporary test directory #3500

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions ioctl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ package ioctl
import (
"context"
"os"
"path"
"strings"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"

"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/testutil"
)

func TestStop(t *testing.T) {
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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))
Expand Down
10 changes: 1 addition & 9 deletions ioctl/cmd/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package account
import (
"crypto/ecdsa"
"math/rand"
"os"
"strconv"
"testing"

Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions ioctl/cmd/alias/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 13 additions & 27 deletions ioctl/newcmd/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -269,19 +266,15 @@ 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

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) {
Expand All @@ -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))

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions ioctl/newcmd/account/accountcreateadd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions ioctl/newcmd/account/accountdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package account

import (
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions ioctl/newcmd/account/accountexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package account

import (
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -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) {
Expand All @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions ioctl/newcmd/account/accountexportpublic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package account

import (
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -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) {
Expand All @@ -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()

Expand Down
Loading