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
@@ -2,7 +2,6 @@ package datasources
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -192,10 +191,9 @@ func TestDatasourceAsConfig(t *testing.T) {
})
t.Run("can read all properties from version 1", func(t *testing.T) {
_ = os.Setenv("TEST_VAR", "name")
t.Setenv("TEST_VAR", "name")
cfgProvider := &configReader{log: log.New("test logger"), orgService: &orgtest.FakeOrgService{}}
cfg, err := cfgProvider.readConfig(context.Background(), allProperties)
_ = os.Unsetenv("TEST_VAR")
if err != nil {
t.Fatalf("readConfig return an error %v", err)
}