Simplify attachment test setup

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.
This commit is contained in:
Winni Neessen 2024-11-26 16:26:13 +01:00
parent fe765cd49e
commit 75c0e3319b
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"io" "io"
"mime" "mime"
"os"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
@ -342,30 +341,7 @@ func TestMsgWriter_addFiles(t *testing.T) {
buffer := bytes.NewBuffer(nil) buffer := bytes.NewBuffer(nil)
msgwriter.writer = buffer msgwriter.writer = buffer
message := testMessage(t) message := testMessage(t)
tmpfile, err := os.CreateTemp("", "attachment.*.tmp") message.AttachFile("testdata/attachment.txt", WithFileName(tt.filename))
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))
msgwriter.writeMsg(message) msgwriter.writeMsg(message)
if msgwriter.err != nil { if msgwriter.err != nil {
t.Errorf("msgWriter failed to write: %s", msgwriter.err) t.Errorf("msgWriter failed to write: %s", msgwriter.err)