From 0c54fb8f2b938d98798597ee6599199bcdc8aa6b Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 14 Jun 2023 16:09:15 -0500 Subject: [PATCH] database: Use TempDir to create temp test dirs. This modifies several of the tests to use t.TempDir. The directory it creates is automatically removed when the test and all its subtests complete. It also changes out some of the cleanup logic for closing the database to use t.Cleanup instead of defer to ensure the close happens during test cleanup after the directories created by t.TempDir have been removed. --- database/ffldb/bench_test.go | 18 ++++++++---------- database/ffldb/driver_test.go | 22 +++++++++------------- database/ffldb/whitebox_test.go | 12 +++++------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/database/ffldb/bench_test.go b/database/ffldb/bench_test.go index c0ae5aa630..d687472c3f 100644 --- a/database/ffldb/bench_test.go +++ b/database/ffldb/bench_test.go @@ -6,8 +6,6 @@ package ffldb import ( - "os" - "path/filepath" "testing" "github.com/decred/dcrd/chaincfg/v3" @@ -20,14 +18,14 @@ import ( func BenchmarkBlockHeader(b *testing.B) { // Start by creating a new database and populating it with the mainnet // genesis block. - dbPath := filepath.Join(os.TempDir(), "ffldb-benchblkhdr") - _ = os.RemoveAll(dbPath) + dbPath := b.TempDir() db, err := database.Create("ffldb", dbPath, blockDataNet) if err != nil { b.Fatal(err) } - defer os.RemoveAll(dbPath) - defer db.Close() + b.Cleanup(func() { + db.Close() + }) mainNetParams := chaincfg.MainNetParams() err = db.Update(func(tx database.Tx) error { block := dcrutil.NewBlock(mainNetParams.GenesisBlock) @@ -62,14 +60,14 @@ func BenchmarkBlockHeader(b *testing.B) { func BenchmarkBlock(b *testing.B) { // Start by creating a new database and populating it with the mainnet // genesis block. - dbPath := filepath.Join(os.TempDir(), "ffldb-benchblk") - _ = os.RemoveAll(dbPath) + dbPath := b.TempDir() db, err := database.Create("ffldb", dbPath, blockDataNet) if err != nil { b.Fatal(err) } - defer os.RemoveAll(dbPath) - defer db.Close() + b.Cleanup(func() { + db.Close() + }) mainNetParams := chaincfg.MainNetParams() err = db.Update(func(tx database.Tx) error { block := dcrutil.NewBlock(mainNetParams.GenesisBlock) diff --git a/database/ffldb/driver_test.go b/database/ffldb/driver_test.go index be4b54d886..2b98ca2aaa 100644 --- a/database/ffldb/driver_test.go +++ b/database/ffldb/driver_test.go @@ -7,8 +7,6 @@ package ffldb_test import ( "fmt" - "os" - "path/filepath" "reflect" "runtime" "testing" @@ -103,14 +101,12 @@ func TestCreateOpenFail(t *testing.T) { // Ensure operations against a closed database return the expected // error. - dbPath := filepath.Join(os.TempDir(), "ffldb-createfail-v2") - _ = os.RemoveAll(dbPath) + dbPath := t.TempDir() db, err := database.Create(dbType, dbPath, blockDataNet) if err != nil { t.Errorf("Create: unexpected error: %v", err) return } - defer os.RemoveAll(dbPath) db.Close() wantErrKind = database.ErrDbNotOpen @@ -154,15 +150,15 @@ func TestPersistence(t *testing.T) { t.Parallel() // Create a new database to run tests against. - dbPath := filepath.Join(os.TempDir(), "ffldb-persistencetest-v2") - _ = os.RemoveAll(dbPath) + dbPath := t.TempDir() 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() + t.Cleanup(func() { + db.Close() + }) // Create a bucket, put some values into it, and store a block so they // can be tested for existence on re-open. @@ -261,15 +257,15 @@ func TestInterface(t *testing.T) { t.Parallel() // Create a new database to run tests against. - dbPath := filepath.Join(os.TempDir(), "ffldb-interfacetest-v2") - _ = os.RemoveAll(dbPath) + dbPath := t.TempDir() 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() + t.Cleanup(func() { + db.Close() + }) // Ensure the driver type is the expected value. gotDbType := db.Type() diff --git a/database/ffldb/whitebox_test.go b/database/ffldb/whitebox_test.go index ff1440f567..d2bf11eda1 100644 --- a/database/ffldb/whitebox_test.go +++ b/database/ffldb/whitebox_test.go @@ -141,7 +141,7 @@ func TestCornerCases(t *testing.T) { t.Parallel() // Create a file at the database path to force the open below to fail. - dbPath := filepath.Join(os.TempDir(), "ffldb-errors-v2") + dbPath := t.TempDir() _ = os.RemoveAll(dbPath) fi, err := os.Create(dbPath) if err != nil { @@ -159,7 +159,6 @@ func TestCornerCases(t *testing.T) { if err == nil { idb.Close() } - _ = os.RemoveAll(dbPath) return } @@ -171,8 +170,9 @@ func TestCornerCases(t *testing.T) { t.Errorf("openDB: unexpected error: %v", err) return } - defer os.RemoveAll(dbPath) - defer idb.Close() + t.Cleanup(func() { + idb.Close() + }) // Ensure attempting to write to a file that can't be created returns // the expected error. @@ -578,14 +578,12 @@ 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-v2") - _ = os.RemoveAll(dbPath) + dbPath := t.TempDir() 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.