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
+1 -7
View File
@@ -1,7 +1,6 @@
package setting
import (
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -14,12 +13,7 @@ func TestDynamicSettingsSupport_Override(t *testing.T) {
keyName := "bar"
expected := "dynamic value"
err := os.Setenv(envKey, expected)
require.NoError(t, err)
defer func() {
err := os.Unsetenv(envKey)
require.NoError(t, err)
}()
t.Setenv(envKey, expected)
value := cfg.SectionWithEnvOverrides(sectionName).Key(keyName).MustString("default value")
require.Equal(t, expected, value)