Merge pull request #8295 from Juneezee/test/t.TempDir

test: use `T.TempDir` to create temporary test directory
This commit is contained in:
Łukasz Magiera 2022-03-17 12:30:41 +01:00 committed by GitHub
commit 73ab064137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 31 additions and 158 deletions

View File

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -78,20 +77,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
} }
} }
@ -105,17 +97,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

@ -2,8 +2,6 @@
package splitstore package splitstore
import ( import (
"io/ioutil"
"os"
"testing" "testing"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
@ -36,14 +34,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 {
@ -165,14 +156,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 {
@ -235,14 +219,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 {
@ -334,14 +311,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 {
@ -447,14 +417,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

@ -5,9 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -86,14 +84,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)
@ -287,14 +278,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"})
@ -434,14 +418,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 {
@ -541,14 +518,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

@ -6,7 +6,6 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -60,9 +59,7 @@ func TestNoLogRestore(t *testing.T) {
func TestLogRestore(t *testing.T) { func TestLogRestore(t *testing.T) {
//stm: @OTHER_DATASTORE_RESTORE_001 //stm: @OTHER_DATASTORE_RESTORE_001
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,18 +17,15 @@ 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)
defer closer()
//stm: @NODE_FS_REPO_LOCK_001,@NODE_FS_REPO_LOCK_002,@NODE_FS_REPO_UNLOCK_001 //stm: @NODE_FS_REPO_LOCK_001,@NODE_FS_REPO_LOCK_002,@NODE_FS_REPO_UNLOCK_001
//stm: @NODE_FS_REPO_SET_API_ENDPOINT_001, @NODE_FS_REPO_GET_API_ENDPOINT_001 //stm: @NODE_FS_REPO_SET_API_ENDPOINT_001, @NODE_FS_REPO_GET_API_ENDPOINT_001
//stm: @NODE_FS_REPO_GET_CONFIG_001, @NODE_FS_REPO_SET_CONFIG_001 //stm: @NODE_FS_REPO_GET_CONFIG_001, @NODE_FS_REPO_SET_CONFIG_001
//stm: @NODE_FS_REPO_LIST_KEYS_001, @NODE_FS_REPO_PUT_KEY_001 //stm: @NODE_FS_REPO_LIST_KEYS_001, @NODE_FS_REPO_PUT_KEY_001
//stm: @NODE_FS_REPO_GET_KEY_001, NODE_FS_REPO_DELETE_KEY_001 //stm: @NODE_FS_REPO_GET_KEY_001, NODE_FS_REPO_DELETE_KEY_001
repo := genFsRepo(t)
basicTest(t, repo) basicTest(t, repo)
} }