improve function gdebug.TestDataPath and update all usage codes

This commit is contained in:
John 2020-03-21 19:41:05 +08:00
parent 16958413bb
commit c2966817ce
14 changed files with 42 additions and 42 deletions

View File

@ -10,10 +10,10 @@ import (
"path/filepath"
)
// TestDataPath retrieves and returns the testdata path of current package.
// It is used for unit testing cases only.
// The parameter <names> specifies the underlying sub-folders/sub-files,
// which will be joined with current system separator.
// TestDataPath retrieves and returns the testdata path of current package,
// which is used for unit testing cases only.
// The parameter <names> specifies the its sub-folders/sub-files,
// which will be joined with current system separator and returned with the path.
func TestDataPath(names ...string) string {
path := CallerDirectory() + string(filepath.Separator) + "testdata"
for _, name := range names {

View File

@ -67,7 +67,7 @@ func Test_Basic(t *testing.T) {
}
func Test_File(t *testing.T) {
path := gfile.Join(gdebug.TestDataPath(), "test")
path := gdebug.TestDataPath("test")
expect := "dGVzdA=="
gtest.C(t, func(t *gtest.T) {
b, err := gbase64.EncodeFile(path)

View File

@ -43,7 +43,7 @@ func Test_Gzip_UnGzip(t *testing.T) {
}
func Test_Gzip_UnGzip_File(t *testing.T) {
srcPath := gfile.Join(gdebug.TestDataPath(), "gzip", "file.txt")
srcPath := gdebug.TestDataPath("gzip", "file.txt")
dstPath1 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "gzip.zip")
dstPath2 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "file.txt")

View File

@ -20,8 +20,8 @@ import (
func Test_ZipPath(t *testing.T) {
// file
gtest.C(t, func(t *gtest.T) {
srcPath := gfile.Join(gdebug.TestDataPath(), "zip", "path1", "1.txt")
dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip")
srcPath := gdebug.TestDataPath("zip", "path1", "1.txt")
dstPath := gdebug.TestDataPath("zip", "zip.zip")
t.Assert(gfile.Exists(dstPath), false)
err := gcompress.ZipPath(srcPath, dstPath)
@ -44,8 +44,8 @@ func Test_ZipPath(t *testing.T) {
})
// directory
gtest.C(t, func(t *gtest.T) {
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip")
srcPath := gdebug.TestDataPath("zip")
dstPath := gdebug.TestDataPath("zip", "zip.zip")
pwd := gfile.Pwd()
err := gfile.Chdir(srcPath)
@ -77,10 +77,10 @@ func Test_ZipPath(t *testing.T) {
})
// multiple paths joined using char ','
gtest.C(t, func(t *gtest.T) {
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1")
srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2")
dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip")
srcPath := gdebug.TestDataPath("zip")
srcPath1 := gdebug.TestDataPath("zip", "path1")
srcPath2 := gdebug.TestDataPath("zip", "path2")
dstPath := gdebug.TestDataPath("zip", "zip.zip")
pwd := gfile.Pwd()
err := gfile.Chdir(srcPath)
@ -116,9 +116,9 @@ func Test_ZipPath(t *testing.T) {
func Test_ZipPathWriter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1")
srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2")
srcPath := gdebug.TestDataPath("zip")
srcPath1 := gdebug.TestDataPath("zip", "path1")
srcPath2 := gdebug.TestDataPath("zip", "path2")
pwd := gfile.Pwd()
err := gfile.Chdir(srcPath)

View File

@ -14,14 +14,14 @@ import (
)
func Example_LoadJson() {
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "json", "data1.json")
jsonFilePath := gdebug.TestDataPath("json", "data1.json")
j, _ := gjson.Load(jsonFilePath)
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
}
func Example_LoadXml() {
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "xml", "data1.xml")
jsonFilePath := gdebug.TestDataPath("xml", "data1.xml")
j, _ := gjson.Load(jsonFilePath)
fmt.Println(j.Get("doc.name"))
fmt.Println(j.Get("doc.score"))

View File

@ -22,7 +22,7 @@ import (
var (
configContent = gfile.GetContents(
gfile.Join(gdebug.TestDataPath(), "config", "config.toml"),
gdebug.TestDataPath("config", "config.toml"),
)
)

View File

@ -19,7 +19,7 @@ import (
func Test_Database(t *testing.T) {
databaseContent := gfile.GetContents(
gfile.Join(gdebug.TestDataPath(), "database", "config.toml"),
gdebug.TestDataPath("database", "config.toml"),
)
gtest.C(t, func(t *gtest.T) {
var err error

View File

@ -19,7 +19,7 @@ import (
func Test_Redis(t *testing.T) {
redisContent := gfile.GetContents(
gfile.Join(gdebug.TestDataPath(), "redis", "config.toml"),
gdebug.TestDataPath("redis", "config.toml"),
)
gtest.C(t, func(t *gtest.T) {

View File

@ -52,7 +52,7 @@ func Test_View(t *testing.T) {
func Test_View_Config(t *testing.T) {
// view1 test1
gtest.C(t, func(t *gtest.T) {
dirPath := gfile.Join(gdebug.TestDataPath(), "view1")
dirPath := gdebug.TestDataPath("view1")
gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
defer gcfg.ClearContent()
defer instances.Clear()
@ -74,7 +74,7 @@ func Test_View_Config(t *testing.T) {
})
// view1 test2
gtest.C(t, func(t *gtest.T) {
dirPath := gfile.Join(gdebug.TestDataPath(), "view1")
dirPath := gdebug.TestDataPath("view1")
gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
defer gcfg.ClearContent()
defer instances.Clear()
@ -96,7 +96,7 @@ func Test_View_Config(t *testing.T) {
})
// view2
gtest.C(t, func(t *gtest.T) {
dirPath := gfile.Join(gdebug.TestDataPath(), "view2")
dirPath := gdebug.TestDataPath("view2")
gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
defer gcfg.ClearContent()
defer instances.Clear()
@ -118,7 +118,7 @@ func Test_View_Config(t *testing.T) {
})
// view2
gtest.C(t, func(t *gtest.T) {
dirPath := gfile.Join(gdebug.TestDataPath(), "view2")
dirPath := gdebug.TestDataPath("view2")
gcfg.SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
defer gcfg.ClearContent()
defer instances.Clear()

View File

@ -178,7 +178,7 @@ func Test_Middleware_With_Static(t *testing.T) {
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1"))
s.SetServerRoot(gdebug.TestDataPath("static1"))
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
@ -250,7 +250,7 @@ func Test_Middleware_Hook_With_Static(t *testing.T) {
})
s.SetPort(p)
//s.SetDumpRouterMap(false)
s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1"))
s.SetServerRoot(gdebug.TestDataPath("static1"))
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

View File

@ -45,7 +45,7 @@ func Test_Params_File_Single(t *testing.T) {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt")
srcPath := gdebug.TestDataPath("upload", "file1.txt")
dstPath := gfile.Join(dstDirPath, "file1.txt")
content := client.PostContent("/upload/single", g.Map{
"file": "@file:" + srcPath,
@ -61,7 +61,7 @@ func Test_Params_File_Single(t *testing.T) {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt")
srcPath := gdebug.TestDataPath("upload", "file2.txt")
content := client.PostContent("/upload/single", g.Map{
"file": "@file:" + srcPath,
"randomlyRename": true,
@ -98,7 +98,7 @@ func Test_Params_File_CustomName(t *testing.T) {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
srcPath := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt")
srcPath := gdebug.TestDataPath("upload", "file1.txt")
dstPath := gfile.Join(dstDirPath, "my.txt")
content := client.PostContent("/upload/single", g.Map{
"file": "@file:" + srcPath,
@ -135,8 +135,8 @@ func Test_Params_File_Batch(t *testing.T) {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
srcPath1 := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt")
srcPath2 := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt")
srcPath1 := gdebug.TestDataPath("upload", "file1.txt")
srcPath2 := gdebug.TestDataPath("upload", "file2.txt")
dstPath1 := gfile.Join(dstDirPath, "file1.txt")
dstPath2 := gfile.Join(dstDirPath, "file2.txt")
content := client.PostContent("/upload/batch", g.Map{
@ -155,8 +155,8 @@ func Test_Params_File_Batch(t *testing.T) {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
srcPath1 := gfile.Join(gdebug.TestDataPath(), "upload", "file1.txt")
srcPath2 := gfile.Join(gdebug.TestDataPath(), "upload", "file2.txt")
srcPath1 := gdebug.TestDataPath("upload", "file1.txt")
srcPath2 := gdebug.TestDataPath("upload", "file2.txt")
content := client.PostContent("/upload/batch", g.Map{
"file[0]": "@file:" + srcPath1,
"file[1]": "@file:" + srcPath2,

View File

@ -66,7 +66,7 @@ func Test_Static_ServerRoot_Security(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := ports.PopRand()
s := g.Server(p)
s.SetServerRoot(gfile.Join(gdebug.TestDataPath(), "static1"))
s.SetServerRoot(gdebug.TestDataPath("static1"))
s.SetPort(p)
s.Start()
defer s.Shutdown()

View File

@ -24,7 +24,7 @@ import (
func Test_Template_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "basic"))
v := gview.New(gdebug.TestDataPath("template", "basic"))
p := ports.PopRand()
s := g.Server(p)
s.SetView(v)
@ -49,7 +49,7 @@ func Test_Template_Basic(t *testing.T) {
func Test_Template_Encode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "basic"))
v := gview.New(gdebug.TestDataPath("template", "basic"))
v.SetAutoEncode(true)
p := ports.PopRand()
s := g.Server(p)
@ -75,7 +75,7 @@ func Test_Template_Encode(t *testing.T) {
func Test_Template_Layout1(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "layout1"))
v := gview.New(gdebug.TestDataPath("template", "layout1"))
p := ports.PopRand()
s := g.Server(p)
s.SetView(v)
@ -105,7 +105,7 @@ func Test_Template_Layout1(t *testing.T) {
func Test_Template_Layout2(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "layout2"))
v := gview.New(gdebug.TestDataPath("template", "layout2"))
p := ports.PopRand()
s := g.Server(p)
s.SetView(v)

View File

@ -18,7 +18,7 @@ import (
func Test_Config(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gview.Config{
Paths: []string{gfile.Join(gdebug.TestDataPath(), "config")},
Paths: []string{gdebug.TestDataPath("config")},
Data: g.Map{
"name": "gf",
},
@ -45,7 +45,7 @@ func Test_ConfigWithMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
view := gview.New()
err := view.SetConfigWithMap(g.Map{
"Paths": []string{gfile.Join(gdebug.TestDataPath(), "config")},
"Paths": []string{gdebug.TestDataPath("config")},
"DefaultFile": "test.html",
"Delimiters": []string{"${", "}"},
"Data": g.Map{