Compare commits

...

23 commits

Author SHA1 Message Date
a32613a966
Merge c28bd7e331 into c47f08dc7f 2024-10-28 17:42:34 +01:00
c28bd7e331
Prevent Test Execution Without Env Variable
Added a check for the "PERFORM_UNIX_OPEN_WRITE_TESTS" environment variable to conditionally skip tests. This ensures tests are only executed when the variable is set to "true". Additionally, commented out the TestMsg_WriteToTempFileFailed function.
2024-10-28 17:42:18 +01:00
1c6b9faf15
Add tests for Msg.HasSendError method.
Introduce several unit tests for the Msg.HasSendError function to ensure its correct behavior under different conditions like unsent messages, failed deliveries, and temporary errors. This enhances the validation of message error states within the system.
2024-10-28 17:34:35 +01:00
7d670a1f24
Add temporary failure condition in client test
Introduce a new `FailTemp` property to simulate a temporary failure during the DATA transmission phase in tests. This helps ensure better coverage and handling of specific error conditions.
2024-10-28 17:31:11 +01:00
2f3809f33e
Add tests for Msg.UpdateReader method
Implemented new tests to validate the Msg.UpdateReader method, ensuring its functionality for both success and failure scenarios. Removed obsolete tests for Msg.NewReader to maintain code clarity and relevance.
2024-10-28 17:20:40 +01:00
4ecc6f2b0c
Refactor Msg tests and remove unused code
Reorganized message writing tests to create more concise and specific scenarios, including new context tests and WriteToFile tests. Removed outdated and redundant test cases to streamline the test suite.
2024-10-28 17:14:06 +01:00
a59173fae0
Refactor buffer initialization in NewReader.
Replaced bytes.Buffer{} with bytes.NewBuffer(nil) for buffer initialization in NewReader method of Msg. This change enhances clarity and consistency in buffer handling.
2024-10-28 17:04:44 +01:00
d39953c837
Update environment variables in CI workflow
Standardize variables by using expressions for consistency. This change ensures that all environment variables for Unix and Sendmail tests are sourced from GitHub variables.
2024-10-28 16:32:24 +01:00
6cbb6745bb
Add new test cases for message attachments and long headers
Introduce tests for "AttachFile" with a normal file and "WriteTo" with long headers. Additionally, remove outdated and redundant test functions.
2024-10-28 15:58:14 +01:00
4506472319
Add missing newline after test function
A newline was added after an anonymous function block to improve code readability. This change adheres to proper formatting practices and enhances the maintainability of the code.
2024-10-28 15:43:32 +01:00
759452f346
Refactor tests to remove redundant unix-specific cases
Unified the test cases for AttachFile, AttachReader, AttachReadSeeker, EmbedFile, EmbedReader, and EmbedReadSeeker by incorporating the previously separate UNIX-specific tests into the general test files. This change enhances maintainability by reducing code duplication and ensuring more consistent test coverage across platforms.
2024-10-28 15:42:20 +01:00
4eb9d8a1fa
Add tests for Msg.Write and WriteTo error handling
Implemented tests for the Msg.Write method and enhanced WriteTo error handling. Also added a failReadWriteSeekCloser type to explicitly simulate IO failures in tests, ensuring comprehensive reliability of error handling mechanisms.
2024-10-28 15:14:43 +01:00
afa65585a0
Add test for WriteToSkipMiddleware function in Msg structure
Implement a new test for the WriteToSkipMiddleware function to validate its behavior with normal mail parts. Ensure that the function properly skips the specified middleware and writes the message to a buffer correctly.
2024-10-28 14:58:55 +01:00
4a519a3b1f
Add tests for middleware application on messages
Introduced comprehensive test cases for applying middlewares (uppercase and encode) to messages. Also added tests for writing messages to memory buffers and parsing them back. Corrected messages in helper functions for consistency.
2024-10-28 14:54:42 +01:00
ae44d37d03
Add test for Msg_Reset function
This test ensures that the Reset method correctly clears all fields in the Msg struct. It verifies that the From, To, Subject, Body, Attachments, and Embeds fields are empty after calling Reset.
2024-10-28 12:30:13 +01:00
80bf7240b4
Remove redundant attachment and embed tests
Eliminated multiple test functions for file attachments and embeds, including TestMsg_AttachFile, TestMsg_AttachFromEmbedFS, and TestMsg_EmbedFile. These tests either duplicated functionality or were no longer relevant to the current code base. Simplifying the test suite enhances maintainability and reduces unnecessary checks.
2024-10-28 12:21:58 +01:00
f48ff6e150
Add tests for embedding functionalities in Msg
Introduce unit tests for EmbedReadSeeker, EmbedHTMLTemplate, EmbedTextTemplate, and EmbedFromEmbedFS methods in the Msg package. These tests cover various use cases and error scenarios, ensuring the correct handling of embedded content.
2024-10-28 12:17:20 +01:00
c395c83a06
Add tests for embedding files and readers
Introduced unit tests to verify the embedding of files and readers in messages, including tests specific to Unix file handling scenarios. These tests ensure correct handling of embed errors and proper cleanup of temporary files.
2024-10-28 12:01:31 +01:00
ae7b6a68c5
Add tests for the EmbedFile functionality in msg_test.go
This commit introduces three new test cases to validate the EmbedFile function in the msg_test.go file. The tests cover embedding an existing file, handling a non-existent file, and referencing pre-tested options.
2024-10-28 11:37:04 +01:00
d02f469658
Add test cases for AttachFromEmbedFS errors
Introduce two new test cases for the AttachFromEmbedFS method. These tests verify that the method correctly handles an invalid file path and a nil embed FS, ensuring error scenarios are properly managed.
2024-10-28 11:35:05 +01:00
e779777c9b
Add tests for message attachment methods using templates
Included unit tests to verify the functionality of the AttachHTMLTemplate and AttachTextTemplate methods, ensuring valid, invalid, and nil templates are handled correctly. Also added tests for AttachFromEmbedFS to validate attachments from embedded files.
2024-10-28 11:30:34 +01:00
f576b92ce2
Increase code coverage targets to 90% and tighten thresholds.
Updated project and patch coverage targets to 90% from 85% and 80%, respectively. Tightened thresholds from 5% to 2% to enforce stricter quality controls.
2024-10-28 11:30:01 +01:00
466c2892bf
Add AttachReadSeeker tests for Unix and general cases
Introduce tests for the AttachReadSeeker method, detailing scenarios for general use and Unix-specific paths. Ensure file handling and error conditions are properly validated to maintain robustness.
2024-10-28 09:34:05 +01:00
6 changed files with 1583 additions and 1200 deletions

View file

@ -31,8 +31,8 @@ jobs:
go: ['1.23']
env:
PERFORM_ONLINE_TEST: ${{ vars.PERFORM_ONLINE_TEST }}
PERFORM_UNIX_OPEN_WRITE_TESTS: "true"
TEST_SENDMAIL: ${{ vars.TEST_SENDMAIL }}
PERFORM_UNIX_OPEN_WRITE_TESTS: ${{ vars.PERFORM_UNIX_OPEN_WRITE_TESTS }}
PERFORM_SENDMAIL_TESTS: ${{ vars.PERFORM_SENDMAIL_TESTS }}
TEST_HOST: ${{ secrets.TEST_HOST }}
TEST_USER: ${{ secrets.TEST_USER }}
TEST_PASS: ${{ secrets.TEST_PASS }}

View file

@ -3559,6 +3559,7 @@ type serverProps struct {
FailOnQuit bool
FailOnReset bool
FailOnSTARTTLS bool
FailTemp bool
FeatureSet string
ListenPort int
SSLListener bool
@ -3720,6 +3721,10 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server
writeLine("500 5.0.0 Error during DATA transmission")
break
}
if props.FailTemp {
writeLine("451 4.3.0 Error: fail on DATA close")
break
}
writeLine("250 2.0.0 Ok: queued as 1234567890")
break
}

View file

@ -6,17 +6,17 @@ coverage:
status:
project:
default:
target: 85%
threshold: 5%
target: 90%
threshold: 2%
base: auto
if_ci_failed: error
only_pulls: false
patch:
default:
target: 80%
target: 90%
base: auto
if_ci_failed: error
threshold: 5%
threshold: 2%
comment:
require_changes: true

4
msg.go
View file

@ -2371,8 +2371,8 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sendmailPath strin
// - https://datatracker.ietf.org/doc/html/rfc5322
func (m *Msg) NewReader() *Reader {
reader := &Reader{}
buffer := bytes.Buffer{}
_, err := m.Write(&buffer)
buffer := bytes.NewBuffer(nil)
_, err := m.Write(buffer)
if err != nil {
reader.err = fmt.Errorf("failed to write Msg to Reader buffer: %w", err)
}

File diff suppressed because it is too large Load diff

View file

@ -9,11 +9,9 @@ package mail
import (
"bytes"
"context"
"errors"
"os"
"testing"
"time"
)
func TestMsg_AttachFile_unixOnly(t *testing.T) {
@ -22,7 +20,7 @@ func TestMsg_AttachFile_unixOnly(t *testing.T) {
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
}
tempFile, err := os.CreateTemp("testdata/tmp", "attachfile-open-write-test.*.txt")
tempFile, err := os.CreateTemp("", "attachfile-open-write-test.*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %s", err)
}
@ -53,134 +51,76 @@ func TestMsg_AttachFile_unixOnly(t *testing.T) {
t.Errorf("expected error to be %s, got: %s", os.ErrPermission, err)
}
})
t.Run("AttachFile with fileFromFS fails on copy", func(t *testing.T) {
tempfile, err := os.CreateTemp("testdata/tmp", "attachfile-close-early.*.txt")
}
func TestMsg_EmbedFile_unixOnly(t *testing.T) {
t.Run("EmbedFile with fileFromFS fails on open", func(t *testing.T) {
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
}
tempFile, err := os.CreateTemp("", "embedfile-open-write-test.*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %s", err)
}
t.Cleanup(func() {
if err := os.Remove(tempfile.Name()); err != nil {
if err := os.Remove(tempFile.Name()); err != nil {
t.Errorf("failed to remove temp file: %s", err)
}
})
if err = os.Chmod(tempFile.Name(), 0o000); err != nil {
t.Fatalf("failed to chmod temp file: %s", err)
}
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
message.AttachFile("testdata/attachment.txt")
attachments := message.GetAttachments()
if len(attachments) != 1 {
t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments))
message.EmbedFile(tempFile.Name())
embeds := message.GetEmbeds()
if len(embeds) != 1 {
t.Fatalf("failed to get embeds, expected 1, got: %d", len(embeds))
}
messageBuf, err := os.Open(tempfile.Name())
if err != nil {
t.Fatalf("failed to open temp file: %s", err)
}
// We close early to cause an error during io.Copy
if err = messageBuf.Close(); err != nil {
t.Fatalf("failed to close temp file: %s", err)
}
_, err = attachments[0].Writer(messageBuf)
messageBuf := bytes.NewBuffer(nil)
_, err = embeds[0].Writer(messageBuf)
if err == nil {
t.Error("writer func expected to fail, but didn't")
}
if !errors.Is(err, os.ErrPermission) {
t.Errorf("expected error to be %s, got: %s", os.ErrPermission, err)
}
})
}
func TestMsg_AttachReader_unixOnly(t *testing.T) {
t.Run("AttachReader with fileFromReader fails on copy", func(t *testing.T) {
tempfile, err := os.CreateTemp("", "attachfile-close-early.*.txt")
func TestMsg_WriteToFile_unixOnly(t *testing.T) {
t.Run("WriteToFile fails on create", func(t *testing.T) {
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
}
tempfile, err := os.CreateTemp("", "testmail-create.*.eml")
if err != nil {
t.Fatalf("failed to create temp file: %s", err)
}
t.Cleanup(func() {
if err := os.Remove(tempfile.Name()); err != nil {
t.Errorf("failed to remove temp file: %s", err)
}
})
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
file, err := os.Open("testdata/attachment.txt")
if err != nil {
t.Fatalf("failed to open file: %s", err)
if err = os.Chmod(tempfile.Name(), 0o000); err != nil {
t.Fatalf("failed to chmod temp file: %s", err)
}
t.Cleanup(func() {
if err := file.Close(); err != nil {
t.Errorf("failed to close file: %s", err)
}
})
if err = message.AttachReader("attachment.txt", file); err != nil {
t.Fatalf("failed to attach reader: %s", err)
}
attachments := message.GetAttachments()
if len(attachments) != 1 {
t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments))
}
messageBuf, err := os.Open(tempfile.Name())
if err != nil {
t.Fatalf("failed to open temp file: %s", err)
}
// We close early to cause an error during io.Copy
if err = messageBuf.Close(); err != nil {
if err = tempfile.Close(); err != nil {
t.Fatalf("failed to close temp file: %s", err)
}
_, err = attachments[0].Writer(messageBuf)
if err == nil {
t.Error("writer func expected to fail, but didn't")
if err = os.Remove(tempfile.Name()); err != nil {
t.Fatalf("failed to remove temp file: %s", err)
}
})
message := testMessage(t)
if err = message.WriteToFile(tempfile.Name()); err == nil {
t.Errorf("expected error, got nil")
}
})
}
// TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg
func TestMsg_WriteToSendmailWithContext(t *testing.T) {
if os.Getenv("TEST_SENDMAIL") != "true" {
t.Skipf("TEST_SENDMAIL variable is not set. Skipping sendmail test")
}
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) {
ctx, cfn := context.WithTimeout(context.Background(), time.Second*10)
defer cfn()
m.SetBodyString(TypeTextPlain, "Plain")
if err := m.WriteToSendmailWithContext(ctx, tt.sp); err != nil && !tt.sf {
t.Errorf("WriteToSendmailWithCommand() failed: %s", err)
}
m.Reset()
})
}
}
// TestMsg_WriteToSendmail will test the output to the local sendmail command
func TestMsg_WriteToSendmail(t *testing.T) {
if os.Getenv("TEST_SENDMAIL") != "true" {
t.Skipf("TEST_SENDMAIL variable is not set. Skipping sendmail test")
}
_, 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)
}
}
/*
func TestMsg_WriteToTempFileFailed(t *testing.T) {
m := NewMsg()
_ = m.From("Toni Tester <tester@example.com>")
@ -202,3 +142,6 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) {
t.Errorf("WriteToTempFile() did not fail as expected")
}
}
*/