~sircmpwn/aerc

test(message): fixture harness for MessageInfo v1 PROPOSED

Jeff Martin: 1
 test(message): fixture harness for MessageInfo

 3 files changed, 158 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/11797/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] test(message): fixture harness for MessageInfo Export this patch

This change introduces a fixture-based test runner with the eventual
goal of testing against a corpus of desired RFC5322 messages.

I made this CR because I noticed aerc leaves certain messages in the
list in the pending "[..]" state, and I'm unable to view or modify the
message. Mostly these messages are spam, but some are from what I assume
are old and misconfigured mail clients, and other MUAs handle them fine.
So it seems useful to have a test suite like this, if for no other
reason than to better define the types of messages aerc wont bother with.

Gathered these fixtures from
https://docs.python.org/3/library/email.examples.html and
https://github.com/jstedfast/MimeKit.
---
If folks are interested in merging this, I have some messages in my
archives I could scrub and add here too. Mostly just felt like
introducing a patch :). Thanks for the great work to everyone involved!

 worker/lib/parse_test.go               | 81 ++++++++++++++++++++++++++
 worker/lib/testdata/message/fail/basic | 70 ++++++++++++++++++++++
 worker/lib/testdata/message/ok/basic   |  7 +++
 3 files changed, 158 insertions(+)
 create mode 100644 worker/lib/parse_test.go
 create mode 100644 worker/lib/testdata/message/fail/basic
 create mode 100644 worker/lib/testdata/message/ok/basic

diff --git a/worker/lib/parse_test.go b/worker/lib/parse_test.go
new file mode 100644
index 0000000..1a49dfb
--- /dev/null
+++ b/worker/lib/parse_test.go
@@ -0,0 +1,81 @@
package lib

import (
	"bytes"
	"io"
	"io/ioutil"
	"path/filepath"
	"testing"

	"git.sr.ht/~sircmpwn/aerc/models"
)

func TestWorkerParseOK(t *testing.T) {
	rootDir := "testdata/message/ok"
	msgFiles, err := ioutil.ReadDir(rootDir)
	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))
			if _, err := MessageInfo(m); err != nil {
				t.Fatal(err)
			}
		})
	}
}

func TestWorkerParseFail(t *testing.T) {
	rootDir := "testdata/message/fail"
	msgFiles, err := ioutil.ReadDir(rootDir)
	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))
			if _, err := MessageInfo(m); err == nil {
				t.Fatal("expected error")
			}
		})
	}

}

type MockRawMessage struct {
	body []byte
}

func NewMockRawMessage(body []byte) *MockRawMessage {
	return &MockRawMessage{
		body: body,
	}
}

func NewMockRawMessageFromPath(p string) *MockRawMessage {
	b, err := ioutil.ReadFile(p)
	die(err)
	return NewMockRawMessage(b)
}

func (m *MockRawMessage) NewReader() (io.Reader, error) {
	return bytes.NewReader(m.body), nil
}
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)
	}
}
diff --git a/worker/lib/testdata/message/fail/basic b/worker/lib/testdata/message/fail/basic
new file mode 100644
index 0000000..9705ef2
--- /dev/null
+++ b/worker/lib/testdata/message/fail/basic
@@ -0,0 +1,70 @@
From -
From: fejj@gnome.org
To: fejj@gnome.org
Date: Fri, 23 Aug 2002 2:32:53 -0400
Subject: this'll probably break a lot of mime parsers
X-Test-Header:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="x"

--x
Content-Type: multipart/mixed; boundary="xy"

--xy
Content-Type: multipart/mixed; boundary="xyz"

--xyz
Content-Type: text/plain

Hello world.

multipart/mixed; boundary="x"
  multipart/mixed; boundary="xy"
     multipart/mixed; boundary="xyz"
        text/plain                         <-- You are here.
From -
From: fejj@gnome.org
To: fejj@gnome.org
Date: Fri, 23 Aug 2002 2:32:53 -0400
Subject: this'll probably break even more mime parsers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="x"

--x
Content-Type: multipart/mixed; boundary="x"

--x
Content-Type: multipart/mixed; boundary="x"

--x
Content-Type: text/plain

Hello world.

multipart/mixed; boundary="x"
  multipart/mixed; boundary="x"
     multipart/mixed; boundary="x"
        text/plain                         <-- You are here.
From -
From: fejj@gnome.org
To: fejj@gnome.org
Date: Fri, 23 Aug 2002 2:32:53 -0400
Subject: if a mime parser can correctly handle this, I'm impressed
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=""

--
Content-Type: multipart/mixed; boundary=""

--
Content-Type: multipart/mixed; boundary=""

--
Content-Type: text/plain

Hello world.

multipart/mixed; boundary=""
  multipart/mixed; boundary=""
     multipart/mixed; boundary=""
        text/plain                         <-- You are here.
diff --git a/worker/lib/testdata/message/ok/basic b/worker/lib/testdata/message/ok/basic
new file mode 100644
index 0000000..8e06445
--- /dev/null
+++ b/worker/lib/testdata/message/ok/basic
@@ -0,0 +1,7 @@
To: Penelope Pussycat <penelope@example.com>, Fabrette Pussycat <fabrette@example.com>
From: Pepé Le Pew <pepe@example.com>
Subject: Ayons asperges pour le déjeuner

Salut!

Cela ressemble à un excellent recipie[1] déjeuner.
--
2.25.1