Skip to content

Commit

Permalink
Simplify attachment test setup
Browse files Browse the repository at this point in the history
Removed temporary file creation and copying in msgwriter_test.go. Directly attach the source file during the test to streamline the setup process. This change reduces complexity and potential points of failure in the test code.
  • Loading branch information
wneessen committed Nov 26, 2024
1 parent fe765cd commit 75c0e33
Showing 1 changed file with 1 addition and 25 deletions.
26 changes: 1 addition & 25 deletions msgwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"mime"
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -342,30 +341,7 @@ func TestMsgWriter_addFiles(t *testing.T) {
buffer := bytes.NewBuffer(nil)
msgwriter.writer = buffer
message := testMessage(t)
tmpfile, err := os.CreateTemp("", "attachment.*.tmp")
if err != nil {
t.Fatalf("failed to create tempfile: %s", err)
}
t.Cleanup(func() {
if err = os.Remove(tmpfile.Name()); err != nil {
t.Errorf("failed to remove tempfile: %s", err)
}
})

source, err := os.Open("testdata/attachment.txt")
if err != nil {
t.Fatalf("failed to open source file: %s", err)
}
if _, err = io.Copy(tmpfile, source); err != nil {
t.Fatalf("failed to copy source file: %s", err)
}
if err = tmpfile.Close(); err != nil {
t.Fatalf("failed to close tempfile: %s", err)
}
if err = source.Close(); err != nil {
t.Fatalf("failed to close source file: %s", err)
}
message.AttachFile(tmpfile.Name(), WithFileName(tt.filename))
message.AttachFile("testdata/attachment.txt", WithFileName(tt.filename))
msgwriter.writeMsg(message)
if msgwriter.err != nil {
t.Errorf("msgWriter failed to write: %s", msgwriter.err)
Expand Down

0 comments on commit 75c0e33

Please sign in to comment.