From ba00d56168d059aa274b003fba28d661148d2b19 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 24 Mar 2024 23:26:55 +0100 Subject: [PATCH] Add failure test case for writing to temporary file Added a new test case "TestMsg_WriteToTempFile_fail" in msg_test.go to validate the failure scenario when attempting to write a message to a non-existent temporary directory. This new test will ensure error handling is properly conducted in such situations. --- msg_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/msg_test.go b/msg_test.go index e73bf81..4cfaa5d 100644 --- a/msg_test.go +++ b/msg_test.go @@ -2582,6 +2582,22 @@ func TestMsg_WriteToTempFile(t *testing.T) { _ = os.Remove(f) } +// TestMsg_WriteToTempFile_fail will test the output to temporary files +// with failure +func TestMsg_WriteToTempFile_fail(t *testing.T) { + m := NewMsg() + _ = m.From("Toni Tester ") + _ = m.To("Ellenor Tester ") + m.SetBodyString(TypeTextPlain, "This is a test") + _ = os.Setenv("TMPDIR", "/does/not/exist") + f, err := m.WriteToTempFile() + if err == nil { + t.Error("write message to temporary output file was supposed to fail, but didn't") + } + _ = os.Unsetenv("TMPDIR") + _ = os.Remove(f) +} + // TestMsg_WriteToFile will test the output to a file func TestMsg_WriteToFile(t *testing.T) { f, err := os.CreateTemp("", "go-mail-test_*.eml")