2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-27 14:40:22 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/os/gcmd"
|
2019-06-10 19:50:45 +08:00
|
|
|
"os"
|
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/genv"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-06-10 19:50:45 +08:00
|
|
|
)
|
|
|
|
|
2019-07-27 14:40:22 +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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Map(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
value := gconv.String(gtime.TimestampNano())
|
2019-07-27 14:40:22 +08:00
|
|
|
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")
|
2019-07-27 14:40:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Get(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
value := gconv.String(gtime.TimestampNano())
|
2019-07-27 14:40:22 +08:00
|
|
|
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)
|
2021-10-21 18:22:47 +08:00
|
|
|
t.AssertEQ(genv.Get(key).String(), "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)
|
2021-10-21 18:22:47 +08:00
|
|
|
t.AssertEQ(genv.Get(key).String(), "TEST")
|
2021-09-27 21:27:24 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Contains(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
value := gconv.String(gtime.TimestampNano())
|
2019-07-27 14:40:22 +08:00
|
|
|
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)
|
2019-07-27 14:40:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Set(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
value := gconv.String(gtime.TimestampNano())
|
2019-07-27 14:40:22 +08:00
|
|
|
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")
|
|
|
|
})
|
|
|
|
}
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Build(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-27 14:40:22 +08:00
|
|
|
s := genv.Build(map[string]string{
|
2019-07-29 21:01:19 +08:00
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
2019-07-27 14:40:22 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN("k1=v1", s)
|
|
|
|
t.AssertIN("k2=v2", s)
|
2019-07-27 14:40:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Remove(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-20 14:14:11 +08:00
|
|
|
value := gconv.String(gtime.TimestampNano())
|
2019-07-27 14:40:22 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
2020-12-04 23:44:54 +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)
|
|
|
|
})
|
|
|
|
}
|