Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.Setenv to set env vars in tests #882

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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