-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain_test.go
88 lines (71 loc) · 1.39 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"math/rand"
"os"
"path/filepath"
"reflect"
"strconv"
"testing"
"time"
"github.com/boltdb/bolt"
)
func TestMain(m *testing.M) {
db = MustOpenDB()
exitVal := m.Run()
db.MustClose()
os.Exit(exitVal)
}
func randInt() int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(10000)
}
func tempfilePath(prefix string) string {
var name string
dir := os.TempDir()
conflict := true
for i := 0; i < 10000; i++ {
name = filepath.Join(dir, prefix+strconv.Itoa(randInt()))
if _, err := os.Stat(name); os.IsNotExist(err) {
conflict = false
break
}
}
if conflict {
panic("couldn't find a suitable tempfile path")
}
return name
}
func MustOpenDB() *DB {
bdb, err := bolt.Open(tempfilePath("nogo-db-"), 0666, nil)
if err != nil {
panic(err)
}
return &DB{bdb}
}
func (db *DB) Reset() {
db.Update(func(tx *bolt.Tx) error {
// Delete bucket
tx.DeleteBucket(blacklistKey)
return nil
})
if err := db.Update(func(tx *bolt.Tx) error {
// Create bucket
_, err := tx.CreateBucket(blacklistKey)
return err
}); err != nil {
panic(err)
}
}
func (db *DB) MustClose() {
defer os.Remove(db.Path())
if err := db.Close(); err != nil {
panic(err)
}
}
func testEqual(t *testing.T, msg string, args ...interface{}) bool {
if !reflect.DeepEqual(args[len(args)-2], args[len(args)-1]) {
t.Errorf(msg, args...)
return false
}
return true
}