You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/ntest/mock/env_test.go

56 lines
1.1 KiB
Go

1 year ago
package mock_test
import (
"git.noahlan.cn/noahlan/ntool/nenv"
"git.noahlan.cn/noahlan/ntool/ntest/assert"
"git.noahlan.cn/noahlan/ntool/ntest/mock"
"os"
"testing"
)
func TestMockEnvValue(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
mock.MockEnvValue("APP_COMMAND", "new val", func(nv string) {
is.Eq("new val", nv)
})
shellVal := "custom-value"
mock.MockEnvValue("SHELL", shellVal, func(newVal string) {
is.Eq(shellVal, newVal)
})
is.Eq("", os.Getenv("APP_COMMAND"))
is.Panics(func() {
mock.MockEnvValue("invalid=", "value", nil)
})
}
func TestMockEnvValues(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
mock.MockEnvValues(map[string]string{
"APP_COMMAND": "new val",
}, func() {
is.Eq("new val", os.Getenv("APP_COMMAND"))
})
is.Eq("", os.Getenv("APP_COMMAND"))
}
func TestMockOsEnvByText(t *testing.T) {
envStr := `
APP_COMMAND = login
APP_ENV = dev
APP_DEBUG = true
`
mock.MockOsEnvByText(envStr, func() {
assert.Len(t, nenv.Environ(), 3)
assert.Eq(t, "true", os.Getenv("APP_DEBUG"))
assert.Eq(t, "login", os.Getenv("APP_COMMAND"))
})
}