1package store
2
3import (
4 "io"
5 "os"
6 "strings"
7 "testing"
8
9 "github.com/mjl-/mox/mlog"
10)
11
12func TestMboxReader(t *testing.T) {
13 createTemp := func(log mlog.Log, pattern string) (*os.File, error) {
14 return os.CreateTemp("", pattern)
15 }
16 mboxf, err := os.Open("../testdata/importtest.mbox")
17 if err != nil {
18 t.Fatalf("open mbox: %v", err)
19 }
20 defer mboxf.Close()
21
22 log := mlog.New("mboxreader", nil)
23 mr := NewMboxReader(log, createTemp, mboxf.Name(), mboxf)
24 _, mf0, _, err := mr.Next()
25 if err != nil {
26 t.Fatalf("next mbox message: %v", err)
27 }
28 defer os.Remove(mf0.Name())
29 defer mf0.Close()
30
31 _, mf1, _, err := mr.Next()
32 if err != nil {
33 t.Fatalf("next mbox message: %v", err)
34 }
35 defer os.Remove(mf1.Name())
36 defer mf1.Close()
37
38 _, _, _, err = mr.Next()
39 if err != io.EOF {
40 t.Fatalf("got err %v, expected eof for next mbox message", err)
41 }
42}
43
44func TestMaildirReader(t *testing.T) {
45 createTemp := func(log mlog.Log, pattern string) (*os.File, error) {
46 return os.CreateTemp("", pattern)
47 }
48 // todo: rename 1642966915.1.mox to "1642966915.1.mox:2,"? cannot have that name in the git repo because go module (or the proxy) doesn't like it. could also add some flags and test they survive the import.
49 newf, err := os.Open("../testdata/importtest.maildir/new")
50 if err != nil {
51 t.Fatalf("open maildir new: %v", err)
52 }
53 defer newf.Close()
54
55 curf, err := os.Open("../testdata/importtest.maildir/cur")
56 if err != nil {
57 t.Fatalf("open maildir cur: %v", err)
58 }
59 defer curf.Close()
60
61 log := mlog.New("maildirreader", nil)
62 mr := NewMaildirReader(log, createTemp, newf, curf)
63 _, mf0, _, err := mr.Next()
64 if err != nil {
65 t.Fatalf("next maildir message: %v", err)
66 }
67 defer os.Remove(mf0.Name())
68 defer mf0.Close()
69
70 _, mf1, _, err := mr.Next()
71 if err != nil {
72 t.Fatalf("next maildir message: %v", err)
73 }
74 defer os.Remove(mf1.Name())
75 defer mf1.Close()
76
77 _, _, _, err = mr.Next()
78 if err != io.EOF {
79 t.Fatalf("got err %v, expected eof for next maildir message", err)
80 }
81}
82
83func TestParseDovecotKeywords(t *testing.T) {
84 const data = `0 Old
851 Junk
862 NonJunk
873 $Forwarded
884 $Junk
89`
90 flags, err := ParseDovecotKeywordsFlags(strings.NewReader(data), mlog.New("dovecotkeywords", nil))
91 if err != nil {
92 t.Fatalf("parsing dovecot-keywords: %v", err)
93 }
94 got := strings.Join(flags, ",")
95 want := "old,junk,nonjunk,$forwarded,$junk"
96 if got != want {
97 t.Fatalf("parsing dovecot keywords, got %q, expect %q", got, want)
98
99 }
100}
101