diff --git a/cert_test.go b/cert_test.go index a2b51afe14..11959420a4 100644 --- a/cert_test.go +++ b/cert_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2018 The Decred developers +// Copyright (c) 2018-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -9,25 +9,26 @@ import ( "crypto/x509" "encoding/pem" "os" + "path/filepath" "testing" ) // TestCertCreationWithHosts creates a certificate pair with extra hosts and // ensures the extra hosts are present in the generated files. func TestCertCreationWithHosts(t *testing.T) { - certFile, err := os.CreateTemp(t.TempDir(), "certfile") + tempDir := t.TempDir() + + certFile, err := os.Create(filepath.Join(tempDir, "certfile")) if err != nil { t.Fatalf("Unable to create temp certfile: %s", err) } certFile.Close() - defer os.Remove(certFile.Name()) - keyFile, err := os.CreateTemp(t.TempDir(), "keyfile") + keyFile, err := os.Create(filepath.Join(tempDir, "keyfile")) if err != nil { t.Fatalf("Unable to create temp keyfile: %s", err) } keyFile.Close() - defer os.Remove(keyFile.Name()) // Generate cert pair with extra hosts. hostnames := []string{"hostname1", "hostname2"} @@ -57,19 +58,19 @@ func TestCertCreationWithHosts(t *testing.T) { // TestCertCreationWithOutHosts ensures the creating a certificate pair without // any hosts works as intended. func TestCertCreationWithOutHosts(t *testing.T) { - certFile, err := os.CreateTemp(t.TempDir(), "certfile") + tempDir := t.TempDir() + + certFile, err := os.Create(filepath.Join(tempDir, "certfile")) if err != nil { t.Fatalf("Unable to create temp certfile: %s", err) } certFile.Close() - defer os.Remove(certFile.Name()) - keyFile, err := os.CreateTemp(t.TempDir(), "keyfile") + keyFile, err := os.Create(filepath.Join(tempDir, "keyfile")) if err != nil { t.Fatalf("Unable to create temp keyfile: %s", err) } keyFile.Close() - defer os.Remove(keyFile.Name()) // Generate cert pair with no extra hosts. err = genCertPair(certFile.Name(), keyFile.Name(), nil, elliptic.P521()) diff --git a/database/ffldb/whitebox_test.go b/database/ffldb/whitebox_test.go index 7dd59024f7..8eee34edef 100644 --- a/database/ffldb/whitebox_test.go +++ b/database/ffldb/whitebox_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2015-2016 The btcsuite developers -// Copyright (c) 2016-2025 The Decred developers +// Copyright (c) 2016-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -141,8 +141,7 @@ func TestCornerCases(t *testing.T) { t.Parallel() // Create a file at the database path to force the open below to fail. - dbPath := t.TempDir() - _ = os.RemoveAll(dbPath) + dbPath := filepath.Join(t.TempDir(), "db") fi, err := os.Create(dbPath) if err != nil { t.Errorf("os.Create: unexpected error: %v", err) diff --git a/internal/blockchain/common_test.go b/internal/blockchain/common_test.go index dd94116de7..633a3f23b0 100644 --- a/internal/blockchain/common_test.go +++ b/internal/blockchain/common_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2016 The btcsuite developers -// Copyright (c) 2015-2025 The Decred developers +// Copyright (c) 2015-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -13,7 +13,6 @@ import ( "fmt" "math" "math/bits" - "os" "reflect" "testing" "time" @@ -86,10 +85,9 @@ func createTestDatabase(t testing.TB, dbType string, net wire.CurrencyNet) (data } // createTestUtxoDatabase creates a test UTXO database with the provided -// database name. It also returns a teardown function the caller should invoke -// when done testing to clean up. -func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, func(), error) { - // Construct the database filepath +// database name, and ensures the database is closed when the test is completed. +func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, error) { + // Construct the database filepath. dbPath := t.TempDir() // Open the database (will create it if needed). @@ -100,17 +98,15 @@ func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, func(), error) { } db, err := leveldb.OpenFile(dbPath, &opts) if err != nil { - return nil, nil, err + return nil, err } - // Setup a teardown function for cleaning up. This function is returned to - // the caller to be invoked when it is done testing. - teardown := func() { + // Close database when the test is complete. + t.Cleanup(func() { _ = db.Close() - _ = os.RemoveAll(dbPath) - } + }) - return db, teardown, nil + return db, nil } // chainSetup is used to create a new db and chain instance with the genesis @@ -127,13 +123,7 @@ func chainSetup(t testing.TB, params *chaincfg.Params) (*BlockChain, error) { } // Create a test UTXO database. - utxoDb, teardownUtxoDb, err := createTestUtxoDatabase(t) - if err != nil { - return nil, err - } - t.Cleanup(func() { - teardownUtxoDb() - }) + utxoBackend := createTestUtxoBackend(t) // Copy the chain params to ensure any modifications the tests do to // the chain parameters do not affect the global instance. @@ -146,7 +136,6 @@ func chainSetup(t testing.TB, params *chaincfg.Params) (*BlockChain, error) { } // Create the main chain instance. - utxoBackend := NewLevelDbUtxoBackend(utxoDb) chain, err := New(context.Background(), &Config{ DB: db, diff --git a/internal/blockchain/indexers/existsaddrindex_test.go b/internal/blockchain/indexers/existsaddrindex_test.go index 199e91097a..c4236c64fe 100644 --- a/internal/blockchain/indexers/existsaddrindex_test.go +++ b/internal/blockchain/indexers/existsaddrindex_test.go @@ -1,11 +1,10 @@ -// Copyright (c) 2021-2022 The Decred developers +// Copyright (c) 2021-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. package indexers import ( - "context" "testing" "time" @@ -35,8 +34,7 @@ func TestExistsAddrIndexAsync(t *testing.T) { bk3 := addBlock(t, chain, &g, "bk3") // Initialize the exists address index. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() subber := NewIndexSubscriber(ctx) go subber.Run(ctx) diff --git a/internal/blockchain/indexers/indexsubscriber_test.go b/internal/blockchain/indexers/indexsubscriber_test.go index 7ffea2c206..c56c06b8b7 100644 --- a/internal/blockchain/indexers/indexsubscriber_test.go +++ b/internal/blockchain/indexers/indexsubscriber_test.go @@ -1,11 +1,10 @@ -// Copyright (c) 2021-2022 The Decred developers +// Copyright (c) 2021-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. package indexers import ( - "context" "testing" "time" @@ -34,8 +33,7 @@ func TestIndexSubscriberAsync(t *testing.T) { bk3 := addBlock(t, chain, &g, "bk3") // Initialize all indexes. - ctx, pCancel := context.WithCancel(context.Background()) - defer pCancel() + ctx := t.Context() subber := NewIndexSubscriber(ctx) diff --git a/internal/blockchain/indexers/txindex_test.go b/internal/blockchain/indexers/txindex_test.go index 82e1fc109b..9b0fe38187 100644 --- a/internal/blockchain/indexers/txindex_test.go +++ b/internal/blockchain/indexers/txindex_test.go @@ -1,11 +1,10 @@ -// Copyright (c) 2021-2023 The Decred developers +// Copyright (c) 2021-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. package indexers import ( - "context" "fmt" "sync" "testing" @@ -326,8 +325,7 @@ func TestTxIndexAsync(t *testing.T) { bk3 := addBlock(t, chain, &g, "bk3") // Initialize the tx index. - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() subber := NewIndexSubscriber(ctx) go subber.Run(ctx) diff --git a/internal/blockchain/utxobackend_test.go b/internal/blockchain/utxobackend_test.go index 2510b14615..72f5fc419b 100644 --- a/internal/blockchain/utxobackend_test.go +++ b/internal/blockchain/utxobackend_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2023 The Decred developers +// Copyright (c) 2021-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -17,16 +17,13 @@ import ( ) // createTestUtxoBackend creates a test backend with the utxo set bucket. -func createTestUtxoBackend(t *testing.T) UtxoBackend { +func createTestUtxoBackend(t testing.TB) UtxoBackend { t.Helper() - db, teardown, err := createTestUtxoDatabase(t) + db, err := createTestUtxoDatabase(t) if err != nil { t.Fatalf("error creating test database: %v", err) } - t.Cleanup(func() { - teardown() - }) return NewLevelDbUtxoBackend(db) } diff --git a/internal/blockchain/validate_test.go b/internal/blockchain/validate_test.go index 81cfb67cb2..040d070207 100644 --- a/internal/blockchain/validate_test.go +++ b/internal/blockchain/validate_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2016 The btcsuite developers -// Copyright (c) 2015-2025 The Decred developers +// Copyright (c) 2015-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -286,15 +286,10 @@ func TestCheckBlockHeaderContext(t *testing.T) { } // Create a test UTXO database. - utxoDb, teardownUtxoDb, err := createTestUtxoDatabase(t) - if err != nil { - t.Fatalf("Failed to create UTXO database: %v\n", err) - } - defer teardownUtxoDb() + utxoBackend := createTestUtxoBackend(t) // Create a new BlockChain instance using the underlying database for // the simnet network. - utxoBackend := NewLevelDbUtxoBackend(utxoDb) chain, err := New(context.Background(), &Config{ DB: db, diff --git a/internal/integration/rpctests/treasury_test.go b/internal/integration/rpctests/treasury_test.go index f8681ccca0..a92a81958b 100644 --- a/internal/integration/rpctests/treasury_test.go +++ b/internal/integration/rpctests/treasury_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Decred developers +// Copyright (c) 2020-2026 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -242,8 +242,7 @@ func TestTreasury(t *testing.T) { t.Fatal(err) } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() err = hn.SetUp(ctx, false, 0) if err != nil {