mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
重构测试代码
This commit is contained in:
@ -28,7 +28,7 @@ install:
|
||||
- cat /etc/hosts
|
||||
|
||||
script:
|
||||
- cd g
|
||||
- cd g/os/gfile
|
||||
- GOARCH=386 go test -v ./...
|
||||
- GOARCH=amd64 go test -v ./... -race -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
|
||||
@ -3,18 +3,43 @@ package gfile
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//创建测试文件
|
||||
func CreateTestFile(filename,content string) error{
|
||||
TempDir:=os.TempDir()
|
||||
err:=ioutil.WriteFile(TempDir+filename,[]byte(content),0666)
|
||||
return err
|
||||
}
|
||||
|
||||
//测试完删除文件或目录
|
||||
func DelTestFiles(filenames string){
|
||||
os.RemoveAll(filenames)
|
||||
}
|
||||
|
||||
//创建目录
|
||||
func CreateDir(paths string){
|
||||
TempDir:=os.TempDir()
|
||||
os.Mkdir(TempDir+paths,0666)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestGetContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
|
||||
|
||||
var (
|
||||
filepaths string = "./testfile/dirfiles/t1.txt"
|
||||
filepaths string = "/testfile_t1.txt"
|
||||
)
|
||||
CreateTestFile(filepaths,"my name is jroam")
|
||||
|
||||
gtest.Assert(GetContents(filepaths), "my name is jroam")
|
||||
gtest.Assert(GetContents(""), "")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
})
|
||||
}
|
||||
@ -22,12 +47,14 @@ func TestGetContents(t *testing.T) {
|
||||
func TestGetBinContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths1 string = "./testfile/dirfiles/t1.txt" //存在文件
|
||||
filepaths2 string = "./testfile/dirfiles/t1_no.txt" //不存大文件
|
||||
filepaths1 string = "/testfile_t1.txt" //存在文件
|
||||
filepaths2 string = os.TempDir()+"/testfile_t1_no.txt" //不存大文件
|
||||
readcontent []byte
|
||||
str1 string="my name is jroam"
|
||||
)
|
||||
CreateTestFile(filepaths1,str1)
|
||||
readcontent = GetBinContents(filepaths1)
|
||||
gtest.Assert(readcontent, []byte("my name is jroam"))
|
||||
gtest.Assert(readcontent, []byte(str1))
|
||||
|
||||
readcontent = GetBinContents(filepaths2)
|
||||
gtest.Assert(readcontent, nil)
|
||||
@ -37,6 +64,8 @@ func TestGetBinContents(t *testing.T) {
|
||||
//}
|
||||
gtest.Assert(GetBinContents(filepaths2), nil)
|
||||
|
||||
defer DelTestFiles(filepaths1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -44,25 +73,30 @@ func TestGetBinContents(t *testing.T) {
|
||||
func TestTruncate(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths1 string = "./testfile/havefile1/GetContents.txt" //存在文件
|
||||
filepaths1 string = "/testfile_GetContents.txt" //存在文件
|
||||
err error
|
||||
)
|
||||
CreateTestFile(filepaths1,"abcdefghijkmln")
|
||||
defer DelTestFiles(filepaths1)
|
||||
err = Truncate(filepaths1, 200)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = Truncate("", 200)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestPutContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/PutContents.txt"
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
CreateTestFile(filepaths,"a")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
err = PutContents(filepaths, "test!")
|
||||
gtest.Assert(err, nil)
|
||||
@ -70,33 +104,38 @@ func TestPutContents(t *testing.T) {
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!")
|
||||
gtest.Assert(string(readcontent), "atest!")
|
||||
|
||||
err = PutContents("", "test!")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestPutContentsAppend(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/PutContents.txt"
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"a")
|
||||
defer DelTestFiles(filepaths)
|
||||
err = PutContentsAppend(filepaths, "hello")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!hello")
|
||||
gtest.Assert(string(readcontent), "ahello")
|
||||
|
||||
err = PutContentsAppend("", "hello")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -104,10 +143,12 @@ func TestPutContentsAppend(t *testing.T) {
|
||||
func TestPutBinContents(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/PutContents.txt"
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
CreateTestFile(filepaths,"a")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
err = PutBinContents(filepaths, []byte("test!!"))
|
||||
gtest.Assert(err, nil)
|
||||
@ -115,22 +156,25 @@ func TestPutBinContents(t *testing.T) {
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(filepaths)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(readcontent), "test!!")
|
||||
gtest.Assert(string(readcontent), "atest!!")
|
||||
|
||||
err = PutBinContents("", []byte("test!!"))
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestPutBinContentsAppend(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/PutContents.txt" //原文件内容: yy
|
||||
filepaths string = "/testfile_PutContents.txt" //原文件内容: yy
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"")
|
||||
defer DelTestFiles(filepaths)
|
||||
err = PutBinContentsAppend(filepaths, []byte("word"))
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
@ -142,16 +186,20 @@ func TestPutBinContentsAppend(t *testing.T) {
|
||||
err = PutBinContentsAppend("", []byte("word"))
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/GetContents.txt" //原文件内容: abcdefghijk
|
||||
filepaths string = "/testfile_GetContents.txt" //文件内容: abcdefghijk
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"abcdefghijk")
|
||||
defer DelTestFiles(filepaths)
|
||||
readcontent = GetBinContentsByTwoOffsetsByPath(filepaths, 2, 5)
|
||||
|
||||
gtest.Assert(string(readcontent), "cde")
|
||||
@ -159,6 +207,7 @@ func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
readcontent = GetBinContentsByTwoOffsetsByPath("", 2, 5)
|
||||
gtest.Assert(len(readcontent), 0)
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -166,16 +215,19 @@ func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
func TestGetNextCharOffsetByPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
filepaths string = "./testfile/havefile1/GetContents.txt" //原文件内容: abcdefghijk
|
||||
filepaths string = "/testfile_GetContents.txt" //文件内容: abcdefghijk
|
||||
localindex int64
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"abcdefghijk")
|
||||
defer DelTestFiles(filepaths)
|
||||
localindex = GetNextCharOffsetByPath(filepaths, 'd', 1)
|
||||
gtest.Assert(localindex, 3)
|
||||
|
||||
localindex = GetNextCharOffsetByPath("", 'd', 1)
|
||||
gtest.Assert(localindex, -1)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -233,9 +285,12 @@ func TestGetBinContentsTilCharByPath(t *testing.T) {
|
||||
var (
|
||||
reads []byte
|
||||
indexs int64
|
||||
filepaths string = "./testfile/havefile1/GetContents.txt"
|
||||
filepaths string = "/testfile_GetContents.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(filepaths,"abcdefghijklmn")
|
||||
defer DelTestFiles(filepaths)
|
||||
|
||||
reads, _ = GetBinContentsTilCharByPath(filepaths, 'c', 2)
|
||||
gtest.Assert(string(reads), "c")
|
||||
|
||||
@ -245,6 +300,8 @@ func TestGetBinContentsTilCharByPath(t *testing.T) {
|
||||
_, indexs = GetBinContentsTilCharByPath(filepaths, 'x', 1)
|
||||
gtest.Assert(indexs, -1)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -256,7 +313,6 @@ func TestHome(t *testing.T) {
|
||||
)
|
||||
|
||||
reads, err = Home()
|
||||
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertNE(reads, "")
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
func TestSearch(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/dirfiles"
|
||||
paths1 string = "/testfiless"
|
||||
paths2 string = "./testfile/dirfiles_no"
|
||||
tpath string
|
||||
tpath2 string
|
||||
@ -18,6 +18,9 @@ func TestSearch(t *testing.T) {
|
||||
err error
|
||||
)
|
||||
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
tpath, err = Search(paths1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
package gfile
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -13,10 +11,16 @@ import (
|
||||
func TestIsDir(t *testing.T) {
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(IsDir("./testfile"), true)
|
||||
paths:="/testfile"
|
||||
CreateDir(paths)
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
gtest.Assert(IsDir(paths), true)
|
||||
gtest.Assert(IsDir("./testfile2"), false)
|
||||
gtest.Assert(IsDir("./testfile/tt.txt"), false)
|
||||
gtest.Assert(IsDir(""), false)
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@ -24,22 +28,24 @@ func TestIsDir(t *testing.T) {
|
||||
func TestCreate(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
err error
|
||||
filepaths []string
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
filepaths = append(filepaths, "./testfile/createfile/c1.txt")
|
||||
filepaths = append(filepaths, "./testfile/file1/c2.txt")
|
||||
filepaths = append(filepaths, os.TempDir()+"/testfile_cc1.txt")
|
||||
filepaths = append(filepaths, os.TempDir()+"/testfile_cc2.txt")
|
||||
|
||||
|
||||
for _, v := range filepaths {
|
||||
_, err = Create(v)
|
||||
fileobj, err = Create(v)
|
||||
defer DelTestFiles(v)
|
||||
fileobj.Close()
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
}
|
||||
stname := gconv.String(gtime.Now().Second())
|
||||
|
||||
_, err = Create("./testfile/createfile/c" + stname + ".txt")
|
||||
gtest.Assert(err, nil)
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
@ -51,17 +57,23 @@ func TestOpen(t *testing.T) {
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/file1/nc1.txt")
|
||||
flags = append(flags, false)
|
||||
file1:="/testfile_nc1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
|
||||
files = append(files, "./testfile/file1/c1.txt")
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
for k, v := range files {
|
||||
_, err = Open(v)
|
||||
files = append(files, "./testfile/file1/c1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
fileobj, err = Open(v)
|
||||
fileobj.Close()
|
||||
if flags[k] {
|
||||
gtest.Assert(err, nil)
|
||||
} else {
|
||||
@ -70,6 +82,8 @@ func TestOpen(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -79,22 +93,30 @@ func TestOpenFile(t *testing.T) {
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/file1/nc1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
files = append(files, "./testfile/tt.txt")
|
||||
f1:="/testfile_tt.txt"
|
||||
CreateTestFile(f1,"")
|
||||
defer DelTestFiles(f1)
|
||||
|
||||
|
||||
files = append(files, f1)
|
||||
flags = append(flags, true)
|
||||
|
||||
for k, v := range files {
|
||||
_, err = OpenFile(v, os.O_RDWR, 0666)
|
||||
fileobj, err = OpenFile(v, os.O_RDWR, 0666)
|
||||
fileobj.Close()
|
||||
if flags[k] {
|
||||
gtest.Assert(err, nil)
|
||||
} else {
|
||||
gtest.AssertNE(err, nil)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
@ -106,16 +128,20 @@ func TestOpenWithFlag(t *testing.T) {
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/dirfiles/t1.txt")
|
||||
file1:="/testfile_t1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
files = append(files, "./testfile/dirfiles/t1_no.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
_, err = OpenWithFlag(v, os.O_RDWR)
|
||||
fileobj, err = OpenWithFlag(v, os.O_RDWR)
|
||||
fileobj.Close()
|
||||
if flags[k] {
|
||||
gtest.Assert(err, nil)
|
||||
} else {
|
||||
@ -124,6 +150,8 @@ func TestOpenWithFlag(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -133,16 +161,20 @@ func TestOpenWithFlagPerm(t *testing.T) {
|
||||
err error
|
||||
files []string
|
||||
flags []bool
|
||||
fileobj *os.File
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/file1/nc1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
files = append(files, "./testfile/tt.txt")
|
||||
file1:="/testfile_nc1.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
files = append(files, "./testfile/tt.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
_, err = OpenWithFlagPerm(v, os.O_RDWR, 666)
|
||||
fileobj, err = OpenWithFlagPerm(v, os.O_RDWR, 666)
|
||||
fileobj.Close()
|
||||
if flags[k] {
|
||||
gtest.Assert(err, nil)
|
||||
} else {
|
||||
@ -151,6 +183,8 @@ func TestOpenWithFlagPerm(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -163,7 +197,11 @@ func TestExists(t *testing.T) {
|
||||
flags []bool
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/havefile1/GetContents.txt")
|
||||
file1:="/testfile_GetContents.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
files = append(files, "./testfile/havefile1/tt_no.txt")
|
||||
@ -179,6 +217,8 @@ func TestExists(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -199,13 +239,19 @@ func TestIsFile(t *testing.T) {
|
||||
flags []bool
|
||||
)
|
||||
|
||||
files = append(files, "./testfile/havefile1/nc1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
files = append(files, "./testfile/havefile1/GetContents.txt")
|
||||
file1:="/testfile_tt.txt"
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
files = append(files, file1)
|
||||
flags = append(flags, true)
|
||||
|
||||
files = append(files, "./testfile")
|
||||
dir1:="/testfiless"
|
||||
CreateDir(dir1)
|
||||
defer DelTestFiles(dir1)
|
||||
files = append(files, dir1)
|
||||
flags = append(flags, false)
|
||||
|
||||
files = append(files, "./testfiledd/tt1.txt")
|
||||
flags = append(flags, false)
|
||||
|
||||
for k, v := range files {
|
||||
@ -225,11 +271,13 @@ func TestInfo(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
err error
|
||||
paths string = "./testfile/tt.txt"
|
||||
paths string = "/testfile_t1.txt"
|
||||
files os.FileInfo
|
||||
files2 os.FileInfo
|
||||
)
|
||||
|
||||
CreateTestFile(paths,"")
|
||||
defer DelTestFiles(paths)
|
||||
files, err = Info(paths)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
@ -238,6 +286,8 @@ func TestInfo(t *testing.T) {
|
||||
|
||||
gtest.Assert(files, files2)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -276,22 +326,31 @@ func TestInfo(t *testing.T) {
|
||||
func TestCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "./testfile/havefile1/copyfile1.txt"
|
||||
topath string = "./testfile/havefile1/copyfile2.txt"
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = os.TempDir()+"/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
CreateTestFile(paths,"")
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
gtest.Assert(Copy(paths, topath), nil)
|
||||
defer DelTestFiles(topath)
|
||||
|
||||
gtest.Assert(IsFile(topath), true)
|
||||
|
||||
gtest.AssertNE(Copy("", ""), nil)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestDirNames(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "./testfile/dirfiles"
|
||||
paths string = "/testdirs"
|
||||
err error
|
||||
readlist []string
|
||||
)
|
||||
@ -299,7 +358,17 @@ func TestDirNames(t *testing.T) {
|
||||
"t1.txt",
|
||||
"t2.txt",
|
||||
}
|
||||
readlist, err = DirNames(paths)
|
||||
|
||||
//=================创建测试文件
|
||||
CreateDir(paths)
|
||||
for _,v:=range havelist{
|
||||
CreateTestFile(paths+"/"+v,"")
|
||||
}
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
|
||||
|
||||
readlist, err = DirNames(os.TempDir()+paths)
|
||||
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(havelist, readlist)
|
||||
@ -307,13 +376,18 @@ func TestDirNames(t *testing.T) {
|
||||
_, err = DirNames("")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGlob(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "./testfile/dirfiles/*.txt"
|
||||
paths string = "/testfiles/*.txt"
|
||||
dirpath string="/testfiles"
|
||||
err error
|
||||
resultlist []string
|
||||
)
|
||||
@ -324,10 +398,19 @@ func TestGlob(t *testing.T) {
|
||||
}
|
||||
|
||||
havelist2 := []string{
|
||||
"testfile/dirfiles/t1.txt",
|
||||
"testfile/dirfiles/t2.txt",
|
||||
"testfiles/t1.txt",
|
||||
"testfiles/t2.txt",
|
||||
}
|
||||
|
||||
//===============================构建测试文件
|
||||
CreateDir(dirpath)
|
||||
for _,v:=range havelist1{
|
||||
CreateTestFile(dirpath+"/"+v,"")
|
||||
}
|
||||
defer DelTestFiles(dirpath)
|
||||
|
||||
|
||||
|
||||
resultlist, err = Glob(paths, true)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(resultlist, havelist1)
|
||||
@ -344,8 +427,7 @@ func TestGlob(t *testing.T) {
|
||||
_, err = Glob("", true)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
_, err = Glob("", false)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
@ -353,10 +435,14 @@ func TestGlob(t *testing.T) {
|
||||
func TestRemove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "./testfile/delfile/t1.txt"
|
||||
paths string = "/testfile_t1.txt"
|
||||
)
|
||||
CreateTestFile(paths,"")
|
||||
gtest.Assert(Remove(os.TempDir()+paths), nil)
|
||||
|
||||
gtest.Assert(Remove(paths), nil)
|
||||
gtest.Assert(Remove(""), nil)
|
||||
|
||||
defer DelTestFiles(paths)
|
||||
|
||||
})
|
||||
}
|
||||
@ -364,35 +450,47 @@ func TestRemove(t *testing.T) {
|
||||
func TestIsReadable(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1/GetContents.txt"
|
||||
paths2 string = "./testfile/havefile1/GetContents_no.txt"
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
gtest.Assert(IsReadable(paths1), true)
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(IsReadable(os.TempDir()+paths1), true)
|
||||
gtest.Assert(IsReadable(paths2), false)
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsWritable(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1/GetContents.txt"
|
||||
paths2 string = "./testfile/havefile1/GetContents_no.txt"
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
gtest.Assert(IsWritable(paths1), true)
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
defer DelTestFiles(paths1)
|
||||
gtest.Assert(IsWritable(os.TempDir()+paths1), true)
|
||||
gtest.Assert(IsWritable(paths2), false)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestChmod(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1/GetContents.txt"
|
||||
paths2 string = "./testfile/havefile1/GetContents_no.txt"
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
paths2 string = "./testfile_GetContents_no.txt"
|
||||
)
|
||||
CreateTestFile(paths1,"")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(Chmod(paths1, 0777), nil)
|
||||
gtest.Assert(Chmod(os.TempDir()+paths1, 0777), nil)
|
||||
gtest.AssertNE(Chmod(paths2, 0777), nil)
|
||||
|
||||
})
|
||||
@ -401,15 +499,22 @@ func TestChmod(t *testing.T) {
|
||||
func TestScanDir(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/dirfiles"
|
||||
paths1 string = "/testfiledirs"
|
||||
files []string
|
||||
err error
|
||||
)
|
||||
files, err = ScanDir(paths1, "t*")
|
||||
|
||||
CreateDir(paths1)
|
||||
CreateTestFile(paths1+"/t1.txt","")
|
||||
CreateTestFile(paths1+"/t2.txt","")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
|
||||
files, err = ScanDir(os.TempDir()+paths1, "t*")
|
||||
|
||||
result := []string{
|
||||
"./testfile/dirfiles/t1.txt",
|
||||
"./testfile/dirfiles/t2.txt",
|
||||
paths1+"/t1.txt",
|
||||
paths1+"/t2.txt",
|
||||
}
|
||||
|
||||
gtest.Assert(err, nil)
|
||||
@ -423,6 +528,9 @@ func TestScanDir(t *testing.T) {
|
||||
_, err = ScanDir("", "t*")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -430,12 +538,16 @@ func TestScanDir(t *testing.T) {
|
||||
func TestRealPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/dirfiles"
|
||||
paths1 string = "/testfile_files"
|
||||
readlPath string
|
||||
|
||||
tempstr string
|
||||
)
|
||||
readlPath = RealPath(paths1)
|
||||
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
readlPath = RealPath(os.TempDir()+paths1)
|
||||
readlPath = filepath.ToSlash(readlPath)
|
||||
|
||||
tempstr, _ = filepath.Abs("./")
|
||||
@ -447,6 +559,8 @@ func TestRealPath(t *testing.T) {
|
||||
|
||||
gtest.Assert(RealPath("./nodirs"), "")
|
||||
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -492,11 +606,17 @@ func TestSelfDir(t *testing.T) {
|
||||
func TestBasename(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1/GetContents.txt"
|
||||
paths1 string = "/testfilerr_GetContents.txt"
|
||||
readlPath string
|
||||
)
|
||||
readlPath = Basename(paths1)
|
||||
gtest.Assert(readlPath, "GetContents.txt")
|
||||
|
||||
|
||||
CreateTestFile(paths1,"")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
|
||||
readlPath = Basename(os.TempDir()+paths1)
|
||||
gtest.Assert(readlPath, "testfilerr_GetContents.txt")
|
||||
|
||||
})
|
||||
}
|
||||
@ -504,12 +624,15 @@ func TestBasename(t *testing.T) {
|
||||
func TestDir(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1"
|
||||
paths1 string = "/testfiless"
|
||||
readlPath string
|
||||
)
|
||||
readlPath = Dir(paths1)
|
||||
CreateDir(paths1)
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(readlPath, "testfile")
|
||||
readlPath = Dir(os.TempDir()+paths1)
|
||||
|
||||
gtest.Assert(readlPath, "testfiless")
|
||||
|
||||
})
|
||||
}
|
||||
@ -518,10 +641,17 @@ func TestDir(t *testing.T) {
|
||||
func TestExt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths1 string = "./testfile/havefile1/GetContents.txt"
|
||||
paths1 string = "/testfile_GetContents.txt"
|
||||
dirpath1 ="/testdirs"
|
||||
)
|
||||
CreateTestFile(paths1,"")
|
||||
defer DelTestFiles(paths1)
|
||||
|
||||
gtest.Assert(Ext(paths1), ".txt")
|
||||
CreateDir(dirpath1)
|
||||
defer DelTestFiles(dirpath1)
|
||||
|
||||
gtest.Assert(Ext(os.TempDir()+paths1), ".txt")
|
||||
gtest.Assert(Ext(os.TempDir()+dirpath1), "")
|
||||
|
||||
})
|
||||
}
|
||||
@ -541,17 +671,19 @@ func TestTempDir(t *testing.T) {
|
||||
func TestMkdir(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
tpath string = "./testfile/createdir"
|
||||
tpath string = "/testfile/createdir"
|
||||
err error
|
||||
)
|
||||
|
||||
err = Mkdir(tpath)
|
||||
defer DelTestFiles(tpath)
|
||||
|
||||
err = Mkdir(os.TempDir()+tpath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = Mkdir("")
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
err = Mkdir(tpath + "2/t1")
|
||||
err = Mkdir(os.TempDir()+tpath + "2/t1")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
})
|
||||
@ -560,12 +692,15 @@ func TestMkdir(t *testing.T) {
|
||||
func TestStat(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
tpath1 string = "./testfile/dirfiles/t1.txt"
|
||||
tpath2 string = "./testfile/dirfiles/t1_no.txt"
|
||||
tpath1 string = "/testfile_t1.txt"
|
||||
tpath2 string = "./testfile_t1_no.txt"
|
||||
err error
|
||||
)
|
||||
|
||||
_, err = Stat(tpath1)
|
||||
CreateTestFile(tpath1,"")
|
||||
defer DelTestFiles(tpath1)
|
||||
|
||||
_, err = Stat(os.TempDir()+tpath1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
_, err = Stat(tpath2)
|
||||
@ -577,9 +712,7 @@ func TestStat(t *testing.T) {
|
||||
func TestMainPkgPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
//tpath1 string ="./testfile/dirfiles/t1.txt"
|
||||
reads string
|
||||
//err error
|
||||
)
|
||||
|
||||
reads = MainPkgPath()
|
||||
|
||||
@ -3,21 +3,47 @@ package gfile
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMTime(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
//拷贝到其它地方,再测试的时候,这个文件的修改值会变,所以用大于来断言
|
||||
gtest.AssertGT(MTime("./testfile/dirfiles/t1.txt"), 1454883732)
|
||||
|
||||
var (
|
||||
file1 string ="/testfile_t1.txt"
|
||||
err error
|
||||
fileobj os.FileInfo
|
||||
)
|
||||
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
fileobj, err = os.Stat(os.TempDir()+file1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
gtest.AssertGT(MTime(os.TempDir()+file1), fileobj.ModTime().Unix())
|
||||
gtest.Assert(MTime(""), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMTimeMillisecond(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
file1 string ="/testfile_t1.txt"
|
||||
err error
|
||||
fileobj os.FileInfo
|
||||
)
|
||||
|
||||
CreateTestFile(file1,"")
|
||||
defer DelTestFiles(file1)
|
||||
fileobj, err = os.Stat(os.TempDir()+file1)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
|
||||
|
||||
//这里本不为0,但github中的ci测试时,值为0
|
||||
gtest.AssertGTE(MTimeMillisecond("./testfile/dirfiles/t1.txt"), 0)
|
||||
gtest.AssertGTE(MTimeMillisecond(os.TempDir()+file1),fileobj.ModTime().Nanosecond()/1000000)
|
||||
gtest.Assert(MTimeMillisecond(""), 0)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
my name is jroam
|
||||
Binary file not shown.
@ -1 +0,0 @@
|
||||
test!!word
|
||||
@ -1 +0,0 @@
|
||||
21
|
||||
@ -1 +0,0 @@
|
||||
2d
|
||||
@ -1 +0,0 @@
|
||||
21
|
||||
@ -1 +0,0 @@
|
||||
2s
|
||||
@ -1,13 +0,0 @@
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵIJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>:
|
||||
|
||||
delfile
|
||||
t1.txt
|
||||
|
||||
havefile1
|
||||
copyfile1.txt
|
||||
ttn1.txt
|
||||
ttm1.txt
|
||||
|
||||
|
||||
dirfiles
|
||||
*
|
||||
Reference in New Issue
Block a user