mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 02:57:52 +08:00
e9d328aa16
This allows turning the following pattern in tests: value, cleanup := setupSomething() defer cleanup() into the following: value := setupSomething(t)
79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
package testutil
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
const envName = "ELVISH_TEST_ENV"
|
|
|
|
func TestSetenv_ExistingEnv(t *testing.T) {
|
|
os.Setenv(envName, "old value")
|
|
defer os.Unsetenv(envName)
|
|
|
|
c := &cleanuper{}
|
|
v := Setenv(c, envName, "new value")
|
|
|
|
if v != "new value" {
|
|
t.Errorf("did not return new value")
|
|
}
|
|
if os.Getenv(envName) != "new value" {
|
|
t.Errorf("did not set to new value")
|
|
}
|
|
c.runCleanups()
|
|
if os.Getenv(envName) != "old value" {
|
|
t.Errorf("did not restore to old value")
|
|
}
|
|
}
|
|
|
|
func TestSetenv_NewEnv(t *testing.T) {
|
|
os.Unsetenv(envName)
|
|
|
|
c := &cleanuper{}
|
|
v := Setenv(c, envName, "new value")
|
|
|
|
if v != "new value" {
|
|
t.Errorf("did not return new value")
|
|
}
|
|
if os.Getenv(envName) != "new value" {
|
|
t.Errorf("did not set to new value")
|
|
}
|
|
c.runCleanups()
|
|
if _, exists := os.LookupEnv(envName); exists {
|
|
t.Errorf("did not remove")
|
|
}
|
|
}
|
|
|
|
func TestUnsetenv_ExistingEnv(t *testing.T) {
|
|
os.Setenv(envName, "old value")
|
|
defer os.Unsetenv(envName)
|
|
|
|
c := &cleanuper{}
|
|
Unsetenv(c, envName)
|
|
|
|
if _, exists := os.LookupEnv(envName); exists {
|
|
t.Errorf("did not unset")
|
|
}
|
|
c.runCleanups()
|
|
if os.Getenv(envName) != "old value" {
|
|
t.Errorf("did not restore to old value")
|
|
}
|
|
}
|
|
|
|
func TestUnsetenv_NewEnv(t *testing.T) {
|
|
os.Unsetenv(envName)
|
|
|
|
c := &cleanuper{}
|
|
Unsetenv(c, envName)
|
|
|
|
if _, exists := os.LookupEnv(envName); exists {
|
|
t.Errorf("did not unset")
|
|
}
|
|
c.runCleanups()
|
|
if _, exists := os.LookupEnv(envName); exists {
|
|
t.Errorf("did not remove")
|
|
}
|
|
}
|
|
|
|
// SaveEnv tested as a dependency of Setenv and Unsetenv
|