github.com/zhongdalu/gf@v1.0.0/g/frame/gins/gins_config_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  package gins_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/zhongdalu/gf/g/os/gcfg"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/zhongdalu/gf/g/frame/gins"
    16  	"github.com/zhongdalu/gf/g/os/gfile"
    17  	"github.com/zhongdalu/gf/g/os/gtime"
    18  	"github.com/zhongdalu/gf/g/test/gtest"
    19  )
    20  
    21  func Test_Config(t *testing.T) {
    22  	config := `
    23  # 模板引擎目录
    24  viewpath = "/home/www/templates/"
    25  test = "v=1"
    26  # MySQL数据库配置
    27  [database]
    28      [[database.default]]
    29          host     = "127.0.0.1"
    30          port     = "3306"
    31          user     = "root"
    32          pass     = ""
    33          name     = "test"
    34          type     = "mysql"
    35          role     = "master"
    36          charset  = "utf8"
    37          priority = "1"
    38      [[database.default]]
    39          host     = "127.0.0.1"
    40          port     = "3306"
    41          user     = "root"
    42          pass     = "8692651"
    43          name     = "test"
    44          type     = "mysql"
    45          role     = "master"
    46          charset  = "utf8"
    47          priority = "1"
    48  # Redis数据库配置
    49  [redis]
    50      disk  = "127.0.0.1:6379,0"
    51      cache = "127.0.0.1:6379,1"
    52  `
    53  	gtest.Case(t, func() {
    54  		gtest.AssertNE(gins.Config(), nil)
    55  	})
    56  
    57  	// relative path
    58  	gtest.Case(t, func() {
    59  		path := "config.toml"
    60  		err := gfile.PutContents(path, config)
    61  		gtest.Assert(err, nil)
    62  		defer gfile.Remove(path)
    63  		defer gins.Config().Clear()
    64  		gtest.Assert(gins.Config().Get("test"), "v=1")
    65  		gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
    66  		gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
    67  	})
    68  	// for gfsnotify callbacks to refresh cache of config file
    69  	time.Sleep(500 * time.Millisecond)
    70  
    71  	// relative path, config folder
    72  	gtest.Case(t, func() {
    73  		path := "config/config.toml"
    74  		err := gfile.PutContents(path, config)
    75  		gtest.Assert(err, nil)
    76  		defer gfile.Remove(path)
    77  		defer gins.Config().Clear()
    78  		gtest.Assert(gins.Config().Get("test"), "v=1")
    79  		gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
    80  		gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
    81  	})
    82  	// for gfsnotify callbacks to refresh cache of config file
    83  	time.Sleep(500 * time.Millisecond)
    84  
    85  	gtest.Case(t, func() {
    86  		path := "test.toml"
    87  		err := gfile.PutContents(path, config)
    88  		gtest.Assert(err, nil)
    89  		defer gfile.Remove(path)
    90  		defer gins.Config("test").Clear()
    91  		gins.Config("test").SetFileName("test.toml")
    92  		gtest.Assert(gins.Config("test").Get("test"), "v=1")
    93  		gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
    94  		gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
    95  	})
    96  	// for gfsnotify callbacks to refresh cache of config file
    97  	time.Sleep(500 * time.Millisecond)
    98  
    99  	gtest.Case(t, func() {
   100  		path := "config/test.toml"
   101  		err := gfile.PutContents(path, config)
   102  		gtest.Assert(err, nil)
   103  		defer gfile.Remove(path)
   104  		defer gins.Config("test").Clear()
   105  		gins.Config("test").SetFileName("test.toml")
   106  		gtest.Assert(gins.Config("test").Get("test"), "v=1")
   107  		gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   108  		gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   109  	})
   110  	// for gfsnotify callbacks to refresh cache of config file
   111  	time.Sleep(500 * time.Millisecond)
   112  
   113  	// absolute path
   114  	gtest.Case(t, func() {
   115  		path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
   116  		file := fmt.Sprintf(`%s/%s`, path, "config.toml")
   117  		err := gfile.PutContents(file, config)
   118  		gtest.Assert(err, nil)
   119  		defer gfile.Remove(file)
   120  		defer gins.Config().Clear()
   121  		gtest.Assert(gins.Config().AddPath(path), nil)
   122  		gtest.Assert(gins.Config().Get("test"), "v=1")
   123  		gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
   124  		gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
   125  	})
   126  	time.Sleep(500 * time.Millisecond)
   127  
   128  	gtest.Case(t, func() {
   129  		path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.Nanosecond())
   130  		file := fmt.Sprintf(`%s/%s`, path, "config.toml")
   131  		err := gfile.PutContents(file, config)
   132  		gtest.Assert(err, nil)
   133  		defer gfile.Remove(file)
   134  		defer gins.Config().Clear()
   135  		gtest.Assert(gins.Config().AddPath(path), nil)
   136  		gtest.Assert(gins.Config().Get("test"), "v=1")
   137  		gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
   138  		gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
   139  	})
   140  	time.Sleep(500 * time.Millisecond)
   141  
   142  	gtest.Case(t, func() {
   143  		path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
   144  		file := fmt.Sprintf(`%s/%s`, path, "test.toml")
   145  		err := gfile.PutContents(file, config)
   146  		gtest.Assert(err, nil)
   147  		defer gfile.Remove(file)
   148  		defer gins.Config("test").Clear()
   149  		gins.Config("test").SetFileName("test.toml")
   150  		gtest.Assert(gins.Config("test").AddPath(path), nil)
   151  		gtest.Assert(gins.Config("test").Get("test"), "v=1")
   152  		gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   153  		gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   154  	})
   155  	time.Sleep(500 * time.Millisecond)
   156  
   157  	gtest.Case(t, func() {
   158  		path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.Nanosecond())
   159  		file := fmt.Sprintf(`%s/%s`, path, "test.toml")
   160  		err := gfile.PutContents(file, config)
   161  		gtest.Assert(err, nil)
   162  		defer gfile.Remove(file)
   163  		defer gins.Config().Clear()
   164  		gins.Config("test").SetFileName("test.toml")
   165  		gtest.Assert(gins.Config("test").AddPath(path), nil)
   166  		gtest.Assert(gins.Config("test").Get("test"), "v=1")
   167  		gtest.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
   168  		gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
   169  	})
   170  }
   171  
   172  func Test_Basic2(t *testing.T) {
   173  	config := `log-path = "logs"`
   174  	gtest.Case(t, func() {
   175  		path := gcfg.DEFAULT_CONFIG_FILE
   176  		err := gfile.PutContents(path, config)
   177  		gtest.Assert(err, nil)
   178  		defer func() {
   179  			_ = gfile.Remove(path)
   180  		}()
   181  
   182  		gtest.Assert(gins.Config().Get("log-path"), "logs")
   183  	})
   184  }