Skip to content
Snippets Groups Projects
Unverified Commit c3d8128c authored by Eng Zer Jun's avatar Eng Zer Jun Committed by GitHub
Browse files

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

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: default avatarEng Zer Jun <engzerjun@gmail.com>

Signed-off-by: default avatarEng Zer Jun <engzerjun@gmail.com>
parent 0008d897
No related branches found
No related tags found
No related merge requests found
......@@ -50,9 +50,9 @@ func TestS3FS(t *testing.T) {
err = json.Unmarshal(content, &config)
assert.Nil(t, err)
os.Setenv("AWS_REGION", config.Region)
os.Setenv("AWS_ACCESS_KEY_ID", config.APIKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", config.APISecret)
t.Setenv("AWS_REGION", config.Region)
t.Setenv("AWS_ACCESS_KEY_ID", config.APIKey)
t.Setenv("AWS_SECRET_ACCESS_KEY", config.APISecret)
t.Run("file service", func(t *testing.T) {
testFileService(t, func() FileService {
......@@ -129,9 +129,9 @@ func TestS3FSMinioServer(t *testing.T) {
assert.Nil(t, err)
// set s3 credentials
os.Setenv("AWS_REGION", "test")
os.Setenv("AWS_ACCESS_KEY_ID", "minioadmin")
os.Setenv("AWS_SECRET_ACCESS_KEY", "minioadmin")
t.Setenv("AWS_REGION", "test")
t.Setenv("AWS_ACCESS_KEY_ID", "minioadmin")
t.Setenv("AWS_SECRET_ACCESS_KEY", "minioadmin")
endpoint := "http://localhost:9000"
......@@ -195,9 +195,9 @@ func BenchmarkS3FS(b *testing.B) {
err = json.Unmarshal(content, &config)
assert.Nil(b, err)
os.Setenv("AWS_REGION", config.Region)
os.Setenv("AWS_ACCESS_KEY_ID", config.APIKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", config.APISecret)
b.Setenv("AWS_REGION", config.Region)
b.Setenv("AWS_ACCESS_KEY_ID", config.APIKey)
b.Setenv("AWS_SECRET_ACCESS_KEY", config.APISecret)
b.ResetTimer()
......
......@@ -15,7 +15,6 @@
package metric
import (
"os"
"strconv"
"testing"
......@@ -26,16 +25,16 @@ func TestEnvOrDefaultBool(t *testing.T) {
key := "MO_TEST"
assert.Equal(t, envOrDefaultBool(key, 42), int32(42))
for _, s := range []string{"1", "True", "T", "true"} {
os.Setenv(key, s)
t.Setenv(key, s)
assert.Equal(t, envOrDefaultBool(key, 42), int32(1))
}
for _, s := range []string{"0", "f", "false"} {
os.Setenv(key, s)
t.Setenv(key, s)
assert.Equal(t, envOrDefaultBool(key, 42), int32(0))
}
for _, s := range []string{"", "nope", "stop"} {
os.Setenv(key, s)
t.Setenv(key, s)
assert.Equal(t, envOrDefaultBool(key, 42), int32(42))
}
}
......@@ -45,11 +44,11 @@ func TestEnvOrDefaultInt(t *testing.T) {
assert.Equal(t, envOrDefaultInt[int32](key, 42), int32(42))
for _, i := range []int{1, 2, 3, 5} {
os.Setenv(key, strconv.Itoa(i))
t.Setenv(key, strconv.Itoa(i))
assert.Equal(t, envOrDefaultInt[int32](key, 42), int32(i))
}
for _, s := range []string{"x", "02f1", "ffs", "0x12"} {
os.Setenv(key, s)
t.Setenv(key, s)
assert.Equal(t, envOrDefaultInt[int64](key, 42), int64(42))
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment