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 +1,6 @@
package alerting
import (
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -13,11 +12,8 @@ func TestNotificationPolicy(t *testing.T) {
envKey = "NOTIFIER_EMAIL_REMINDER_FREQUENCY"
envValue = "4h"
)
err := os.Setenv(envKey, envValue)
require.NoError(t, err)
defer func() {
_ = os.Unsetenv(envKey)
}()
t.Setenv(envKey, envValue)
data := `orgId: 123
receiver: test
continue: true
@@ -25,7 +21,7 @@ repeat_interval: ${NOTIFIER_EMAIL_REMINDER_FREQUENCY}
`
var model NotificiationPolicyV1
err = yaml.Unmarshal([]byte(data), &model)
err := yaml.Unmarshal([]byte(data), &model)
require.NoError(t, err)
np, err := model.mapToModel()
require.NoError(t, err)
@@ -3,7 +3,6 @@ package dashboards
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
@@ -47,10 +46,9 @@ func TestDashboardsAsConfig(t *testing.T) {
})
t.Run("Can read config file version 1 format", func(t *testing.T) {
_ = os.Setenv("TEST_VAR", "general")
t.Setenv("TEST_VAR", "general")
cfgProvider := configReader{path: simpleDashboardConfig, log: logger, orgService: orgFake}
cfg, err := cfgProvider.readConfig(context.Background())
_ = os.Unsetenv("TEST_VAR")
require.NoError(t, err)
validateDashboardAsConfig(t, cfg)
@@ -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)
}
@@ -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)
}
@@ -2,7 +2,6 @@ package plugins
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -55,11 +54,7 @@ func TestConfigReader(t *testing.T) {
},
}
err := os.Setenv("ENABLE_PLUGIN_VAR", "test-plugin")
require.NoError(t, err)
t.Cleanup(func() {
_ = os.Unsetenv("ENABLE_PLUGIN_VAR")
})
t.Setenv("ENABLE_PLUGIN_VAR", "test-plugin")
cfgProvider := newConfigReader(log.New("test logger"), pm)
cfg, err := cfgProvider.readConfig(context.Background(), correctProperties)
@@ -16,25 +16,10 @@ import (
func TestValues(t *testing.T) {
t.Run("Values", func(t *testing.T) {
err := os.Setenv("INT", "1")
require.NoError(t, err)
err = os.Setenv("STRING", "test")
require.NoError(t, err)
err = os.Setenv("EMPTYSTRING", "")
require.NoError(t, err)
err = os.Setenv("BOOL", "true")
require.NoError(t, err)
defer func() {
err := os.Unsetenv("INT")
require.NoError(t, err)
err = os.Unsetenv("STRING")
require.NoError(t, err)
err = os.Unsetenv("EMPTYSTRING")
require.NoError(t, err)
err = os.Unsetenv("BOOL")
require.NoError(t, err)
}()
t.Setenv("INT", "1")
t.Setenv("STRING", "test")
t.Setenv("EMPTYSTRING", "")
t.Setenv("BOOL", "true")
t.Run("IntValue", func(t *testing.T) {
type Data struct {