mirror of
https://github.com/btcsuite/btcd.git
synced 2025-03-12 10:30:49 +01:00
multi:use t.TempDir replace os.MkdirTemp
This commit is contained in:
parent
2a64716b1c
commit
6f09db1924
5 changed files with 10 additions and 33 deletions
|
@ -3,7 +3,6 @@ package addrmgr
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -107,11 +106,7 @@ func TestAddrManagerSerialization(t *testing.T) {
|
||||||
|
|
||||||
// We'll start by creating our address manager backed by a temporary
|
// We'll start by creating our address manager backed by a temporary
|
||||||
// directory.
|
// directory.
|
||||||
tempDir, err := os.MkdirTemp("", "addrmgr")
|
tempDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
addrMgr := New(tempDir, nil)
|
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
|
// We'll start by creating our address manager backed by a temporary
|
||||||
// directory.
|
// directory.
|
||||||
tempDir, err := os.MkdirTemp("", "addrmgr")
|
tempDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
addrMgr := New(tempDir, nil)
|
addrMgr := New(tempDir, nil)
|
||||||
|
|
||||||
|
|
|
@ -22,10 +22,7 @@ func TestCreateDefaultConfigFile(t *testing.T) {
|
||||||
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
|
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
|
||||||
|
|
||||||
// Setup a temporary directory
|
// Setup a temporary directory
|
||||||
tmpDir, err := os.MkdirTemp("", "btcd")
|
tmpDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed creating a temporary directory: %v", err)
|
|
||||||
}
|
|
||||||
testpath := filepath.Join(tmpDir, "test.conf")
|
testpath := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
// copy config file to location of btcd binary
|
// 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)
|
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)
|
err = createDefaultConfigFile(testpath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -103,14 +103,13 @@ func TestCreateOpenFail(t *testing.T) {
|
||||||
|
|
||||||
// Ensure operations against a closed database return the expected
|
// Ensure operations against a closed database return the expected
|
||||||
// error.
|
// error.
|
||||||
dbPath := filepath.Join(os.TempDir(), "ffldb-createfail")
|
dbPath := filepath.Join(t.TempDir(), "ffldb-createfail")
|
||||||
_ = os.RemoveAll(dbPath)
|
_ = os.RemoveAll(dbPath)
|
||||||
db, err := database.Create(dbType, dbPath, blockDataNet)
|
db, err := database.Create(dbType, dbPath, blockDataNet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Create: unexpected error: %v", err)
|
t.Errorf("Create: unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dbPath)
|
|
||||||
db.Close()
|
db.Close()
|
||||||
|
|
||||||
wantErrCode = database.ErrDbNotOpen
|
wantErrCode = database.ErrDbNotOpen
|
||||||
|
@ -154,14 +153,13 @@ func TestPersistence(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create a new database to run tests against.
|
// 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)
|
_ = os.RemoveAll(dbPath)
|
||||||
db, err := database.Create(dbType, dbPath, blockDataNet)
|
db, err := database.Create(dbType, dbPath, blockDataNet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dbPath)
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Create a bucket, put some values into it, and store a block so they
|
// 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()
|
t.Parallel()
|
||||||
|
|
||||||
// Create a new database to run tests against.
|
// 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)
|
_ = os.RemoveAll(dbPath)
|
||||||
db, err := database.Create(dbType, dbPath, blockDataNet)
|
db, err := database.Create(dbType, dbPath, blockDataNet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dbPath)
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Ensure the driver type is the expected value.
|
// Ensure the driver type is the expected value.
|
||||||
|
|
|
@ -165,8 +165,8 @@ func TestConvertErr(t *testing.T) {
|
||||||
func TestCornerCases(t *testing.T) {
|
func TestCornerCases(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create a file at the datapase path to force the open below to fail.
|
// Create a file at the database path to force the open below to fail.
|
||||||
dbPath := filepath.Join(os.TempDir(), "ffldb-errors")
|
dbPath := filepath.Join(t.TempDir(), "ffldb-errors")
|
||||||
_ = os.RemoveAll(dbPath)
|
_ = os.RemoveAll(dbPath)
|
||||||
fi, err := os.Create(dbPath)
|
fi, err := os.Create(dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -603,14 +603,13 @@ func testCorruption(tc *testContext) bool {
|
||||||
// correctly.
|
// correctly.
|
||||||
func TestFailureScenarios(t *testing.T) {
|
func TestFailureScenarios(t *testing.T) {
|
||||||
// Create a new database to run tests against.
|
// 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)
|
_ = os.RemoveAll(dbPath)
|
||||||
idb, err := database.Create(dbType, dbPath, blockDataNet)
|
idb, err := database.Create(dbType, dbPath, blockDataNet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dbPath)
|
|
||||||
defer idb.Close()
|
defer idb.Close()
|
||||||
|
|
||||||
// Create a test context to pass around.
|
// Create a test context to pass around.
|
||||||
|
|
Loading…
Add table
Reference in a new issue