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"
"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)

View File

@ -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")

View File

@ -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")

View File

@ -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 {

View File

@ -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 {

View File

@ -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},

View File

@ -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())))

View File

@ -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,

View File

@ -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)

View File

@ -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()

View File

@ -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)
}