From c229842f5742a2afb472d5836b7dc4245d862bf0 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 11 Mar 2022 14:27:36 +0800 Subject: [PATCH] test: use `T.TempDir` to create temporary test directory The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun --- blockstore/badger/blockstore_test.go | 19 +------- blockstore/splitstore/checkpoint_test.go | 11 +---- blockstore/splitstore/coldset_test.go | 11 +---- blockstore/splitstore/markset_test.go | 47 ++----------------- blockstore/splitstore/splitstore_test.go | 38 ++------------- .../sector-storage/ffiwrapper/sealer_test.go | 26 ++++------ .../stores/http_handler_test.go | 7 +-- extern/sector-storage/stores/local_test.go | 3 +- extern/sector-storage/stores/remote_test.go | 6 +-- lib/backupds/backupds_test.go | 5 +- node/repo/fsrepo_test.go | 16 ++----- 11 files changed, 31 insertions(+), 158 deletions(-) diff --git a/blockstore/badger/blockstore_test.go b/blockstore/badger/blockstore_test.go index 4619d4ec3..77e33279b 100644 --- a/blockstore/badger/blockstore_test.go +++ b/blockstore/badger/blockstore_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -73,20 +72,13 @@ func newBlockstore(optsSupplier func(path string) Options) func(tb testing.TB) ( return func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) { tb.Helper() - path, err := ioutil.TempDir("", "") - if err != nil { - tb.Fatal(err) - } + path = tb.TempDir() db, err := Open(optsSupplier(path)) if err != nil { tb.Fatal(err) } - tb.Cleanup(func() { - _ = os.RemoveAll(path) - }) - return db, path } } @@ -100,17 +92,10 @@ func openBlockstore(optsSupplier func(path string) Options) func(tb testing.TB, func testMove(t *testing.T, optsF func(string) Options) { ctx := context.Background() - basePath, err := ioutil.TempDir("", "") - if err != nil { - t.Fatal(err) - } + basePath := t.TempDir() dbPath := filepath.Join(basePath, "db") - t.Cleanup(func() { - _ = os.RemoveAll(basePath) - }) - db, err := Open(optsF(dbPath)) if err != nil { t.Fatal(err) diff --git a/blockstore/splitstore/checkpoint_test.go b/blockstore/splitstore/checkpoint_test.go index 4fefe40cf..241707d65 100644 --- a/blockstore/splitstore/checkpoint_test.go +++ b/blockstore/splitstore/checkpoint_test.go @@ -1,8 +1,6 @@ package splitstore import ( - "io/ioutil" - "os" "path/filepath" "testing" @@ -11,14 +9,7 @@ import ( ) func TestCheckpoint(t *testing.T) { - dir, err := ioutil.TempDir("", "checkpoint.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(dir) - }) + dir := t.TempDir() path := filepath.Join(dir, "checkpoint") diff --git a/blockstore/splitstore/coldset_test.go b/blockstore/splitstore/coldset_test.go index 60216ebd4..8fc23a68e 100644 --- a/blockstore/splitstore/coldset_test.go +++ b/blockstore/splitstore/coldset_test.go @@ -2,8 +2,6 @@ package splitstore import ( "fmt" - "io/ioutil" - "os" "path/filepath" "testing" @@ -12,14 +10,7 @@ import ( ) func TestColdSet(t *testing.T) { - dir, err := ioutil.TempDir("", "coldset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(dir) - }) + dir := t.TempDir() path := filepath.Join(dir, "coldset") diff --git a/blockstore/splitstore/markset_test.go b/blockstore/splitstore/markset_test.go index b4b871602..ef70ccfc2 100644 --- a/blockstore/splitstore/markset_test.go +++ b/blockstore/splitstore/markset_test.go @@ -1,8 +1,6 @@ package splitstore import ( - "io/ioutil" - "os" "testing" cid "github.com/ipfs/go-cid" @@ -31,14 +29,7 @@ func TestBadgerMarkSet(t *testing.T) { } func testMarkSet(t *testing.T, lsType string) { - path, err := ioutil.TempDir("", "markset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() env, err := OpenMarkSetEnv(path, lsType) if err != nil { @@ -156,14 +147,7 @@ func testMarkSet(t *testing.T, lsType string) { } func testMarkSetVisitor(t *testing.T, lsType string) { - path, err := ioutil.TempDir("", "markset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() env, err := OpenMarkSetEnv(path, lsType) if err != nil { @@ -225,14 +209,7 @@ func testMarkSetVisitor(t *testing.T, lsType string) { } func testMarkSetVisitorRecovery(t *testing.T, lsType string) { - path, err := ioutil.TempDir("", "markset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() env, err := OpenMarkSetEnv(path, lsType) if err != nil { @@ -324,14 +301,7 @@ func testMarkSetVisitorRecovery(t *testing.T, lsType string) { } func testMarkSetRecovery(t *testing.T, lsType string) { - path, err := ioutil.TempDir("", "markset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() env, err := OpenMarkSetEnv(path, lsType) if err != nil { @@ -437,14 +407,7 @@ func testMarkSetRecovery(t *testing.T, lsType string) { } func testMarkSetMarkMany(t *testing.T, lsType string) { - path, err := ioutil.TempDir("", "markset.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() env, err := OpenMarkSetEnv(path, lsType) if err != nil { diff --git a/blockstore/splitstore/splitstore_test.go b/blockstore/splitstore/splitstore_test.go index ee30400a4..3b0b861cf 100644 --- a/blockstore/splitstore/splitstore_test.go +++ b/blockstore/splitstore/splitstore_test.go @@ -4,9 +4,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" "math/rand" - "os" "sync" "sync/atomic" "testing" @@ -85,14 +83,7 @@ func testSplitStore(t *testing.T, cfg *Config) { t.Fatal(err) } - path, err := ioutil.TempDir("", "splitstore.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() // open the splitstore ss, err := Open(path, ds, hot, cold, cfg) @@ -277,14 +268,7 @@ func TestSplitStoreSuppressCompactionNearUpgrade(t *testing.T) { t.Fatal(err) } - path, err := ioutil.TempDir("", "splitstore.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() // open the splitstore ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) @@ -424,14 +408,7 @@ func testSplitStoreReification(t *testing.T, f func(context.Context, blockstore. } } - path, err := ioutil.TempDir("", "splitstore.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) if err != nil { @@ -531,14 +508,7 @@ func testSplitStoreReificationLimit(t *testing.T, f func(context.Context, blocks } } - path, err := ioutil.TempDir("", "splitstore.*") - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _ = os.RemoveAll(path) - }) + path := t.TempDir() ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) if err != nil { diff --git a/extern/sector-storage/ffiwrapper/sealer_test.go b/extern/sector-storage/ffiwrapper/sealer_test.go index cf8978464..191d8a9d1 100644 --- a/extern/sector-storage/ffiwrapper/sealer_test.go +++ b/extern/sector-storage/ffiwrapper/sealer_test.go @@ -295,7 +295,7 @@ func TestSealAndVerify(t *testing.T) { if err != nil { t.Fatalf("%+v", err) } - cleanup := func() { + t.Cleanup(func() { if t.Failed() { fmt.Printf("not removing %s\n", cdir) return @@ -303,8 +303,7 @@ func TestSealAndVerify(t *testing.T) { if err := os.RemoveAll(cdir); err != nil { t.Error(err) } - } - defer cleanup() + }) si := storage.SectorRef{ ID: abi.SectorID{Miner: miner, Number: 1}, @@ -369,7 +368,7 @@ func TestSealPoStNoCommit(t *testing.T) { t.Fatalf("%+v", err) } - cleanup := func() { + t.Cleanup(func() { if t.Failed() { fmt.Printf("not removing %s\n", dir) return @@ -377,8 +376,7 @@ func TestSealPoStNoCommit(t *testing.T) { if err := os.RemoveAll(dir); err != nil { t.Error(err) } - } - defer cleanup() + }) si := storage.SectorRef{ ID: abi.SectorID{Miner: miner, Number: 1}, @@ -434,13 +432,11 @@ func TestSealAndVerify3(t *testing.T) { t.Fatalf("%+v", err) } - cleanup := func() { + t.Cleanup(func() { if err := os.RemoveAll(dir); err != nil { t.Error(err) } - } - - defer cleanup() + }) var wg sync.WaitGroup @@ -512,7 +508,7 @@ func TestSealAndVerifyAggregate(t *testing.T) { if err != nil { t.Fatalf("%+v", err) } - cleanup := func() { + t.Cleanup(func() { if t.Failed() { fmt.Printf("not removing %s\n", cdir) return @@ -520,8 +516,7 @@ func TestSealAndVerifyAggregate(t *testing.T) { if err := os.RemoveAll(cdir); err != nil { t.Error(err) } - } - defer cleanup() + }) avi := proof5.AggregateSealVerifyProofAndInfos{ Miner: miner, @@ -917,7 +912,7 @@ func TestMulticoreSDR(t *testing.T) { t.Fatalf("%+v", err) } - cleanup := func() { + t.Cleanup(func() { if t.Failed() { fmt.Printf("not removing %s\n", dir) return @@ -925,8 +920,7 @@ func TestMulticoreSDR(t *testing.T) { if err := os.RemoveAll(dir); err != nil { t.Error(err) } - } - defer cleanup() + }) si := storage.SectorRef{ ID: abi.SectorID{Miner: miner, Number: 1}, diff --git a/extern/sector-storage/stores/http_handler_test.go b/extern/sector-storage/stores/http_handler_test.go index 673aba55d..257807574 100644 --- a/extern/sector-storage/stores/http_handler_test.go +++ b/extern/sector-storage/stores/http_handler_test.go @@ -397,12 +397,7 @@ func TestRemoteGetSector(t *testing.T) { stat, err := os.Stat(tempFile2.Name()) require.NoError(t, err) - tempDir, err := ioutil.TempDir("", "TestRemoteGetSector-") - require.NoError(t, err) - - defer func() { - _ = os.RemoveAll(tempDir) - }() + tempDir := t.TempDir() require.NoError(t, os.Rename(tempFile2.Name(), filepath.Join(tempDir, stat.Name()))) diff --git a/extern/sector-storage/stores/local_test.go b/extern/sector-storage/stores/local_test.go index ac5f6f341..cd8222a93 100644 --- a/extern/sector-storage/stores/local_test.go +++ b/extern/sector-storage/stores/local_test.go @@ -74,8 +74,7 @@ var _ LocalStorage = &TestingLocalStorage{} func TestLocalStorage(t *testing.T) { ctx := context.TODO() - root, err := ioutil.TempDir("", "sector-storage-teststorage-") - require.NoError(t, err) + root := t.TempDir() tstor := &TestingLocalStorage{ root: root, diff --git a/extern/sector-storage/stores/remote_test.go b/extern/sector-storage/stores/remote_test.go index a7a82a728..239da9879 100644 --- a/extern/sector-storage/stores/remote_test.go +++ b/extern/sector-storage/stores/remote_test.go @@ -64,11 +64,7 @@ func TestMoveShared(t *testing.T) { ctx := context.Background() - dir, err := ioutil.TempDir("", "stores-remote-test-") - require.NoError(t, err) - t.Cleanup(func() { - _ = os.RemoveAll(dir) - }) + dir := t.TempDir() openRepo := func(dir string) repo.LockedRepo { r, err := repo.NewFS(dir) diff --git a/lib/backupds/backupds_test.go b/lib/backupds/backupds_test.go index c681491e3..904f3881c 100644 --- a/lib/backupds/backupds_test.go +++ b/lib/backupds/backupds_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io/ioutil" - "os" "path/filepath" "strings" "testing" @@ -57,9 +56,7 @@ func TestNoLogRestore(t *testing.T) { } func TestLogRestore(t *testing.T) { - logdir, err := ioutil.TempDir("", "backupds-test-") - require.NoError(t, err) - defer os.RemoveAll(logdir) // nolint + logdir := t.TempDir() ds1 := datastore.NewMapDatastore() diff --git a/node/repo/fsrepo_test.go b/node/repo/fsrepo_test.go index 381ebdcbe..f87bd51b7 100644 --- a/node/repo/fsrepo_test.go +++ b/node/repo/fsrepo_test.go @@ -2,16 +2,11 @@ package repo import ( - "io/ioutil" - "os" "testing" ) -func genFsRepo(t *testing.T) (*FsRepo, func()) { - path, err := ioutil.TempDir("", "lotus-repo-") - if err != nil { - t.Fatal(err) - } +func genFsRepo(t *testing.T) *FsRepo { + path := t.TempDir() repo, err := NewFS(path) if err != nil { @@ -22,13 +17,10 @@ func genFsRepo(t *testing.T) (*FsRepo, func()) { if err != ErrRepoExists && err != nil { t.Fatal(err) } - return repo, func() { - _ = os.RemoveAll(path) - } + return repo } func TestFsBasic(t *testing.T) { - repo, closer := genFsRepo(t) - defer closer() + repo := genFsRepo(t) basicTest(t, repo) }