github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcfg_test
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/os/gtime"
    16  
    17  	"github.com/gogf/gf/encoding/gjson"
    18  	"github.com/gogf/gf/frame/g"
    19  	"github.com/gogf/gf/os/gcfg"
    20  	"github.com/gogf/gf/os/gfile"
    21  	"github.com/gogf/gf/test/gtest"
    22  )
    23  
    24  func init() {
    25  	os.Setenv("GF_GCFG_ERRORPRINT", "false")
    26  }
    27  
    28  func Test_Basic1(t *testing.T) {
    29  	config := `
    30  v1    = 1
    31  v2    = "true"
    32  v3    = "off"
    33  v4    = "1.23"
    34  array = [1,2,3]
    35  [redis]
    36      disk  = "127.0.0.1:6379,0"
    37      cache = "127.0.0.1:6379,1"
    38  `
    39  	gtest.C(t, func(t *gtest.T) {
    40  		path := gcfg.DefaultConfigFile
    41  		err := gfile.PutContents(path, config)
    42  		t.Assert(err, nil)
    43  		defer gfile.Remove(path)
    44  
    45  		c := gcfg.New()
    46  		t.Assert(c.Get("v1"), 1)
    47  		t.AssertEQ(c.GetInt("v1"), 1)
    48  		t.AssertEQ(c.GetInt8("v1"), int8(1))
    49  		t.AssertEQ(c.GetInt16("v1"), int16(1))
    50  		t.AssertEQ(c.GetInt32("v1"), int32(1))
    51  		t.AssertEQ(c.GetInt64("v1"), int64(1))
    52  		t.AssertEQ(c.GetUint("v1"), uint(1))
    53  		t.AssertEQ(c.GetUint8("v1"), uint8(1))
    54  		t.AssertEQ(c.GetUint16("v1"), uint16(1))
    55  		t.AssertEQ(c.GetUint32("v1"), uint32(1))
    56  		t.AssertEQ(c.GetUint64("v1"), uint64(1))
    57  
    58  		t.AssertEQ(c.GetVar("v1").String(), "1")
    59  		t.AssertEQ(c.GetVar("v1").Bool(), true)
    60  		t.AssertEQ(c.GetVar("v2").String(), "true")
    61  		t.AssertEQ(c.GetVar("v2").Bool(), true)
    62  
    63  		t.AssertEQ(c.GetString("v1"), "1")
    64  		t.AssertEQ(c.GetFloat32("v4"), float32(1.23))
    65  		t.AssertEQ(c.GetFloat64("v4"), float64(1.23))
    66  		t.AssertEQ(c.GetString("v2"), "true")
    67  		t.AssertEQ(c.GetBool("v2"), true)
    68  		t.AssertEQ(c.GetBool("v3"), false)
    69  
    70  		t.AssertEQ(c.Contains("v1"), true)
    71  		t.AssertEQ(c.Contains("v2"), true)
    72  		t.AssertEQ(c.Contains("v3"), true)
    73  		t.AssertEQ(c.Contains("v4"), true)
    74  		t.AssertEQ(c.Contains("v5"), false)
    75  
    76  		t.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
    77  		t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
    78  		t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3})
    79  		t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3})
    80  		t.AssertEQ(c.GetMap("redis"), map[string]interface{}{
    81  			"disk":  "127.0.0.1:6379,0",
    82  			"cache": "127.0.0.1:6379,1",
    83  		})
    84  		filepath, _ := c.GetFilePath()
    85  		t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
    86  	})
    87  }
    88  
    89  func Test_Basic2(t *testing.T) {
    90  	config := `log-path = "logs"`
    91  	gtest.C(t, func(t *gtest.T) {
    92  		path := gcfg.DefaultConfigFile
    93  		err := gfile.PutContents(path, config)
    94  		t.Assert(err, nil)
    95  		defer func() {
    96  			_ = gfile.Remove(path)
    97  		}()
    98  
    99  		c := gcfg.New()
   100  		t.Assert(c.Get("log-path"), "logs")
   101  	})
   102  }
   103  
   104  func Test_Content(t *testing.T) {
   105  	content := `
   106  v1    = 1
   107  v2    = "true"
   108  v3    = "off"
   109  v4    = "1.23"
   110  array = [1,2,3]
   111  [redis]
   112      disk  = "127.0.0.1:6379,0"
   113      cache = "127.0.0.1:6379,1"
   114  `
   115  	gcfg.SetContent(content)
   116  	defer gcfg.ClearContent()
   117  
   118  	gtest.C(t, func(t *gtest.T) {
   119  		c := gcfg.New()
   120  		t.Assert(c.Get("v1"), 1)
   121  		t.AssertEQ(c.GetInt("v1"), 1)
   122  		t.AssertEQ(c.GetInt8("v1"), int8(1))
   123  		t.AssertEQ(c.GetInt16("v1"), int16(1))
   124  		t.AssertEQ(c.GetInt32("v1"), int32(1))
   125  		t.AssertEQ(c.GetInt64("v1"), int64(1))
   126  		t.AssertEQ(c.GetUint("v1"), uint(1))
   127  		t.AssertEQ(c.GetUint8("v1"), uint8(1))
   128  		t.AssertEQ(c.GetUint16("v1"), uint16(1))
   129  		t.AssertEQ(c.GetUint32("v1"), uint32(1))
   130  		t.AssertEQ(c.GetUint64("v1"), uint64(1))
   131  
   132  		t.AssertEQ(c.GetVar("v1").String(), "1")
   133  		t.AssertEQ(c.GetVar("v1").Bool(), true)
   134  		t.AssertEQ(c.GetVar("v2").String(), "true")
   135  		t.AssertEQ(c.GetVar("v2").Bool(), true)
   136  
   137  		t.AssertEQ(c.GetString("v1"), "1")
   138  		t.AssertEQ(c.GetFloat32("v4"), float32(1.23))
   139  		t.AssertEQ(c.GetFloat64("v4"), float64(1.23))
   140  		t.AssertEQ(c.GetString("v2"), "true")
   141  		t.AssertEQ(c.GetBool("v2"), true)
   142  		t.AssertEQ(c.GetBool("v3"), false)
   143  
   144  		t.AssertEQ(c.Contains("v1"), true)
   145  		t.AssertEQ(c.Contains("v2"), true)
   146  		t.AssertEQ(c.Contains("v3"), true)
   147  		t.AssertEQ(c.Contains("v4"), true)
   148  		t.AssertEQ(c.Contains("v5"), false)
   149  
   150  		t.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
   151  		t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
   152  		t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3})
   153  		t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3})
   154  		t.AssertEQ(c.GetMap("redis"), map[string]interface{}{
   155  			"disk":  "127.0.0.1:6379,0",
   156  			"cache": "127.0.0.1:6379,1",
   157  		})
   158  	})
   159  }
   160  
   161  func Test_SetFileName(t *testing.T) {
   162  	config := `
   163  {
   164  	"array": [
   165  		1,
   166  		2,
   167  		3
   168  	],
   169  	"redis": {
   170  		"cache": "127.0.0.1:6379,1",
   171  		"disk": "127.0.0.1:6379,0"
   172  	},
   173  	"v1": 1,
   174  	"v2": "true",
   175  	"v3": "off",
   176  	"v4": "1.234"
   177  }
   178  `
   179  	gtest.C(t, func(t *gtest.T) {
   180  		path := "config.json"
   181  		err := gfile.PutContents(path, config)
   182  		t.Assert(err, nil)
   183  		defer func() {
   184  			_ = gfile.Remove(path)
   185  		}()
   186  
   187  		c := gcfg.New()
   188  		c.SetFileName(path)
   189  		t.Assert(c.Get("v1"), 1)
   190  		t.AssertEQ(c.GetInt("v1"), 1)
   191  		t.AssertEQ(c.GetInt8("v1"), int8(1))
   192  		t.AssertEQ(c.GetInt16("v1"), int16(1))
   193  		t.AssertEQ(c.GetInt32("v1"), int32(1))
   194  		t.AssertEQ(c.GetInt64("v1"), int64(1))
   195  		t.AssertEQ(c.GetUint("v1"), uint(1))
   196  		t.AssertEQ(c.GetUint8("v1"), uint8(1))
   197  		t.AssertEQ(c.GetUint16("v1"), uint16(1))
   198  		t.AssertEQ(c.GetUint32("v1"), uint32(1))
   199  		t.AssertEQ(c.GetUint64("v1"), uint64(1))
   200  
   201  		t.AssertEQ(c.GetVar("v1").String(), "1")
   202  		t.AssertEQ(c.GetVar("v1").Bool(), true)
   203  		t.AssertEQ(c.GetVar("v2").String(), "true")
   204  		t.AssertEQ(c.GetVar("v2").Bool(), true)
   205  
   206  		t.AssertEQ(c.GetString("v1"), "1")
   207  		t.AssertEQ(c.GetFloat32("v4"), float32(1.234))
   208  		t.AssertEQ(c.GetFloat64("v4"), float64(1.234))
   209  		t.AssertEQ(c.GetString("v2"), "true")
   210  		t.AssertEQ(c.GetBool("v2"), true)
   211  		t.AssertEQ(c.GetBool("v3"), false)
   212  
   213  		t.AssertEQ(c.Contains("v1"), true)
   214  		t.AssertEQ(c.Contains("v2"), true)
   215  		t.AssertEQ(c.Contains("v3"), true)
   216  		t.AssertEQ(c.Contains("v4"), true)
   217  		t.AssertEQ(c.Contains("v5"), false)
   218  
   219  		t.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
   220  		t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
   221  		t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3})
   222  		t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3})
   223  		t.AssertEQ(c.GetMap("redis"), map[string]interface{}{
   224  			"disk":  "127.0.0.1:6379,0",
   225  			"cache": "127.0.0.1:6379,1",
   226  		})
   227  		filepath, _ := c.GetFilePath()
   228  		t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
   229  	})
   230  }
   231  
   232  func TestCfg_New(t *testing.T) {
   233  	gtest.C(t, func(t *gtest.T) {
   234  		os.Setenv("GF_GCFG_PATH", "config")
   235  		c := gcfg.New("config.yml")
   236  		t.Assert(c.Get("name"), nil)
   237  		t.Assert(c.GetFileName(), "config.yml")
   238  
   239  		configPath := gfile.Pwd() + gfile.Separator + "config"
   240  		_ = gfile.Mkdir(configPath)
   241  		defer gfile.Remove(configPath)
   242  
   243  		c = gcfg.New("config.yml")
   244  		t.Assert(c.Get("name"), nil)
   245  
   246  		_ = os.Unsetenv("GF_GCFG_PATH")
   247  		c = gcfg.New("config.yml")
   248  		t.Assert(c.Get("name"), nil)
   249  	})
   250  }
   251  
   252  func TestCfg_SetPath(t *testing.T) {
   253  	gtest.C(t, func(t *gtest.T) {
   254  		c := gcfg.New("config.yml")
   255  		err := c.SetPath("tmp")
   256  		t.AssertNE(err, nil)
   257  		err = c.SetPath("gcfg.go")
   258  		t.AssertNE(err, nil)
   259  		t.Assert(c.Get("name"), nil)
   260  	})
   261  }
   262  
   263  func TestCfg_SetViolenceCheck(t *testing.T) {
   264  	gtest.C(t, func(t *gtest.T) {
   265  		c := gcfg.New("config.yml")
   266  		c.SetViolenceCheck(true)
   267  		t.Assert(c.Get("name"), nil)
   268  	})
   269  }
   270  
   271  func TestCfg_AddPath(t *testing.T) {
   272  	gtest.C(t, func(t *gtest.T) {
   273  		c := gcfg.New("config.yml")
   274  		err := c.AddPath("tmp")
   275  		t.AssertNE(err, nil)
   276  		err = c.AddPath("gcfg.go")
   277  		t.AssertNE(err, nil)
   278  		t.Assert(c.Get("name"), nil)
   279  	})
   280  }
   281  
   282  func TestCfg_FilePath(t *testing.T) {
   283  	gtest.C(t, func(t *gtest.T) {
   284  		c := gcfg.New("config.yml")
   285  		path, _ := c.GetFilePath("tmp")
   286  		t.Assert(path, "")
   287  		path, _ = c.GetFilePath("tmp")
   288  		t.Assert(path, "")
   289  	})
   290  }
   291  
   292  func TestCfg_et(t *testing.T) {
   293  	config := `log-path = "logs"`
   294  	gtest.C(t, func(t *gtest.T) {
   295  		path := gcfg.DefaultConfigFile
   296  		err := gfile.PutContents(path, config)
   297  		t.Assert(err, nil)
   298  		defer gfile.Remove(path)
   299  
   300  		c := gcfg.New()
   301  		t.Assert(c.Get("log-path"), "logs")
   302  
   303  		err = c.Set("log-path", "custom-logs")
   304  		t.Assert(err, nil)
   305  		t.Assert(c.Get("log-path"), "custom-logs")
   306  	})
   307  }
   308  
   309  func TestCfg_Get(t *testing.T) {
   310  	gtest.C(t, func(t *gtest.T) {
   311  		var err error
   312  		configPath := gfile.TempDir(gtime.TimestampNanoStr())
   313  		err = gfile.Mkdir(configPath)
   314  		t.Assert(err, nil)
   315  		defer gfile.Remove(configPath)
   316  
   317  		defer gfile.Chdir(gfile.Pwd())
   318  		err = gfile.Chdir(configPath)
   319  		t.Assert(err, nil)
   320  
   321  		err = gfile.PutContents(
   322  			gfile.Join(configPath, "config.yml"),
   323  			"wrong config",
   324  		)
   325  		t.Assert(err, nil)
   326  		c := gcfg.New("config.yml")
   327  		t.Assert(c.Get("name"), nil)
   328  		t.Assert(c.GetVar("name").Val(), nil)
   329  		t.Assert(c.Contains("name"), false)
   330  		t.Assert(c.GetMap("name"), nil)
   331  		t.Assert(c.GetArray("name"), nil)
   332  		t.Assert(c.GetString("name"), "")
   333  		t.Assert(c.GetStrings("name"), nil)
   334  		t.Assert(c.GetInterfaces("name"), nil)
   335  		t.Assert(c.GetBool("name"), false)
   336  		t.Assert(c.GetFloat32("name"), 0)
   337  		t.Assert(c.GetFloat64("name"), 0)
   338  		t.Assert(c.GetFloats("name"), nil)
   339  		t.Assert(c.GetInt("name"), 0)
   340  		t.Assert(c.GetInt8("name"), 0)
   341  		t.Assert(c.GetInt16("name"), 0)
   342  		t.Assert(c.GetInt32("name"), 0)
   343  		t.Assert(c.GetInt64("name"), 0)
   344  		t.Assert(c.GetInts("name"), nil)
   345  		t.Assert(c.GetUint("name"), 0)
   346  		t.Assert(c.GetUint8("name"), 0)
   347  		t.Assert(c.GetUint16("name"), 0)
   348  		t.Assert(c.GetUint32("name"), 0)
   349  		t.Assert(c.GetUint64("name"), 0)
   350  		t.Assert(c.GetTime("name").Format("2006-01-02"), "0001-01-01")
   351  		t.Assert(c.GetGTime("name"), nil)
   352  		t.Assert(c.GetDuration("name").String(), "0s")
   353  		name := struct {
   354  			Name string
   355  		}{}
   356  		t.Assert(c.GetStruct("name", &name) == nil, false)
   357  
   358  		c.Clear()
   359  
   360  		arr, _ := gjson.Encode(
   361  			g.Map{
   362  				"name":   "gf",
   363  				"time":   "2019-06-12",
   364  				"person": g.Map{"name": "gf"},
   365  				"floats": g.Slice{1, 2, 3},
   366  			},
   367  		)
   368  		err = gfile.PutBytes(
   369  			gfile.Join(configPath, "config.yml"),
   370  			arr,
   371  		)
   372  		t.Assert(err, nil)
   373  		t.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12")
   374  		t.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12")
   375  		t.Assert(c.GetDuration("time").String(), "0s")
   376  
   377  		err = c.GetStruct("person", &name)
   378  		t.Assert(err, nil)
   379  		t.Assert(name.Name, "gf")
   380  		t.Assert(c.GetFloats("floats") == nil, false)
   381  	})
   382  }
   383  
   384  func TestCfg_Config(t *testing.T) {
   385  	gtest.C(t, func(t *gtest.T) {
   386  		gcfg.SetContent("gf", "config.yml")
   387  		t.Assert(gcfg.GetContent("config.yml"), "gf")
   388  		gcfg.SetContent("gf1", "config.yml")
   389  		t.Assert(gcfg.GetContent("config.yml"), "gf1")
   390  		gcfg.RemoveContent("config.yml")
   391  		gcfg.ClearContent()
   392  		t.Assert(gcfg.GetContent("name"), "")
   393  	})
   394  }
   395  
   396  func TestCfg_With_UTF8_BOM(t *testing.T) {
   397  	gtest.C(t, func(t *gtest.T) {
   398  		cfg := g.Cfg("test-cfg-with-utf8-bom")
   399  		t.Assert(cfg.SetPath("testdata"), nil)
   400  		cfg.SetFileName("cfg-with-utf8-bom.toml")
   401  		t.Assert(cfg.GetInt("test.testInt"), 1)
   402  		t.Assert(cfg.GetString("test.testStr"), "test")
   403  	})
   404  }