fix issue in unit testing case of package gcdg

This commit is contained in:
John
2020-03-16 20:46:03 +08:00
parent 8230c72ec6
commit 8ed3cf9c97
2 changed files with 39 additions and 19 deletions

View File

@ -10,7 +10,7 @@ package gcfg_test
import (
"github.com/gogf/gf/debug/gdebug"
"io/ioutil"
"github.com/gogf/gf/os/gtime"
"os"
"testing"
@ -40,9 +40,7 @@ array = [1,2,3]
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer func() {
_ = gfile.Remove(path)
}()
defer gfile.Remove(path)
c := gcfg.New()
gtest.Assert(c.Get("v1"), 1)
@ -310,9 +308,8 @@ func TestCfg_New(t *testing.T) {
configPath := gfile.Pwd() + gfile.Separator + "config"
_ = gfile.Mkdir(configPath)
defer func() {
_ = gfile.Remove(configPath)
}()
defer gfile.Remove(configPath)
c = gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
@ -364,12 +361,21 @@ func TestCfg_FilePath(t *testing.T) {
func TestCfg_Get(t *testing.T) {
gtest.Case(t, func() {
configPath := gfile.Pwd() + gfile.Separator + "config"
_ = gfile.Mkdir(configPath)
defer func() {
_ = gfile.Remove(configPath)
}()
_ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", []byte("wrong config"), 0644)
var err error
configPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(configPath)
gtest.Assert(err, nil)
defer gfile.Remove(configPath)
defer gfile.Chdir(gfile.Pwd())
err = gfile.Chdir(configPath)
gtest.Assert(err, nil)
err = gfile.PutContents(
gfile.Join(configPath, "config.yml"),
"wrong config",
)
gtest.Assert(err, nil)
c := gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
gtest.Assert(c.GetVar("name").Val(), nil)
@ -404,13 +410,24 @@ func TestCfg_Get(t *testing.T) {
c.Clear()
arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}})
_ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", arr, 0644)
arr, _ := gjson.Encode(
g.Map{
"name": "gf",
"time": "2019-06-12",
"person": g.Map{"name": "gf"},
"floats": g.Slice{1, 2, 3},
},
)
err = gfile.PutBytes(
gfile.Join(configPath, "config.yml"),
arr,
)
gtest.Assert(err, nil)
gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12")
gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12")
gtest.Assert(c.GetDuration("time").String(), "0s")
//t.Log(c.GetString("person"))
err := c.GetStruct("person", &name)
err = c.GetStruct("person", &name)
gtest.Assert(err, nil)
gtest.Assert(name.Name, "gf")
gtest.Assert(c.GetFloats("floats") == nil, false)

View File

@ -7,12 +7,14 @@
// Package gspath implements file index and search for folders.
//
// It searches file internally with high performance in order by the directory adding sequence.
// Note that: If caching feature enabled, there would be a searching delay after adding/deleting files.
// Note that:
// If caching feature enabled, there would be a searching delay after adding/deleting files.
package gspath
import (
"errors"
"fmt"
"github.com/gogf/gf/internal/intlog"
"os"
"sort"
"strings"
@ -50,7 +52,7 @@ func New(path string, cache bool) *SPath {
}
if len(path) > 0 {
if _, err := sp.Add(path); err != nil {
//fmt.Errorf(err.Error())
intlog.Print(err)
}
}
return sp
@ -111,6 +113,7 @@ func (sp *SPath) Set(path string) (realPath string, err error) {
sp.removeMonitorByPath(v)
}
}
intlog.Print("paths clear:", sp.paths)
sp.paths.Clear()
if sp.cache != nil {
sp.cache.Clear()