2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-06 17:59:55 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
package gfile_test
|
2019-04-10 16:22:29 +08:00
|
|
|
|
2019-04-15 15:27:49 +08:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2019-07-27 11:34:04 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2019-04-15 15:27:49 +08:00
|
|
|
)
|
|
|
|
|
2019-07-27 11:34:04 +08:00
|
|
|
func Test_Search(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-15 15:27:49 +08:00
|
|
|
var (
|
|
|
|
paths1 string = "/testfiless"
|
|
|
|
paths2 string = "./testfile/dirfiles_no"
|
|
|
|
tpath string
|
|
|
|
tpath2 string
|
|
|
|
tempstr string
|
2019-04-15 15:34:35 +08:00
|
|
|
ypaths1 string
|
2019-04-15 15:27:49 +08:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
createDir(paths1)
|
|
|
|
defer delTestFiles(paths1)
|
2019-04-15 18:14:38 +08:00
|
|
|
ypaths1 = paths1
|
2019-04-15 15:27:49 +08:00
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
tpath, err = gfile.Search(testpath() + paths1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
|
|
|
tpath = filepath.ToSlash(tpath)
|
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
// 自定义优先路径
|
|
|
|
tpath2, err = gfile.Search(testpath() + paths1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-04-15 15:27:49 +08:00
|
|
|
tpath2 = filepath.ToSlash(tpath2)
|
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
tempstr = testpath()
|
2019-04-15 15:27:49 +08:00
|
|
|
paths1 = tempstr + paths1
|
|
|
|
paths1 = filepath.ToSlash(paths1)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(tpath, paths1)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(tpath2, tpath)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
// 测试给定目录
|
|
|
|
tpath2, err = gfile.Search(paths1, "testfiless")
|
2019-04-15 15:27:49 +08:00
|
|
|
tpath2 = filepath.ToSlash(tpath2)
|
2019-04-15 18:14:38 +08:00
|
|
|
tempss := filepath.ToSlash(paths1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(tpath2, tempss)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
// 测试当前目录
|
2019-04-15 15:27:49 +08:00
|
|
|
tempstr, _ = filepath.Abs("./")
|
2019-04-16 22:11:44 +08:00
|
|
|
tempstr = testpath()
|
2019-04-15 15:34:35 +08:00
|
|
|
paths1 = tempstr + ypaths1
|
2019-04-15 15:27:49 +08:00
|
|
|
paths1 = filepath.ToSlash(paths1)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(tpath2, paths1)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
2019-04-16 22:11:44 +08:00
|
|
|
// 测试目录不存在时
|
|
|
|
_, err = gfile.Search(paths2)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-04-15 15:27:49 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|