2022-11-05 18:25:29 -04:00
|
|
|
/*
|
|
|
|
Package env provides functions to test code that read environment variables
|
2018-02-28 10:11:02 -05:00
|
|
|
or the current working directory.
|
|
|
|
*/
|
2020-02-22 12:12:14 -05:00
|
|
|
package env // import "gotest.tools/v3/env"
|
2017-10-03 16:44:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/internal/cleanup"
|
2017-10-03 16:44:18 -04:00
|
|
|
)
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
type helperT interface {
|
|
|
|
Helper()
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:44:18 -04:00
|
|
|
// 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.
|
2020-02-22 12:12:14 -05:00
|
|
|
//
|
|
|
|
// 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.
|
2022-09-22 09:38:19 -04:00
|
|
|
//
|
|
|
|
// Deprecated: use t.SetEnv
|
2018-02-28 10:11:02 -05:00
|
|
|
func Patch(t assert.TestingT, key, value string) func() {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
oldValue, envVarExists := os.LookupEnv(key)
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, os.Setenv(key, value))
|
2020-02-22 12:12:14 -05:00
|
|
|
clean := func() {
|
2018-02-28 10:11:02 -05:00
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
if !envVarExists {
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, os.Unsetenv(key))
|
2017-10-03 16:44:18 -04:00
|
|
|
return
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, os.Setenv(key, oldValue))
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
cleanup.Cleanup(t, clean)
|
|
|
|
return clean
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchAll sets the environment to env, and returns a function which will
|
|
|
|
// reset the environment back to the previous state.
|
2020-02-22 12:12:14 -05:00
|
|
|
//
|
|
|
|
// 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.
|
2018-02-28 10:11:02 -05:00
|
|
|
func PatchAll(t assert.TestingT, env map[string]string) func() {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-10-03 16:44:18 -04:00
|
|
|
oldEnv := os.Environ()
|
|
|
|
os.Clearenv()
|
|
|
|
|
|
|
|
for key, value := range env {
|
2017-12-21 15:25:54 -05:00
|
|
|
assert.NilError(t, os.Setenv(key, value), "setenv %s=%s", key, value)
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
clean := func() {
|
2018-02-28 10:11:02 -05:00
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-10-03 16:44:18 -04:00
|
|
|
os.Clearenv()
|
|
|
|
for key, oldVal := range ToMap(oldEnv) {
|
2017-12-21 15:25:54 -05:00
|
|
|
assert.NilError(t, os.Setenv(key, oldVal), "setenv %s=%s", key, oldVal)
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
cleanup.Cleanup(t, clean)
|
|
|
|
return clean
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:01:34 -04:00
|
|
|
// ToMap takes a list of strings in the format returned by [os.Environ] and
|
2017-10-03 16:44:18 -04:00
|
|
|
// returns a mapping of keys to values.
|
|
|
|
func ToMap(env []string) map[string]string {
|
|
|
|
result := map[string]string{}
|
|
|
|
for _, raw := range env {
|
2017-12-21 15:25:54 -05:00
|
|
|
key, value := getParts(raw)
|
|
|
|
result[key] = value
|
2017-10-03 16:44:18 -04:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
|
2017-12-21 15:25:54 -05:00
|
|
|
func getParts(raw string) (string, string) {
|
2018-11-14 06:28:16 -05:00
|
|
|
if raw == "" {
|
|
|
|
return "", ""
|
|
|
|
}
|
2017-12-21 15:25:54 -05:00
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
// ChangeWorkingDir to the directory, and return a function which restores the
|
|
|
|
// previous working directory.
|
2020-02-22 12:12:14 -05:00
|
|
|
//
|
|
|
|
// 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.
|
2018-02-28 10:11:02 -05:00
|
|
|
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))
|
2020-02-22 12:12:14 -05:00
|
|
|
clean := func() {
|
2018-02-28 10:11:02 -05:00
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert.NilError(t, os.Chdir(cwd))
|
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
cleanup.Cleanup(t, clean)
|
|
|
|
return clean
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|