2023-02-16 11:57:27 +03:00
package store
2023-01-30 16:27:06 +03:00
import (
"io"
"os"
2023-02-16 11:57:27 +03:00
"strings"
2023-01-30 16:27:06 +03:00
"testing"
"github.com/mjl-/mox/mlog"
)
func TestMboxReader ( t * testing . T ) {
createTemp := func ( pattern string ) ( * os . File , error ) {
return os . CreateTemp ( "" , pattern )
}
2023-02-16 11:57:27 +03:00
mboxf , err := os . Open ( "../testdata/importtest.mbox" )
2023-01-30 16:27:06 +03:00
if err != nil {
t . Fatalf ( "open mbox: %v" , err )
}
defer mboxf . Close ( )
2023-02-16 11:57:27 +03:00
mr := NewMboxReader ( createTemp , mboxf . Name ( ) , mboxf , mlog . New ( "mboxreader" ) )
2023-01-30 16:27:06 +03:00
_ , mf0 , _ , err := mr . Next ( )
if err != nil {
t . Fatalf ( "next mbox message: %v" , err )
}
defer mf0 . Close ( )
defer os . Remove ( mf0 . Name ( ) )
_ , mf1 , _ , err := mr . Next ( )
if err != nil {
t . Fatalf ( "next mbox message: %v" , err )
}
defer mf1 . Close ( )
defer os . Remove ( mf1 . Name ( ) )
_ , _ , _ , err = mr . Next ( )
if err != io . EOF {
t . Fatalf ( "got err %v, expected eof for next mbox message" , err )
}
}
func TestMaildirReader ( t * testing . T ) {
createTemp := func ( pattern string ) ( * os . File , error ) {
return os . CreateTemp ( "" , pattern )
}
2023-01-30 16:38:55 +03:00
// 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.
2023-02-16 11:57:27 +03:00
newf , err := os . Open ( "../testdata/importtest.maildir/new" )
2023-01-30 16:27:06 +03:00
if err != nil {
t . Fatalf ( "open maildir new: %v" , err )
}
defer newf . Close ( )
2023-02-16 11:57:27 +03:00
curf , err := os . Open ( "../testdata/importtest.maildir/cur" )
2023-01-30 16:27:06 +03:00
if err != nil {
t . Fatalf ( "open maildir cur: %v" , err )
}
defer curf . Close ( )
2023-02-16 11:57:27 +03:00
mr := NewMaildirReader ( createTemp , newf , curf , mlog . New ( "maildirreader" ) )
2023-01-30 16:27:06 +03:00
_ , mf0 , _ , err := mr . Next ( )
if err != nil {
t . Fatalf ( "next maildir message: %v" , err )
}
defer mf0 . Close ( )
defer os . Remove ( mf0 . Name ( ) )
_ , mf1 , _ , err := mr . Next ( )
if err != nil {
t . Fatalf ( "next maildir message: %v" , err )
}
defer mf1 . Close ( )
defer os . Remove ( mf1 . Name ( ) )
_ , _ , _ , err = mr . Next ( )
if err != io . EOF {
t . Fatalf ( "got err %v, expected eof for next maildir message" , err )
}
}
2023-02-16 11:57:27 +03:00
func TestParseDovecotKeywords ( t * testing . T ) {
const data = ` 0 Old
1 Junk
2 NonJunk
3 $ Forwarded
4 $ Junk
`
keywords , err := ParseDovecotKeywords ( strings . NewReader ( data ) , mlog . New ( "dovecotkeywords" ) )
if err != nil {
t . Fatalf ( "parsing dovecot-keywords: %v" , err )
}
got := strings . Join ( keywords , "," )
want := "Old,Junk,NonJunk,$Forwarded,$Junk"
if got != want {
t . Fatalf ( "parsing dovecot keywords, got %q, want %q" , got , want )
}
}