2021-01-07 01:17:03 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*" -benchmem
|
|
|
|
|
|
|
|
package gcfg
|
|
|
|
|
|
|
|
import (
|
2021-09-19 10:01:09 +08:00
|
|
|
"context"
|
2021-11-15 20:26:31 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
|
|
|
"github.com/gogf/gf/v2/os/genv"
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-01-07 01:17:03 +08:00
|
|
|
)
|
|
|
|
|
2021-09-19 10:01:09 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
)
|
|
|
|
|
2021-01-07 01:17:03 +08:00
|
|
|
func Test_Instance_Basic(t *testing.T) {
|
|
|
|
config := `
|
|
|
|
array = [1.0, 2.0, 3.0]
|
|
|
|
v1 = 1.0
|
|
|
|
v2 = "true"
|
|
|
|
v3 = "off"
|
|
|
|
v4 = "1.234"
|
|
|
|
|
|
|
|
[redis]
|
|
|
|
cache = "127.0.0.1:6379,1"
|
|
|
|
disk = "127.0.0.1:6379,0"
|
|
|
|
|
|
|
|
`
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
path := DefaultConfigFile
|
|
|
|
err := gfile.PutContents(path, config)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
defer func() {
|
|
|
|
t.Assert(gfile.Remove(path), nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := Instance()
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(c.MustGet(ctx, "v1"), 1)
|
|
|
|
filepath, _ := c.GetAdapter().(*AdapterFile).GetFilePath()
|
2021-03-16 14:39:01 +08:00
|
|
|
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
|
2021-01-07 01:17:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Instance_AutoLocateConfigFile(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(Instance("gf") != nil, true)
|
|
|
|
})
|
|
|
|
// Automatically locate the configuration file with supported file extensions.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
pwd := gfile.Pwd()
|
2021-02-26 13:57:47 +08:00
|
|
|
t.AssertNil(gfile.Chdir(gdebug.TestDataPath()))
|
2021-01-07 01:17:03 +08:00
|
|
|
defer gfile.Chdir(pwd)
|
|
|
|
t.Assert(Instance("c1") != nil, true)
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance("c1").MustGet(ctx, "my-config"), "1")
|
|
|
|
t.Assert(Instance("folder1/c1").MustGet(ctx, "my-config"), "2")
|
2021-01-07 01:17:03 +08:00
|
|
|
})
|
|
|
|
// Automatically locate the configuration file with supported file extensions.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
pwd := gfile.Pwd()
|
2021-02-26 13:57:47 +08:00
|
|
|
t.AssertNil(gfile.Chdir(gdebug.TestDataPath("folder1")))
|
2021-01-07 01:17:03 +08:00
|
|
|
defer gfile.Chdir(pwd)
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance("c2").MustGet(ctx, "my-config"), 2)
|
2021-01-07 01:17:03 +08:00
|
|
|
})
|
2021-02-26 13:57:47 +08:00
|
|
|
// Default configuration file.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-19 10:01:09 +08:00
|
|
|
localInstances.Clear()
|
2021-02-26 13:57:47 +08:00
|
|
|
pwd := gfile.Pwd()
|
|
|
|
t.AssertNil(gfile.Chdir(gdebug.TestDataPath("default")))
|
|
|
|
defer gfile.Chdir(pwd)
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance().MustGet(ctx, "my-config"), 1)
|
2021-02-26 13:57:47 +08:00
|
|
|
|
2021-09-19 10:01:09 +08:00
|
|
|
localInstances.Clear()
|
2021-02-26 13:57:47 +08:00
|
|
|
t.AssertNil(genv.Set("GF_GCFG_FILE", "config.json"))
|
|
|
|
defer genv.Set("GF_GCFG_FILE", "")
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance().MustGet(ctx, "my-config"), 2)
|
2021-02-26 13:57:47 +08:00
|
|
|
})
|
2021-01-07 01:17:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Instance_EnvPath(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envpath"))
|
|
|
|
defer genv.Set("GF_GCFG_PATH", "")
|
|
|
|
t.Assert(Instance("c3") != nil, true)
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance("c3").MustGet(ctx, "my-config"), "3")
|
|
|
|
t.Assert(Instance("c4").MustGet(ctx, "my-config"), "4")
|
|
|
|
localInstances = gmap.NewStrAnyMap(true)
|
2021-01-07 01:17:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Instance_EnvFile(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envfile"))
|
|
|
|
defer genv.Set("GF_GCFG_PATH", "")
|
|
|
|
genv.Set("GF_GCFG_FILE", "c6.json")
|
|
|
|
defer genv.Set("GF_GCFG_FILE", "")
|
2021-09-19 10:01:09 +08:00
|
|
|
t.Assert(Instance().MustGet(ctx, "my-config"), "6")
|
|
|
|
localInstances = gmap.NewStrAnyMap(true)
|
2021-01-07 01:17:03 +08:00
|
|
|
})
|
|
|
|
}
|