2020-08-21 20:58:30 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2022-01-19 19:10:08 +01:00
|
|
|
"os"
|
2020-08-21 20:58:30 +02:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2020-08-21 20:58:30 +02:00
|
|
|
)
|
2022-06-02 14:43:34 +02:00
|
|
|
|
|
|
|
func TestMessageInfoParser(t *testing.T) {
|
|
|
|
rootDir := "testdata/message/valid"
|
2022-08-17 16:19:45 +02:00
|
|
|
msgFiles, err := os.ReadDir(rootDir)
|
2022-06-02 14:43:34 +02:00
|
|
|
die(err)
|
|
|
|
|
|
|
|
for _, fi := range msgFiles {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := fi.Name()
|
|
|
|
t.Run(p, func(t *testing.T) {
|
|
|
|
m := newMockRawMessageFromPath(filepath.Join(rootDir, p))
|
|
|
|
mi, err := MessageInfo(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create MessageInfo with:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if perr := mi.Error; perr != nil {
|
|
|
|
t.Fatal("Expected no parsing error, but got:", mi.Error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 20:58:30 +02:00
|
|
|
|
|
|
|
func TestMessageInfoHandledError(t *testing.T) {
|
|
|
|
rootDir := "testdata/message/invalid"
|
2022-08-17 16:19:45 +02:00
|
|
|
msgFiles, err := os.ReadDir(rootDir)
|
2020-08-21 20:58:30 +02:00
|
|
|
die(err)
|
|
|
|
|
|
|
|
for _, fi := range msgFiles {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := fi.Name()
|
|
|
|
t.Run(p, func(t *testing.T) {
|
|
|
|
m := newMockRawMessageFromPath(filepath.Join(rootDir, p))
|
|
|
|
mi, err := MessageInfo(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if perr := mi.Error; perr == nil {
|
|
|
|
t.Fatal("Expected MessageInfo.Error, got none")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockRawMessage struct {
|
2022-01-19 19:10:08 +01:00
|
|
|
path string
|
2020-08-21 20:58:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:10:08 +01:00
|
|
|
func newMockRawMessageFromPath(p string) *mockRawMessage {
|
2020-08-21 20:58:30 +02:00
|
|
|
return &mockRawMessage{
|
2022-01-19 19:10:08 +01:00
|
|
|
path: p,
|
2020-08-21 20:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:10:08 +01:00
|
|
|
func (m *mockRawMessage) NewReader() (io.ReadCloser, error) {
|
|
|
|
return os.Open(m.path)
|
2020-08-21 20:58:30 +02:00
|
|
|
}
|
|
|
|
func (m *mockRawMessage) ModelFlags() ([]models.Flag, error) { return nil, nil }
|
|
|
|
func (m *mockRawMessage) Labels() ([]string, error) { return nil, nil }
|
|
|
|
func (m *mockRawMessage) UID() uint32 { return 0 }
|
|
|
|
|
|
|
|
func die(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|