Tests: use t.Setenv to set env vars (#69516)

This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2023-06-05 11:31:03 +02:00
committed by GitHub
parent 4c794fe8b9
commit cf1945d0c3
13 changed files with 35 additions and 98 deletions
@@ -3,7 +3,6 @@ package notifiers
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -70,7 +69,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Can read correct properties", func(t *testing.T) {
setup()
_ = os.Setenv("TEST_VAR", "default")
t.Setenv("TEST_VAR", "default")
cfgProvider := &configReader{
orgService: orgService,
encryptionService: encryptionService,
@@ -78,7 +77,6 @@ func TestNotificationAsConfig(t *testing.T) {
}
cfg, err := cfgProvider.readConfig(context.Background(), correctProperties)
_ = os.Unsetenv("TEST_VAR")
if err != nil {
t.Fatalf("readConfig return an error %v", err)
}