1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package testutil
import "os"
// Setenv sets the value of an environment variable for the duration of a test.
// It returns value.
func Setenv(c Cleanuper, name, value string) string {
SaveEnv(c, name)
os.Setenv(name, value)
return value
}
// Setenv unsets an environment variable for the duration of a test.
func Unsetenv(c Cleanuper, name string) {
SaveEnv(c, name)
os.Unsetenv(name)
}
// SaveEnv saves the current value of an environment variable so that it will be
// restored after a test has finished.
func SaveEnv(c Cleanuper, name string) {
oldValue, existed := os.LookupEnv(name)
if existed {
c.Cleanup(func() { os.Setenv(name, oldValue) })
} else {
c.Cleanup(func() { os.Unsetenv(name) })
}
}
|