From f7cfe5289a513e7a9cbed40d395eb83dad9ed67a Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 27 Oct 2024 16:08:50 +0100 Subject: [PATCH] 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. --- msg_nowin_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ msg_test.go | 19 +----------------- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/msg_nowin_test.go b/msg_nowin_test.go index e56e65e..1964b41 100644 --- a/msg_nowin_test.go +++ b/msg_nowin_test.go @@ -85,6 +85,55 @@ func TestMsg_AttachFile_unixOnly(t *testing.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") + } + if !errors.Is(err, os.ErrClosed) { + t.Errorf("expected error to be %s, got: %s", os.ErrClosed, err) + } + }) +} + // TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg func TestMsg_WriteToSendmailWithContext(t *testing.T) { if os.Getenv("TEST_SENDMAIL") != "true" { diff --git a/msg_test.go b/msg_test.go index 09e0465..0f1b656 100644 --- a/msg_test.go +++ b/msg_test.go @@ -4533,7 +4533,7 @@ func TestMsg_AttachReader(t *testing.T) { t.Errorf("failed to close file: %s", err) } }) - if err := message.AttachReader("attachment.txt", file); err != nil { + if err = message.AttachReader("attachment.txt", file); err != nil { t.Fatalf("failed to attach reader: %s", err) } attachments := message.GetAttachments() @@ -4556,23 +4556,6 @@ func TestMsg_AttachReader(t *testing.T) { t.Errorf("expected message body to be %s, got: %s", "This is a test attachment", got) } }) - /* - t.Run("AttachReader with non-existant file", func(t *testing.T) { - message := NewMsg() - if message == nil { - t.Fatal("message is nil") - } - message.AttachReader("testdata/non-existant-file.txt") - attachments := message.GetAttachments() - if len(attachments) != 0 { - t.Fatalf("failed to retrieve attachments list") - } - }) - t.Run("AttachReader with options", func(t *testing.T) { - t.Log("all options have already been tested in file_test.go") - }) - - */ } /*