elvish/pkg/eval/vars/env_test.go

39 lines
778 B
Go
Raw Normal View History

2018-11-18 22:23:31 +08:00
package vars
import (
"os"
"testing"
2022-06-05 23:01:10 +08:00
"src.elv.sh/pkg/testutil"
2018-11-18 22:23:31 +08:00
)
2022-06-05 23:01:10 +08:00
func TestEnvVariable(t *testing.T) {
2018-11-18 22:23:31 +08:00
name := "elvish_test"
2022-06-05 23:01:10 +08:00
testutil.Unsetenv(t, name)
v := FromEnv(name).(envVariable)
2022-06-05 23:01:10 +08:00
if set := v.IsSet(); set != false {
t.Errorf("EnvVariable.Set returns true for unset env variable")
2018-11-18 22:23:31 +08:00
}
2022-06-05 23:01:10 +08:00
err := v.Set("foo")
if err != nil || os.Getenv(name) != "foo" {
t.Errorf("EnvVariable.Set doesn't alter env value")
}
if set := v.IsSet(); set != true {
t.Errorf("EnvVariable.Set returns false for set env variable")
2018-11-18 22:23:31 +08:00
}
err = v.Set(true)
if err != errEnvMustBeString {
t.Errorf("envVariable.Set to a non-string value didn't return an error")
}
2022-06-05 23:01:10 +08:00
os.Setenv(name, "bar")
if v.Get() != "bar" {
t.Errorf("EnvVariable.Get doesn't return value set elsewhere")
}
2018-11-18 22:23:31 +08:00
}