mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-23 22:20:51 +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
|
// TestMsg_hasAlt tests the hasAlt() method of the Msg
|
||||||
|
|
||||||
|
|
|
@ -120,28 +120,27 @@ func TestMsg_WriteToFile_unixOnly(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func TestMsg_WriteToTempFile_unixOnly(t *testing.T) {
|
||||||
func TestMsg_WriteToTempFileFailed(t *testing.T) {
|
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
|
||||||
m := NewMsg()
|
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
|
||||||
_ = m.From("Toni Tester <tester@example.com>")
|
}
|
||||||
_ = m.To("Ellenor Tester <ellinor@example.com>")
|
|
||||||
m.SetBodyString(TypeTextPlain, "This is a test")
|
|
||||||
|
|
||||||
curTmpDir := os.Getenv("TMPDIR")
|
t.Run("WriteToTempFile fails on invalid TMPDIR", func(t *testing.T) {
|
||||||
defer func() {
|
// We store the current TMPDIR variable so we can set it back when the test is over
|
||||||
if err := os.Setenv("TMPDIR", curTmpDir); err != nil {
|
curTmpDir := os.Getenv("TMPDIR")
|
||||||
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
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)
|
||||||
}
|
}
|
||||||
}()
|
message := testMessage(t)
|
||||||
|
_, err := message.WriteToTempFile()
|
||||||
if err := os.Setenv("TMPDIR", "/invalid/directory/that/does/not/exist"); err != nil {
|
if err == nil {
|
||||||
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
t.Errorf("expected writing to invalid TMPDIR to fail, got: %s", err)
|
||||||
}
|
}
|
||||||
_, err := m.WriteToTempFile()
|
})
|
||||||
if err == nil {
|
|
||||||
t.Errorf("WriteToTempFile() did not fail as expected")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in a new issue