#18: Method renaming and tests

- Renamed `Msg.WriteToTempfile` to `Msg.WriteToTempFile`
- Added test coverage for `Msg.WriteToFile` and `Msg.WriteToTempFile`
This commit is contained in:
Winni Neessen 2022-06-09 10:11:37 +02:00
parent 491b5941aa
commit 85bbf744fa
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 45 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
htpl "html/template"
"io"
"net/mail"
"os"
"strings"
"testing"
ttpl "text/template"
@ -1700,3 +1701,43 @@ func TestMsg_EmbedHTMLTemplate(t *testing.T) {
})
}
}
// TestMsg_WriteToTempFile will test the output to temporary files
func TestMsg_WriteToTempFile(t *testing.T) {
m := NewMsg()
_ = m.From("Toni Tester <tester@example.com>")
_ = m.To("Ellenor Tester <ellinor@example.com>")
m.SetBodyString(TypeTextPlain, "This is a test")
f, err := m.WriteToTempFile()
if err != nil {
t.Errorf("failed to write message to temporary output file: %s", err)
}
_ = os.Remove(f)
}
// TestMsg_WriteToFile will test the output to a file
func TestMsg_WriteToFile(t *testing.T) {
f, err := os.CreateTemp("", "go-mail-test_*.eml")
if err != nil {
t.Errorf("failed to create temporary output file: %s", err)
}
defer func() {
_ = f.Close()
_ = os.Remove(f.Name())
}()
m := NewMsg()
_ = m.From("Toni Tester <tester@example.com>")
_ = m.To("Ellenor Tester <ellinor@example.com>")
m.SetBodyString(TypeTextPlain, "This is a test")
if err := m.WriteToFile(f.Name()); err != nil {
t.Errorf("failed to write to output file: %s", err)
}
fi, err := os.Stat(f.Name())
if err != nil {
t.Errorf("failed to stat output file: %s", err)
}
if fi.Size() <= 0 {
t.Errorf("output file is expected to contain data but its size is zero")
}
}

View file

@ -8,9 +8,9 @@ import (
"os"
)
// WriteToTempfile will create a temporary file and output the Msg to this file
// WriteToTempFile will create a temporary file and output the Msg to this file
// The method will return the filename of the temporary file
func (m *Msg) WriteToTempfile() (string, error) {
func (m *Msg) WriteToTempFile() (string, error) {
f, err := os.CreateTemp("", "go-mail_*.eml")
if err != nil {
return "", fmt.Errorf("failed to create output file: %w", err)

View file

@ -8,9 +8,9 @@ import (
"io/ioutil"
)
// WriteToTempfile will create a temporary file and output the Msg to this file
// WriteToTempFile will create a temporary file and output the Msg to this file
// The method will return the filename of the temporary file
func (m *Msg) WriteToTempfile() (string, error) {
func (m *Msg) WriteToTempFile() (string, error) {
f, err := ioutil.TempFile("", "go-mail_*.eml")
if err != nil {
return "", fmt.Errorf("failed to create output file: %w", err)