kvdb: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-08-15 21:07:05 +08:00
parent d926ad1f84
commit 0edc16aa01
No known key found for this signature in database
GPG key ID: DAEBBD2E34C111E6
3 changed files with 6 additions and 25 deletions

View file

@ -1,8 +1,6 @@
package kvdb package kvdb
import ( import (
"io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -16,19 +14,12 @@ type boltFixture struct {
} }
func NewBoltFixture(t *testing.T) *boltFixture { func NewBoltFixture(t *testing.T) *boltFixture {
tempDir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
return &boltFixture{ return &boltFixture{
t: t, t: t,
tempDir: tempDir, tempDir: t.TempDir(),
} }
} }
func (b *boltFixture) Cleanup() {
os.RemoveAll(b.tempDir)
}
func (b *boltFixture) NewBackend() walletdb.DB { func (b *boltFixture) NewBackend() walletdb.DB {
dbPath := filepath.Join(b.tempDir) dbPath := filepath.Join(b.tempDir)

View file

@ -76,7 +76,6 @@ func TestBolt(t *testing.T) {
t.Parallel() t.Parallel()
f := NewBoltFixture(t) f := NewBoltFixture(t)
defer f.Cleanup()
test.test(t, f.NewBackend()) test.test(t, f.NewBackend())
}) })

View file

@ -5,8 +5,6 @@ package etcd
import ( import (
"context" "context"
"io/ioutil"
"os"
"testing" "testing"
"time" "time"
@ -46,10 +44,7 @@ func NewTestEtcdInstance(t *testing.T, path string) (*Config, func()) {
// NewTestEtcdTestFixture creates a new etcd-test fixture. This is helper // NewTestEtcdTestFixture creates a new etcd-test fixture. This is helper
// object to facilitate etcd tests and ensure pre and post conditions. // object to facilitate etcd tests and ensure pre and post conditions.
func NewEtcdTestFixture(t *testing.T) *EtcdTestFixture { func NewEtcdTestFixture(t *testing.T) *EtcdTestFixture {
tmpDir, err := ioutil.TempDir("", "etcd") tmpDir := t.TempDir()
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
config, etcdCleanup := NewTestEtcdInstance(t, tmpDir) config, etcdCleanup := NewTestEtcdInstance(t, tmpDir)
@ -59,7 +54,6 @@ func NewEtcdTestFixture(t *testing.T) *EtcdTestFixture {
Password: config.Pass, Password: config.Pass,
}) })
if err != nil { if err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("unable to create etcd test fixture: %v", err) t.Fatalf("unable to create etcd test fixture: %v", err)
} }
@ -69,13 +63,10 @@ func NewEtcdTestFixture(t *testing.T) *EtcdTestFixture {
cli.Lease = namespace.NewLease(cli.Lease, defaultNamespace) cli.Lease = namespace.NewLease(cli.Lease, defaultNamespace)
return &EtcdTestFixture{ return &EtcdTestFixture{
t: t, t: t,
cli: cli, cli: cli,
config: config, config: config,
cleanup: func() { cleanup: etcdCleanup,
etcdCleanup()
os.RemoveAll(tmpDir)
},
} }
} }