Compare commits

..

33 commits

Author SHA1 Message Date
9682755e25
Delete unused .cirrus.yml configuration file
The .cirrus.yml file was removed as it is no longer needed for our CI/CD processes. This helps clean up the repository and avoids confusion over unused configurations.
2024-10-27 18:03:09 +01:00
8fb05a33ff
Update tempfile path in unit tests
Changed tempfile creation path to "testdata/tmp" for better test organization and to ensure temporary files are stored within the testdata directory. This change affects the TestMsg_AttachFile_unixOnly tests.
2024-10-27 18:02:37 +01:00
7b9df7de47
Remove harden-runner step from CI workflow
The harden-runner step has been removed from the FreeBSD testing matrix in the CI workflow. This change simplifies the workflow and removes an additional security auditing step.
2024-10-27 17:55:12 +01:00
bdffa22ad8
Remove duplicate Go test step from CI workflow
Eliminated a redundant "Run go test" step from the GitHub Actions CI configuration. This helps streamline the workflow and avoid unnecessary repetitions in the CI process.
2024-10-27 17:50:36 +01:00
89f29b241e
Add FreeBSD testing to CI workflow
Included FreeBSD versions 13.4, 14.0, and 14.1 in the CI pipeline. Configured the workflow to harden the runner, checkout code, and run tests on the FreeBSD virtual machine.
2024-10-27 17:49:04 +01:00
eed3dec7d6
Clean up redundant error checks in msg_nowin_test.go
Removed unnecessary assertions for specific error types in test cases. This streamlines the tests, maintaining focus on the primary failure conditions without checking the error type.
2024-10-27 16:19:16 +01:00
3be41b1aea
Fix chmod notation to use octal literal syntax
Updated the chmod call in msg_nowin_test.go to use the consistent and proper octal literal syntax (0o000) instead of 0000. This change improves code readability and aligns with Go convention.
2024-10-27 16:10:46 +01:00
f7cfe5289a
Remove obsolete tests and add error handling test
Removed commented-out tests to clean up the codebase. Added a new test to verify behavior when the file is closed early during attachment, ensuring robustness.
2024-10-27 16:08:50 +01:00
946ad294ce
Add tests for Msg attachment handling
Introduce new unit tests for verifying the `AttachReader` method and enhancing error handling in attachment functions. These tests cover scenarios for success, file permissions, and early file closure errors.
2024-10-27 15:54:39 +01:00
0cf636ee9b
Add test for AttachFile method and change Skip to Log
Replace t.Skip with t.Log to avoid skipping tests and ensure they log their status. Introduce a comprehensive test for the AttachFile method, covering cases with existing, non-existent files, and using options.
2024-10-27 15:28:42 +01:00
84f9d0583d
Refactor buffer initialization to use bytes.NewBuffer
Replaced direct bytes.Buffer{} initialization with bytes.NewBuffer(nil) for better consistency and potential performance improvements. This refactor affects both the HTML and plain text alternative writers.
2024-10-27 15:17:30 +01:00
254dc81706
Add tests for AddAlternativeTextTemplate method
Include various scenarios like default, with existing body string, nil template, and invalid template. These tests help in validating the addition of alternative text templates to messages.
2024-10-27 15:16:49 +01:00
e00ddda3a3
Fix test expectation and add HTML template tests
Corrected the expected number of parts in an existing test from 1 to 2. Additionally, introduced tests for adding HTML templates to message bodies, including cases with a valid template, a body string, a nil template, and an invalid template.
2024-10-27 15:13:57 +01:00
602f8a6e29
Add comprehensive tests for message alternatives
Introduced tests for AddAlternativeString and AddAlternativeWriter methods, including variations for charset, encoding, content description, and simultaneous usages. Ensure accurate handling of content types, encodings, and body strings. This enhances coverage and reliability of message formatting features.
2024-10-27 15:10:01 +01:00
4db66696a6
Add tests for SetBodyTextTemplate method in msg_test.go
Implemented thorough unit tests to validate the SetBodyTextTemplate method in msg_test.go. These tests cover default behavior, handling of nil templates, and invalid templates, ensuring robustness and reliability.
2024-10-27 14:56:34 +01:00
f8caa5599b
Refactor buffer creation in msg.go
Replace `bytes.Buffer{}` with `bytes.NewBuffer(nil)` for buffer instantiation to improve clarity and consistency. This change simplifies the buffer creation process and aligns it with common Go practices.
2024-10-27 14:55:51 +01:00
35ffc95102
Add tests for SetBodyWriter and SetBodyHTMLTemplate
Introduced new tests to validate the functionality of `SetBodyWriter` with `WithPartEncoding` and `WithPartContentDescription`. Also added comprehensive tests for `SetBodyHTMLTemplate`, covering default behavior and error cases.
2024-10-27 14:50:14 +01:00
432e21f162
Refactor buffer initialization in message template execution
Change buffer initialization from an empty `bytes.Buffer` to `bytes.NewBuffer(nil)` to streamline buffer allocation. This adjustment ensures better flexibility and aligns with best practices in handling buffer slices.
2024-10-27 14:33:00 +01:00
0aa81d724b
Reorganize and cleanup msg_test.go
Removed redundant and commented-out test functions to improve code readability and maintainability. Moved the essential helper functions (checkAddrHeader and checkGenHeader) to the end to keep the structure organized.
2024-10-27 14:20:44 +01:00
78ee1a2a81
Fix timeout message in client_test.go
Updated the skip message to accurately reflect the timeout reason when closing the test server connection. This improves the clarity of the error reporting in tests.
2024-10-27 12:11:48 +01:00
b510d2654c
Handle timeout errors in test client cleanup
Added a check in the client cleanup function to skip the test if the client connection fails due to a timeout. This ensures that network-related timeouts do not cause test failures, providing more reliable test outcomes.
2024-10-27 12:10:51 +01:00
056ec60734
Remove newline characters in test data strings
Trim newline characters from test attachment and embed strings. Updated assertions to compare strings without trailing newlines for consistency.
2024-10-27 12:05:18 +01:00
cb5ac8b0e2
Suppress inspection for deprecated method usage in test
Added GoLand directive to suppress deprecation warnings for SetAttachements method in msg_test.go. This method is already fully tested by SetAttachments and the test is marked to be skipped.
2024-10-27 11:32:15 +01:00
6376f29190
Add ContentType tests and simplify error messages
Introduced new test cases for ContentType values to ensure proper handling. Additionally, simplified multiple error messages in existing tests to enhance readability and maintain consistency.
2024-10-27 11:27:38 +01:00
9a01629c47
Add unit tests for msg embed setters and unsetters
Introduce tests for `SetEmbeds` with single, multiple, and no files. Add `UnsetAllEmbeds` and `UnsetAllParts` tests to ensure embeds and attachments are removed correctly.
2024-10-27 10:49:02 +01:00
2dad9d36b2
Add embed and attachment tests
Introduced tests for embedding files and managing attachments in messages. Validated behavior for setting and unsetting attachments and retrieving embedded files with various scenarios.
2024-10-27 10:42:32 +01:00
c8c7d18ba9
Add test attachment with license information
Introduced a new test attachment file to the testdata directory. Included a corresponding license file to specify copyright and licensing details.
2024-10-27 10:30:20 +01:00
472a5a6454
Add tests for SetAttachments in msg_test.go
Implemented comprehensive tests for SetAttachments including scenarios with single, multiple, and no files. Additionally, deprecated the SetAttachements function and noted it as fully tested by SetAttachments.
2024-10-27 10:25:53 +01:00
f2619737e8
Add tests for Msg GetBoundary method
Introduce unit tests for the GetBoundary method in the Msg type. Ensure proper functionality for messages with and without a boundary set.
2024-10-27 10:13:14 +01:00
b7ca41af81
Remove redundant TestMsg_SetAttachments
Eliminated the TestMsg_SetAttachments function from the test suite as it duplicates functionality covered by other tests. Removing this redundant code improves maintainability and clarity.
2024-10-27 10:09:56 +01:00
66e25d82d3
Add test for GetAttachments method with no attachment
Introduced a new test case to check `GetAttachments` when no attachments are present in the message object. Removed the redundant `TestMsg_GetAttachments` function to simplify the test suite.
2024-10-27 10:09:25 +01:00
babf7b9780
Add tests for GetAttachments method in Msg
This commit introduces unit tests for the GetAttachments method in the Msg struct to ensure it correctly handles both single and multiple attachments. The tests verify the name and content of the attachments to validate the expected behavior.
2024-10-27 10:07:05 +01:00
43f9ffa3af
Add tests for GetGenHeader and GetParts methods in Msg
Introduce comprehensive tests for the GetGenHeader and GetParts methods. These tests cover single and multiple values for headers, body parts retrieval, and edge cases such as nil values, ensuring robustness and correctness.
2024-10-27 09:58:22 +01:00
11 changed files with 1794 additions and 514 deletions

View file

@ -1,23 +0,0 @@
# SPDX-FileCopyrightText: 2022 Winni Neessen <winni@neessen.dev>
#
# SPDX-License-Identifier: MIT
freebsd_task:
name: FreeBSD
matrix:
- name: FreeBSD 13.3
freebsd_instance:
image_family: freebsd-13-3
- name: FreeBSD 14.0
freebsd_instance:
image_family: freebsd-14-0
env:
TEST_SKIP_SENDMAIL: 1
pkginstall_script:
- pkg install -y go
test_script:
- go test -race -cover -shuffle=on ./...

View file

@ -136,6 +136,26 @@ jobs:
- name: Run go test
run: |
go test -race -shuffle=on ./...
test-fbsd:
name: Test on FreeBSD ${{ matrix.osver }}
runs-on: ubuntu-latest
concurrency:
group: ci-test-freebsd-${{ matrix.osver }}
cancel-in-progress: true
strategy:
matrix:
osver: ['13.4', '14.0', '14.1']
steps:
- name: Checkout Code
uses: actions/checkout@61b9e3751b92087fd0b06925ba6dd6314e06f089 # master
- name: Run go test on FreeBSD
uses: vmactions/freebsd-vm@v1
with:
usesh: true
prepare: |
pkg install -y go
run: |
go test -race -shuffle=on ./...
reuse:
name: REUSE Compliance Check
runs-on: ubuntu-latest

View file

@ -2579,6 +2579,10 @@ func TestClient_Send(t *testing.T) {
}
t.Cleanup(func() {
if err := client.Close(); err != nil {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
t.Skip("failed to close the test server connection due to timeout")
}
t.Errorf("failed to close client: %s", err)
}
})

24
msg.go
View file

@ -1681,11 +1681,11 @@ func (m *Msg) SetBodyHTMLTemplate(tpl *ht.Template, data interface{}, opts ...Pa
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.Execute(&buffer, data); err != nil {
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(&buffer)
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
@ -1712,11 +1712,11 @@ func (m *Msg) SetBodyTextTemplate(tpl *tt.Template, data interface{}, opts ...Pa
if tpl == nil {
return errors.New(errTplPointerNil)
}
buf := bytes.Buffer{}
if err := tpl.Execute(&buf, data); err != nil {
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(&buf)
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextPlain, writeFunc, opts...)
return nil
}
@ -1786,11 +1786,11 @@ func (m *Msg) AddAlternativeHTMLTemplate(tpl *ht.Template, data interface{}, opt
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.Execute(&buffer, data); err != nil {
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(&buffer)
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
@ -1816,11 +1816,11 @@ func (m *Msg) AddAlternativeTextTemplate(tpl *tt.Template, data interface{}, opt
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.Execute(&buffer, data); err != nil {
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(&buffer)
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextPlain, writeFunc, opts...)
return nil
}

View file

@ -8,12 +8,126 @@
package mail
import (
"bytes"
"context"
"errors"
"os"
"testing"
"time"
)
func TestMsg_AttachFile_unixOnly(t *testing.T) {
t.Run("AttachFile with fileFromFS fails on open", func(t *testing.T) {
tempfile, err := os.CreateTemp("testdata/tmp", "attachfile-unable-to-open.*.txt")
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)
}
})
if err = os.Chmod(tempfile.Name(), 0o000); err != nil {
t.Fatalf("failed to chmod temp file to 0000: %s", err)
}
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
message.AttachFile(tempfile.Name())
attachments := message.GetAttachments()
if len(attachments) != 1 {
t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments))
}
messageBuf := bytes.NewBuffer(nil)
_, err = attachments[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)
}
})
t.Run("AttachFile with fileFromFS fails on copy", func(t *testing.T) {
tempfile, err := os.CreateTemp("testdata/tmp", "attachfile-close-early.*.txt")
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")
}
message.AttachFile("testdata/attachment.txt")
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 {
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")
}
})
}
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")
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)
}
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 {
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")
}
})
}
// TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg
func TestMsg_WriteToSendmailWithContext(t *testing.T) {
if os.Getenv("TEST_SENDMAIL") != "true" {

File diff suppressed because it is too large Load diff

1
testdata/attachment.txt vendored Normal file
View file

@ -0,0 +1 @@
This is a test attachment

3
testdata/attachment.txt.license vendored Normal file
View file

@ -0,0 +1,3 @@
// SPDX-FileCopyrightText: Copyright (c) 2022-2024 The go-mail Authors
//
// SPDX-License-Identifier: MIT

1
testdata/embed.txt vendored Normal file
View file

@ -0,0 +1 @@
This is a test embed

3
testdata/embed.txt.license vendored Normal file
View file

@ -0,0 +1,3 @@
// SPDX-FileCopyrightText: Copyright (c) 2022-2024 The go-mail Authors
//
// SPDX-License-Identifier: MIT

0
testdata/tmp/.gitkeep vendored Normal file
View file