From 712177ee03ccb60f7659d9bcef9a2784a368b8cd Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Mon, 15 Aug 2022 21:05:01 +0800 Subject: [PATCH] chainntnfs: use `T.TempDir` to create temporary test directory Signed-off-by: Eng Zer Jun --- chainntnfs/bitcoindnotify/bitcoind_test.go | 9 +++++---- chainntnfs/btcdnotify/btcd_test.go | 9 +++++---- chainntnfs/height_hint_cache_test.go | 9 +++++---- chainntnfs/test/test_interface.go | 6 +----- chainntnfs/test_utils.go | 17 ++++------------- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind_test.go b/chainntnfs/bitcoindnotify/bitcoind_test.go index 39a29e9b3..63b2c44dc 100644 --- a/chainntnfs/bitcoindnotify/bitcoind_test.go +++ b/chainntnfs/bitcoindnotify/bitcoind_test.go @@ -5,7 +5,6 @@ package bitcoindnotify import ( "bytes" - "io/ioutil" "testing" "time" @@ -35,10 +34,12 @@ var ( func initHintCache(t *testing.T) *chainntnfs.HeightHintCache { t.Helper() - tempDir, err := ioutil.TempDir("", "kek") - require.NoError(t, err, "unable to create temp dir") - db, err := channeldb.Open(tempDir) + db, err := channeldb.Open(t.TempDir()) require.NoError(t, err, "unable to create db") + t.Cleanup(func() { + require.NoError(t, db.Close()) + }) + testCfg := chainntnfs.CacheConfig{ QueryDisable: false, } diff --git a/chainntnfs/btcdnotify/btcd_test.go b/chainntnfs/btcdnotify/btcd_test.go index 4ff3b89d4..2db472556 100644 --- a/chainntnfs/btcdnotify/btcd_test.go +++ b/chainntnfs/btcdnotify/btcd_test.go @@ -5,7 +5,6 @@ package btcdnotify import ( "bytes" - "io/ioutil" "testing" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -33,10 +32,12 @@ var ( func initHintCache(t *testing.T) *chainntnfs.HeightHintCache { t.Helper() - tempDir, err := ioutil.TempDir("", "kek") - require.NoError(t, err, "unable to create temp dir") - db, err := channeldb.Open(tempDir) + db, err := channeldb.Open(t.TempDir()) require.NoError(t, err, "unable to create db") + t.Cleanup(func() { + require.NoError(t, db.Close()) + }) + testCfg := chainntnfs.CacheConfig{ QueryDisable: false, } diff --git a/chainntnfs/height_hint_cache_test.go b/chainntnfs/height_hint_cache_test.go index f984b871d..e97a3d8ef 100644 --- a/chainntnfs/height_hint_cache_test.go +++ b/chainntnfs/height_hint_cache_test.go @@ -2,7 +2,6 @@ package chainntnfs import ( "bytes" - "io/ioutil" "testing" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -24,13 +23,15 @@ func initHintCache(t *testing.T) *HeightHintCache { func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache { t.Helper() - tempDir, err := ioutil.TempDir("", "kek") - require.NoError(t, err, "unable to create temp dir") - db, err := channeldb.Open(tempDir) + db, err := channeldb.Open(t.TempDir()) require.NoError(t, err, "unable to create db") hintCache, err := NewHeightHintCache(cfg, db.Backend) require.NoError(t, err, "unable to create hint cache") + t.Cleanup(func() { + require.NoError(t, db.Close()) + }) + return hintCache } diff --git a/chainntnfs/test/test_interface.go b/chainntnfs/test/test_interface.go index 3f81e77de..cf1aa8b9a 100644 --- a/chainntnfs/test/test_interface.go +++ b/chainntnfs/test/test_interface.go @@ -6,7 +6,6 @@ package chainntnfstest import ( "bytes" "fmt" - "io/ioutil" "log" "sync" "testing" @@ -1833,10 +1832,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) { } // Initialize a height hint cache for each notifier. - tempDir, err := ioutil.TempDir("", "channeldb") - if err != nil { - t.Fatalf("unable to create temp dir: %v", err) - } + tempDir := t.TempDir() db, err := channeldb.Open(tempDir) if err != nil { t.Fatalf("unable to create db: %v", err) diff --git a/chainntnfs/test_utils.go b/chainntnfs/test_utils.go index 2898b30cc..2040593ae 100644 --- a/chainntnfs/test_utils.go +++ b/chainntnfs/test_utils.go @@ -6,9 +6,7 @@ package chainntnfs import ( "errors" "fmt" - "io/ioutil" "math/rand" - "os" "os/exec" "path/filepath" "testing" @@ -196,8 +194,7 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, t.Helper() - tempBitcoindDir, err := ioutil.TempDir("", "bitcoind") - require.NoError(t, err, "unable to create temp dir") + tempBitcoindDir := t.TempDir() rpcPort := rand.Intn(65536-1024) + 1024 zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket" @@ -220,7 +217,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, bitcoind := exec.Command("bitcoind", args...) if err := bitcoind.Start(); err != nil { - os.RemoveAll(tempBitcoindDir) t.Fatalf("unable to start bitcoind: %v", err) } @@ -251,7 +247,8 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, } var conn *chain.BitcoindConn - err = wait.NoError(func() error { + err := wait.NoError(func() error { + var err error conn, err = chain.NewBitcoindConn(cfg) if err != nil { return err @@ -262,7 +259,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, if err != nil { bitcoind.Process.Kill() bitcoind.Wait() - os.RemoveAll(tempBitcoindDir) t.Fatalf("unable to establish connection to bitcoind: %v", err) } @@ -270,7 +266,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, conn.Stop() bitcoind.Process.Kill() bitcoind.Wait() - os.RemoveAll(tempBitcoindDir) } } @@ -279,15 +274,13 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, func()) { t.Helper() - spvDir, err := ioutil.TempDir("", "neutrino") - require.NoError(t, err, "unable to create temp dir") + spvDir := t.TempDir() dbName := filepath.Join(spvDir, "neutrino.db") spvDatabase, err := walletdb.Create( "bdb", dbName, true, kvdb.DefaultDBTimeout, ) if err != nil { - os.RemoveAll(spvDir) t.Fatalf("unable to create walletdb: %v", err) } @@ -301,7 +294,6 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, } spvNode, err := neutrino.NewChainService(spvConfig) if err != nil { - os.RemoveAll(spvDir) spvDatabase.Close() t.Fatalf("unable to create neutrino: %v", err) } @@ -316,6 +308,5 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, return spvNode, func() { spvNode.Stop() spvDatabase.Close() - os.RemoveAll(spvDir) } }