multi:use t.TempDir replace os.MkdirTemp

This commit is contained in:
petersssong 2025-03-07 16:54:47 +08:00
parent 2a64716b1c
commit 6f09db1924
5 changed files with 10 additions and 33 deletions

View file

@ -3,7 +3,6 @@ package addrmgr
import (
"math/rand"
"net"
"os"
"testing"
"time"
@ -107,11 +106,7 @@ func TestAddrManagerSerialization(t *testing.T) {
// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
addrMgr := New(tempDir, nil)
@ -147,11 +142,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {
// We'll start by creating our address manager backed by a temporary
// directory.
tempDir, err := os.MkdirTemp("", "addrmgr")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
addrMgr := New(tempDir, nil)

View file

@ -9,7 +9,7 @@ import (
"os"
"path/filepath"
"slices"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/database"

View file

@ -22,10 +22,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
// Setup a temporary directory
tmpDir, err := os.MkdirTemp("", "btcd")
if err != nil {
t.Fatalf("Failed creating a temporary directory: %v", err)
}
tmpDir := t.TempDir()
testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of btcd binary
@ -43,13 +40,6 @@ func TestCreateDefaultConfigFile(t *testing.T) {
t.Fatalf("Failed copying sample config file: %v", err)
}
// Clean-up
defer func() {
os.Remove(testpath)
os.Remove(tmpConfigFile)
os.Remove(tmpDir)
}()
err = createDefaultConfigFile(testpath)
if err != nil {

View file

@ -103,14 +103,13 @@ func TestCreateOpenFail(t *testing.T) {
// Ensure operations against a closed database return the expected
// error.
dbPath := filepath.Join(os.TempDir(), "ffldb-createfail")
dbPath := filepath.Join(t.TempDir(), "ffldb-createfail")
_ = os.RemoveAll(dbPath)
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Create: unexpected error: %v", err)
return
}
defer os.RemoveAll(dbPath)
db.Close()
wantErrCode = database.ErrDbNotOpen
@ -154,14 +153,13 @@ func TestPersistence(t *testing.T) {
t.Parallel()
// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-persistencetest")
dbPath := filepath.Join(t.TempDir(), "ffldb-persistencetest")
_ = os.RemoveAll(dbPath)
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer db.Close()
// Create a bucket, put some values into it, and store a block so they
@ -447,14 +445,13 @@ func TestInterface(t *testing.T) {
t.Parallel()
// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-interfacetest")
dbPath := filepath.Join(t.TempDir(), "ffldb-interfacetest")
_ = os.RemoveAll(dbPath)
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer db.Close()
// Ensure the driver type is the expected value.

View file

@ -165,8 +165,8 @@ func TestConvertErr(t *testing.T) {
func TestCornerCases(t *testing.T) {
t.Parallel()
// Create a file at the datapase path to force the open below to fail.
dbPath := filepath.Join(os.TempDir(), "ffldb-errors")
// Create a file at the database path to force the open below to fail.
dbPath := filepath.Join(t.TempDir(), "ffldb-errors")
_ = os.RemoveAll(dbPath)
fi, err := os.Create(dbPath)
if err != nil {
@ -603,14 +603,13 @@ func testCorruption(tc *testContext) bool {
// correctly.
func TestFailureScenarios(t *testing.T) {
// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-failurescenarios")
dbPath := filepath.Join(t.TempDir(), "ffldb-failurescenarios")
_ = os.RemoveAll(dbPath)
idb, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer idb.Close()
// Create a test context to pass around.