From af9915e4e7c32669dd7300b876a7c416fe39e979 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 20 Sep 2024 19:45:49 +0200 Subject: [PATCH] Add test for WriteToTempFile failure scenario This new test verifies error handling for WriteToTempFile when the TMPDIR environment variable is set to an invalid directory. It ensures that the method fails as expected under these conditions, improving code robustness. --- msg_nowin_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/msg_nowin_test.go b/msg_nowin_test.go index 820be24..e338c57 100644 --- a/msg_nowin_test.go +++ b/msg_nowin_test.go @@ -61,3 +61,18 @@ func TestMsg_WriteToSendmail(t *testing.T) { t.Errorf("WriteToSendmail failed: %s", err) } } + +func TestMsg_WriteToTempFileFailed(t *testing.T) { + m := NewMsg() + _ = m.From("Toni Tester ") + _ = m.To("Ellenor Tester ") + m.SetBodyString(TypeTextPlain, "This is a test") + + 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") + } +}