-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfsstore_test.go
103 lines (88 loc) · 2.35 KB
/
fsstore_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
package imapsql
import (
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"testing"
backendtests "github.com/foxcpp/go-imap-backend-tests"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var TestDB = os.Getenv("TEST_DB")
var TestDSN = os.Getenv("TEST_DSN")
func initTestBackend() backendtests.Backend {
driver := TestDB
dsn := TestDSN
if TestDB == "" {
driver = "sqlite3"
dsn = ":memory:"
}
randSrc := rand.NewSource(0)
prng := rand.New(randSrc)
tempDir, err := ioutil.TempDir("", "go-imap-sql-tests-")
if err != nil {
panic(err)
}
// This is meant for DB debugging.
if os.Getenv("PRESERVE_SQLITE3_DB") == "1" {
log.Println("Using sqlite3 DB in temporary directory.")
driver = "sqlite3"
dsn = filepath.Join(tempDir, "test.db")
}
storeDir := filepath.Join(tempDir, "store")
if err := os.MkdirAll(storeDir, os.ModeDir|os.ModePerm); err != nil {
panic(err)
}
var log Logger
if testing.Verbose() {
log = globalLogger{}
} else {
log = DummyLogger{}
}
b, err := New(driver, dsn, &FSStore{Root: storeDir}, Opts{
PRNG: prng,
Log: log,
})
if err != nil {
panic(err)
}
return b
}
func cleanBackend(bi backendtests.Backend) {
b := bi.(*Backend)
if os.Getenv("PRESERVE_DB") != "1" && os.Getenv("PRESERVE_SQLITE3_DB") != "1" {
// Remove things manually in the right order so we will not hit
// foreign key constraint when dropping tables.
if _, err := b.DB.Exec(`DELETE FROM msgs`); err != nil {
log.Println("DELETE FROM msgs", err)
}
if _, err := b.DB.Exec(`DELETE FROM extKeys`); err != nil {
log.Println("DELETE FROM extKeys", err)
}
if _, err := b.DB.Exec(`DROP TABLE flags`); err != nil {
log.Println("DROP TABLE flags", err)
}
if _, err := b.DB.Exec(`DROP TABLE msgs`); err != nil {
log.Println("DROP TABLE msgs", err)
}
if _, err := b.DB.Exec(`DROP TABLE mboxes`); err != nil {
log.Println("DROP TABLE mboxes", err)
}
if _, err := b.DB.Exec(`DROP TABLE users`); err != nil {
log.Println("DROP TABLE users", err)
}
if _, err := b.DB.Exec(`DROP TABLE extKeys`); err != nil {
log.Println("DROP TABLE extKeys", err)
}
if err := os.RemoveAll(b.extStore.(*FSStore).Root); err != nil {
log.Println(err)
}
}
b.Close()
}
func TestWithFSStore(t *testing.T) {
backendtests.RunTests(t, initTestBackend, cleanBackend)
}