mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 09:57:51 +08:00
e9d328aa16
This allows turning the following pattern in tests: value, cleanup := setupSomething() defer cleanup() into the following: value := setupSomething(t)
29 lines
719 B
Go
29 lines
719 B
Go
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) })
|
|
}
|
|
}
|