mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-22 10:40:39 +01:00
Add test coverage for WriteToTempFile method
Implemented unit tests for WriteToTempFile, including successful writing to a temporary file and failure cases due to an invalid TMPDIR. These tests ensure the method behaves correctly under different conditions and enhances code robustness.
This commit is contained in:
parent
c28bd7e331
commit
d0280ea9ad
2 changed files with 61 additions and 22 deletions
40
msg_test.go
40
msg_test.go
|
@ -6037,6 +6037,46 @@ func TestMsg_HasSendError(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestMsg_WriteToTempFile(t *testing.T) {
|
||||
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
|
||||
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
|
||||
}
|
||||
|
||||
t.Run("WriteToTempFile succeeds", func(t *testing.T) {
|
||||
message := testMessage(t)
|
||||
tempFile, err := message.WriteToTempFile()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to write message to temp file: %s", err)
|
||||
}
|
||||
parsed, err := EMLToMsgFromFile(tempFile)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse message in buffer: %s", err)
|
||||
}
|
||||
checkAddrHeader(t, parsed, HeaderFrom, "WriteTo", 0, 1, TestSenderValid, "")
|
||||
checkAddrHeader(t, parsed, HeaderTo, "WriteTo", 0, 1, TestRcptValid, "")
|
||||
checkGenHeader(t, parsed, HeaderSubject, "WriteTo", 0, 1, "Testmail")
|
||||
parts := parsed.GetParts()
|
||||
if len(parts) != 1 {
|
||||
t.Fatalf("expected 1 parts, got: %d", len(parts))
|
||||
}
|
||||
if parts[0].contentType != TypeTextPlain {
|
||||
t.Errorf("expected contentType to be %s, got: %s", TypeTextPlain, parts[0].contentType)
|
||||
}
|
||||
if parts[0].encoding != EncodingQP {
|
||||
t.Errorf("expected encoding to be %s, got: %s", EncodingQP, parts[0].encoding)
|
||||
}
|
||||
messageBuf := bytes.NewBuffer(nil)
|
||||
_, err = parts[0].writeFunc(messageBuf)
|
||||
if err != nil {
|
||||
t.Errorf("writer func failed: %s", err)
|
||||
}
|
||||
got := strings.TrimSpace(messageBuf.String())
|
||||
if !strings.HasSuffix(got, "Testmail") {
|
||||
t.Errorf("expected message buffer to contain Testmail, got: %s", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
// TestMsg_hasAlt tests the hasAlt() method of the Msg
|
||||
|
||||
|
|
|
@ -120,28 +120,27 @@ func TestMsg_WriteToFile_unixOnly(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
/*
|
||||
func TestMsg_WriteToTempFileFailed(t *testing.T) {
|
||||
m := NewMsg()
|
||||
_ = m.From("Toni Tester <tester@example.com>")
|
||||
_ = m.To("Ellenor Tester <ellinor@example.com>")
|
||||
m.SetBodyString(TypeTextPlain, "This is a test")
|
||||
func TestMsg_WriteToTempFile_unixOnly(t *testing.T) {
|
||||
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
|
||||
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
|
||||
}
|
||||
|
||||
curTmpDir := os.Getenv("TMPDIR")
|
||||
defer func() {
|
||||
if err := os.Setenv("TMPDIR", curTmpDir); err != nil {
|
||||
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
||||
t.Run("WriteToTempFile fails on invalid TMPDIR", func(t *testing.T) {
|
||||
// We store the current TMPDIR variable so we can set it back when the test is over
|
||||
curTmpDir := os.Getenv("TMPDIR")
|
||||
t.Cleanup(func() {
|
||||
if err := os.Setenv("TMPDIR", curTmpDir); err != nil {
|
||||
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := os.Setenv("TMPDIR", "/invalid/directory/that/does/not/exist"); err != nil {
|
||||
t.Fatalf("failed to set TMPDIR environment variable: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := os.Setenv("TMPDIR", "/invalid/directory/that/does/not/exist"); err != nil {
|
||||
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
||||
}
|
||||
_, err := m.WriteToTempFile()
|
||||
if err == nil {
|
||||
t.Errorf("WriteToTempFile() did not fail as expected")
|
||||
}
|
||||
message := testMessage(t)
|
||||
_, err := message.WriteToTempFile()
|
||||
if err == nil {
|
||||
t.Errorf("expected writing to invalid TMPDIR to fail, got: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue