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:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user