github.com/gogf/gf/v2@v2.7.4/os/gcfg/gcfg_z_unit_instance_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 // go test *.go -bench=".*" -benchmem 8 9 package gcfg 10 11 import ( 12 "context" 13 "testing" 14 15 "github.com/gogf/gf/v2/container/gmap" 16 "github.com/gogf/gf/v2/os/genv" 17 "github.com/gogf/gf/v2/os/gfile" 18 "github.com/gogf/gf/v2/test/gtest" 19 ) 20 21 var ( 22 ctx = context.TODO() 23 ) 24 25 func Test_Instance_Basic(t *testing.T) { 26 config := ` 27 array = [1.0, 2.0, 3.0] 28 v1 = 1.0 29 v2 = "true" 30 v3 = "off" 31 v4 = "1.234" 32 33 [redis] 34 cache = "127.0.0.1:6379,1" 35 disk = "127.0.0.1:6379,0" 36 37 ` 38 gtest.C(t, func(t *gtest.T) { 39 var ( 40 path = DefaultConfigFileName 41 err = gfile.PutContents(path, config) 42 ) 43 t.AssertNil(err) 44 defer func() { 45 t.AssertNil(gfile.Remove(path)) 46 }() 47 48 c := Instance() 49 t.Assert(c.MustGet(ctx, "v1"), 1) 50 filepath, _ := c.GetAdapter().(*AdapterFile).GetFilePath() 51 t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path) 52 }) 53 } 54 55 func Test_Instance_AutoLocateConfigFile(t *testing.T) { 56 gtest.C(t, func(t *gtest.T) { 57 t.Assert(Instance("gf") != nil, true) 58 }) 59 // Automatically locate the configuration file with supported file extensions. 60 gtest.C(t, func(t *gtest.T) { 61 pwd := gfile.Pwd() 62 t.AssertNil(gfile.Chdir(gtest.DataPath())) 63 defer gfile.Chdir(pwd) 64 t.Assert(Instance("c1") != nil, true) 65 t.Assert(Instance("c1").MustGet(ctx, "my-config"), "1") 66 t.Assert(Instance("folder1/c1").MustGet(ctx, "my-config"), "2") 67 }) 68 // Automatically locate the configuration file with supported file extensions. 69 gtest.C(t, func(t *gtest.T) { 70 pwd := gfile.Pwd() 71 t.AssertNil(gfile.Chdir(gtest.DataPath("folder1"))) 72 defer gfile.Chdir(pwd) 73 t.Assert(Instance("c2").MustGet(ctx, "my-config"), 2) 74 }) 75 // Default configuration file. 76 gtest.C(t, func(t *gtest.T) { 77 localInstances.Clear() 78 pwd := gfile.Pwd() 79 t.AssertNil(gfile.Chdir(gtest.DataPath("default"))) 80 defer gfile.Chdir(pwd) 81 t.Assert(Instance().MustGet(ctx, "my-config"), 1) 82 83 localInstances.Clear() 84 t.AssertNil(genv.Set("GF_GCFG_FILE", "config.json")) 85 defer genv.Set("GF_GCFG_FILE", "") 86 t.Assert(Instance().MustGet(ctx, "my-config"), 2) 87 }) 88 } 89 90 func Test_Instance_EnvPath(t *testing.T) { 91 gtest.C(t, func(t *gtest.T) { 92 genv.Set("GF_GCFG_PATH", gtest.DataPath("envpath")) 93 defer genv.Set("GF_GCFG_PATH", "") 94 t.Assert(Instance("c3") != nil, true) 95 t.Assert(Instance("c3").MustGet(ctx, "my-config"), "3") 96 t.Assert(Instance("c4").MustGet(ctx, "my-config"), "4") 97 localInstances = gmap.NewStrAnyMap(true) 98 }) 99 } 100 101 func Test_Instance_EnvFile(t *testing.T) { 102 gtest.C(t, func(t *gtest.T) { 103 genv.Set("GF_GCFG_PATH", gtest.DataPath("envfile")) 104 defer genv.Set("GF_GCFG_PATH", "") 105 genv.Set("GF_GCFG_FILE", "c6.json") 106 defer genv.Set("GF_GCFG_FILE", "") 107 t.Assert(Instance().MustGet(ctx, "my-config"), "6") 108 localInstances = gmap.NewStrAnyMap(true) 109 }) 110 }