2016-04-11 20:43:18 -07:00
|
|
|
package vain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-06-22 23:01:21 -07:00
|
|
|
// TestDB returns a populated MemDB in a temp location, as well as a function
|
|
|
|
// to call at cleanup time.
|
|
|
|
func TestDB(t *testing.T) (*MemDB, func()) {
|
2016-04-11 20:43:18 -07:00
|
|
|
dir, err := ioutil.TempDir("", "vain-testing-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create tmpdir for db: %v", err)
|
|
|
|
return nil, func() {}
|
|
|
|
}
|
2016-06-22 23:01:21 -07:00
|
|
|
name := filepath.Join(dir, "test.json")
|
|
|
|
db, err := NewMemDB(name)
|
2016-04-11 20:43:18 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create db: %v", err)
|
|
|
|
return nil, func() {}
|
|
|
|
}
|
|
|
|
return db, func() {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Fatalf("could not clean up tmpdir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|