2021-12-28 01:06:31 +00:00
|
|
|
package bolttest
|
2021-10-05 06:52:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-12-28 01:06:31 +00:00
|
|
|
// New returns a temporary empty database bbolt in the temporary directory
|
2021-11-14 21:12:51 +00:00
|
|
|
// with the cleanup function.
|
2021-12-28 01:06:31 +00:00
|
|
|
func New(tb testing.TB) (*bolt.DB, func()) {
|
2021-10-05 06:52:04 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
tempFile, err := os.CreateTemp(os.TempDir(), "bbolt_*.db")
|
2022-02-01 19:03:08 +00:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-10-05 06:52:04 +00:00
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
filePath := tempFile.Name()
|
2022-02-01 19:03:08 +00:00
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if err = tempFile.Close(); err != nil {
|
2022-02-01 19:03:08 +00:00
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-10-05 06:52:04 +00:00
|
|
|
|
|
|
|
db, err := bolt.Open(filePath, os.ModePerm, nil)
|
2022-02-01 19:03:08 +00:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-10-05 06:52:04 +00:00
|
|
|
|
|
|
|
return db, func() {
|
2022-02-01 19:03:08 +00:00
|
|
|
_ = db.Close()
|
|
|
|
_ = os.Remove(filePath)
|
2021-10-05 06:52:04 +00:00
|
|
|
}
|
|
|
|
}
|