test: use `T.TempDir` to create temporary test directory (#2746)
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 <engzerjun@gmail.com>
This commit is contained in:
parent
79d9bd71bf
commit
83ad76637a
|
@ -23,13 +23,8 @@ const newTestPassword = "new-test-password"
|
|||
|
||||
func TestVerifyAccountPassword(t *testing.T) {
|
||||
accManager := NewGethManager()
|
||||
keyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(keyStoreDir) //nolint: errcheck
|
||||
|
||||
emptyKeyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts_empty")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(emptyKeyStoreDir) //nolint: errcheck
|
||||
keyStoreDir := t.TempDir()
|
||||
emptyKeyStoreDir := t.TempDir()
|
||||
|
||||
// import account keys
|
||||
utils.Init()
|
||||
|
@ -102,13 +97,11 @@ func TestVerifyAccountPassword(t *testing.T) {
|
|||
// TestVerifyAccountPasswordWithAccountBeforeEIP55 verifies if VerifyAccountPassword
|
||||
// can handle accounts before introduction of EIP55.
|
||||
func TestVerifyAccountPasswordWithAccountBeforeEIP55(t *testing.T) {
|
||||
keyStoreDir, err := os.MkdirTemp("", "status-accounts-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(keyStoreDir) //nolint: errcheck
|
||||
keyStoreDir := t.TempDir()
|
||||
|
||||
// Import keys and make sure one was created before EIP55 introduction.
|
||||
utils.Init()
|
||||
err = utils.ImportTestAccount(keyStoreDir, "test-account3-before-eip55.pk")
|
||||
err := utils.ImportTestAccount(keyStoreDir, "test-account3-before-eip55.pk")
|
||||
require.NoError(t, err)
|
||||
|
||||
accManager := NewGethManager()
|
||||
|
@ -143,8 +136,7 @@ type testAccount struct {
|
|||
func (s *ManagerTestSuite) SetupTest() {
|
||||
s.accManager = NewGethManager()
|
||||
|
||||
keyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts")
|
||||
s.Require().NoError(err)
|
||||
keyStoreDir := s.T().TempDir()
|
||||
s.Require().NoError(s.accManager.InitKeystore(keyStoreDir))
|
||||
s.keydir = keyStoreDir
|
||||
|
||||
|
@ -171,10 +163,6 @@ func (s *ManagerTestSuite) SetupTest() {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *ManagerTestSuite) TearDownTest() {
|
||||
s.Require().NoError(os.RemoveAll(s.keydir))
|
||||
}
|
||||
|
||||
func (s *ManagerTestSuite) TestRecoverAccount() {
|
||||
accountInfo, err := s.accManager.RecoverAccount(s.password, s.mnemonic)
|
||||
s.NoError(err)
|
||||
|
|
|
@ -625,9 +625,7 @@ func TestLoginWithKey(t *testing.T) {
|
|||
main := multiaccounts.Account{
|
||||
KeyUID: keyUID,
|
||||
}
|
||||
tmpdir, err := os.MkdirTemp("", "login-with-key-test-")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tmpdir)
|
||||
tmpdir := t.TempDir()
|
||||
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
||||
require.NoError(t, err)
|
||||
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
|
||||
|
@ -668,9 +666,7 @@ func TestVerifyDatabasePassword(t *testing.T) {
|
|||
main := multiaccounts.Account{
|
||||
KeyUID: keyUID,
|
||||
}
|
||||
tmpdir, err := os.MkdirTemp("", "verify-database-password-")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tmpdir)
|
||||
tmpdir := t.TempDir()
|
||||
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
||||
require.NoError(t, err)
|
||||
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
|
||||
|
@ -691,15 +687,13 @@ func TestVerifyDatabasePassword(t *testing.T) {
|
|||
func TestDeleteMultiaccount(t *testing.T) {
|
||||
backend := NewGethStatusBackend()
|
||||
|
||||
rootDataDir, err := os.MkdirTemp("", "test-keystore-dir")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(rootDataDir)
|
||||
rootDataDir := t.TempDir()
|
||||
|
||||
keyStoreDir := filepath.Join(rootDataDir, "keystore")
|
||||
|
||||
backend.rootDataDir = rootDataDir
|
||||
|
||||
err = backend.AccountManager().InitKeystore(keyStoreDir)
|
||||
err := backend.AccountManager().InitKeystore(keyStoreDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
backend.AccountManager()
|
||||
|
@ -794,9 +788,7 @@ func TestConvertAccount(t *testing.T) {
|
|||
return found
|
||||
}
|
||||
|
||||
rootDataDir, err := os.MkdirTemp("", "test-keystore-dir")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(rootDataDir)
|
||||
rootDataDir := t.TempDir()
|
||||
|
||||
keyStoreDir := filepath.Join(rootDataDir, "keystore")
|
||||
|
||||
|
@ -1060,9 +1052,7 @@ func TestLoginAndMigrationsStillWorkWithExistingUsers(t *testing.T) {
|
|||
|
||||
srcFolder := "../static/test-0.97.3-account/"
|
||||
|
||||
tmpdir, err := os.MkdirTemp("", "login-and-migrations-with-existing-users")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tmpdir)
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
copyDir(srcFolder, tmpdir, t)
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -15,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
func setupWalletTest(t *testing.T, password string) (backend *GethStatusBackend, defersFunc func(), err error) {
|
||||
tmpdir, err := os.MkdirTemp("", "verified-account-test-")
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
defers := make([]func(), 0)
|
||||
defersFunc = func() {
|
||||
|
@ -27,10 +26,6 @@ func setupWalletTest(t *testing.T, password string) (backend *GethStatusBackend,
|
|||
return
|
||||
}
|
||||
|
||||
defers = append(defers, func() {
|
||||
os.Remove(tmpdir)
|
||||
})
|
||||
//defer os.Remove(tmpdir)
|
||||
backend = NewGethStatusBackend()
|
||||
backend.UpdateRootDataDir(tmpdir)
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package mailserver
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -15,15 +14,7 @@ import (
|
|||
func TestLevelDB_BuildIteratorWithTopic(t *testing.T) {
|
||||
topic := []byte{0x01, 0x02, 0x03, 0x04}
|
||||
|
||||
dir, err := os.MkdirTemp("/tmp", "status-go-test-level-db")
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := NewLevelDB(dir)
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(dir)
|
||||
}()
|
||||
|
||||
db, err := NewLevelDB(t.TempDir())
|
||||
require.NoError(t, err)
|
||||
|
||||
envelope, err := newTestEnvelope(topic)
|
||||
|
|
|
@ -21,7 +21,6 @@ import (
|
|||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -69,8 +68,7 @@ func (s *MailserverSuite) SetupTest() {
|
|||
s.shh = waku.New(&waku.DefaultConfig, nil)
|
||||
s.shh.RegisterMailServer(s.server)
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "mailserver-test")
|
||||
s.Require().NoError(err)
|
||||
tmpDir := s.T().TempDir()
|
||||
s.dataDir = tmpDir
|
||||
|
||||
s.config = ¶ms.WakuConfig{
|
||||
|
@ -79,10 +77,6 @@ func (s *MailserverSuite) SetupTest() {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *MailserverSuite) TearDownTest() {
|
||||
s.Require().NoError(os.RemoveAll(s.config.DataDir))
|
||||
}
|
||||
|
||||
func (s *MailserverSuite) TestInit() {
|
||||
testCases := []struct {
|
||||
config params.WakuConfig
|
||||
|
|
|
@ -68,17 +68,11 @@ func TestStatusNodeStart(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestStatusNodeWithDataDir(t *testing.T) {
|
||||
var err error
|
||||
|
||||
dir, err := os.MkdirTemp("", "status-node-test")
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
require.NoError(t, os.RemoveAll(dir))
|
||||
}()
|
||||
dir := t.TempDir()
|
||||
|
||||
// keystore directory
|
||||
keyStoreDir := path.Join(dir, "keystore")
|
||||
err = os.MkdirAll(keyStoreDir, os.ModePerm)
|
||||
err := os.MkdirAll(keyStoreDir, os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
|
||||
config := params.NodeConfig{
|
||||
|
|
|
@ -3,7 +3,6 @@ package params_test
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -55,11 +54,7 @@ func TestNewNodeConfigWithDefaults(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNewConfigFromJSON(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "geth-config-tests")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
||||
tmpDir := t.TempDir()
|
||||
json := `{
|
||||
"NetworkId": 3,
|
||||
"DataDir": "` + tmpDir + `",
|
||||
|
@ -84,9 +79,7 @@ func TestNewConfigFromJSON(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestConfigWriteRead(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "geth-config-tests")
|
||||
require.Nil(t, err)
|
||||
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
nodeConfig, err := utils.MakeTestNodeConfigWithDataDir("", tmpDir, params.GoerliNetworkID)
|
||||
require.Nil(t, err, "cannot create new config object")
|
||||
|
|
|
@ -10,10 +10,9 @@ import (
|
|||
)
|
||||
|
||||
func TestProfilingCPU(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "profiling")
|
||||
require.NoError(t, err)
|
||||
dir := t.TempDir()
|
||||
|
||||
err = StartCPUProfile(dir)
|
||||
err := StartCPUProfile(dir)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Block for a bit to collect some metrics.
|
||||
|
@ -38,10 +37,9 @@ func TestProfilingCPU(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProfilingMem(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "profiling")
|
||||
require.NoError(t, err)
|
||||
dir := t.TempDir()
|
||||
|
||||
err = WriteHeapFile(dir)
|
||||
err := WriteHeapFile(dir)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify that the file has some content.
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -53,8 +52,7 @@ func (s *MessageSenderSuite) SetupTest() {
|
|||
s.logger, err = zap.NewDevelopment()
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.tmpDir, err = os.MkdirTemp("", "")
|
||||
s.Require().NoError(err)
|
||||
s.tmpDir = s.T().TempDir()
|
||||
|
||||
identity, err := crypto.GenerateKey()
|
||||
s.Require().NoError(err)
|
||||
|
@ -96,7 +94,6 @@ func (s *MessageSenderSuite) SetupTest() {
|
|||
}
|
||||
|
||||
func (s *MessageSenderSuite) TearDownTest() {
|
||||
os.Remove(s.tmpDir)
|
||||
_ = s.logger.Sync()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package encryption
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -31,9 +30,7 @@ type SQLLitePersistenceKeysStorageTestSuite struct {
|
|||
}
|
||||
|
||||
func (s *SQLLitePersistenceKeysStorageTestSuite) SetupTest() {
|
||||
dir, err := os.MkdirTemp("", "keys-storage-persistence")
|
||||
s.Require().NoError(err)
|
||||
|
||||
dir := s.T().TempDir()
|
||||
key := "blahblahblah"
|
||||
|
||||
db, err := sqlite.Open(filepath.Join(dir, "db.sql"), key, sqlite.ReducedKDFIterationsNumber)
|
||||
|
|
|
@ -2,7 +2,6 @@ package encryption
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -26,8 +25,7 @@ type SQLLitePersistenceTestSuite struct {
|
|||
}
|
||||
|
||||
func (s *SQLLitePersistenceTestSuite) SetupTest() {
|
||||
dir, err := os.MkdirTemp("", "sqlite-persistence")
|
||||
s.Require().NoError(err)
|
||||
dir := s.T().TempDir()
|
||||
|
||||
db, err := sqlite.Open(filepath.Join(dir, "db.sql"), "test-key", sqlite.ReducedKDFIterationsNumber)
|
||||
s.Require().NoError(err)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package sqlite
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -9,9 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "test-open")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(dir)
|
||||
dir := t.TempDir()
|
||||
|
||||
dbPath := filepath.Join(dir, "db.sql")
|
||||
|
||||
|
|
|
@ -62,27 +62,21 @@ func setupTestDB(t *testing.T) (*multiaccounts.Database, func()) {
|
|||
}
|
||||
}
|
||||
|
||||
func makeKeystores(t *testing.T) (string, string, func()) {
|
||||
keyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts")
|
||||
require.NoError(t, err)
|
||||
|
||||
emptyKeyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts_empty")
|
||||
require.NoError(t, err)
|
||||
func makeKeystores(t *testing.T) (string, string) {
|
||||
keyStoreDir := t.TempDir()
|
||||
emptyKeyStoreDir := t.TempDir()
|
||||
|
||||
keyStoreDir = filepath.Join(keyStoreDir, keystoreDir, keyUID)
|
||||
// TODO test case where the keystore dir does not yet exist because the device is new
|
||||
emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, keystoreDir)
|
||||
|
||||
err = os.MkdirAll(keyStoreDir, 0777)
|
||||
err := os.MkdirAll(keyStoreDir, 0777)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.MkdirAll(emptyKeyStoreDir, 0777)
|
||||
require.NoError(t, err)
|
||||
|
||||
return keyStoreDir, emptyKeyStoreDir, func() {
|
||||
os.RemoveAll(keyStoreDir)
|
||||
os.RemoveAll(emptyKeyStoreDir)
|
||||
}
|
||||
return keyStoreDir, emptyKeyStoreDir
|
||||
}
|
||||
|
||||
func initKeys(t *testing.T, keyStoreDir string) {
|
||||
|
@ -122,11 +116,10 @@ func (pms *PayloadMarshallerSuite) SetupTest() {
|
|||
|
||||
db1, db1td := setupTestDB(pms.T())
|
||||
db2, db2td := setupTestDB(pms.T())
|
||||
keystore1, keystore2, kstd := makeKeystores(pms.T())
|
||||
keystore1, keystore2 := makeKeystores(pms.T())
|
||||
pms.teardown = func() {
|
||||
db1td()
|
||||
db2td()
|
||||
kstd()
|
||||
}
|
||||
|
||||
initKeys(pms.T(), keystore1)
|
||||
|
|
|
@ -3,7 +3,6 @@ package pairing
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -51,19 +50,8 @@ type SyncDeviceSuite struct {
|
|||
|
||||
func (s *SyncDeviceSuite) SetupTest() {
|
||||
s.password = "password"
|
||||
|
||||
clientAsSenderTmpdir, err := os.MkdirTemp("", "TestPairingSyncDeviceClientAsSender")
|
||||
require.NoError(s.T(), err)
|
||||
s.clientAsSenderTmpdir = clientAsSenderTmpdir
|
||||
|
||||
clientAsReceiverTmpdir, err := os.MkdirTemp("", "TestPairingSyncDeviceClientAsReceiver")
|
||||
require.NoError(s.T(), err)
|
||||
s.clientAsReceiverTmpdir = clientAsReceiverTmpdir
|
||||
}
|
||||
|
||||
func (s *SyncDeviceSuite) TearDownTest() {
|
||||
os.RemoveAll(s.clientAsSenderTmpdir)
|
||||
os.RemoveAll(s.clientAsReceiverTmpdir)
|
||||
s.clientAsSenderTmpdir = s.T().TempDir()
|
||||
s.clientAsReceiverTmpdir = s.T().TempDir()
|
||||
}
|
||||
|
||||
func (s *SyncDeviceSuite) prepareBackendWithAccount(tmpdir string) *api.GethStatusBackend {
|
||||
|
|
|
@ -35,9 +35,6 @@ func createDB(t *testing.T) (*sql.DB, func()) {
|
|||
func setupTestAPI(t *testing.T) (*API, func()) {
|
||||
db, cancel := createDB(t)
|
||||
|
||||
keyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Creating a dummy status node to simulate what it's done in get_status_node.go
|
||||
upstreamConfig := params.UpstreamRPCConfig{
|
||||
URL: "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938",
|
||||
|
@ -55,7 +52,7 @@ func setupTestAPI(t *testing.T) (*API, func()) {
|
|||
|
||||
// import account keys
|
||||
utils.Init()
|
||||
require.NoError(t, utils.ImportTestAccount(keyStoreDir, utils.GetAccount1PKFile()))
|
||||
require.NoError(t, utils.ImportTestAccount(t.TempDir(), utils.GetAccount1PKFile()))
|
||||
|
||||
return NewAPI(rpcClient, nil, nil, nil, db, time.Now, nil), cancel
|
||||
}
|
||||
|
|
|
@ -102,13 +102,10 @@ func TestRequestMessagesErrors(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestInitProtocol(t *testing.T) {
|
||||
directory, err := os.MkdirTemp("", "status-go-testing")
|
||||
require.NoError(t, err)
|
||||
|
||||
config := params.NodeConfig{
|
||||
ShhextConfig: params.ShhextConfig{
|
||||
InstallationID: "2",
|
||||
BackupDisabledDataDir: directory,
|
||||
BackupDisabledDataDir: t.TempDir(),
|
||||
PFSEnabled: true,
|
||||
MailServerConfirmations: true,
|
||||
ConnectionTarget: 10,
|
||||
|
@ -124,8 +121,7 @@ func TestInitProtocol(t *testing.T) {
|
|||
nodeWrapper := ext.NewTestNodeWrapper(nil, waku)
|
||||
service := New(config, nodeWrapper, nil, nil, db)
|
||||
|
||||
tmpdir, err := os.MkdirTemp("", "test-shhext-service-init-protocol")
|
||||
require.NoError(t, err)
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
sqlDB, err := appdatabase.InitializeDB(fmt.Sprintf("%s/db.sql", tmpdir), "password", sqlite.ReducedKDFIterationsNumber)
|
||||
require.NoError(t, err)
|
||||
|
@ -222,9 +218,7 @@ func (s *ShhExtSuite) createAndAddNode() {
|
|||
}
|
||||
|
||||
func (s *ShhExtSuite) SetupTest() {
|
||||
var err error
|
||||
s.dir, err = os.MkdirTemp("", "status-go-testing")
|
||||
s.Require().NoError(err)
|
||||
s.dir = s.T().TempDir()
|
||||
}
|
||||
|
||||
func (s *ShhExtSuite) TearDownTest() {
|
||||
|
|
|
@ -39,8 +39,7 @@ func createDB(t *testing.T) (*sql.DB, func()) {
|
|||
func setupTestAPI(t *testing.T) (*API, func()) {
|
||||
db, cancel := createDB(t)
|
||||
|
||||
keyStoreDir, err := os.MkdirTemp(os.TempDir(), "accounts")
|
||||
require.NoError(t, err)
|
||||
keyStoreDir := t.TempDir()
|
||||
|
||||
// Creating a dummy status node to simulate what it's done in get_status_node.go
|
||||
upstreamConfig := params.UpstreamRPCConfig{
|
||||
|
|
Loading…
Reference in New Issue