Skip to content

Commit

Permalink
Rename and update tests for UNIX-specific functionality
Browse files Browse the repository at this point in the history
Renamed `msg_nowin_test.go` to `msg_unix_test.go` to better reflect its purpose. Updated the file attachment test to use `/dev/mem` for Unix environments instead of a temporary file with restricted permissions, ensuring compatibility with continuous integration environments.
  • Loading branch information
wneessen committed Oct 27, 2024
1 parent e74adb8 commit 6a43cf4
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions msg_nowin_test.go → msg_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,34 @@
//
// SPDX-License-Identifier: MIT

//go:build !windows
// +build !windows
//go:build linux || freebsd
// +build linux freebsd

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())
// The /dev/mem device should not be readable on normal UNIX systems. We choose this
// approach over os.Chmod(0000) on a temp file, since Github runners give full access
// to the file system
message.AttachFile("/dev/mem")
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)
_, err := attachments[0].Writer(messageBuf)
if err == nil {
t.Error("writer func expected to fail, but didn't")
}
Expand Down Expand Up @@ -128,6 +117,7 @@ func TestMsg_AttachReader_unixOnly(t *testing.T) {
})
}

/*
// TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg
func TestMsg_WriteToSendmailWithContext(t *testing.T) {
if os.Getenv("TEST_SENDMAIL") != "true" {
Expand Down Expand Up @@ -197,3 +187,4 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) {
t.Errorf("WriteToTempFile() did not fail as expected")
}
}
*/

0 comments on commit 6a43cf4

Please sign in to comment.