From d0280ea9ad47e59b69921715bebaa1e55f4e7b78 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 28 Oct 2024 18:52:02 +0100 Subject: [PATCH] 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. --- msg_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ msg_unix_test.go | 43 +++++++++++++++++++++---------------------- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/msg_test.go b/msg_test.go index 3ce2b63..62c74ae 100644 --- a/msg_test.go +++ b/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 diff --git a/msg_unix_test.go b/msg_unix_test.go index 018e215..57e1547 100644 --- a/msg_unix_test.go +++ b/msg_unix_test.go @@ -120,28 +120,27 @@ func TestMsg_WriteToFile_unixOnly(t *testing.T) { }) } -/* -func TestMsg_WriteToTempFileFailed(t *testing.T) { - m := NewMsg() - _ = m.From("Toni Tester ") - _ = m.To("Ellenor Tester ") - 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) + } + }) } - - -*/