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 11:41:08 +08:00
parent ed4cc4b482
commit 18ec2761c0
40 changed files with 137 additions and 428 deletions

View File

@@ -20,10 +20,8 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@@ -715,10 +713,7 @@ func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) {
ctx = namespaces.WithNamespace(ctx, "testing")
ctx = logtest.WithT(ctx, t)
dirname, err := os.MkdirTemp("", strings.Replace(t.Name(), "/", "_", -1)+"-")
if err != nil {
t.Fatal(err)
}
dirname := t.TempDir()
db, err := bolt.Open(filepath.Join(dirname, "meta.db"), 0644, nil)
if err != nil {
@@ -727,9 +722,6 @@ func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) {
return ctx, db, func() {
db.Close()
if err := os.RemoveAll(dirname); err != nil {
t.Log("failed removing temp dir", err)
}
cancel()
}
}

View File

@@ -23,7 +23,6 @@ import (
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"runtime/pprof"
"strings"
@@ -73,10 +72,7 @@ func testDB(t *testing.T, opt ...testOpt) (context.Context, *DB, func()) {
o(&topts)
}
dirname, err := os.MkdirTemp("", strings.Replace(t.Name(), "/", "_", -1)+"-")
if err != nil {
t.Fatal(err)
}
dirname := t.TempDir()
snapshotter, err := native.NewSnapshotter(filepath.Join(dirname, "native"))
if err != nil {
@@ -112,9 +108,6 @@ func testDB(t *testing.T, opt ...testOpt) (context.Context, *DB, func()) {
return ctx, db, func() {
bdb.Close()
if err := os.RemoveAll(dirname); err != nil {
t.Log("failed removing temp dir", err)
}
cancel()
}
}
@@ -764,10 +757,7 @@ type testLease struct {
}
func newStores(t testing.TB) (*DB, content.Store, snapshots.Snapshotter, func()) {
td, err := os.MkdirTemp("", "gc-test-")
if err != nil {
t.Fatal(err)
}
td := t.TempDir()
db, err := bolt.Open(filepath.Join(td, "meta.db"), 0644, nil)
if err != nil {
t.Fatal(err)
@@ -786,6 +776,7 @@ func newStores(t testing.TB) (*DB, content.Store, snapshots.Snapshotter, func())
mdb := NewDB(db, lcs, map[string]snapshots.Snapshotter{"native": nsn})
return mdb, mdb.ContentStore(), mdb.Snapshotter("native"), func() {
os.RemoveAll(td)
nsn.Close()
db.Close()
}
}

View File

@@ -20,7 +20,6 @@ import (
"context"
"io"
"math/rand"
"os"
"path/filepath"
"sort"
"testing"
@@ -42,7 +41,7 @@ func TestResourceMax(t *testing.T) {
}
func TestGCRoots(t *testing.T) {
db, cleanup, err := newDatabase()
db, cleanup, err := newDatabase(t)
if err != nil {
t.Fatal(err)
}
@@ -163,7 +162,7 @@ func TestGCRoots(t *testing.T) {
}
func TestGCRemove(t *testing.T) {
db, cleanup, err := newDatabase()
db, cleanup, err := newDatabase(t)
if err != nil {
t.Fatal(err)
}
@@ -256,7 +255,7 @@ func TestGCRemove(t *testing.T) {
}
func TestGCRefs(t *testing.T) {
db, cleanup, err := newDatabase()
db, cleanup, err := newDatabase(t)
if err != nil {
t.Fatal(err)
}
@@ -387,21 +386,16 @@ func TestGCRefs(t *testing.T) {
}
}
func newDatabase() (*bolt.DB, func(), error) {
td, err := os.MkdirTemp("", "gc-roots-")
if err != nil {
return nil, nil, err
}
func newDatabase(t testing.TB) (*bolt.DB, func(), error) {
td := t.TempDir()
db, err := bolt.Open(filepath.Join(td, "test.db"), 0777, nil)
if err != nil {
os.RemoveAll(td)
return nil, nil, err
}
return db, func() {
db.Close()
os.RemoveAll(td)
}, nil
}