From 0b9a215e7d37c7fe52d96303798c6b2fa04ad9a4 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 20 Sep 2024 21:42:10 +0200 Subject: [PATCH] Add deferred TMPDIR restoration in test setup Store and restore the original TMPDIR value using a deferred function in `msg_nowin_test.go`. This ensures the TMPDIR environment variable is restored after the test runs, preventing potential side effects on other tests or processes. --- msg_nowin_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/msg_nowin_test.go b/msg_nowin_test.go index e338c57..6cde71a 100644 --- a/msg_nowin_test.go +++ b/msg_nowin_test.go @@ -68,6 +68,13 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) { _ = m.To("Ellenor Tester ") m.SetBodyString(TypeTextPlain, "This is a test") + curTmpDir := os.Getenv("TMPDIR") + defer 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.Errorf("failed to set TMPDIR environment variable: %s", err) }