♻️ Refactored TestBolt util usage

This commit is contained in:
Maxim Lebedev 2021-11-15 02:12:51 +05:00
parent 4654988dd1
commit 1a183bbb67
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 7 additions and 14 deletions

View File

@ -9,10 +9,12 @@ import (
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
func TestBolt(tb testing.TB, buckets ...[]byte) (*bolt.DB, func()) { // TestBolt returns a temporary empty database bbolt in the temporary directory
// with the cleanup function.
func TestBolt(tb testing.TB) (*bolt.DB, func()) {
tb.Helper() tb.Helper()
f, err := os.CreateTemp("", "bbolt_*.db") f, err := os.CreateTemp(os.TempDir(), "bbolt_*.db")
require.NoError(tb, err) require.NoError(tb, err)
filePath := f.Name() filePath := f.Name()
@ -21,18 +23,9 @@ func TestBolt(tb testing.TB, buckets ...[]byte) (*bolt.DB, func()) {
db, err := bolt.Open(filePath, os.ModePerm, nil) db, err := bolt.Open(filePath, os.ModePerm, nil)
require.NoError(tb, err) require.NoError(tb, err)
for _, bucket := range buckets { //nolint: errcheck
bucket := bucket
assert.NoError(tb, db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket(bucket)
return err //nolint: errcheck
}))
}
return db, func() { return db, func() {
db.Close() //nolint: errcheck db.Close()
os.Remove(filePath) //nolint: errcheck os.Remove(filePath)
} }
} }