github.com/gogf/gf@v1.16.9/os/genv/genv_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package genv_test 8 9 import ( 10 "github.com/gogf/gf/frame/g" 11 "github.com/gogf/gf/os/gcmd" 12 "os" 13 "testing" 14 15 "github.com/gogf/gf/os/genv" 16 "github.com/gogf/gf/os/gtime" 17 "github.com/gogf/gf/test/gtest" 18 "github.com/gogf/gf/util/gconv" 19 ) 20 21 func Test_GEnv_All(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 t.Assert(os.Environ(), genv.All()) 24 }) 25 } 26 27 func Test_GEnv_Map(t *testing.T) { 28 gtest.C(t, func(t *gtest.T) { 29 value := gconv.String(gtime.TimestampNano()) 30 key := "TEST_ENV_" + value 31 err := os.Setenv(key, "TEST") 32 t.Assert(err, nil) 33 t.Assert(genv.Map()[key], "TEST") 34 }) 35 } 36 37 func Test_GEnv_Get(t *testing.T) { 38 gtest.C(t, func(t *gtest.T) { 39 value := gconv.String(gtime.TimestampNano()) 40 key := "TEST_ENV_" + value 41 err := os.Setenv(key, "TEST") 42 t.Assert(err, nil) 43 t.AssertEQ(genv.Get(key), "TEST") 44 }) 45 } 46 47 func Test_GEnv_Contains(t *testing.T) { 48 gtest.C(t, func(t *gtest.T) { 49 value := gconv.String(gtime.TimestampNano()) 50 key := "TEST_ENV_" + value 51 err := os.Setenv(key, "TEST") 52 t.Assert(err, nil) 53 t.AssertEQ(genv.Contains(key), true) 54 t.AssertEQ(genv.Contains("none"), false) 55 }) 56 } 57 58 func Test_GEnv_Set(t *testing.T) { 59 gtest.C(t, func(t *gtest.T) { 60 value := gconv.String(gtime.TimestampNano()) 61 key := "TEST_ENV_" + value 62 err := genv.Set(key, "TEST") 63 t.Assert(err, nil) 64 t.AssertEQ(os.Getenv(key), "TEST") 65 }) 66 } 67 68 func Test_GEnv_SetMap(t *testing.T) { 69 gtest.C(t, func(t *gtest.T) { 70 err := genv.SetMap(g.MapStrStr{ 71 "K1": "TEST1", 72 "K2": "TEST2", 73 }) 74 t.Assert(err, nil) 75 t.AssertEQ(os.Getenv("K1"), "TEST1") 76 t.AssertEQ(os.Getenv("K2"), "TEST2") 77 }) 78 } 79 func Test_GEnv_Build(t *testing.T) { 80 gtest.C(t, func(t *gtest.T) { 81 s := genv.Build(map[string]string{ 82 "k1": "v1", 83 "k2": "v2", 84 }) 85 t.AssertIN("k1=v1", s) 86 t.AssertIN("k2=v2", s) 87 }) 88 } 89 90 func Test_GEnv_Remove(t *testing.T) { 91 gtest.C(t, func(t *gtest.T) { 92 value := gconv.String(gtime.TimestampNano()) 93 key := "TEST_ENV_" + value 94 err := os.Setenv(key, "TEST") 95 t.Assert(err, nil) 96 err = genv.Remove(key) 97 t.Assert(err, nil) 98 t.AssertEQ(os.Getenv(key), "") 99 }) 100 } 101 102 func Test_GetWithCmd(t *testing.T) { 103 gtest.C(t, func(t *gtest.T) { 104 gcmd.Init("-test", "2") 105 t.Assert(genv.GetWithCmd("TEST"), 2) 106 }) 107 gtest.C(t, func(t *gtest.T) { 108 genv.Set("TEST", "1") 109 defer genv.Remove("TEST") 110 gcmd.Init("-test", "2") 111 t.Assert(genv.GetWithCmd("test"), 1) 112 }) 113 }