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.
This commit is contained in:
Winni Neessen 2024-09-20 21:42:10 +02:00
parent 4053457020
commit 0b9a215e7d
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -68,6 +68,13 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) {
_ = m.To("Ellenor Tester <ellinor@example.com>")
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)
}