Skip to content

Commit 6f09db1

Browse files
committed
multi:use t.TempDir replace os.MkdirTemp
1 parent 2a64716 commit 6f09db1

File tree

5 files changed

+10
-33
lines changed

5 files changed

+10
-33
lines changed

addrmgr/addrmanager_internal_test.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package addrmgr
33
import (
44
"math/rand"
55
"net"
6-
"os"
76
"testing"
87
"time"
98

@@ -107,11 +106,7 @@ func TestAddrManagerSerialization(t *testing.T) {
107106

108107
// We'll start by creating our address manager backed by a temporary
109108
// directory.
110-
tempDir, err := os.MkdirTemp("", "addrmgr")
111-
if err != nil {
112-
t.Fatalf("unable to create temp dir: %v", err)
113-
}
114-
defer os.RemoveAll(tempDir)
109+
tempDir := t.TempDir()
115110

116111
addrMgr := New(tempDir, nil)
117112

@@ -147,11 +142,7 @@ func TestAddrManagerV1ToV2(t *testing.T) {
147142

148143
// We'll start by creating our address manager backed by a temporary
149144
// directory.
150-
tempDir, err := os.MkdirTemp("", "addrmgr")
151-
if err != nil {
152-
t.Fatalf("unable to create temp dir: %v", err)
153-
}
154-
defer os.RemoveAll(tempDir)
145+
tempDir := t.TempDir()
155146

156147
addrMgr := New(tempDir, nil)
157148

cmd/findcheckpoint/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"slices"
12-
12+
1313
"github.com/btcsuite/btcd/btcutil"
1414
"github.com/btcsuite/btcd/chaincfg"
1515
"github.com/btcsuite/btcd/database"

config_test.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
2222
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
2323

2424
// Setup a temporary directory
25-
tmpDir, err := os.MkdirTemp("", "btcd")
26-
if err != nil {
27-
t.Fatalf("Failed creating a temporary directory: %v", err)
28-
}
25+
tmpDir := t.TempDir()
2926
testpath := filepath.Join(tmpDir, "test.conf")
3027

3128
// copy config file to location of btcd binary
@@ -43,13 +40,6 @@ func TestCreateDefaultConfigFile(t *testing.T) {
4340
t.Fatalf("Failed copying sample config file: %v", err)
4441
}
4542

46-
// Clean-up
47-
defer func() {
48-
os.Remove(testpath)
49-
os.Remove(tmpConfigFile)
50-
os.Remove(tmpDir)
51-
}()
52-
5343
err = createDefaultConfigFile(testpath)
5444

5545
if err != nil {

database/ffldb/driver_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ func TestCreateOpenFail(t *testing.T) {
103103

104104
// Ensure operations against a closed database return the expected
105105
// error.
106-
dbPath := filepath.Join(os.TempDir(), "ffldb-createfail")
106+
dbPath := filepath.Join(t.TempDir(), "ffldb-createfail")
107107
_ = os.RemoveAll(dbPath)
108108
db, err := database.Create(dbType, dbPath, blockDataNet)
109109
if err != nil {
110110
t.Errorf("Create: unexpected error: %v", err)
111111
return
112112
}
113-
defer os.RemoveAll(dbPath)
114113
db.Close()
115114

116115
wantErrCode = database.ErrDbNotOpen
@@ -154,14 +153,13 @@ func TestPersistence(t *testing.T) {
154153
t.Parallel()
155154

156155
// Create a new database to run tests against.
157-
dbPath := filepath.Join(os.TempDir(), "ffldb-persistencetest")
156+
dbPath := filepath.Join(t.TempDir(), "ffldb-persistencetest")
158157
_ = os.RemoveAll(dbPath)
159158
db, err := database.Create(dbType, dbPath, blockDataNet)
160159
if err != nil {
161160
t.Errorf("Failed to create test database (%s) %v", dbType, err)
162161
return
163162
}
164-
defer os.RemoveAll(dbPath)
165163
defer db.Close()
166164

167165
// Create a bucket, put some values into it, and store a block so they
@@ -447,14 +445,13 @@ func TestInterface(t *testing.T) {
447445
t.Parallel()
448446

449447
// Create a new database to run tests against.
450-
dbPath := filepath.Join(os.TempDir(), "ffldb-interfacetest")
448+
dbPath := filepath.Join(t.TempDir(), "ffldb-interfacetest")
451449
_ = os.RemoveAll(dbPath)
452450
db, err := database.Create(dbType, dbPath, blockDataNet)
453451
if err != nil {
454452
t.Errorf("Failed to create test database (%s) %v", dbType, err)
455453
return
456454
}
457-
defer os.RemoveAll(dbPath)
458455
defer db.Close()
459456

460457
// Ensure the driver type is the expected value.

database/ffldb/whitebox_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func TestConvertErr(t *testing.T) {
165165
func TestCornerCases(t *testing.T) {
166166
t.Parallel()
167167

168-
// Create a file at the datapase path to force the open below to fail.
169-
dbPath := filepath.Join(os.TempDir(), "ffldb-errors")
168+
// Create a file at the database path to force the open below to fail.
169+
dbPath := filepath.Join(t.TempDir(), "ffldb-errors")
170170
_ = os.RemoveAll(dbPath)
171171
fi, err := os.Create(dbPath)
172172
if err != nil {
@@ -603,14 +603,13 @@ func testCorruption(tc *testContext) bool {
603603
// correctly.
604604
func TestFailureScenarios(t *testing.T) {
605605
// Create a new database to run tests against.
606-
dbPath := filepath.Join(os.TempDir(), "ffldb-failurescenarios")
606+
dbPath := filepath.Join(t.TempDir(), "ffldb-failurescenarios")
607607
_ = os.RemoveAll(dbPath)
608608
idb, err := database.Create(dbType, dbPath, blockDataNet)
609609
if err != nil {
610610
t.Errorf("Failed to create test database (%s) %v", dbType, err)
611611
return
612612
}
613-
defer os.RemoveAll(dbPath)
614613
defer idb.Close()
615614

616615
// Create a test context to pass around.

0 commit comments

Comments
 (0)