14 "github.com/mjl-/mox/mlog"
15 "github.com/mjl-/mox/mox-"
18func TestExport(t *testing.T) {
19 // Set up an account, add 2 messages to different 2 mailboxes. export as tar/zip
20 // and maildir/mbox. check there are 2 files in the repo, no errors.txt.
22 log := mlog.New("export", nil)
24 os.RemoveAll("../testdata/store/data")
25 mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
26 mox.MustLoadConfig(true, false)
27 acc, err := OpenAccount(pkglog, "mjl")
28 tcheck(t, err, "open account")
31 log.Check(err, "closing account")
36 msgFile, err := CreateMessageTemp(pkglog, "mox-test-export")
37 tcheck(t, err, "create temp")
38 defer os.Remove(msgFile.Name()) // To be sure.
40 const msg = "test: test\r\n\r\ntest\r\n"
41 _, err = msgFile.Write([]byte(msg))
42 tcheck(t, err, "write message")
44 m := Message{Received: time.Now(), Size: int64(len(msg))}
45 err = acc.DeliverMailbox(pkglog, "Inbox", &m, msgFile)
46 tcheck(t, err, "deliver")
48 m = Message{Received: time.Now(), Size: int64(len(msg))}
49 err = acc.DeliverMailbox(pkglog, "Trash", &m, msgFile)
50 tcheck(t, err, "deliver")
52 var maildirZip, maildirTar, mboxZip, mboxTar bytes.Buffer
54 archive := func(archiver Archiver, maildir bool) {
56 err = ExportMessages(ctxbg, log, acc.DB, acc.Dir, archiver, maildir, "", true)
57 tcheck(t, err, "export messages")
58 err = archiver.Close()
59 tcheck(t, err, "archiver close")
62 os.RemoveAll("../testdata/exportmaildir")
63 os.RemoveAll("../testdata/exportmbox")
65 archive(ZipArchiver{zip.NewWriter(&maildirZip)}, true)
66 archive(ZipArchiver{zip.NewWriter(&mboxZip)}, false)
67 archive(TarArchiver{tar.NewWriter(&maildirTar)}, true)
68 archive(TarArchiver{tar.NewWriter(&mboxTar)}, false)
69 archive(DirArchiver{filepath.FromSlash("../testdata/exportmaildir")}, true)
70 archive(DirArchiver{filepath.FromSlash("../testdata/exportmbox")}, false)
72 const defaultMailboxes = 6 // Inbox, Drafts, etc
73 if r, err := zip.NewReader(bytes.NewReader(maildirZip.Bytes()), int64(maildirZip.Len())); err != nil {
74 t.Fatalf("reading maildir zip: %v", err)
75 } else if len(r.File) != defaultMailboxes*3+2 {
76 t.Fatalf("maildir zip, expected %d*3 dirs, and 2 files, got %d files", defaultMailboxes, len(r.File))
79 if r, err := zip.NewReader(bytes.NewReader(mboxZip.Bytes()), int64(mboxZip.Len())); err != nil {
80 t.Fatalf("reading mbox zip: %v", err)
81 } else if len(r.File) != defaultMailboxes {
82 t.Fatalf("maildir zip, expected %d files, got %d files", defaultMailboxes, len(r.File))
85 checkTarFiles := func(r io.Reader, n int) {
87 tr := tar.NewReader(r)
95 if h.Name == "errors.txt" {
96 t.Fatalf("got errors.txt")
98 _, err = io.Copy(io.Discard, tr)
99 tcheck(t, err, "copy")
102 t.Fatalf("got %d files, expected %d", have, n)
106 checkTarFiles(&maildirTar, defaultMailboxes*3+2)
107 checkTarFiles(&mboxTar, defaultMailboxes)
109 checkDirFiles := func(dir string, n int) {
112 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
113 if err == nil && !d.IsDir() {
118 tcheck(t, err, "walkdir")
120 t.Fatalf("got %d files, expected %d", have, n)
124 checkDirFiles(filepath.FromSlash("../testdata/exportmaildir"), 2)
125 checkDirFiles(filepath.FromSlash("../testdata/exportmbox"), defaultMailboxes)