2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-06-17 15:05:54 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-18 23:10:53 +01:00
|
|
|
//go:build !windows
|
2022-03-18 23:13:22 +01:00
|
|
|
// +build !windows
|
2022-03-18 23:10:53 +01:00
|
|
|
|
|
|
|
package mail
|
|
|
|
|
2022-03-18 23:23:59 +01:00
|
|
|
import (
|
|
|
|
"context"
|
2022-09-08 15:08:17 +02:00
|
|
|
"os"
|
2022-03-18 23:23:59 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2022-03-18 23:10:53 +01:00
|
|
|
|
2022-03-18 23:23:59 +01:00
|
|
|
// TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg
|
|
|
|
func TestMsg_WriteToSendmailWithContext(t *testing.T) {
|
2023-01-12 01:19:24 +01:00
|
|
|
if os.Getenv("TEST_SKIP_SENDMAIL") != "" {
|
|
|
|
t.Skipf("TEST_SKIP_SENDMAIL variable is set. Skipping sendmail test")
|
|
|
|
}
|
2022-03-18 23:10:53 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
sp string
|
|
|
|
sf bool
|
|
|
|
}{
|
|
|
|
{"Sendmail path: /dev/null", "/dev/null", true},
|
|
|
|
{"Sendmail path: /bin/cat", "/bin/cat", true},
|
|
|
|
{"Sendmail path: /is/invalid", "/is/invalid", true},
|
|
|
|
{"Sendmail path: /bin/echo", "/bin/echo", false},
|
|
|
|
}
|
|
|
|
m := NewMsg()
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-03-18 23:23:59 +01:00
|
|
|
ctx, cfn := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cfn()
|
2022-03-18 23:10:53 +01:00
|
|
|
m.SetBodyString(TypeTextPlain, "Plain")
|
2022-03-18 23:23:59 +01:00
|
|
|
if err := m.WriteToSendmailWithContext(ctx, tt.sp); err != nil && !tt.sf {
|
2022-03-18 23:10:53 +01:00
|
|
|
t.Errorf("WriteToSendmailWithCommand() failed: %s", err)
|
|
|
|
}
|
|
|
|
m.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 15:08:17 +02:00
|
|
|
|
|
|
|
// TestMsg_WriteToSendmail will test the output to the local sendmail command
|
|
|
|
func TestMsg_WriteToSendmail(t *testing.T) {
|
2023-01-12 01:19:24 +01:00
|
|
|
if os.Getenv("TEST_SKIP_SENDMAIL") != "" {
|
|
|
|
t.Skipf("TEST_SKIP_SENDMAIL variable is set. Skipping sendmail test")
|
|
|
|
}
|
2022-09-08 15:08:17 +02:00
|
|
|
_, err := os.Stat(SendmailPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("local sendmail command not found in expected path. Skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := NewMsg()
|
|
|
|
_ = m.From("Toni Tester <tester@example.com>")
|
|
|
|
_ = m.To(TestRcpt)
|
|
|
|
m.SetBodyString(TypeTextPlain, "This is a test")
|
|
|
|
if err := m.WriteToSendmail(); err != nil {
|
|
|
|
t.Errorf("WriteToSendmail failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|