-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaildir_test.go
138 lines (119 loc) · 2.99 KB
/
maildir_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
package maildir
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/amalfra/maildir/v3/lib"
)
var mailDir string
var testData string
var maildir *Maildir
func cleanMaildir() {
err := os.RemoveAll(mailDir)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to clean maildir folder")
os.Exit(1)
}
}
func init() {
mailDir = "/tmp/maildir-test"
testData = "foo"
cleanMaildir()
maildir = NewMaildir(mailDir)
}
func TestPathProperty(t *testing.T) {
if maildir.path != mailDir {
t.Fatalf("path mismatch")
}
}
func TestDirectoriesCreated(t *testing.T) {
for _, subDir := range lib.Subdirs {
if _, err := os.Stat(filepath.Join(mailDir, subDir)); os.IsNotExist(err) {
t.Fatalf("required sub directories not created")
}
}
}
func TestAdd(t *testing.T) {
msg, err := maildir.Add(testData)
if err != nil {
t.Fatalf(fmt.Sprintf("failed to add message. got error: %s", err))
}
msgData, err := msg.GetData()
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get message data. got error: %s", err))
}
if msgData != testData {
t.Fatalf("incorrect data saved in message")
}
}
func TestListNewOneMessage(t *testing.T) {
listing, err := maildir.List("new")
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get messages listing. got error: %s", err))
}
for _, v := range listing {
fileData, err := v.GetData()
if err != nil {
t.Fatalf(fmt.Sprintf("failed to read messages. got error: %s", err))
}
if fileData != testData {
t.Fatalf("incorrect data saved in message")
}
}
}
func TestListNewMultipleMessage(t *testing.T) {
TestAdd(t)
listing, err := maildir.List("new")
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get messages listing. got error: %s", err))
}
for _, v := range listing {
fileData, err := v.GetData()
if err != nil {
t.Fatalf(fmt.Sprintf("failed to read messages. got error: %s", err))
}
if fileData != testData {
t.Fatalf("incorrect data saved in message")
}
}
}
func TestProcessNewMessages(t *testing.T) {
listing, err := maildir.List("cur")
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get messages listing. got error: %s", err))
}
for _, v := range listing {
_, err := v.Process()
if err != nil {
t.Fatalf(fmt.Sprintf("failed to process messages. got error: %s", err))
}
}
}
func TestListCurMultipleMessages(t *testing.T) {
listing, err := maildir.List("cur")
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get messages listing. got error: %s", err))
}
for _, v := range listing {
fileData, err := v.GetData()
if err != nil {
t.Fatalf(fmt.Sprintf("failed to read messages. got error: %s", err))
}
if fileData != testData {
t.Fatalf("incorrect data saved in message")
}
}
}
func TestDeleteMessages(t *testing.T) {
listing, err := maildir.List("cur")
if err != nil {
t.Fatalf(fmt.Sprintf("failed to get messages listing. got error: %s", err))
}
for _, v := range listing {
err = maildir.Delete(v.Key())
if err != nil {
t.Fatalf(fmt.Sprintf("failed to delete messages. got error: %s", err))
}
}
}