github.com/wangyougui/gf/v2@v2.6.5/os/gcfg/gcfg_z_unit_basic_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/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcfg_test
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/wangyougui/gf/v2/os/gcfg"
    15  	"github.com/wangyougui/gf/v2/os/gcmd"
    16  	"github.com/wangyougui/gf/v2/os/genv"
    17  	"github.com/wangyougui/gf/v2/os/gfile"
    18  	"github.com/wangyougui/gf/v2/os/gtime"
    19  	"github.com/wangyougui/gf/v2/test/gtest"
    20  )
    21  
    22  func Test_Basic1(t *testing.T) {
    23  	config := `
    24  v1    = 1
    25  v2    = "true"
    26  v3    = "off"
    27  v4    = "1.23"
    28  array = [1,2,3]
    29  [redis]
    30      disk  = "127.0.0.1:6379,0"
    31      cache = "127.0.0.1:6379,1"
    32  `
    33  	gtest.C(t, func(t *gtest.T) {
    34  		var (
    35  			path = gcfg.DefaultConfigFileName
    36  			err  = gfile.PutContents(path, config)
    37  		)
    38  		t.AssertNil(err)
    39  		defer gfile.Remove(path)
    40  
    41  		c, err := gcfg.New()
    42  		t.AssertNil(err)
    43  		t.Assert(c.MustGet(ctx, "v1"), 1)
    44  		filepath, _ := c.GetAdapter().(*gcfg.AdapterFile).GetFilePath()
    45  		t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
    46  	})
    47  }
    48  
    49  func Test_Basic2(t *testing.T) {
    50  	config := `log-path = "logs"`
    51  	gtest.C(t, func(t *gtest.T) {
    52  		var (
    53  			path = gcfg.DefaultConfigFileName
    54  			err  = gfile.PutContents(path, config)
    55  		)
    56  		t.AssertNil(err)
    57  		defer func() {
    58  			_ = gfile.Remove(path)
    59  		}()
    60  
    61  		c, err := gcfg.New()
    62  		t.AssertNil(err)
    63  		t.Assert(c.MustGet(ctx, "log-path"), "logs")
    64  	})
    65  }
    66  
    67  func Test_Content(t *testing.T) {
    68  	content := `
    69  v1    = 1
    70  v2    = "true"
    71  v3    = "off"
    72  v4    = "1.23"
    73  array = [1,2,3]
    74  [redis]
    75      disk  = "127.0.0.1:6379,0"
    76      cache = "127.0.0.1:6379,1"
    77  `
    78  	gtest.C(t, func(t *gtest.T) {
    79  		c, err := gcfg.New()
    80  		t.AssertNil(err)
    81  		c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
    82  		defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
    83  		t.Assert(c.MustGet(ctx, "v1"), 1)
    84  	})
    85  }
    86  
    87  func Test_SetFileName(t *testing.T) {
    88  	config := `
    89  {
    90  	"array": [
    91  		1,
    92  		2,
    93  		3
    94  	],
    95  	"redis": {
    96  		"cache": "127.0.0.1:6379,1",
    97  		"disk": "127.0.0.1:6379,0"
    98  	},
    99  	"v1": 1,
   100  	"v2": "true",
   101  	"v3": "off",
   102  	"v4": "1.234"
   103  }
   104  `
   105  	gtest.C(t, func(t *gtest.T) {
   106  		path := "config.json"
   107  		err := gfile.PutContents(path, config)
   108  		t.AssertNil(err)
   109  		defer func() {
   110  			_ = gfile.Remove(path)
   111  		}()
   112  
   113  		config, err := gcfg.New()
   114  		t.AssertNil(err)
   115  		c := config.GetAdapter().(*gcfg.AdapterFile)
   116  		c.SetFileName(path)
   117  		t.Assert(c.MustGet(ctx, "v1"), 1)
   118  		t.AssertEQ(c.MustGet(ctx, "v1").Int(), 1)
   119  		t.AssertEQ(c.MustGet(ctx, "v1").Int8(), int8(1))
   120  		t.AssertEQ(c.MustGet(ctx, "v1").Int16(), int16(1))
   121  		t.AssertEQ(c.MustGet(ctx, "v1").Int32(), int32(1))
   122  		t.AssertEQ(c.MustGet(ctx, "v1").Int64(), int64(1))
   123  		t.AssertEQ(c.MustGet(ctx, "v1").Uint(), uint(1))
   124  		t.AssertEQ(c.MustGet(ctx, "v1").Uint8(), uint8(1))
   125  		t.AssertEQ(c.MustGet(ctx, "v1").Uint16(), uint16(1))
   126  		t.AssertEQ(c.MustGet(ctx, "v1").Uint32(), uint32(1))
   127  		t.AssertEQ(c.MustGet(ctx, "v1").Uint64(), uint64(1))
   128  
   129  		t.AssertEQ(c.MustGet(ctx, "v1").String(), "1")
   130  		t.AssertEQ(c.MustGet(ctx, "v1").Bool(), true)
   131  		t.AssertEQ(c.MustGet(ctx, "v2").String(), "true")
   132  		t.AssertEQ(c.MustGet(ctx, "v2").Bool(), true)
   133  
   134  		t.AssertEQ(c.MustGet(ctx, "v1").String(), "1")
   135  		t.AssertEQ(c.MustGet(ctx, "v4").Float32(), float32(1.234))
   136  		t.AssertEQ(c.MustGet(ctx, "v4").Float64(), float64(1.234))
   137  		t.AssertEQ(c.MustGet(ctx, "v2").String(), "true")
   138  		t.AssertEQ(c.MustGet(ctx, "v2").Bool(), true)
   139  		t.AssertEQ(c.MustGet(ctx, "v3").Bool(), false)
   140  
   141  		t.AssertEQ(c.MustGet(ctx, "array").Ints(), []int{1, 2, 3})
   142  		t.AssertEQ(c.MustGet(ctx, "array").Strings(), []string{"1", "2", "3"})
   143  		t.AssertEQ(c.MustGet(ctx, "array").Interfaces(), []interface{}{1, 2, 3})
   144  		t.AssertEQ(c.MustGet(ctx, "redis").Map(), map[string]interface{}{
   145  			"disk":  "127.0.0.1:6379,0",
   146  			"cache": "127.0.0.1:6379,1",
   147  		})
   148  		filepath, _ := c.GetFilePath()
   149  		t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
   150  	})
   151  }
   152  
   153  func TestCfg_Get_WrongConfigFile(t *testing.T) {
   154  	gtest.C(t, func(t *gtest.T) {
   155  		var err error
   156  		configPath := gfile.Temp(gtime.TimestampNanoStr())
   157  		err = gfile.Mkdir(configPath)
   158  		t.AssertNil(err)
   159  		defer gfile.Remove(configPath)
   160  
   161  		defer gfile.Chdir(gfile.Pwd())
   162  		err = gfile.Chdir(configPath)
   163  		t.AssertNil(err)
   164  
   165  		err = gfile.PutContents(
   166  			gfile.Join(configPath, "config.yml"),
   167  			"wrong config",
   168  		)
   169  		t.AssertNil(err)
   170  		adapterFile, err := gcfg.NewAdapterFile("config.yml")
   171  		t.AssertNil(err)
   172  
   173  		c := gcfg.NewWithAdapter(adapterFile)
   174  		v, err := c.Get(ctx, "name")
   175  		t.AssertNE(err, nil)
   176  		t.Assert(v, nil)
   177  		adapterFile.Clear()
   178  	})
   179  }
   180  
   181  func Test_GetWithEnv(t *testing.T) {
   182  	content := `
   183  v1    = 1
   184  v2    = "true"
   185  v3    = "off"
   186  v4    = "1.23"
   187  array = [1,2,3]
   188  [redis]
   189      disk  = "127.0.0.1:6379,0"
   190      cache = "127.0.0.1:6379,1"
   191  `
   192  	gtest.C(t, func(t *gtest.T) {
   193  		c, err := gcfg.New()
   194  		t.AssertNil(err)
   195  		c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
   196  		defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
   197  		t.Assert(c.MustGet(ctx, "v1"), 1)
   198  		t.Assert(c.MustGetWithEnv(ctx, `redis.user`), nil)
   199  		t.Assert(genv.Set("REDIS_USER", `1`), nil)
   200  		defer genv.Remove(`REDIS_USER`)
   201  		t.Assert(c.MustGetWithEnv(ctx, `redis.user`), `1`)
   202  	})
   203  }
   204  
   205  func Test_GetWithCmd(t *testing.T) {
   206  	content := `
   207  v1    = 1
   208  v2    = "true"
   209  v3    = "off"
   210  v4    = "1.23"
   211  array = [1,2,3]
   212  [redis]
   213      disk  = "127.0.0.1:6379,0"
   214      cache = "127.0.0.1:6379,1"
   215  `
   216  	gtest.C(t, func(t *gtest.T) {
   217  
   218  		c, err := gcfg.New()
   219  		t.AssertNil(err)
   220  		c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
   221  		defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
   222  		t.Assert(c.MustGet(ctx, "v1"), 1)
   223  		t.Assert(c.MustGetWithCmd(ctx, `redis.user`), nil)
   224  
   225  		gcmd.Init([]string{"gf", "--redis.user=2"}...)
   226  		t.Assert(c.MustGetWithCmd(ctx, `redis.user`), `2`)
   227  	})
   228  }