github.com/wangyougui/gf/v2@v2.6.5/os/gcfg/gcfg_z_unit_adapter_file_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/gfile"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  )
    18  
    19  func TestAdapterFile_Dump(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		c, err := gcfg.NewAdapterFile("config.yml")
    22  		t.AssertNil(err)
    23  
    24  		t.Assert(c.GetFileName(), "config.yml")
    25  
    26  		c.Dump()
    27  		c.Data(ctx)
    28  	})
    29  
    30  	gtest.C(t, func(t *gtest.T) {
    31  		c, err := gcfg.NewAdapterFile("testdata/default/config.toml")
    32  		t.AssertNil(err)
    33  
    34  		c.Dump()
    35  		c.Data(ctx)
    36  		c.GetPaths()
    37  	})
    38  
    39  }
    40  func TestAdapterFile_Available(t *testing.T) {
    41  	gtest.C(t, func(t *gtest.T) {
    42  		c, err := gcfg.NewAdapterFile("testdata/default/config.toml")
    43  		t.AssertNil(err)
    44  		c.Available(ctx)
    45  	})
    46  }
    47  
    48  func TestAdapterFile_SetPath(t *testing.T) {
    49  	gtest.C(t, func(t *gtest.T) {
    50  		c, err := gcfg.NewAdapterFile("config.yml")
    51  		t.AssertNil(err)
    52  
    53  		err = c.SetPath("/tmp")
    54  		t.AssertNil(err)
    55  
    56  		err = c.SetPath("notexist")
    57  		t.AssertNE(err, nil)
    58  
    59  		err = c.SetPath("testdata/c1.toml")
    60  		t.AssertNE(err, nil)
    61  
    62  		err = c.SetPath("")
    63  		t.AssertNil(err)
    64  
    65  		err = c.SetPath("gcfg.go")
    66  		t.AssertNE(err, nil)
    67  
    68  		v, err := c.Get(ctx, "name")
    69  		t.AssertNE(err, nil)
    70  		t.Assert(v, nil)
    71  	})
    72  }
    73  
    74  func TestAdapterFile_AddPath(t *testing.T) {
    75  	gtest.C(t, func(t *gtest.T) {
    76  		c, err := gcfg.NewAdapterFile("config.yml")
    77  		t.AssertNil(err)
    78  
    79  		err = c.AddPath("/tmp")
    80  		t.AssertNil(err)
    81  
    82  		err = c.AddPath("notexist")
    83  		t.AssertNE(err, nil)
    84  
    85  		err = c.SetPath("testdata/c1.toml")
    86  		t.AssertNE(err, nil)
    87  
    88  		err = c.SetPath("")
    89  		t.AssertNil(err)
    90  
    91  		err = c.AddPath("gcfg.go")
    92  		t.AssertNE(err, nil)
    93  
    94  		v, err := c.Get(ctx, "name")
    95  		t.AssertNE(err, nil)
    96  		t.Assert(v, nil)
    97  	})
    98  }
    99  
   100  func TestAdapterFile_SetViolenceCheck(t *testing.T) {
   101  	gtest.C(t, func(t *gtest.T) {
   102  		c, err := gcfg.NewAdapterFile("config.yml")
   103  		t.AssertNil(err)
   104  		c.SetViolenceCheck(true)
   105  		v, err := c.Get(ctx, "name")
   106  		t.AssertNE(err, nil)
   107  		t.Assert(v, nil)
   108  	})
   109  }
   110  
   111  func TestAdapterFile_FilePath(t *testing.T) {
   112  	gtest.C(t, func(t *gtest.T) {
   113  		c, err := gcfg.NewAdapterFile("config.yml")
   114  		t.AssertNil(err)
   115  
   116  		path, _ := c.GetFilePath("tmp")
   117  		t.Assert(path, "")
   118  
   119  		path, _ = c.GetFilePath("tmp")
   120  		t.Assert(path, "")
   121  	})
   122  }
   123  
   124  func TestAdapterFile_Content(t *testing.T) {
   125  	gtest.C(t, func(t *gtest.T) {
   126  		c, err := gcfg.NewAdapterFile()
   127  		t.AssertNil(err)
   128  
   129  		c.SetContent("gf", "config.yml")
   130  		t.Assert(c.GetContent("config.yml"), "gf")
   131  		c.SetContent("gf1", "config.yml")
   132  		t.Assert(c.GetContent("config.yml"), "gf1")
   133  		c.RemoveContent("config.yml")
   134  		c.ClearContent()
   135  		t.Assert(c.GetContent("name"), "")
   136  	})
   137  }
   138  
   139  func TestAdapterFile_With_UTF8_BOM(t *testing.T) {
   140  	gtest.C(t, func(t *gtest.T) {
   141  		c, err := gcfg.NewAdapterFile("test-cfg-with-utf8-bom")
   142  		t.AssertNil(err)
   143  
   144  		t.Assert(c.SetPath("testdata"), nil)
   145  		c.SetFileName("cfg-with-utf8-bom.toml")
   146  		t.Assert(c.MustGet(ctx, "test.testInt"), 1)
   147  		t.Assert(c.MustGet(ctx, "test.testStr"), "test")
   148  	})
   149  }
   150  
   151  func TestAdapterFile_Set(t *testing.T) {
   152  	config := `log-path = "logs"`
   153  	gtest.C(t, func(t *gtest.T) {
   154  		var (
   155  			path = gcfg.DefaultConfigFileName
   156  			err  = gfile.PutContents(path, config)
   157  		)
   158  		t.AssertNil(err)
   159  		defer gfile.Remove(path)
   160  
   161  		c, err := gcfg.New()
   162  		t.Assert(c.MustGet(ctx, "log-path").String(), "logs")
   163  
   164  		err = c.GetAdapter().(*gcfg.AdapterFile).Set("log-path", "custom-logs")
   165  		t.AssertNil(err)
   166  		t.Assert(c.MustGet(ctx, "log-path").String(), "custom-logs")
   167  	})
   168  }