github.com/gogf/gf/v2@v2.7.4/frame/gins/gins_z_unit_config_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 gins_test 8 9 import ( 10 "context" 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/gogf/gf/v2/frame/gins" 16 "github.com/gogf/gf/v2/os/gcfg" 17 "github.com/gogf/gf/v2/os/gfile" 18 "github.com/gogf/gf/v2/os/gtime" 19 "github.com/gogf/gf/v2/test/gtest" 20 ) 21 22 var ( 23 ctx = context.Background() 24 configContent = gfile.GetContents( 25 gtest.DataPath("config", "config.toml"), 26 ) 27 ) 28 29 func Test_Config1(t *testing.T) { 30 gtest.C(t, func(t *gtest.T) { 31 t.AssertNE(configContent, "") 32 }) 33 gtest.C(t, func(t *gtest.T) { 34 t.AssertNE(gins.Config(), nil) 35 }) 36 } 37 38 func Test_Config2(t *testing.T) { 39 // relative path 40 gtest.C(t, func(t *gtest.T) { 41 var err error 42 dirPath := gfile.Temp(gtime.TimestampNanoStr()) 43 err = gfile.Mkdir(dirPath) 44 t.AssertNil(err) 45 defer gfile.Remove(dirPath) 46 47 name := "config.toml" 48 err = gfile.PutContents(gfile.Join(dirPath, name), configContent) 49 t.AssertNil(err) 50 51 err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath) 52 t.AssertNil(err) 53 54 defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() 55 56 t.Assert(gins.Config().MustGet(ctx, "test"), "v=1") 57 t.Assert(gins.Config().MustGet(ctx, "database.default.1.host"), "127.0.0.1") 58 t.Assert(gins.Config().MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 59 }) 60 // for gfsnotify callbacks to refresh cache of config file 61 time.Sleep(500 * time.Millisecond) 62 63 // relative path, config folder 64 gtest.C(t, func(t *gtest.T) { 65 var err error 66 dirPath := gfile.Temp(gtime.TimestampNanoStr()) 67 t.AssertNil(gfile.Mkdir(dirPath)) 68 defer gfile.Remove(dirPath) 69 70 name := "config/config.toml" 71 err = gfile.PutContents(gfile.Join(dirPath, name), configContent) 72 t.AssertNil(err) 73 74 err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath) 75 t.AssertNil(err) 76 77 defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() 78 79 t.Assert(gins.Config().MustGet(ctx, "test"), "v=1") 80 t.Assert(gins.Config().MustGet(ctx, "database.default.1.host"), "127.0.0.1") 81 t.Assert(gins.Config().MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 82 83 // for gfsnotify callbacks to refresh cache of config file 84 time.Sleep(500 * time.Millisecond) 85 }) 86 } 87 88 func Test_Config3(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 var err error 91 dirPath := gfile.Temp(gtime.TimestampNanoStr()) 92 err = gfile.Mkdir(dirPath) 93 t.AssertNil(err) 94 defer gfile.Remove(dirPath) 95 96 name := "test.toml" 97 err = gfile.PutContents(gfile.Join(dirPath, name), configContent) 98 t.AssertNil(err) 99 100 err = gins.Config("test").GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath) 101 t.AssertNil(err) 102 103 defer gins.Config("test").GetAdapter().(*gcfg.AdapterFile).Clear() 104 gins.Config("test").GetAdapter().(*gcfg.AdapterFile).SetFileName("test.toml") 105 106 t.Assert(gins.Config("test").MustGet(ctx, "test"), "v=1") 107 t.Assert(gins.Config("test").MustGet(ctx, "database.default.1.host"), "127.0.0.1") 108 t.Assert(gins.Config("test").MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 109 }) 110 // for gfsnotify callbacks to refresh cache of config file 111 time.Sleep(500 * time.Millisecond) 112 113 gtest.C(t, func(t *gtest.T) { 114 var err error 115 dirPath := gfile.Temp(gtime.TimestampNanoStr()) 116 err = gfile.Mkdir(dirPath) 117 t.AssertNil(err) 118 defer gfile.Remove(dirPath) 119 120 name := "config/test.toml" 121 err = gfile.PutContents(gfile.Join(dirPath, name), configContent) 122 t.AssertNil(err) 123 124 err = gins.Config("test").GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath) 125 t.AssertNil(err) 126 127 defer gins.Config("test").GetAdapter().(*gcfg.AdapterFile).Clear() 128 gins.Config("test").GetAdapter().(*gcfg.AdapterFile).SetFileName("test.toml") 129 130 t.Assert(gins.Config("test").MustGet(ctx, "test"), "v=1") 131 t.Assert(gins.Config("test").MustGet(ctx, "database.default.1.host"), "127.0.0.1") 132 t.Assert(gins.Config("test").MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 133 }) 134 // for gfsnotify callbacks to refresh cache of config file for next unit testing case. 135 time.Sleep(500 * time.Millisecond) 136 } 137 138 func Test_Config4(t *testing.T) { 139 // absolute path 140 gtest.C(t, func(t *gtest.T) { 141 path := fmt.Sprintf(`%s/%d`, gfile.Temp(), gtime.TimestampNano()) 142 file := fmt.Sprintf(`%s/%s`, path, "config.toml") 143 err := gfile.PutContents(file, configContent) 144 t.AssertNil(err) 145 defer gfile.Remove(file) 146 defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() 147 148 t.Assert(gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(path), nil) 149 t.Assert(gins.Config().MustGet(ctx, "test"), "v=1") 150 t.Assert(gins.Config().MustGet(ctx, "database.default.1.host"), "127.0.0.1") 151 t.Assert(gins.Config().MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 152 }) 153 time.Sleep(500 * time.Millisecond) 154 155 gtest.C(t, func(t *gtest.T) { 156 path := fmt.Sprintf(`%s/%d/config`, gfile.Temp(), gtime.TimestampNano()) 157 file := fmt.Sprintf(`%s/%s`, path, "config.toml") 158 err := gfile.PutContents(file, configContent) 159 t.AssertNil(err) 160 defer gfile.Remove(file) 161 defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() 162 t.Assert(gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(path), nil) 163 t.Assert(gins.Config().MustGet(ctx, "test"), "v=1") 164 t.Assert(gins.Config().MustGet(ctx, "database.default.1.host"), "127.0.0.1") 165 t.Assert(gins.Config().MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 166 }) 167 time.Sleep(500 * time.Millisecond) 168 169 gtest.C(t, func(t *gtest.T) { 170 path := fmt.Sprintf(`%s/%d`, gfile.Temp(), gtime.TimestampNano()) 171 file := fmt.Sprintf(`%s/%s`, path, "test.toml") 172 err := gfile.PutContents(file, configContent) 173 t.AssertNil(err) 174 defer gfile.Remove(file) 175 defer gins.Config("test").GetAdapter().(*gcfg.AdapterFile).Clear() 176 gins.Config("test").GetAdapter().(*gcfg.AdapterFile).SetFileName("test.toml") 177 t.Assert(gins.Config("test").GetAdapter().(*gcfg.AdapterFile).AddPath(path), nil) 178 t.Assert(gins.Config("test").MustGet(ctx, "test"), "v=1") 179 t.Assert(gins.Config("test").MustGet(ctx, "database.default.1.host"), "127.0.0.1") 180 t.Assert(gins.Config("test").MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 181 }) 182 time.Sleep(500 * time.Millisecond) 183 184 gtest.C(t, func(t *gtest.T) { 185 path := fmt.Sprintf(`%s/%d/config`, gfile.Temp(), gtime.TimestampNano()) 186 file := fmt.Sprintf(`%s/%s`, path, "test.toml") 187 err := gfile.PutContents(file, configContent) 188 t.AssertNil(err) 189 defer gfile.Remove(file) 190 defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() 191 gins.Config("test").GetAdapter().(*gcfg.AdapterFile).SetFileName("test.toml") 192 t.Assert(gins.Config("test").GetAdapter().(*gcfg.AdapterFile).AddPath(path), nil) 193 t.Assert(gins.Config("test").MustGet(ctx, "test"), "v=1") 194 t.Assert(gins.Config("test").MustGet(ctx, "database.default.1.host"), "127.0.0.1") 195 t.Assert(gins.Config("test").MustGet(ctx, "redis.disk"), `{"address":"127.0.0.1:6379","db":1}`) 196 }) 197 } 198 199 func Test_Basic2(t *testing.T) { 200 config := `log-path = "logs"` 201 gtest.C(t, func(t *gtest.T) { 202 var ( 203 path = gcfg.DefaultConfigFileName 204 err = gfile.PutContents(path, config) 205 ) 206 t.AssertNil(err) 207 defer func() { 208 _ = gfile.Remove(path) 209 }() 210 211 t.Assert(gins.Config().MustGet(ctx, "log-path"), "logs") 212 }) 213 }