github.com/gogf/gf@v1.16.9/frame/gins/gins_z_unit_config_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  package gins_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/debug/gdebug"
    12  	"github.com/gogf/gf/frame/gins"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/os/gcfg"
    17  
    18  	"github.com/gogf/gf/os/gfile"
    19  	"github.com/gogf/gf/os/gtime"
    20  	"github.com/gogf/gf/test/gtest"
    21  )
    22  
    23  var (
    24  	configContent = gfile.GetContents(
    25  		gdebug.TestDataPath("config", "config.toml"),
    26  	)
    27  )
    28  
    29  func Test_Config1(t *testing.T) {
    30  	gtest.C(t, func(t *gtest.T) {
    31  		t.AssertNE(configContent, "")
    32  	})
    33  	gtest.C(t, func(t *gtest.T) {
    34  		t.AssertNE(gins.Config(), nil)
    35  	})
    36  }
    37  
    38  func Test_Config2(t *testing.T) {
    39  	// relative path
    40  	gtest.C(t, func(t *gtest.T) {
    41  		var err error
    42  		dirPath := gfile.TempDir(gtime.TimestampNanoStr())
    43  		err = gfile.Mkdir(dirPath)
    44  		t.Assert(err, nil)
    45  		defer gfile.Remove(dirPath)
    46  
    47  		name := "config.toml"
    48  		err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
    49  		t.Assert(err, nil)
    50  
    51  		err = gins.Config().AddPath(dirPath)
    52  		t.Assert(err, nil)
    53  
    54  		defer gins.Config().Clear()
    55  
    56  		t.Assert(gins.Config().Get("test"), "v=1")
    57  		t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
    58  		t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
    59  	})
    60  	// for gfsnotify callbacks to refresh cache of config file
    61  	time.Sleep(500 * time.Millisecond)
    62  
    63  	// relative path, config folder
    64  	gtest.C(t, func(t *gtest.T) {
    65  		var err error
    66  		dirPath := gfile.TempDir(gtime.TimestampNanoStr())
    67  		err = gfile.Mkdir(dirPath)
    68  		t.Assert(err, nil)
    69  		defer gfile.Remove(dirPath)
    70  
    71  		name := "config/config.toml"
    72  		err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
    73  		t.Assert(err, nil)
    74  
    75  		err = gins.Config().AddPath(dirPath)
    76  		t.Assert(err, nil)
    77  
    78  		defer gins.Config().Clear()
    79  
    80  		t.Assert(gins.Config().Get("test"), "v=1")
    81  		t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
    82  		t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
    83  
    84  		// for gfsnotify callbacks to refresh cache of config file
    85  		time.Sleep(500 * time.Millisecond)
    86  	})
    87  }
    88  
    89  func Test_Config3(t *testing.T) {
    90  	gtest.C(t, func(t *gtest.T) {
    91  		var err error
    92  		dirPath := gfile.TempDir(gtime.TimestampNanoStr())
    93  		err = gfile.Mkdir(dirPath)
    94  		t.Assert(err, nil)
    95  		defer gfile.Remove(dirPath)
    96  
    97  		name := "test.toml"
    98  		err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
    99  		t.Assert(err, nil)
   100  
   101  		err = gins.Config("test").AddPath(dirPath)
   102  		t.Assert(err, nil)
   103  
   104  		defer gins.Config("test").Clear()
   105  		gins.Config("test").SetFileName("test.toml")
   106  
   107  		t.Assert(gins.Config("test").Get("test"), "v=1")
   108  		t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   109  		t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   110  	})
   111  	// for gfsnotify callbacks to refresh cache of config file
   112  	time.Sleep(500 * time.Millisecond)
   113  
   114  	gtest.C(t, func(t *gtest.T) {
   115  		var err error
   116  		dirPath := gfile.TempDir(gtime.TimestampNanoStr())
   117  		err = gfile.Mkdir(dirPath)
   118  		t.Assert(err, nil)
   119  		defer gfile.Remove(dirPath)
   120  
   121  		name := "config/test.toml"
   122  		err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
   123  		t.Assert(err, nil)
   124  
   125  		err = gins.Config("test").AddPath(dirPath)
   126  		t.Assert(err, nil)
   127  
   128  		defer gins.Config("test").Clear()
   129  		gins.Config("test").SetFileName("test.toml")
   130  
   131  		t.Assert(gins.Config("test").Get("test"), "v=1")
   132  		t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   133  		t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   134  	})
   135  	// for gfsnotify callbacks to refresh cache of config file for next unit testing case.
   136  	time.Sleep(500 * time.Millisecond)
   137  }
   138  
   139  func Test_Config4(t *testing.T) {
   140  	// absolute path
   141  	gtest.C(t, func(t *gtest.T) {
   142  		path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
   143  		file := fmt.Sprintf(`%s/%s`, path, "config.toml")
   144  		err := gfile.PutContents(file, configContent)
   145  		t.Assert(err, nil)
   146  		defer gfile.Remove(file)
   147  		defer gins.Config().Clear()
   148  
   149  		t.Assert(gins.Config().AddPath(path), nil)
   150  		t.Assert(gins.Config().Get("test"), "v=1")
   151  		t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
   152  		t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
   153  	})
   154  	time.Sleep(500 * time.Millisecond)
   155  
   156  	gtest.C(t, func(t *gtest.T) {
   157  		path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
   158  		file := fmt.Sprintf(`%s/%s`, path, "config.toml")
   159  		err := gfile.PutContents(file, configContent)
   160  		t.Assert(err, nil)
   161  		defer gfile.Remove(file)
   162  		defer gins.Config().Clear()
   163  		t.Assert(gins.Config().AddPath(path), nil)
   164  		t.Assert(gins.Config().Get("test"), "v=1")
   165  		t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
   166  		t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
   167  	})
   168  	time.Sleep(500 * time.Millisecond)
   169  
   170  	gtest.C(t, func(t *gtest.T) {
   171  		path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
   172  		file := fmt.Sprintf(`%s/%s`, path, "test.toml")
   173  		err := gfile.PutContents(file, configContent)
   174  		t.Assert(err, nil)
   175  		defer gfile.Remove(file)
   176  		defer gins.Config("test").Clear()
   177  		gins.Config("test").SetFileName("test.toml")
   178  		t.Assert(gins.Config("test").AddPath(path), nil)
   179  		t.Assert(gins.Config("test").Get("test"), "v=1")
   180  		t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   181  		t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   182  	})
   183  	time.Sleep(500 * time.Millisecond)
   184  
   185  	gtest.C(t, func(t *gtest.T) {
   186  		path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
   187  		file := fmt.Sprintf(`%s/%s`, path, "test.toml")
   188  		err := gfile.PutContents(file, configContent)
   189  		t.Assert(err, nil)
   190  		defer gfile.Remove(file)
   191  		defer gins.Config().Clear()
   192  		gins.Config("test").SetFileName("test.toml")
   193  		t.Assert(gins.Config("test").AddPath(path), nil)
   194  		t.Assert(gins.Config("test").Get("test"), "v=1")
   195  		t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   196  		t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   197  	})
   198  }
   199  func Test_Basic2(t *testing.T) {
   200  	config := `log-path = "logs"`
   201  	gtest.C(t, func(t *gtest.T) {
   202  		path := gcfg.DefaultConfigFile
   203  		err := gfile.PutContents(path, config)
   204  		t.Assert(err, nil)
   205  		defer func() {
   206  			_ = gfile.Remove(path)
   207  		}()
   208  
   209  		t.Assert(gins.Config().Get("log-path"), "logs")
   210  	})
   211  }