From 0975abd5eeddde02087ac7457a188eb5d1c9fb14 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Sat, 25 Jun 2022 09:20:52 -0500 Subject: [PATCH] api: use testing.T.Setenv to set env vars in tests --- api/api.go | 4 ++-- api/api_test.go | 26 ++++++++------------------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/api/api.go b/api/api.go index e32707ebf..3d3dcf88d 100644 --- a/api/api.go +++ b/api/api.go @@ -18,8 +18,8 @@ import ( "time" "github.com/gorilla/websocket" - cleanhttp "github.com/hashicorp/go-cleanhttp" - rootcerts "github.com/hashicorp/go-rootcerts" + "github.com/hashicorp/go-cleanhttp" + "github.com/hashicorp/go-rootcerts" ) var ( diff --git a/api/api_test.go b/api/api_test.go index 7b5d35346..82d35acf1 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -11,7 +11,6 @@ import ( "net/http" "net/http/httptest" "net/url" - "os" "strings" "testing" "time" @@ -119,31 +118,22 @@ func TestRequestTime(t *testing.T) { func TestDefaultConfig_env(t *testing.T) { testutil.Parallel(t) - url := "http://1.2.3.4:5678" + testURL := "http://1.2.3.4:5678" auth := []string{"nomaduser", "12345"} region := "test" namespace := "dev" token := "foobar" - os.Setenv("NOMAD_ADDR", url) - defer os.Setenv("NOMAD_ADDR", "") - - os.Setenv("NOMAD_REGION", region) - defer os.Setenv("NOMAD_REGION", "") - - os.Setenv("NOMAD_NAMESPACE", namespace) - defer os.Setenv("NOMAD_NAMESPACE", "") - - os.Setenv("NOMAD_HTTP_AUTH", strings.Join(auth, ":")) - defer os.Setenv("NOMAD_HTTP_AUTH", "") - - os.Setenv("NOMAD_TOKEN", token) - defer os.Setenv("NOMAD_TOKEN", "") + t.Setenv("NOMAD_ADDR", testURL) + t.Setenv("NOMAD_REGION", region) + t.Setenv("NOMAD_NAMESPACE", namespace) + t.Setenv("NOMAD_HTTP_AUTH", strings.Join(auth, ":")) + t.Setenv("NOMAD_TOKEN", token) config := DefaultConfig() - if config.Address != url { - t.Errorf("expected %q to be %q", config.Address, url) + if config.Address != testURL { + t.Errorf("expected %q to be %q", config.Address, testURL) } if config.Region != region {