make helper NewTestDB more helpful with t.TempDir

main
Bel LaPointe 2024-04-12 09:10:23 -06:00
parent 8883856a63
commit 6ba23e61c3
7 changed files with 16 additions and 24 deletions

View File

@ -3,7 +3,7 @@ package main
import (
"context"
"io/ioutil"
"os"
"path"
"time"
"go.etcd.io/bbolt"
@ -20,12 +20,12 @@ type BBolt struct {
db *bbolt.DB
}
func NewTestDB() BBolt {
d, err := ioutil.TempDir(os.TempDir(), "test-db-*")
func NewTestDBIn(d string) BBolt {
d, err := ioutil.TempDir(d, "test-db-*")
if err != nil {
panic(err)
}
db, err := NewDB(d)
db, err := NewDB(path.Join(d, "bb"))
if err != nil {
panic(err)
}

View File

@ -4,18 +4,11 @@ import (
"context"
"errors"
"io"
"path"
"testing"
)
func TestDriverBBolt(t *testing.T) {
bb, err := NewDB(path.Join(t.TempDir(), "bb"))
if err != nil {
t.Fatal(err)
}
defer bb.Close()
testDriver(t, bb)
testDriver(t, NewTestDBIn(t.TempDir()))
}
func testDriver(t *testing.T, d Driver) {

View File

@ -11,8 +11,8 @@ type Queue struct {
driver Driver
}
func NewTestQueue() Queue {
return Queue{driver: NewTestDB()}
func NewTestQueueIn(d string) Queue {
return Queue{driver: NewTestDBIn(d)}
}
func NewQueue(driver Driver) Queue {

View File

@ -2,7 +2,6 @@ package main
import (
"context"
"path"
"strconv"
"testing"
"time"
@ -12,10 +11,7 @@ func TestQueue(t *testing.T) {
ctx, can := context.WithTimeout(context.Background(), time.Second*10)
defer can()
db, err := NewDB(path.Join(t.TempDir(), "q"))
if err != nil {
t.Fatal(err)
}
db := NewTestDBIn(t.TempDir())
defer db.Close()
q := NewQueue(db)

View File

@ -13,8 +13,8 @@ type Storage struct {
driver Driver
}
func NewTestStorage() Storage {
return Storage{driver: NewTestDB()}
func NewTestStorageIn(d string) Storage {
return Storage{driver: NewTestDBIn(d)}
}
func NewStorage(driver Driver) Storage {

6
storage_test.go Normal file
View File

@ -0,0 +1,6 @@
package main
import "testing"
func TestStorage(t *testing.T) {
}

View File

@ -1,3 +0,0 @@
package main
type Writer struct{}