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-15 00:21:31 +08:00
parent 81f0cfdd1e
commit c229842f57
11 changed files with 31 additions and 158 deletions
+4 -12
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)
}