2019-07-27 14:40:22 +08:00
|
|
|
// Copyright 2017-2019 gf Author(https://github.com/gogf/gf). 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 (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_All(t *testing.T) {
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
gtest.Assert(os.Environ(), genv.All())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Map(t *testing.T) {
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-07-27 14:40:22 +08:00
|
|
|
value := gconv.String(gtime.Nanosecond())
|
|
|
|
key := "TEST_ENV_" + value
|
|
|
|
err := os.Setenv(key, "TEST")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(genv.Map()[key], "TEST")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Get(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
value := gconv.String(gtime.Nanosecond())
|
|
|
|
key := "TEST_ENV_" + value
|
2019-06-10 20:24:11 +08:00
|
|
|
err := os.Setenv(key, "TEST")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.AssertEQ(genv.Get(key), "TEST")
|
2019-06-10 19:50:45 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Contains(t *testing.T) {
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-07-27 14:40:22 +08:00
|
|
|
value := gconv.String(gtime.Nanosecond())
|
|
|
|
key := "TEST_ENV_" + value
|
|
|
|
err := os.Setenv(key, "TEST")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.AssertEQ(genv.Contains(key), true)
|
|
|
|
gtest.AssertEQ(genv.Contains("none"), false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Set(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
value := gconv.String(gtime.Nanosecond())
|
|
|
|
key := "TEST_ENV_" + value
|
2019-06-10 20:24:11 +08:00
|
|
|
err := genv.Set(key, "TEST")
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-06-10 20:24:11 +08:00
|
|
|
gtest.AssertEQ(os.Getenv(key), "TEST")
|
2019-06-10 19:50:45 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:40:22 +08:00
|
|
|
func Test_GEnv_Build(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
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
|
|
|
})
|
|
|
|
gtest.AssertIN("k1=v1", s)
|
|
|
|
gtest.AssertIN("k2=v2", s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GEnv_Remove(t *testing.T) {
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-07-27 14:40:22 +08:00
|
|
|
value := gconv.String(gtime.Nanosecond())
|
|
|
|
key := "TEST_ENV_" + value
|
2019-06-10 20:24:11 +08:00
|
|
|
err := os.Setenv(key, "TEST")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
err = genv.Remove(key)
|
2019-06-10 19:50:45 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-06-10 20:24:11 +08:00
|
|
|
gtest.AssertEQ(os.Getenv(key), "")
|
2019-06-10 19:50:45 +08:00
|
|
|
})
|
|
|
|
}
|