Skip to content

Commit

Permalink
all: use T.TempDir to create temporary test directories (ethereum#24633)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jan 9, 2025
1 parent f2aa352 commit 9d53f9b
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 207 deletions.
8 changes: 2 additions & 6 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ func SimulateWalletAddressAndSignFn() (common.Address, func(account accounts.Acc
veryLightScryptN := 2
veryLightScryptP := 1
dir, _ := os.MkdirTemp("", "eth-SimulateWalletAddressAndSignFn-test")

new := func(kd string) *keystore.KeyStore {
return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP)
}

defer os.RemoveAll(dir)
ks := new(dir)

ks := keystore.NewKeyStore(dir, veryLightScryptN, veryLightScryptP)
pass := "" // not used but required by API
a1, err := ks.NewAccount(pass)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1919,14 +1919,10 @@ func TestGolangBindings(t *testing.T) {
}
t.Log("Using config", params.TestXDPoSMockChainConfig)
// Create a temporary workspace for the test suite
ws, err := os.MkdirTemp("", "binding-test")
if err != nil {
t.Fatalf("failed to create temporary workspace: %v", err)
}
// defer os.RemoveAll(ws)
ws := t.TempDir()

pkg := filepath.Join(ws, "bindtest")
if err = os.MkdirAll(pkg, 0700); err != nil {
if err := os.MkdirAll(pkg, 0700); err != nil {
t.Fatalf("failed to create package: %v", err)
}
// Generate the test suite for all the contracts
Expand Down
1 change: 0 additions & 1 deletion accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestWatchNewFile(t *testing.T) {
t.Parallel()

dir, ks := tmpKeyStore(t, false)
defer os.RemoveAll(dir)

// Ensure the watcher is started before adding any files.
ks.Accounts()
Expand Down
39 changes: 12 additions & 27 deletions accounts/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var testSigData = make([]byte, 32)

func TestKeyStore(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)

a, err := ks.NewAccount("foo")
if err != nil {
Expand Down Expand Up @@ -71,8 +70,7 @@ func TestKeyStore(t *testing.T) {
}

func TestSign(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)

pass := "" // not used but required by API
a1, err := ks.NewAccount(pass)
Expand All @@ -88,8 +86,7 @@ func TestSign(t *testing.T) {
}

func TestSignWithPassphrase(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)

pass := "passwd"
acc, err := ks.NewAccount(pass)
Expand All @@ -116,8 +113,7 @@ func TestSignWithPassphrase(t *testing.T) {
}

func TestTimedUnlock(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)

pass := "foo"
a1, err := ks.NewAccount(pass)
Expand Down Expand Up @@ -151,8 +147,7 @@ func TestTimedUnlock(t *testing.T) {
}

func TestOverrideUnlock(t *testing.T) {
dir, ks := tmpKeyStore(t, false)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, false)

pass := "foo"
a1, err := ks.NewAccount(pass)
Expand Down Expand Up @@ -192,8 +187,7 @@ func TestOverrideUnlock(t *testing.T) {

// This test should fail under -race if signing races the expiration goroutine.
func TestSignRace(t *testing.T) {
dir, ks := tmpKeyStore(t, false)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, false)

// Create a test account.
a1, err := ks.NewAccount("")
Expand Down Expand Up @@ -221,8 +215,7 @@ func TestSignRace(t *testing.T) {
// addition and removal of wallet event subscriptions.
func TestWalletNotifierLifecycle(t *testing.T) {
// Create a temporary kesytore to test with
dir, ks := tmpKeyStore(t, false)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, false)

// Ensure that the notification updater is not running yet
time.Sleep(250 * time.Millisecond)
Expand Down Expand Up @@ -282,8 +275,7 @@ type walletEvent struct {
// Tests that wallet notifications and correctly fired when accounts are added
// or deleted from the keystore.
func TestWalletNotifications(t *testing.T) {
dir, ks := tmpKeyStore(t, false)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, false)

// Subscribe to the wallet feed and collect events.
var (
Expand Down Expand Up @@ -344,8 +336,7 @@ func TestWalletNotifications(t *testing.T) {

// TestImportECDSA tests the import functionality of a keystore.
func TestImportECDSA(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)
key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("failed to generate key: %v", key)
Expand All @@ -363,8 +354,7 @@ func TestImportECDSA(t *testing.T) {

// TestImportExport tests the import and export functionality of a keystore.
func TestImportExport(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)
acc, err := ks.NewAccount("old")
if err != nil {
t.Fatalf("failed to create account: %v", acc)
Expand Down Expand Up @@ -394,8 +384,7 @@ func TestImportExport(t *testing.T) {
// TestImportRace tests the keystore on races.
// This test should fail under -race if importing races.
func TestImportRace(t *testing.T) {
dir, ks := tmpKeyStore(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStore(t, true)
acc, err := ks.NewAccount("old")
if err != nil {
t.Fatalf("failed to create account: %v", acc)
Expand All @@ -404,8 +393,7 @@ func TestImportRace(t *testing.T) {
if err != nil {
t.Fatalf("failed to export account: %v", acc)
}
dir2, ks2 := tmpKeyStore(t, true)
defer os.RemoveAll(dir2)
_, ks2 := tmpKeyStore(t, true)
var atom uint32
var wg sync.WaitGroup
wg.Add(2)
Expand Down Expand Up @@ -461,10 +449,7 @@ func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) {
}

func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
d, err := os.MkdirTemp("", "eth-keystore-test")
if err != nil {
t.Fatal(err)
}
d := t.TempDir()
newKs := NewPlaintextKeyStore
if encrypted {
newKs = func(kd string) *KeyStore { return NewKeyStore(kd, veryLightScryptN, veryLightScryptP) }
Expand Down
16 changes: 4 additions & 12 deletions accounts/keystore/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
Expand All @@ -31,10 +30,7 @@ import (
)

func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
d, err := os.MkdirTemp("", "geth-keystore-test")
if err != nil {
t.Fatal(err)
}
d := t.TempDir()
if encrypted {
ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
} else {
Expand All @@ -44,8 +40,7 @@ func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
}

func TestKeyStorePlain(t *testing.T) {
dir, ks := tmpKeyStoreIface(t, false)
defer os.RemoveAll(dir)
_, ks := tmpKeyStoreIface(t, false)

pass := "" // not used but required by API
k1, account, err := storeNewKey(ks, rand.Reader, pass)
Expand All @@ -65,8 +60,7 @@ func TestKeyStorePlain(t *testing.T) {
}

func TestKeyStorePassphrase(t *testing.T) {
dir, ks := tmpKeyStoreIface(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStoreIface(t, true)

pass := "foo"
k1, account, err := storeNewKey(ks, rand.Reader, pass)
Expand All @@ -86,8 +80,7 @@ func TestKeyStorePassphrase(t *testing.T) {
}

func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
dir, ks := tmpKeyStoreIface(t, true)
defer os.RemoveAll(dir)
_, ks := tmpKeyStoreIface(t, true)

pass := "foo"
k1, account, err := storeNewKey(ks, rand.Reader, pass)
Expand All @@ -101,7 +94,6 @@ func TestKeyStorePassphraseDecryptionFail(t *testing.T) {

func TestImportPreSaleKey(t *testing.T) {
dir, ks := tmpKeyStoreIface(t, true)
defer os.RemoveAll(dir)

// file content of a presale key file generated with:
// python pyethsaletool.py genwallet
Expand Down
8 changes: 3 additions & 5 deletions cmd/XDC/accountcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// are copied into a temporary keystore directory.

func tmpDatadirWithKeystore(t *testing.T) string {
datadir := tmpdir(t)
datadir := t.TempDir()
keystore := filepath.Join(datadir, "keystore")
source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore")
if err := cp.CopyAll(keystore, source); err != nil {
Expand All @@ -43,8 +43,7 @@ func tmpDatadirWithKeystore(t *testing.T) string {
}

func TestAccountListEmpty(t *testing.T) {
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()
XDC := runXDC(t, "account", "list", "--datadir", datadir)
XDC.ExpectExit()
}
Expand Down Expand Up @@ -144,8 +143,7 @@ Repeat passphrase: {{.InputLine "foobar2"}}
}

func TestWalletImport(t *testing.T) {
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()
XDC := runXDC(t, "wallet", "import", "--datadir", datadir, "--lightkdf", "testdata/guswallet.json")
defer XDC.ExpectExit()
XDC.Expect(`
Expand Down
13 changes: 4 additions & 9 deletions cmd/XDC/consolecmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"crypto/rand"
"math/big"
"os"
"path/filepath"
"runtime"
"strconv"
Expand All @@ -39,8 +38,7 @@ const (
// then terminated by closing the input stream.
func TestConsoleWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()

// Start a XDC console, make sure it's cleaned up and terminate the console
XDC := runXDC(t,
Expand Down Expand Up @@ -77,8 +75,7 @@ at block: 0 ({{niltime}})
func TestIPCAttachWelcome(t *testing.T) {
// Configure the instance for IPC attachement
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()
var ipc string
if runtime.GOOS == "windows" {
ipc = `\\.\pipe\XDC` + strconv.Itoa(trulyRandInt(100000, 999999))
Expand All @@ -100,8 +97,7 @@ func TestIPCAttachWelcome(t *testing.T) {
func TestHTTPAttachWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()
XDC := runXDC(t,
"--datadir", datadir, "--XDCx-datadir", datadir+"/XDCx/"+time.Now().String(),
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
Expand All @@ -117,8 +113,7 @@ func TestHTTPAttachWelcome(t *testing.T) {
func TestWSAttachWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()
XDC := runXDC(t,
"--datadir", datadir, "--XDCx-datadir", datadir+"/XDCx/"+time.Now().String(),
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
Expand Down
3 changes: 1 addition & 2 deletions cmd/XDC/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ func TestDAOForkBlockNewChain(t *testing.T) {

func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBlock *big.Int, expectVote bool) {
// Create a temporary data directory to use and inspect later
datadir := tmpdir(t)
defer os.RemoveAll(datadir)
datadir := t.TempDir()

// Start a XDC instance with the requested flags set and immediately terminate
if genesis != "" {
Expand Down
17 changes: 2 additions & 15 deletions cmd/XDC/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ import (
"github.com/docker/docker/pkg/reexec"
)

func tmpdir(t *testing.T) string {
dir, err := os.MkdirTemp("", "XDC-test")
if err != nil {
t.Fatal(err)
}
return dir
}

type testXDC struct {
*cmdtest.TestCmd

Expand Down Expand Up @@ -78,15 +70,10 @@ func runXDC(t *testing.T, args ...string) *testXDC {
}
}
if tt.Datadir == "" {
tt.Datadir = tmpdir(t)
// The temporary datadir will be removed automatically if something fails below.
tt.Datadir = t.TempDir()
tt.Cleanup = func() { os.RemoveAll(tt.Datadir) }
args = append([]string{"--datadir", tt.Datadir}, args...)
// Remove the temporary datadir if something fails below.
defer func() {
if t.Failed() {
tt.Cleanup()
}
}()
}

// Boot "XDC". This actually runs the test binary but the TestMain
Expand Down
7 changes: 1 addition & 6 deletions cmd/ethkey/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func TestMessageSignVerify(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "ethkey-test")
if err != nil {
t.Fatal("Can't create temporary directory:", err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

keyfile := filepath.Join(tmpdir, "the-keyfile")
message := "test message"
Expand Down
15 changes: 4 additions & 11 deletions cmd/utils/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,17 @@ func TestWalkMatch(t *testing.T) {
root string
pattern string
}
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
test1Dir, _ := os.MkdirTemp(dir, "test1")
test2Dir, _ := os.MkdirTemp(dir, "test2")
err = os.WriteFile(filepath.Join(test1Dir, "test1.ldb"), []byte("hello"), os.ModePerm)
test1Dir := t.TempDir()
test2Dir := t.TempDir()

err := os.WriteFile(filepath.Join(test1Dir, "test1.ldb"), []byte("hello"), os.ModePerm)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(filepath.Join(test2Dir, "test2.abc"), []byte("hello"), os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer func() {
os.RemoveAll(test1Dir)
os.RemoveAll(test2Dir)
}()

tests := []struct {
name string
Expand Down
Loading

0 comments on commit 9d53f9b

Please sign in to comment.