Skip to content

Commit

Permalink
test: use T.Setenv to set env vars in tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Juneezee committed Sep 1, 2022
1 parent e41e186 commit a248796
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 55 deletions.
39 changes: 13 additions & 26 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config_test

import (
"errors"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -30,33 +29,22 @@ const (
ENV_TEST_MAP_LAYER_DEFAULT_TAG = "postgis"
)

func setEnv() {
func setEnv(t *testing.T) {
x := strconv.FormatFloat(ENV_TEST_CENTER_X, 'f', -1, 64)
y := strconv.FormatFloat(ENV_TEST_CENTER_Y, 'f', -1, 64)
z := strconv.FormatFloat(ENV_TEST_CENTER_Z, 'f', -1, 64)

os.Setenv("ENV_TEST_PORT", ENV_TEST_PORT)
os.Setenv("ENV_TEST_CENTER_X", x)
os.Setenv("ENV_TEST_CENTER_Y", y)
os.Setenv("ENV_TEST_CENTER_Z", z)
os.Setenv("ENV_TEST_HOST_1", ENV_TEST_HOST_1)
os.Setenv("ENV_TEST_HOST_2", ENV_TEST_HOST_2)
os.Setenv("ENV_TEST_HOST_3", ENV_TEST_HOST_3)
os.Setenv("ENV_TEST_WEBSERVER_HEADER_STRING", ENV_TEST_WEBSERVER_HEADER_STRING)
os.Setenv("ENV_TEST_WEBSERVER_PORT", ENV_TEST_WEBSERVER_PORT)
os.Setenv("ENV_TEST_PROVIDER_LAYER", ENV_TEST_PROVIDER_LAYER)
os.Setenv("ENV_TEST_MAP_LAYER_DEFAULT_TAG", ENV_TEST_MAP_LAYER_DEFAULT_TAG)
}

func unsetEnv() {
os.Unsetenv("ENV_TEST_PORT")
os.Unsetenv("ENV_TEST_CENTER_X")
os.Unsetenv("ENV_TEST_CENTER_Y")
os.Unsetenv("ENV_TEST_CENTER_Z")
os.Unsetenv("ENV_TEST_WEBSERVER_HEADER_STRING")
os.Unsetenv("ENV_TEST_WEBSERVER_PORT")
os.Unsetenv("ENV_TEST_PROVIDER_LAYER")
os.Unsetenv("ENV_TEST_MAP_LAYER_DEFAULT_TAG")
t.Setenv("ENV_TEST_PORT", ENV_TEST_PORT)
t.Setenv("ENV_TEST_CENTER_X", x)
t.Setenv("ENV_TEST_CENTER_Y", y)
t.Setenv("ENV_TEST_CENTER_Z", z)
t.Setenv("ENV_TEST_HOST_1", ENV_TEST_HOST_1)
t.Setenv("ENV_TEST_HOST_2", ENV_TEST_HOST_2)
t.Setenv("ENV_TEST_HOST_3", ENV_TEST_HOST_3)
t.Setenv("ENV_TEST_WEBSERVER_HEADER_STRING", ENV_TEST_WEBSERVER_HEADER_STRING)
t.Setenv("ENV_TEST_WEBSERVER_PORT", ENV_TEST_WEBSERVER_PORT)
t.Setenv("ENV_TEST_PROVIDER_LAYER", ENV_TEST_PROVIDER_LAYER)
t.Setenv("ENV_TEST_MAP_LAYER_DEFAULT_TAG", ENV_TEST_MAP_LAYER_DEFAULT_TAG)
}

func TestParse(t *testing.T) {
Expand All @@ -66,8 +54,7 @@ func TestParse(t *testing.T) {
expectedErr error
}

setEnv()
defer unsetEnv()
setEnv(t)

fn := func(tc tcase) func(*testing.T) {
return func(t *testing.T) {
Expand Down
10 changes: 1 addition & 9 deletions internal/env/dict_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package env_test

import (
"os"
"reflect"
"testing"

Expand All @@ -22,16 +21,9 @@ func TestDict(t *testing.T) {
return func(t *testing.T) {
// setup our env vars
for k, v := range tc.envVars {
os.Setenv(k, v)
t.Setenv(k, v)
}

// clean up env vars
defer (func() {
for k, _ := range tc.envVars {
os.Unsetenv(k)
}
})()

var val interface{}
var err error

Expand Down
10 changes: 1 addition & 9 deletions internal/env/types_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package env

import (
"os"
"testing"
)

Expand All @@ -17,16 +16,9 @@ func TestReplaceEnvVar(t *testing.T) {
return func(t *testing.T) {
// setup our env vars
for k, v := range tc.envVars {
os.Setenv(k, v)
t.Setenv(k, v)
}

// clean up env vars
defer (func() {
for k, _ := range tc.envVars {
os.Unsetenv(k)
}
})()

out, err := replaceEnvVar(tc.in)
if tc.expectedErr != nil {
if err == nil {
Expand Down
15 changes: 4 additions & 11 deletions internal/ttools/getenv_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ttools

import (
"os"
"testing"
)

Expand All @@ -10,14 +9,9 @@ const (
ENV_TEST_EMPTY_VARIABLE = ""
)

func setEnv() {
os.Setenv("ENV_TEST_VARIABLE", ENV_TEST_VARIABLE)
os.Setenv("ENV_TEST_EMPTY_VARIABLE", ENV_TEST_EMPTY_VARIABLE)
}

func unsetEnv() {
os.Unsetenv("ENV_TEST_VARIABLE")
os.Unsetenv("ENV_TEST_EMPTY_VARIABLE")
func setEnv(t *testing.T) {
t.Setenv("ENV_TEST_VARIABLE", ENV_TEST_VARIABLE)
t.Setenv("ENV_TEST_EMPTY_VARIABLE", ENV_TEST_EMPTY_VARIABLE)
}

func TestGetEnvDefault(t *testing.T) {
Expand All @@ -27,8 +21,7 @@ func TestGetEnvDefault(t *testing.T) {
expected string
}

setEnv()
defer unsetEnv()
setEnv(t)

fn := func(tc tcase) func(*testing.T) {
return func(t *testing.T) {
Expand Down

0 comments on commit a248796

Please sign in to comment.