-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_test.go
144 lines (115 loc) · 2.47 KB
/
r_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package r
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/boltdb/bolt"
)
// const dbTestPath = "test.db"
type testDB struct {
*bolt.DB
TestPath string
}
func (t *testDB) New() (*testDB, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
t.TestPath = f.Name()
return t, nil
}
func (t *testDB) Open() error {
db, err := bolt.Open(t.TestPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
t.DB = db
if err != nil {
return err
}
return nil
}
func (t *testDB) Close() {
defer os.Remove(t.TestPath)
t.DB.Close()
}
func TestResetLastCommand(t *testing.T) {
db := new(testDB)
db, err := db.New()
if err != nil {
t.Error(err)
}
// Test r Session
s := new(Session)
s.BoltPath = db.TestPath
s.ResetLastCommand()
err = db.Open()
if err != nil {
t.Error(err)
}
var val string
err = db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(lastCommandBucket))
if err != nil {
return err
}
val = string(b.Get([]byte("command")))
return nil
})
db.Close()
if err != nil {
t.Error(err)
}
if val != "" {
t.Error("last command should be blank, but has value:", val)
}
}
func TestCheckForHistory(t *testing.T) {
db := new(testDB)
db, err := db.New()
if err != nil {
t.Error(err)
}
// Test r Session
s := new(Session)
s.BoltPath = db.TestPath
s.Global = false
err = s.CheckForHistory()
if err.Error() != "r doesn't have a history. Execute commands to build one" {
t.Error("There shouldn't be a history")
}
db.Open()
// check for global bucket
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(globalCommandBucket))
if err != nil {
return err
}
return nil
})
if err != nil {
t.Error(err)
}
db.DB.Close() // Close boltDB so checkForHistory can open
err = s.CheckForHistory()
if err.Error() != "Current directory doesn't have a history. Execute commands to build one" {
t.Error("There should be a global bucket", err)
}
db.Open()
// check for global bucket
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(directoryBucket))
if err != nil {
return err
}
return nil
})
if err != nil {
t.Error(err)
}
db.DB.Close() // Close boltDB so checkForHistory can open
err = s.CheckForHistory()
if err.Error() != "Current directory doesn't have a history. Execute commands to build one" {
t.Error("There should be a global bucket", err)
}
// Close and delete TestDB
db.Close()
}