mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
初步完成测试代码重构
This commit is contained in:
@ -4,38 +4,53 @@ import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//创建测试文件
|
||||
func CreateTestFile(filename,content string) error{
|
||||
TempDir:=os.TempDir()
|
||||
err:=ioutil.WriteFile(TempDir+filename,[]byte(content),0666)
|
||||
func CreateTestFile(filename, content string) error {
|
||||
TempDir := os.TempDir()
|
||||
err := ioutil.WriteFile(TempDir+filename, []byte(content), 0666)
|
||||
return err
|
||||
}
|
||||
|
||||
//测试完删除文件或目录
|
||||
func DelTestFiles(filenames string){
|
||||
func DelTestFiles(filenames string) {
|
||||
os.RemoveAll(filenames)
|
||||
}
|
||||
|
||||
//创建目录
|
||||
func CreateDir(paths string){
|
||||
TempDir:=os.TempDir()
|
||||
os.Mkdir(TempDir+paths,0666)
|
||||
func CreateDir(paths string) {
|
||||
TempDir := os.TempDir()
|
||||
os.Mkdir(TempDir+paths, 0666)
|
||||
}
|
||||
|
||||
//统一格式化文件目录为"/"
|
||||
func Formatpaths(paths []string) []string {
|
||||
for k, v := range paths {
|
||||
paths[k] = filepath.ToSlash(v)
|
||||
paths[k] = strings.Replace(paths[k], "./", "/", 1)
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
//统一格式化文件目录为"/"
|
||||
func Formatpath(paths string) string {
|
||||
paths = filepath.ToSlash(paths)
|
||||
paths = strings.Replace(paths, "./", "/", 1)
|
||||
return paths
|
||||
}
|
||||
|
||||
func TestGetContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
|
||||
|
||||
var (
|
||||
filepaths string = "/testfile_t1.txt"
|
||||
)
|
||||
CreateTestFile(filepaths,"my name is jroam")
|
||||
CreateTestFile(filepaths, "my name is jroam")
|
||||
|
||||
gtest.Assert(GetContents(os.TempDir()+filepaths), "my name is jroam")
|
||||
gtest.Assert(GetContents(""), "")
|
||||
@ -47,24 +62,23 @@ func TestGetContents(t *testing.T) {
|
||||
func TestGetBinContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths1 string = "/testfile_t1.txt" //存在文件
|
||||
filepaths2 string = os.TempDir()+"/testfile_t1_no.txt" //不存大文件
|
||||
filepaths1 string = "/testfile_t1.txt" //存在文件
|
||||
filepaths2 string = os.TempDir() + "/testfile_t1_no.txt" //不存大文件
|
||||
readcontent []byte
|
||||
str1 string="my name is jroam"
|
||||
str1 string = "my name is jroam"
|
||||
)
|
||||
CreateTestFile(filepaths1,str1)
|
||||
readcontent = GetBinContents(os.TempDir()+filepaths1)
|
||||
CreateTestFile(filepaths1, str1)
|
||||
defer DelTestFiles(filepaths1)
|
||||
readcontent = GetBinContents(os.TempDir() + filepaths1)
|
||||
gtest.Assert(readcontent, []byte(str1))
|
||||
|
||||
readcontent = GetBinContents(filepaths2)
|
||||
gtest.Assert(readcontent, nil)
|
||||
gtest.Assert(string(readcontent), "")
|
||||
|
||||
//if readcontent!=nil{
|
||||
// t.Error("文件应不存在")
|
||||
//}
|
||||
gtest.Assert(GetBinContents(filepaths2), nil)
|
||||
|
||||
defer DelTestFiles(filepaths1)
|
||||
gtest.Assert(string(GetBinContents(filepaths2)), "")
|
||||
|
||||
})
|
||||
}
|
||||
@ -76,15 +90,14 @@ func TestTruncate(t *testing.T) {
|
||||
filepaths1 string = "/testfile_GetContents.txt" //存在文件
|
||||
err error
|
||||
)
|
||||
CreateTestFile(filepaths1,"abcdefghijkmln")
|
||||
defer DelTestFiles(filepaths1)
|
||||
CreateTestFile(filepaths1, "abcdefghijkmln")
|
||||
defer DelTestFiles(filepaths1)
|
||||
err = Truncate(os.TempDir()+filepaths1, 200)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = Truncate("", 200)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -95,21 +108,20 @@ func TestPutContents(t *testing.T) {
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
CreateTestFile(filepaths,"a")
|
||||
defer DelTestFiles(filepaths)
|
||||
CreateTestFile(filepaths, "a")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
err = PutContents(os.TempDir()+filepaths, "test!")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir()+filepaths)
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!")
|
||||
|
||||
err = PutContents("", "test!")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -121,21 +133,19 @@ func TestPutContentsAppend(t *testing.T) {
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"a")
|
||||
CreateTestFile(filepaths, "a")
|
||||
defer DelTestFiles(filepaths)
|
||||
err = PutContentsAppend(os.TempDir()+filepaths, "hello")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir()+filepaths)
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "ahello")
|
||||
|
||||
err = PutContentsAppend("", "hello")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -147,22 +157,20 @@ func TestPutBinContents(t *testing.T) {
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
CreateTestFile(filepaths,"a")
|
||||
CreateTestFile(filepaths, "a")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
err = PutBinContents(os.TempDir()+filepaths, []byte("test!!"))
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir()+filepaths)
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!!")
|
||||
|
||||
err = PutBinContents("", []byte("test!!"))
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -173,21 +181,19 @@ func TestPutBinContentsAppend(t *testing.T) {
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
CreateTestFile(filepaths,"test!!")
|
||||
CreateTestFile(filepaths, "test!!")
|
||||
defer DelTestFiles(filepaths)
|
||||
err = PutBinContentsAppend(os.TempDir()+filepaths, []byte("word"))
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir()+filepaths)
|
||||
readcontent, err = ioutil.ReadFile(os.TempDir() + filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!!word")
|
||||
|
||||
err = PutBinContentsAppend("", []byte("word"))
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -198,7 +204,7 @@ func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"abcdefghijk")
|
||||
CreateTestFile(filepaths, "abcdefghijk")
|
||||
defer DelTestFiles(filepaths)
|
||||
readcontent = GetBinContentsByTwoOffsetsByPath(os.TempDir()+filepaths, 2, 5)
|
||||
|
||||
@ -207,7 +213,6 @@ func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
readcontent = GetBinContentsByTwoOffsetsByPath("", 2, 5)
|
||||
gtest.Assert(len(readcontent), 0)
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -218,7 +223,7 @@ func TestGetNextCharOffsetByPath(t *testing.T) {
|
||||
filepaths string = "/testfile_GetContents.txt" //文件内容: abcdefghijk
|
||||
localindex int64
|
||||
)
|
||||
CreateTestFile(filepaths,"abcdefghijk")
|
||||
CreateTestFile(filepaths, "abcdefghijk")
|
||||
defer DelTestFiles(filepaths)
|
||||
localindex = GetNextCharOffsetByPath(os.TempDir()+filepaths, 'd', 1)
|
||||
gtest.Assert(localindex, 3)
|
||||
@ -226,8 +231,6 @@ func TestGetNextCharOffsetByPath(t *testing.T) {
|
||||
localindex = GetNextCharOffsetByPath("", 'd', 1)
|
||||
gtest.Assert(localindex, -1)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -288,7 +291,7 @@ func TestGetBinContentsTilCharByPath(t *testing.T) {
|
||||
filepaths string = "/testfile_GetContents.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"abcdefghijklmn")
|
||||
CreateTestFile(filepaths, "abcdefghijklmn")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
reads, _ = GetBinContentsTilCharByPath(os.TempDir()+filepaths, 'c', 2)
|
||||
@ -300,8 +303,6 @@ func TestGetBinContentsTilCharByPath(t *testing.T) {
|
||||
_, indexs = GetBinContentsTilCharByPath(os.TempDir()+filepaths, 'x', 1)
|
||||
gtest.Assert(indexs, -1)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -21,21 +21,18 @@ func TestSearch(t *testing.T) {
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
|
||||
|
||||
tpath, err = Search(os.TempDir()+paths1)
|
||||
tpath, err = Search(os.TempDir() + paths1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
tpath = filepath.ToSlash(tpath)
|
||||
|
||||
//==================自定义优先路径
|
||||
tpath2, err = Search(os.TempDir()+paths1)
|
||||
tpath2, err = Search(os.TempDir() + paths1)
|
||||
gtest.Assert(err, nil)
|
||||
tpath2 = filepath.ToSlash(tpath2)
|
||||
|
||||
|
||||
//tempstr, _ = filepath.Abs("./")
|
||||
tempstr=os.TempDir()
|
||||
tempstr = os.TempDir()
|
||||
paths1 = tempstr + paths1
|
||||
paths1 = filepath.ToSlash(paths1)
|
||||
//paths1 = strings.Replace(paths1, "./", "/", 1)
|
||||
@ -44,24 +41,19 @@ func TestSearch(t *testing.T) {
|
||||
|
||||
gtest.Assert(tpath2, tpath)
|
||||
|
||||
|
||||
//测试当前目录
|
||||
tpath2, err = Search(os.TempDir()+paths1,"./")
|
||||
tpath2, err = Search(os.TempDir()+paths1, "./")
|
||||
gtest.Assert(err, nil)
|
||||
tpath2 = filepath.ToSlash(tpath2)
|
||||
|
||||
//测试当前目录
|
||||
tempstr, _ = filepath.Abs("./")
|
||||
tempstr=os.TempDir()
|
||||
tempstr = os.TempDir()
|
||||
paths1 = tempstr + paths1
|
||||
paths1 = filepath.ToSlash(paths1)
|
||||
|
||||
gtest.Assert(tpath2, paths1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//测试目录不存在时
|
||||
_, err = Search(paths2)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
func TestIsDir(t *testing.T) {
|
||||
|
||||
gtest.Case(t, func() {
|
||||
paths:="/testfile"
|
||||
paths := "/testfile"
|
||||
CreateDir(paths)
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
@ -20,7 +20,6 @@ func TestIsDir(t *testing.T) {
|
||||
gtest.Assert(IsDir("./testfile/tt.txt"), false)
|
||||
gtest.Assert(IsDir(""), false)
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -28,25 +27,22 @@ func TestIsDir(t *testing.T) {
|
||||
func TestCreate(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
err error
|
||||
filepaths []string
|
||||
fileobj *os.File
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
filepaths = append(filepaths, os.TempDir()+"/testfile_cc1.txt")
|
||||
filepaths = append(filepaths, os.TempDir()+"/testfile_cc2.txt")
|
||||
|
||||
|
||||
for _, v := range filepaths {
|
||||
fileobj, err = Create(v)
|
||||
defer DelTestFiles(v)
|
||||
fileobj.Close()
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -54,15 +50,14 @@ func TestCreate(t *testing.T) {
|
||||
func TestOpen(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
|
||||
)
|
||||
|
||||
file1:="/testfile_nc1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
file1 := "/testfile_nc1.txt"
|
||||
CreateTestFile(file1, "")
|
||||
defer DelTestFiles(file1)
|
||||
|
||||
files = append(files, file1)
|
||||
@ -72,7 +67,7 @@ func TestOpen(t *testing.T) {
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
fileobj, err = Open(os.TempDir()+v)
|
||||
fileobj, err = Open(os.TempDir() + v)
|
||||
fileobj.Close()
|
||||
if flags[k] {
|
||||
gtest.Assert(err, nil)
|
||||
@ -82,27 +77,24 @@ func TestOpen(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenFile(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/file1/nc1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
f1:="/testfile_tt.txt"
|
||||
CreateTestFile(f1,"")
|
||||
defer DelTestFiles(f1)
|
||||
|
||||
f1 := "/testfile_tt.txt"
|
||||
CreateTestFile(f1, "")
|
||||
defer DelTestFiles(f1)
|
||||
|
||||
files = append(files, f1)
|
||||
flags = append(flags, true)
|
||||
@ -116,7 +108,6 @@ func TestOpenFile(t *testing.T) {
|
||||
gtest.AssertNE(err, nil)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
@ -125,14 +116,14 @@ func TestOpenFile(t *testing.T) {
|
||||
func TestOpenWithFlag(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
file1:="/testfile_t1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
file1 := "/testfile_t1.txt"
|
||||
CreateTestFile(file1, "")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
@ -150,21 +141,19 @@ func TestOpenWithFlag(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenWithFlagPerm(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
file1:="/testfile_nc1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
file1 := "/testfile_nc1.txt"
|
||||
CreateTestFile(file1, "")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
@ -183,8 +172,6 @@ func TestOpenWithFlagPerm(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -197,8 +184,8 @@ func TestExists(t *testing.T) {
|
||||
flags []bool
|
||||
)
|
||||
|
||||
file1:="/testfile_GetContents.txt"
|
||||
CreateTestFile(file1,"")
|
||||
file1 := "/testfile_GetContents.txt"
|
||||
CreateTestFile(file1, "")
|
||||
defer DelTestFiles(file1)
|
||||
|
||||
files = append(files, file1)
|
||||
@ -208,7 +195,7 @@ func TestExists(t *testing.T) {
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
flag = Exists(os.TempDir()+v)
|
||||
flag = Exists(os.TempDir() + v)
|
||||
if flags[k] {
|
||||
gtest.Assert(flag, true)
|
||||
} else {
|
||||
@ -217,8 +204,6 @@ func TestExists(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -239,13 +224,13 @@ func TestIsFile(t *testing.T) {
|
||||
flags []bool
|
||||
)
|
||||
|
||||
file1:="/testfile_tt.txt"
|
||||
CreateTestFile(file1,"")
|
||||
file1 := "/testfile_tt.txt"
|
||||
CreateTestFile(file1, "")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
dir1:="/testfiless"
|
||||
dir1 := "/testfiless"
|
||||
CreateDir(dir1)
|
||||
defer DelTestFiles(dir1)
|
||||
files = append(files, dir1)
|
||||
@ -255,7 +240,7 @@ func TestIsFile(t *testing.T) {
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
flag = IsFile(os.TempDir()+v)
|
||||
flag = IsFile(os.TempDir() + v)
|
||||
if flags[k] {
|
||||
gtest.Assert(flag, true)
|
||||
} else {
|
||||
@ -276,18 +261,16 @@ func TestInfo(t *testing.T) {
|
||||
files2 os.FileInfo
|
||||
)
|
||||
|
||||
CreateTestFile(paths,"")
|
||||
CreateTestFile(paths, "")
|
||||
defer DelTestFiles(paths)
|
||||
files, err = Info(os.TempDir()+paths)
|
||||
files, err = Info(os.TempDir() + paths)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
files2, err = os.Stat(os.TempDir()+paths)
|
||||
files2, err = os.Stat(os.TempDir() + paths)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
gtest.Assert(files, files2)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -327,10 +310,10 @@ func TestCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = os.TempDir()+"/testfile_copyfile2.txt"
|
||||
topath string = os.TempDir() + "/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(paths,"")
|
||||
CreateTestFile(paths, "")
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
gtest.Assert(Copy(os.TempDir()+paths, topath), nil)
|
||||
@ -340,10 +323,6 @@ func TestCopy(t *testing.T) {
|
||||
|
||||
gtest.AssertNE(Copy("", ""), nil)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -361,14 +340,12 @@ func TestDirNames(t *testing.T) {
|
||||
|
||||
//=================创建测试文件
|
||||
CreateDir(paths)
|
||||
for _,v:=range havelist{
|
||||
CreateTestFile(paths+"/"+v,"")
|
||||
for _, v := range havelist {
|
||||
CreateTestFile(paths+"/"+v, "")
|
||||
}
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
|
||||
|
||||
readlist, err = DirNames(os.TempDir()+paths)
|
||||
readlist, err = DirNames(os.TempDir() + paths)
|
||||
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(havelist, readlist)
|
||||
@ -376,10 +353,6 @@ func TestDirNames(t *testing.T) {
|
||||
_, err = DirNames("")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -387,7 +360,7 @@ func TestGlob(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "/testfiles/*.txt"
|
||||
dirpath string="/testfiles"
|
||||
dirpath string = "/testfiles"
|
||||
err error
|
||||
resultlist []string
|
||||
)
|
||||
@ -398,37 +371,29 @@ func TestGlob(t *testing.T) {
|
||||
}
|
||||
|
||||
havelist2 := []string{
|
||||
os.TempDir()+"testfiles/t1.txt",
|
||||
os.TempDir()+"testfiles/t2.txt",
|
||||
os.TempDir() + "/testfiles/t1.txt",
|
||||
os.TempDir() + "/testfiles/t2.txt",
|
||||
}
|
||||
|
||||
//===============================构建测试文件
|
||||
CreateDir(dirpath)
|
||||
for _,v:=range havelist1{
|
||||
CreateTestFile(dirpath+"/"+v,"")
|
||||
for _, v := range havelist1 {
|
||||
CreateTestFile(dirpath+"/"+v, "")
|
||||
}
|
||||
defer DelTestFiles(dirpath)
|
||||
|
||||
|
||||
|
||||
resultlist, err = Glob(os.TempDir()+paths, true)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(resultlist, havelist1)
|
||||
|
||||
resultlist, err = Glob(os.TempDir()+paths, false)
|
||||
|
||||
//转换成统一的目录分隔符
|
||||
for k, v := range resultlist {
|
||||
resultlist[k] = filepath.ToSlash(v)
|
||||
}
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(resultlist, havelist2)
|
||||
gtest.Assert(Formatpaths(resultlist), Formatpaths(havelist2))
|
||||
|
||||
_, err = Glob("", true)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -437,7 +402,7 @@ func TestRemove(t *testing.T) {
|
||||
var (
|
||||
paths string = "/testfile_t1.txt"
|
||||
)
|
||||
CreateTestFile(paths,"")
|
||||
CreateTestFile(paths, "")
|
||||
gtest.Assert(Remove(os.TempDir()+paths), nil)
|
||||
|
||||
gtest.Assert(Remove(""), nil)
|
||||
@ -454,14 +419,12 @@ func TestIsReadable(t *testing.T) {
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
CreateTestFile(paths1, "")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(IsReadable(os.TempDir()+paths1), true)
|
||||
gtest.Assert(IsReadable(paths2), false)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -472,12 +435,11 @@ func TestIsWritable(t *testing.T) {
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
CreateTestFile(paths1, "")
|
||||
defer DelTestFiles(paths1)
|
||||
gtest.Assert(IsWritable(os.TempDir()+paths1), true)
|
||||
gtest.Assert(IsWritable(paths2), false)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -487,7 +449,7 @@ func TestChmod(t *testing.T) {
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
CreateTestFile(paths1,"")
|
||||
CreateTestFile(paths1, "")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(Chmod(os.TempDir()+paths1, 0777), nil)
|
||||
@ -505,32 +467,24 @@ func TestScanDir(t *testing.T) {
|
||||
)
|
||||
|
||||
CreateDir(paths1)
|
||||
CreateTestFile(paths1+"/t1.txt","")
|
||||
CreateTestFile(paths1+"/t2.txt","")
|
||||
CreateTestFile(paths1+"/t1.txt", "")
|
||||
CreateTestFile(paths1+"/t2.txt", "")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
|
||||
files, err = ScanDir(os.TempDir()+paths1, "t*")
|
||||
|
||||
result := []string{
|
||||
paths1+"/t1.txt",
|
||||
paths1+"/t2.txt",
|
||||
os.TempDir() + paths1 + "/t1.txt",
|
||||
os.TempDir() + paths1 + "/t2.txt",
|
||||
}
|
||||
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
for k, v := range files {
|
||||
files[k] = filepath.ToSlash(v)
|
||||
}
|
||||
|
||||
gtest.Assert(files, result)
|
||||
gtest.Assert(Formatpaths(files), Formatpaths(result))
|
||||
|
||||
_, err = ScanDir("", "t*")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -547,20 +501,17 @@ func TestRealPath(t *testing.T) {
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
readlPath = RealPath(os.TempDir()+paths1)
|
||||
readlPath = filepath.ToSlash(readlPath)
|
||||
readlPath = RealPath("./")
|
||||
//readlPath = filepath.ToSlash(readlPath)
|
||||
|
||||
tempstr, _ = filepath.Abs("./")
|
||||
paths1 = tempstr + paths1
|
||||
paths1 = filepath.ToSlash(paths1)
|
||||
paths1 = strings.Replace(paths1, "./", "/", 1)
|
||||
//paths1 = tempstr + paths1
|
||||
//paths1=Formatpath(tempstr)
|
||||
|
||||
gtest.Assert(readlPath, paths1)
|
||||
gtest.Assert(readlPath, tempstr)
|
||||
|
||||
gtest.Assert(RealPath("./nodirs"), "")
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -610,12 +561,10 @@ func TestBasename(t *testing.T) {
|
||||
readlPath string
|
||||
)
|
||||
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
CreateTestFile(paths1, "")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
|
||||
readlPath = Basename(os.TempDir()+paths1)
|
||||
readlPath = Basename(os.TempDir() + paths1)
|
||||
gtest.Assert(readlPath, "testfilerr_GetContents.txt")
|
||||
|
||||
})
|
||||
@ -628,11 +577,11 @@ func TestDir(t *testing.T) {
|
||||
readlPath string
|
||||
)
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
readlPath = Dir(os.TempDir()+paths1)
|
||||
readlPath = Dir(os.TempDir() + paths1)
|
||||
|
||||
gtest.Assert(readlPath, "testfiless")
|
||||
gtest.Assert(readlPath, os.TempDir())
|
||||
|
||||
})
|
||||
}
|
||||
@ -641,14 +590,14 @@ func TestDir(t *testing.T) {
|
||||
func TestExt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
dirpath1 ="/testdirs"
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
dirpath1 = "/testdirs"
|
||||
)
|
||||
CreateTestFile(paths1,"")
|
||||
CreateTestFile(paths1, "")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
CreateDir(dirpath1)
|
||||
defer DelTestFiles(dirpath1)
|
||||
defer DelTestFiles(dirpath1)
|
||||
|
||||
gtest.Assert(Ext(os.TempDir()+paths1), ".txt")
|
||||
gtest.Assert(Ext(os.TempDir()+dirpath1), "")
|
||||
@ -677,13 +626,13 @@ func TestMkdir(t *testing.T) {
|
||||
|
||||
defer DelTestFiles(tpath)
|
||||
|
||||
err = Mkdir(os.TempDir()+tpath)
|
||||
err = Mkdir(os.TempDir() + tpath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = Mkdir("")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
err = Mkdir(os.TempDir()+tpath + "2/t1")
|
||||
err = Mkdir(os.TempDir() + tpath + "2/t1")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
})
|
||||
@ -697,10 +646,10 @@ func TestStat(t *testing.T) {
|
||||
err error
|
||||
)
|
||||
|
||||
CreateTestFile(tpath1,"")
|
||||
CreateTestFile(tpath1, "")
|
||||
defer DelTestFiles(tpath1)
|
||||
|
||||
_, err = Stat(os.TempDir()+tpath1)
|
||||
_, err = Stat(os.TempDir() + tpath1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
_, err = Stat(tpath2)
|
||||
|
||||
@ -21,7 +21,7 @@ func TestMTime(t *testing.T) {
|
||||
fileobj, err = os.Stat(os.TempDir() + file1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
gtest.AssertGT(MTime(os.TempDir()+file1), fileobj.ModTime().Unix())
|
||||
gtest.Assert(MTime(os.TempDir()+file1), fileobj.ModTime().Unix())
|
||||
gtest.Assert(MTime(""), 0)
|
||||
})
|
||||
}
|
||||
@ -39,7 +39,6 @@ func TestMTimeMillisecond(t *testing.T) {
|
||||
fileobj, err = os.Stat(os.TempDir() + file1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
gtest.AssertGTE(MTimeMillisecond(os.TempDir()+file1), fileobj.ModTime().Nanosecond()/1000000)
|
||||
gtest.Assert(MTimeMillisecond(""), 0)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user