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"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"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) {
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
}
}
@ -105,17 +97,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

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

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

@ -6,7 +6,6 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@ -60,9 +59,7 @@ func TestNoLogRestore(t *testing.T) {
func TestLogRestore(t *testing.T) {
//stm: @OTHER_DATASTORE_RESTORE_001
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,18 +17,15 @@ 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()
//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_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_GET_KEY_001, NODE_FS_REPO_DELETE_KEY_001
repo := genFsRepo(t)
basicTest(t, repo)
}