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 <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-03-11 14:27:36 +08:00
parent 81f0cfdd1e
commit c229842f57
No known key found for this signature in database
GPG Key ID: DAEBBD2E34C111E6
11 changed files with 31 additions and 158 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "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) { return func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
tb.Helper() tb.Helper()
path, err := ioutil.TempDir("", "") path = tb.TempDir()
if err != nil {
tb.Fatal(err)
}
db, err := Open(optsSupplier(path)) db, err := Open(optsSupplier(path))
if err != nil { if err != nil {
tb.Fatal(err) tb.Fatal(err)
} }
tb.Cleanup(func() {
_ = os.RemoveAll(path)
})
return db, 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) { func testMove(t *testing.T, optsF func(string) Options) {
ctx := context.Background() ctx := context.Background()
basePath, err := ioutil.TempDir("", "") basePath := t.TempDir()
if err != nil {
t.Fatal(err)
}
dbPath := filepath.Join(basePath, "db") dbPath := filepath.Join(basePath, "db")
t.Cleanup(func() {
_ = os.RemoveAll(basePath)
})
db, err := Open(optsF(dbPath)) db, err := Open(optsF(dbPath))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -1,8 +1,6 @@
package splitstore package splitstore
import ( import (
"io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -11,14 +9,7 @@ import (
) )
func TestCheckpoint(t *testing.T) { func TestCheckpoint(t *testing.T) {
dir, err := ioutil.TempDir("", "checkpoint.*") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
path := filepath.Join(dir, "checkpoint") path := filepath.Join(dir, "checkpoint")

View File

@ -2,8 +2,6 @@ package splitstore
import ( import (
"fmt" "fmt"
"io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -12,14 +10,7 @@ import (
) )
func TestColdSet(t *testing.T) { func TestColdSet(t *testing.T) {
dir, err := ioutil.TempDir("", "coldset.*") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
path := filepath.Join(dir, "coldset") path := filepath.Join(dir, "coldset")

View File

@ -1,8 +1,6 @@
package splitstore package splitstore
import ( import (
"io/ioutil"
"os"
"testing" "testing"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
@ -31,14 +29,7 @@ func TestBadgerMarkSet(t *testing.T) {
} }
func testMarkSet(t *testing.T, lsType string) { func testMarkSet(t *testing.T, lsType string) {
path, err := ioutil.TempDir("", "markset.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
env, err := OpenMarkSetEnv(path, lsType) env, err := OpenMarkSetEnv(path, lsType)
if err != nil { if err != nil {
@ -156,14 +147,7 @@ func testMarkSet(t *testing.T, lsType string) {
} }
func testMarkSetVisitor(t *testing.T, lsType string) { func testMarkSetVisitor(t *testing.T, lsType string) {
path, err := ioutil.TempDir("", "markset.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
env, err := OpenMarkSetEnv(path, lsType) env, err := OpenMarkSetEnv(path, lsType)
if err != nil { if err != nil {
@ -225,14 +209,7 @@ func testMarkSetVisitor(t *testing.T, lsType string) {
} }
func testMarkSetVisitorRecovery(t *testing.T, lsType string) { func testMarkSetVisitorRecovery(t *testing.T, lsType string) {
path, err := ioutil.TempDir("", "markset.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
env, err := OpenMarkSetEnv(path, lsType) env, err := OpenMarkSetEnv(path, lsType)
if err != nil { if err != nil {
@ -324,14 +301,7 @@ func testMarkSetVisitorRecovery(t *testing.T, lsType string) {
} }
func testMarkSetRecovery(t *testing.T, lsType string) { func testMarkSetRecovery(t *testing.T, lsType string) {
path, err := ioutil.TempDir("", "markset.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
env, err := OpenMarkSetEnv(path, lsType) env, err := OpenMarkSetEnv(path, lsType)
if err != nil { if err != nil {
@ -437,14 +407,7 @@ func testMarkSetRecovery(t *testing.T, lsType string) {
} }
func testMarkSetMarkMany(t *testing.T, lsType string) { func testMarkSetMarkMany(t *testing.T, lsType string) {
path, err := ioutil.TempDir("", "markset.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
env, err := OpenMarkSetEnv(path, lsType) env, err := OpenMarkSetEnv(path, lsType)
if err != nil { if err != nil {

View File

@ -4,9 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -85,14 +83,7 @@ func testSplitStore(t *testing.T, cfg *Config) {
t.Fatal(err) t.Fatal(err)
} }
path, err := ioutil.TempDir("", "splitstore.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
// open the splitstore // open the splitstore
ss, err := Open(path, ds, hot, cold, cfg) ss, err := Open(path, ds, hot, cold, cfg)
@ -277,14 +268,7 @@ func TestSplitStoreSuppressCompactionNearUpgrade(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
path, err := ioutil.TempDir("", "splitstore.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
// open the splitstore // open the splitstore
ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) 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.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"})
if err != nil { if err != nil {
@ -531,14 +508,7 @@ func testSplitStoreReificationLimit(t *testing.T, f func(context.Context, blocks
} }
} }
path, err := ioutil.TempDir("", "splitstore.*") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(path)
})
ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"}) ss, err := Open(path, ds, hot, cold, &Config{MarkSetType: "map"})
if err != nil { if err != nil {

View File

@ -295,7 +295,7 @@ func TestSealAndVerify(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cleanup := func() { t.Cleanup(func() {
if t.Failed() { if t.Failed() {
fmt.Printf("not removing %s\n", cdir) fmt.Printf("not removing %s\n", cdir)
return return
@ -303,8 +303,7 @@ func TestSealAndVerify(t *testing.T) {
if err := os.RemoveAll(cdir); err != nil { if err := os.RemoveAll(cdir); err != nil {
t.Error(err) t.Error(err)
} }
} })
defer cleanup()
si := storage.SectorRef{ si := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1}, ID: abi.SectorID{Miner: miner, Number: 1},
@ -369,7 +368,7 @@ func TestSealPoStNoCommit(t *testing.T) {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cleanup := func() { t.Cleanup(func() {
if t.Failed() { if t.Failed() {
fmt.Printf("not removing %s\n", dir) fmt.Printf("not removing %s\n", dir)
return return
@ -377,8 +376,7 @@ func TestSealPoStNoCommit(t *testing.T) {
if err := os.RemoveAll(dir); err != nil { if err := os.RemoveAll(dir); err != nil {
t.Error(err) t.Error(err)
} }
} })
defer cleanup()
si := storage.SectorRef{ si := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1}, ID: abi.SectorID{Miner: miner, Number: 1},
@ -434,13 +432,11 @@ func TestSealAndVerify3(t *testing.T) {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cleanup := func() { t.Cleanup(func() {
if err := os.RemoveAll(dir); err != nil { if err := os.RemoveAll(dir); err != nil {
t.Error(err) t.Error(err)
} }
} })
defer cleanup()
var wg sync.WaitGroup var wg sync.WaitGroup
@ -512,7 +508,7 @@ func TestSealAndVerifyAggregate(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cleanup := func() { t.Cleanup(func() {
if t.Failed() { if t.Failed() {
fmt.Printf("not removing %s\n", cdir) fmt.Printf("not removing %s\n", cdir)
return return
@ -520,8 +516,7 @@ func TestSealAndVerifyAggregate(t *testing.T) {
if err := os.RemoveAll(cdir); err != nil { if err := os.RemoveAll(cdir); err != nil {
t.Error(err) t.Error(err)
} }
} })
defer cleanup()
avi := proof5.AggregateSealVerifyProofAndInfos{ avi := proof5.AggregateSealVerifyProofAndInfos{
Miner: miner, Miner: miner,
@ -917,7 +912,7 @@ func TestMulticoreSDR(t *testing.T) {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cleanup := func() { t.Cleanup(func() {
if t.Failed() { if t.Failed() {
fmt.Printf("not removing %s\n", dir) fmt.Printf("not removing %s\n", dir)
return return
@ -925,8 +920,7 @@ func TestMulticoreSDR(t *testing.T) {
if err := os.RemoveAll(dir); err != nil { if err := os.RemoveAll(dir); err != nil {
t.Error(err) t.Error(err)
} }
} })
defer cleanup()
si := storage.SectorRef{ si := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1}, ID: abi.SectorID{Miner: miner, Number: 1},

View File

@ -397,12 +397,7 @@ func TestRemoteGetSector(t *testing.T) {
stat, err := os.Stat(tempFile2.Name()) stat, err := os.Stat(tempFile2.Name())
require.NoError(t, err) require.NoError(t, err)
tempDir, err := ioutil.TempDir("", "TestRemoteGetSector-") tempDir := t.TempDir()
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
require.NoError(t, os.Rename(tempFile2.Name(), filepath.Join(tempDir, stat.Name()))) require.NoError(t, os.Rename(tempFile2.Name(), filepath.Join(tempDir, stat.Name())))

View File

@ -74,8 +74,7 @@ var _ LocalStorage = &TestingLocalStorage{}
func TestLocalStorage(t *testing.T) { func TestLocalStorage(t *testing.T) {
ctx := context.TODO() ctx := context.TODO()
root, err := ioutil.TempDir("", "sector-storage-teststorage-") root := t.TempDir()
require.NoError(t, err)
tstor := &TestingLocalStorage{ tstor := &TestingLocalStorage{
root: root, root: root,

View File

@ -64,11 +64,7 @@ func TestMoveShared(t *testing.T) {
ctx := context.Background() ctx := context.Background()
dir, err := ioutil.TempDir("", "stores-remote-test-") dir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
openRepo := func(dir string) repo.LockedRepo { openRepo := func(dir string) repo.LockedRepo {
r, err := repo.NewFS(dir) r, err := repo.NewFS(dir)

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -57,9 +56,7 @@ func TestNoLogRestore(t *testing.T) {
} }
func TestLogRestore(t *testing.T) { func TestLogRestore(t *testing.T) {
logdir, err := ioutil.TempDir("", "backupds-test-") logdir := t.TempDir()
require.NoError(t, err)
defer os.RemoveAll(logdir) // nolint
ds1 := datastore.NewMapDatastore() ds1 := datastore.NewMapDatastore()

View File

@ -2,16 +2,11 @@
package repo package repo
import ( import (
"io/ioutil"
"os"
"testing" "testing"
) )
func genFsRepo(t *testing.T) (*FsRepo, func()) { func genFsRepo(t *testing.T) *FsRepo {
path, err := ioutil.TempDir("", "lotus-repo-") path := t.TempDir()
if err != nil {
t.Fatal(err)
}
repo, err := NewFS(path) repo, err := NewFS(path)
if err != nil { if err != nil {
@ -22,13 +17,10 @@ func genFsRepo(t *testing.T) (*FsRepo, func()) {
if err != ErrRepoExists && err != nil { if err != ErrRepoExists && err != nil {
t.Fatal(err) t.Fatal(err)
} }
return repo, func() { return repo
_ = os.RemoveAll(path)
}
} }
func TestFsBasic(t *testing.T) { func TestFsBasic(t *testing.T) {
repo, closer := genFsRepo(t) repo := genFsRepo(t)
defer closer()
basicTest(t, repo) basicTest(t, repo)
} }