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")) }) }