Replace use of env test util with standard library call

As of Go 1.17, `Setenv` can be used to set environment variables
specific to a single test. This also removes a package which gets
vendored just for this.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2025-07-02 15:34:01 -07:00
parent ab92fc4524
commit e93e15afb0
3 changed files with 7 additions and 131 deletions

View File

@@ -15,7 +15,6 @@ import (
"github.com/docker/docker/api/types"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/env"
"gotest.tools/v3/skip"
)
@@ -86,10 +85,11 @@ func TestNewClientWithOpsFromEnv(t *testing.T) {
},
}
env.PatchAll(t, nil)
for _, tc := range testcases {
t.Run(tc.doc, func(t *testing.T) {
env.PatchAll(t, tc.envs)
for key, value := range tc.envs {
t.Setenv(key, value)
}
client, err := NewClientWithOpts(FromEnv)
if tc.expectedError != "" {
assert.Check(t, is.Error(err, tc.expectedError))
@@ -228,12 +228,10 @@ func TestParseHostURL(t *testing.T) {
}
func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
env.PatchAll(t, map[string]string{
"DOCKER_HOST": "",
"DOCKER_API_VERSION": "",
"DOCKER_TLS_VERIFY": "",
"DOCKER_CERT_PATH": "",
})
t.Setenv("DOCKER_HOST", "")
t.Setenv("DOCKER_API_VERSION", "")
t.Setenv("DOCKER_TLS_VERIFY", "")
t.Setenv("DOCKER_CERT_PATH", "")
client, err := NewClientWithOpts(FromEnv)
assert.NilError(t, err)

View File

@@ -1,121 +0,0 @@
/*
Package env provides functions to test code that read environment variables
or the current working directory.
*/
package env // import "gotest.tools/v3/env"
import (
"os"
"strings"
"gotest.tools/v3/assert"
"gotest.tools/v3/internal/cleanup"
)
type helperT interface {
Helper()
}
// Patch changes the value of an environment variable, and returns a
// function which will reset the the value of that variable back to the
// previous state.
//
// When used with Go 1.14+ the unpatch function will be called automatically
// when the test ends, unless the TEST_NOCLEANUP env var is set to true.
//
// Deprecated: use t.SetEnv
func Patch(t assert.TestingT, key, value string) func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
oldValue, envVarExists := os.LookupEnv(key)
assert.NilError(t, os.Setenv(key, value))
clean := func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if !envVarExists {
assert.NilError(t, os.Unsetenv(key))
return
}
assert.NilError(t, os.Setenv(key, oldValue))
}
cleanup.Cleanup(t, clean)
return clean
}
// PatchAll sets the environment to env, and returns a function which will
// reset the environment back to the previous state.
//
// When used with Go 1.14+ the unpatch function will be called automatically
// when the test ends, unless the TEST_NOCLEANUP env var is set to true.
func PatchAll(t assert.TestingT, env map[string]string) func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
oldEnv := os.Environ()
os.Clearenv()
for key, value := range env {
assert.NilError(t, os.Setenv(key, value), "setenv %s=%s", key, value)
}
clean := func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
os.Clearenv()
for key, oldVal := range ToMap(oldEnv) {
assert.NilError(t, os.Setenv(key, oldVal), "setenv %s=%s", key, oldVal)
}
}
cleanup.Cleanup(t, clean)
return clean
}
// ToMap takes a list of strings in the format returned by [os.Environ] and
// returns a mapping of keys to values.
func ToMap(env []string) map[string]string {
result := map[string]string{}
for _, raw := range env {
key, value := getParts(raw)
result[key] = value
}
return result
}
func getParts(raw string) (string, string) {
if raw == "" {
return "", ""
}
// Environment variables on windows can begin with =
// http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
parts := strings.SplitN(raw[1:], "=", 2)
key := raw[:1] + parts[0]
if len(parts) == 1 {
return key, ""
}
return key, parts[1]
}
// ChangeWorkingDir to the directory, and return a function which restores the
// previous working directory.
//
// When used with Go 1.14+ the previous working directory will be restored
// automatically when the test ends, unless the TEST_NOCLEANUP env var is set to
// true.
func ChangeWorkingDir(t assert.TestingT, dir string) func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
cwd, err := os.Getwd()
assert.NilError(t, err)
assert.NilError(t, os.Chdir(dir))
clean := func() {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
assert.NilError(t, os.Chdir(cwd))
}
cleanup.Cleanup(t, clean)
return clean
}

1
vendor/modules.txt vendored
View File

@@ -1684,7 +1684,6 @@ gopkg.in/yaml.v3
gotest.tools/v3/assert
gotest.tools/v3/assert/cmp
gotest.tools/v3/assert/opt
gotest.tools/v3/env
gotest.tools/v3/fs
gotest.tools/v3/golden
gotest.tools/v3/icmd