From c0c4049964e0079849d4f55a97374298fafff496 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 27 Oct 2024 20:59:24 +0100 Subject: [PATCH] Enable Unix write tests and fix OS version matrix. Added a test environment variable to control Unix write tests and included their setup in the test function. Also corrected the OS version matrix by uncommenting '14.0' and '13.4'. --- .github/workflows/ci.yml | 3 ++- msg_unix_test.go | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed2356c..8d07bef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,7 @@ jobs: go: ['1.23'] env: PERFORM_ONLINE_TEST: ${{ vars.PERFORM_ONLINE_TEST }} + PERFORM_UNIX_OPEN_WRITE_TESTS: "true" TEST_SENDMAIL: ${{ vars.TEST_SENDMAIL }} TEST_HOST: ${{ secrets.TEST_HOST }} TEST_USER: ${{ secrets.TEST_USER }} @@ -145,7 +146,7 @@ jobs: strategy: matrix: osver: ['14.1'] - #osver: ['13.4', '14.0', '14.1'] + osver: ['14.1', '14.0', 13.4'] steps: - name: Checkout Code uses: actions/checkout@61b9e3751b92087fd0b06925ba6dd6314e06f089 # master diff --git a/msg_unix_test.go b/msg_unix_test.go index be702e7..f671d97 100644 --- a/msg_unix_test.go +++ b/msg_unix_test.go @@ -9,27 +9,43 @@ package mail import ( "bytes" + "context" "errors" "os" "testing" + "time" ) func TestMsg_AttachFile_unixOnly(t *testing.T) { t.Run("AttachFile with fileFromFS fails on open", func(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") + } + + tempFile, err := os.CreateTemp("testdata/tmp", "attachfile-open-write-test.*.txt") + if err != nil { + t.Fatalf("failed to create temp file: %s", err) + } + t.Cleanup(func() { + if err := os.Remove(tempFile.Name()); err != nil { + t.Errorf("failed to remove temp file: %s", err) + } + }) + if err = os.Chmod(tempFile.Name(), 0o000); err != nil { + t.Fatalf("failed to chmod temp file: %s", err) + } + message := NewMsg() if message == nil { t.Fatal("message is nil") } - // The /dev/mem device should not be readable on normal UNIX systems. We choose this - // approach over os.Chmod(0000) on a temp file, since Github runners give full access - // to the file system - message.AttachFile("/dev/mem") + message.AttachFile(tempFile.Name()) attachments := message.GetAttachments() if len(attachments) != 1 { t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments)) } messageBuf := bytes.NewBuffer(nil) - _, err := attachments[0].Writer(messageBuf) + _, err = attachments[0].Writer(messageBuf) if err == nil { t.Error("writer func expected to fail, but didn't") } @@ -117,7 +133,6 @@ func TestMsg_AttachReader_unixOnly(t *testing.T) { }) } -/* // TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg func TestMsg_WriteToSendmailWithContext(t *testing.T) { if os.Getenv("TEST_SENDMAIL") != "true" { @@ -187,4 +202,3 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) { t.Errorf("WriteToTempFile() did not fail as expected") } } -*/