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)
|
|
|
|
|
2022-06-06 04:47:34 +08:00
|
|
|
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
|
|
|
}
|
2021-06-06 10:52:51 +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
|
|
|
}
|
2021-06-06 10:52:51 +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
|
|
|
}
|