gf/os/gcfg/gcfg_z_unit_basic_test.go

229 lines
5.6 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-03-19 13:58:18 +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.
// go test *.go -bench=".*" -benchmem
package gcfg_test
import (
2019-06-21 22:23:07 +08:00
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gcfg"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/os/gtime"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2019-03-19 13:58:18 +08:00
)
func Test_Basic1(t *testing.T) {
config := `
2019-03-19 13:58:18 +08:00
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2022-03-01 21:14:45 +08:00
var (
path = gcfg.DefaultConfigFileName
err = gfile.PutContents(path, config)
)
t.AssertNil(err)
defer gfile.Remove(path)
c, err := gcfg.New()
t.AssertNil(err)
t.Assert(c.MustGet(ctx, "v1"), 1)
filepath, _ := c.GetAdapter().(*gcfg.AdapterFile).GetFilePath()
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
})
2019-03-19 13:58:18 +08:00
}
func Test_Basic2(t *testing.T) {
config := `log-path = "logs"`
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2022-03-01 21:14:45 +08:00
var (
path = gcfg.DefaultConfigFileName
err = gfile.PutContents(path, config)
)
t.AssertNil(err)
defer func() {
_ = gfile.Remove(path)
}()
c, err := gcfg.New()
t.AssertNil(err)
t.Assert(c.MustGet(ctx, "log-path"), "logs")
})
}
func Test_Content(t *testing.T) {
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.New()
t.AssertNil(err)
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
t.Assert(c.MustGet(ctx, "v1"), 1)
})
2019-04-03 09:59:15 +08:00
}
func Test_SetFileName(t *testing.T) {
config := `
2019-04-03 09:59:15 +08:00
{
"array": [
1,
2,
3
],
"redis": {
"cache": "127.0.0.1:6379,1",
"disk": "127.0.0.1:6379,0"
},
"v1": 1,
"v2": "true",
"v3": "off",
"v4": "1.234"
}
`
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path := "config.json"
err := gfile.PutContents(path, config)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-06-21 22:23:07 +08:00
defer func() {
_ = gfile.Remove(path)
}()
config, err := gcfg.New()
t.AssertNil(err)
c := config.GetAdapter().(*gcfg.AdapterFile)
c.SetFileName(path)
t.Assert(c.MustGet(ctx, "v1"), 1)
t.AssertEQ(c.MustGet(ctx, "v1").Int(), 1)
t.AssertEQ(c.MustGet(ctx, "v1").Int8(), int8(1))
t.AssertEQ(c.MustGet(ctx, "v1").Int16(), int16(1))
t.AssertEQ(c.MustGet(ctx, "v1").Int32(), int32(1))
t.AssertEQ(c.MustGet(ctx, "v1").Int64(), int64(1))
t.AssertEQ(c.MustGet(ctx, "v1").Uint(), uint(1))
t.AssertEQ(c.MustGet(ctx, "v1").Uint8(), uint8(1))
t.AssertEQ(c.MustGet(ctx, "v1").Uint16(), uint16(1))
t.AssertEQ(c.MustGet(ctx, "v1").Uint32(), uint32(1))
t.AssertEQ(c.MustGet(ctx, "v1").Uint64(), uint64(1))
t.AssertEQ(c.MustGet(ctx, "v1").String(), "1")
t.AssertEQ(c.MustGet(ctx, "v1").Bool(), true)
t.AssertEQ(c.MustGet(ctx, "v2").String(), "true")
t.AssertEQ(c.MustGet(ctx, "v2").Bool(), true)
t.AssertEQ(c.MustGet(ctx, "v1").String(), "1")
t.AssertEQ(c.MustGet(ctx, "v4").Float32(), float32(1.234))
t.AssertEQ(c.MustGet(ctx, "v4").Float64(), float64(1.234))
t.AssertEQ(c.MustGet(ctx, "v2").String(), "true")
t.AssertEQ(c.MustGet(ctx, "v2").Bool(), true)
t.AssertEQ(c.MustGet(ctx, "v3").Bool(), false)
t.AssertEQ(c.MustGet(ctx, "array").Ints(), []int{1, 2, 3})
t.AssertEQ(c.MustGet(ctx, "array").Strings(), []string{"1", "2", "3"})
t.AssertEQ(c.MustGet(ctx, "array").Interfaces(), []interface{}{1, 2, 3})
t.AssertEQ(c.MustGet(ctx, "redis").Map(), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
filepath, _ := c.GetFilePath()
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
})
2019-04-03 09:59:15 +08:00
}
func TestCfg_Get_WrongConfigFile(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var err error
configPath := gfile.Temp(gtime.TimestampNanoStr())
err = gfile.Mkdir(configPath)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
defer gfile.Remove(configPath)
defer gfile.Chdir(gfile.Pwd())
err = gfile.Chdir(configPath)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
err = gfile.PutContents(
gfile.Join(configPath, "config.yml"),
"wrong config",
)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
adapterFile, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)
c := gcfg.NewWithAdapter(adapterFile)
v, err := c.Get(ctx, "name")
t.AssertNE(err, nil)
t.Assert(v, nil)
adapterFile.Clear()
2020-07-25 10:57:40 +08:00
})
}
func Test_GetWithEnv(t *testing.T) {
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.New()
t.AssertNil(err)
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
t.Assert(c.MustGet(ctx, "v1"), 1)
t.Assert(c.MustGetWithEnv(ctx, `redis.user`), nil)
t.Assert(genv.Set("REDIS_USER", `1`), nil)
defer genv.Remove(`REDIS_USER`)
t.Assert(c.MustGetWithEnv(ctx, `redis.user`), `1`)
})
}
func Test_GetWithCmd(t *testing.T) {
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.New()
t.AssertNil(err)
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
t.Assert(c.MustGet(ctx, "v1"), 1)
t.Assert(c.MustGetWithCmd(ctx, `redis.user`), nil)
gcmd.Init([]string{"gf", "--redis.user=2"}...)
t.Assert(c.MustGetWithCmd(ctx, `redis.user`), `2`)
})
}