gf/os/genv/genv_test.go

124 lines
2.8 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-06-10 19:50:45 +08:00
package genv_test
import (
2020-12-05 00:06:03 +08:00
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcmd"
2019-06-10 19:50:45 +08:00
"os"
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/genv"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
2019-06-10 19:50:45 +08:00
)
func Test_GEnv_All(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(os.Environ(), genv.All())
2019-06-10 19:50:45 +08:00
})
}
func Test_GEnv_Map(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(genv.Map()[key], "TEST")
})
}
func Test_GEnv_Get(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
2019-06-10 20:24:11 +08:00
err := os.Setenv(key, "TEST")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertEQ(genv.Get(key), "TEST")
2019-06-10 19:50:45 +08:00
})
}
2021-09-27 21:27:24 +08:00
func Test_GEnv_GetVar(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.Assert(err, nil)
t.AssertEQ(genv.GetVar(key).String(), "TEST")
})
}
func Test_GEnv_Contains(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertEQ(genv.Contains(key), true)
t.AssertEQ(genv.Contains("none"), false)
})
}
func Test_GEnv_Set(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
2019-06-10 20:24:11 +08:00
err := genv.Set(key, "TEST")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertEQ(os.Getenv(key), "TEST")
2019-06-10 19:50:45 +08:00
})
}
2020-12-05 00:06:03 +08:00
func Test_GEnv_SetMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := genv.SetMap(g.MapStrStr{
"K1": "TEST1",
"K2": "TEST2",
})
t.Assert(err, nil)
t.AssertEQ(os.Getenv("K1"), "TEST1")
t.AssertEQ(os.Getenv("K2"), "TEST2")
})
}
func Test_GEnv_Build(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
s := genv.Build(map[string]string{
2019-07-29 21:01:19 +08:00
"k1": "v1",
"k2": "v2",
})
2020-03-19 22:56:12 +08:00
t.AssertIN("k1=v1", s)
t.AssertIN("k2=v2", s)
})
}
func Test_GEnv_Remove(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
2019-06-10 20:24:11 +08:00
err := os.Setenv(key, "TEST")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-06-10 20:24:11 +08:00
err = genv.Remove(key)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertEQ(os.Getenv(key), "")
2019-06-10 19:50:45 +08:00
})
}
func Test_GetWithCmd(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gcmd.Init("-test", "2")
t.Assert(genv.GetWithCmd("TEST"), 2)
})
gtest.C(t, func(t *gtest.T) {
genv.Set("TEST", "1")
defer genv.Remove("TEST")
gcmd.Init("-test", "2")
t.Assert(genv.GetWithCmd("test"), 1)
})
}