forked from cerc-io/plugeth
all: use T.TempDir to create temporary test directories (#24633)
This commit replaces ioutil.TempDir 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 ioutil.TempDir
had 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:
+6
-11
@@ -32,11 +32,7 @@ import (
|
||||
// ones or automatically generated temporary ones.
|
||||
func TestDatadirCreation(t *testing.T) {
|
||||
// Create a temporary data dir and check that it can be used by a node
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create manual data dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
dir := t.TempDir()
|
||||
|
||||
node, err := New(&Config{DataDir: dir})
|
||||
if err != nil {
|
||||
@@ -62,7 +58,10 @@ func TestDatadirCreation(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temporary file: %v", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer func() {
|
||||
file.Close()
|
||||
os.Remove(file.Name())
|
||||
}()
|
||||
|
||||
dir = filepath.Join(file.Name(), "invalid/path")
|
||||
node, err = New(&Config{DataDir: dir})
|
||||
@@ -109,11 +108,7 @@ func TestIPCPathResolution(t *testing.T) {
|
||||
// ephemeral.
|
||||
func TestNodeKeyPersistency(t *testing.T) {
|
||||
// Create a temporary folder and make sure no key is present
|
||||
dir, err := ioutil.TempDir("", "node-test")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temporary data directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
dir := t.TempDir()
|
||||
|
||||
keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
|
||||
|
||||
|
||||
+1
-7
@@ -20,10 +20,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -88,11 +86,7 @@ func TestNodeStartMultipleTimes(t *testing.T) {
|
||||
// Tests that if the data dir is already in use, an appropriate error is returned.
|
||||
func TestNodeUsedDataDir(t *testing.T) {
|
||||
// Create a temporary folder to use as the data directory
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temporary data directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
dir := t.TempDir()
|
||||
|
||||
// Create a new node based on the data directory
|
||||
original, err := New(&Config{DataDir: dir})
|
||||
|
||||
Reference in New Issue
Block a user