test: use T.Setenv to set env vars in tests (#189)

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
2022-06-30 09:15:34 +09:00
committed by GitHub
parent a5b7c2b08d
commit ce3b81e2a1
8 changed files with 20 additions and 52 deletions
+1 -5
View File
@@ -2,7 +2,6 @@ package tmpl
import (
"fmt"
"os"
"reflect"
"testing"
)
@@ -234,10 +233,7 @@ func Test_renderTemplateToString(t *testing.T) {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.args.envs {
err := os.Setenv(k, v)
if err != nil {
t.Error("renderTemplateToString() could not set env var for testing")
}
t.Setenv(k, v)
}
got, err := renderTemplateToString(tt.args.s, tt.args.data)
if (err != nil) != tt.wantErr {