2019-04-07 22:46:14 +08:00
|
|
|
|
package gfile
|
|
|
|
|
|
|
|
|
|
import (
|
2019-04-12 09:47:55 +08:00
|
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
2019-04-07 22:46:14 +08:00
|
|
|
|
"os"
|
2019-04-08 23:13:28 +08:00
|
|
|
|
"path/filepath"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
"strings"
|
2019-04-07 22:46:14 +08:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestIsDir(t *testing.T) {
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
2019-04-13 18:27:01 +08:00
|
|
|
|
paths := "/testfile"
|
2019-04-12 18:15:31 +08:00
|
|
|
|
CreateDir(paths)
|
|
|
|
|
defer DelTestFiles(paths)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(IsDir(Testpath()+paths), true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
gtest.Assert(IsDir("./testfile2"), false)
|
|
|
|
|
gtest.Assert(IsDir("./testfile/tt.txt"), false)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
gtest.Assert(IsDir(""), false)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-07 22:46:14 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestCreate(t *testing.T) {
|
2019-04-07 22:46:14 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
err error
|
2019-04-07 22:46:14 +08:00
|
|
|
|
filepaths []string
|
2019-04-13 18:27:01 +08:00
|
|
|
|
fileobj *os.File
|
2019-04-07 22:46:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
filepaths = append(filepaths, "/testfile_cc1.txt")
|
|
|
|
|
filepaths = append(filepaths, "/testfile_cc2.txt")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for _, v := range filepaths {
|
2019-04-15 22:52:19 +08:00
|
|
|
|
fileobj, err = Create(Testpath() + v)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(v)
|
|
|
|
|
fileobj.Close()
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
}
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
err error
|
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj *os.File
|
2019-04-07 22:46:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
file1 := "/testfile_nc1.txt"
|
|
|
|
|
CreateTestFile(file1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(file1)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 18:15:31 +08:00
|
|
|
|
files = append(files, file1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 18:15:31 +08:00
|
|
|
|
files = append(files, "./testfile/file1/c1.txt")
|
|
|
|
|
flags = append(flags, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 18:15:31 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
fileobj, err = Open(Testpath() + v)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj.Close()
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestOpenFile(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
err error
|
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj *os.File
|
2019-04-07 22:46:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
files = append(files, "./testfile/file1/nc1.txt")
|
|
|
|
|
flags = append(flags, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
f1 := "/testfile_tt.txt"
|
|
|
|
|
CreateTestFile(f1, "")
|
|
|
|
|
defer DelTestFiles(f1)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
|
|
|
|
files = append(files, f1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
fileobj, err = OpenFile(Testpath()+v, os.O_RDWR, 0666)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj.Close()
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOpenWithFlag(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
err error
|
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj *os.File
|
2019-04-07 22:46:14 +08:00
|
|
|
|
)
|
2019-04-14 22:41:11 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
file1 := "/testfile_t1.txt"
|
|
|
|
|
CreateTestFile(file1, "")
|
|
|
|
|
defer DelTestFiles(file1)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
files = append(files, file1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, true)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-14 22:41:11 +08:00
|
|
|
|
files = append(files, "/testfiless/dirfiles/t1_no.txt")
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
fileobj, err = OpenWithFlag(Testpath()+v, os.O_RDWR)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj.Close()
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOpenWithFlagPerm(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
err error
|
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj *os.File
|
2019-04-07 22:46:14 +08:00
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
file1 := "/testfile_nc1.txt"
|
|
|
|
|
CreateTestFile(file1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(file1)
|
|
|
|
|
files = append(files, file1)
|
|
|
|
|
flags = append(flags, true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-14 22:41:11 +08:00
|
|
|
|
files = append(files, "/testfileyy/tt.txt")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
flags = append(flags, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
fileobj, err = OpenWithFlagPerm(Testpath()+v, os.O_RDWR, 666)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
fileobj.Close()
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
flag bool
|
2019-04-07 22:46:14 +08:00
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
file1 := "/testfile_GetContents.txt"
|
|
|
|
|
CreateTestFile(file1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(file1)
|
|
|
|
|
|
|
|
|
|
files = append(files, file1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
files = append(files, "./testfile/havefile1/tt_no.txt")
|
|
|
|
|
flags = append(flags, false)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
flag = Exists(Testpath() + v)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(flag, true)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.Assert(flag, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPwd(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
paths, err := os.Getwd()
|
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
gtest.Assert(Pwd(), paths)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsFile(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
flag bool
|
2019-04-07 22:46:14 +08:00
|
|
|
|
files []string
|
|
|
|
|
flags []bool
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
file1 := "/testfile_tt.txt"
|
|
|
|
|
CreateTestFile(file1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(file1)
|
|
|
|
|
files = append(files, file1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, true)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
dir1 := "/testfiless"
|
2019-04-12 18:15:31 +08:00
|
|
|
|
CreateDir(dir1)
|
|
|
|
|
defer DelTestFiles(dir1)
|
|
|
|
|
files = append(files, dir1)
|
|
|
|
|
flags = append(flags, false)
|
|
|
|
|
|
|
|
|
|
files = append(files, "./testfiledd/tt1.txt")
|
2019-04-12 09:47:55 +08:00
|
|
|
|
flags = append(flags, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
for k, v := range files {
|
2019-04-13 23:00:57 +08:00
|
|
|
|
flag = IsFile(Testpath() + v)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
if flags[k] {
|
|
|
|
|
gtest.Assert(flag, true)
|
|
|
|
|
} else {
|
|
|
|
|
gtest.Assert(flag, false)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
err error
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths string = "/testfile_t1.txt"
|
2019-04-12 09:47:55 +08:00
|
|
|
|
files os.FileInfo
|
2019-04-07 22:46:14 +08:00
|
|
|
|
files2 os.FileInfo
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths)
|
2019-04-13 23:00:57 +08:00
|
|
|
|
files, err = Info(Testpath() + paths)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
files2, err = os.Stat(Testpath() + paths)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(files, files2)
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 23:14:48 +08:00
|
|
|
|
func TestMove(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
paths string = "/ovetest"
|
|
|
|
|
filepaths string = "/testfile_ttn1.txt"
|
|
|
|
|
topath string = "/testfile_ttn2.txt"
|
|
|
|
|
)
|
|
|
|
|
CreateDir("/ovetest")
|
|
|
|
|
CreateTestFile(paths+filepaths, "a")
|
|
|
|
|
|
|
|
|
|
defer DelTestFiles(paths)
|
|
|
|
|
|
|
|
|
|
yfile := Testpath() + paths + filepaths
|
|
|
|
|
tofile := Testpath() + paths + topath
|
|
|
|
|
|
|
|
|
|
gtest.Assert(Move(yfile, tofile), nil)
|
|
|
|
|
|
|
|
|
|
//检查移动后的文件是否真实存在
|
|
|
|
|
_, err := os.Stat(tofile)
|
|
|
|
|
gtest.Assert(os.IsNotExist(err), false)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRename(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
paths string = "/testfiles"
|
|
|
|
|
ypath string = "/testfilettm1.txt"
|
|
|
|
|
topath string = "/testfilettm2.txt"
|
|
|
|
|
)
|
|
|
|
|
CreateDir(paths)
|
|
|
|
|
CreateTestFile(paths+ypath, "a")
|
|
|
|
|
defer DelTestFiles(paths)
|
|
|
|
|
|
|
|
|
|
ypath = Testpath() + paths + ypath
|
|
|
|
|
topath = Testpath() + paths + topath
|
|
|
|
|
|
|
|
|
|
gtest.Assert(Rename(ypath, topath), nil)
|
|
|
|
|
gtest.Assert(IsFile(topath), true)
|
|
|
|
|
|
|
|
|
|
gtest.AssertNE(Rename("", ""), nil)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-08 23:13:28 +08:00
|
|
|
|
func TestCopy(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths string = "/testfile_copyfile1.txt"
|
2019-04-15 22:52:19 +08:00
|
|
|
|
topath string = "/testfile_copyfile2.txt"
|
2019-04-08 23:13:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(Copy(Testpath()+paths, Testpath()+topath), nil)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(topath)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(IsFile(Testpath()+topath), true)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.AssertNE(Copy("", ""), nil)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestDirNames(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths string = "/testdirs"
|
2019-04-12 09:47:55 +08:00
|
|
|
|
err error
|
2019-04-08 23:13:28 +08:00
|
|
|
|
readlist []string
|
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
havelist := []string{
|
2019-04-08 23:13:28 +08:00
|
|
|
|
"t1.txt",
|
|
|
|
|
"t2.txt",
|
|
|
|
|
}
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
|
|
|
|
//=================创建测试文件
|
|
|
|
|
CreateDir(paths)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
for _, v := range havelist {
|
|
|
|
|
CreateTestFile(paths+"/"+v, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
}
|
|
|
|
|
defer DelTestFiles(paths)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
readlist, err = DirNames(Testpath() + paths)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
gtest.Assert(havelist, readlist)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
_, err = DirNames("")
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGlob(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths string = "/testfiles/*.txt"
|
2019-04-13 18:27:01 +08:00
|
|
|
|
dirpath string = "/testfiles"
|
2019-04-12 09:47:55 +08:00
|
|
|
|
err error
|
2019-04-08 23:13:28 +08:00
|
|
|
|
resultlist []string
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
havelist1 := []string{
|
2019-04-08 23:13:28 +08:00
|
|
|
|
"t1.txt",
|
|
|
|
|
"t2.txt",
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
havelist2 := []string{
|
2019-04-13 23:00:57 +08:00
|
|
|
|
Testpath() + "/testfiles/t1.txt",
|
|
|
|
|
Testpath() + "/testfiles/t2.txt",
|
2019-04-12 18:15:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===============================构建测试文件
|
|
|
|
|
CreateDir(dirpath)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
for _, v := range havelist1 {
|
|
|
|
|
CreateTestFile(dirpath+"/"+v, "")
|
2019-04-08 23:13:28 +08:00
|
|
|
|
}
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(dirpath)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
resultlist, err = Glob(Testpath()+paths, true)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
gtest.Assert(resultlist, havelist1)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
resultlist, err = Glob(Testpath()+paths, false)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
gtest.Assert(Formatpaths(resultlist), Formatpaths(havelist2))
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
_, err = Glob("", true)
|
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-08 23:13:28 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths string = "/testfile_t1.txt"
|
2019-04-08 23:13:28 +08:00
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths, "")
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(Remove(Testpath()+paths), nil)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
|
|
|
|
gtest.Assert(Remove(""), nil)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths)
|
2019-04-08 23:13:28 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-04-07 22:46:14 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestIsReadable(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfile_GetContents.txt"
|
|
|
|
|
paths2 string = "./testfile_GetContents_no.txt"
|
2019-04-10 13:45:42 +08:00
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(IsReadable(Testpath()+paths1), true)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(IsReadable(paths2), false)
|
2019-04-10 13:45:42 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestIsWritable(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfile_GetContents.txt"
|
|
|
|
|
paths2 string = "./testfile_GetContents_no.txt"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(IsWritable(Testpath()+paths1), true)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(IsWritable(paths2), false)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestChmod(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfile_GetContents.txt"
|
|
|
|
|
paths2 string = "./testfile_GetContents_no.txt"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(Chmod(Testpath()+paths1, 0777), nil)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.AssertNE(Chmod(paths2, 0777), nil)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestScanDir(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfiledirs"
|
2019-04-12 09:47:55 +08:00
|
|
|
|
files []string
|
|
|
|
|
err error
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
|
|
|
|
CreateDir(paths1)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1+"/t1.txt", "")
|
|
|
|
|
CreateTestFile(paths1+"/t2.txt", "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
files, err = ScanDir(Testpath()+paths1, "t*")
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
result := []string{
|
2019-04-13 23:00:57 +08:00
|
|
|
|
Testpath() + paths1 + "/t1.txt",
|
|
|
|
|
Testpath() + paths1 + "/t2.txt",
|
2019-04-10 16:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
gtest.Assert(Formatpaths(files), Formatpaths(result))
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
_, err = ScanDir("", "t*")
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取绝对目录地址
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestRealPath(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfile_files"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
readlPath string
|
|
|
|
|
|
|
|
|
|
tempstr string
|
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
|
|
|
|
CreateDir(paths1)
|
|
|
|
|
defer DelTestFiles(paths1)
|
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
readlPath = RealPath("./")
|
|
|
|
|
//readlPath = filepath.ToSlash(readlPath)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tempstr, _ = filepath.Abs("./")
|
2019-04-13 18:27:01 +08:00
|
|
|
|
//paths1 = tempstr + paths1
|
|
|
|
|
//paths1=Formatpath(tempstr)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
gtest.Assert(readlPath, tempstr)
|
2019-04-10 17:54:28 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(RealPath("./nodirs"), "")
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取当前执行文件的目录
|
|
|
|
|
//注意:当用go test运行测试时,会产生临时的目录文件
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestSelfPath(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
paths1 string
|
2019-04-10 16:22:29 +08:00
|
|
|
|
readlPath string
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tempstr string
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
readlPath = SelfPath()
|
|
|
|
|
readlPath = filepath.ToSlash(readlPath)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
//
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tempstr, _ = filepath.Abs(os.Args[0])
|
|
|
|
|
paths1 = filepath.ToSlash(tempstr)
|
|
|
|
|
paths1 = strings.Replace(paths1, "./", "/", 1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(readlPath, paths1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestSelfDir(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
|
|
|
|
paths1 string
|
2019-04-10 16:22:29 +08:00
|
|
|
|
readlPath string
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tempstr string
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
readlPath = SelfDir()
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tempstr, _ = filepath.Abs(os.Args[0])
|
|
|
|
|
paths1 = filepath.Dir(tempstr)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(readlPath, paths1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestBasename(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfilerr_GetContents.txt"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
readlPath string
|
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
readlPath = Basename(Testpath() + paths1)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
gtest.Assert(readlPath, "testfilerr_GetContents.txt")
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestDir(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
paths1 string = "/testfiless"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
readlPath string
|
|
|
|
|
)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
CreateDir(paths1)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
readlPath = Dir(Testpath() + paths1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(readlPath, Testpath())
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取文件名
|
|
|
|
|
func TestExt(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-13 18:27:01 +08:00
|
|
|
|
paths1 string = "/testfile_GetContents.txt"
|
|
|
|
|
dirpath1 = "/testdirs"
|
2019-04-10 16:22:29 +08:00
|
|
|
|
)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
CreateTestFile(paths1, "")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(paths1)
|
|
|
|
|
|
|
|
|
|
CreateDir(dirpath1)
|
2019-04-13 18:27:01 +08:00
|
|
|
|
defer DelTestFiles(dirpath1)
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
gtest.Assert(Ext(Testpath()+paths1), ".txt")
|
|
|
|
|
gtest.Assert(Ext(Testpath()+dirpath1), "")
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestTempDir(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-10 16:22:29 +08:00
|
|
|
|
tpath string
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
tpath = TempDir()
|
|
|
|
|
gtest.Assert(tpath, os.TempDir())
|
2019-04-10 16:22:29 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 22:16:16 +08:00
|
|
|
|
func TestMkdir(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-12 18:15:31 +08:00
|
|
|
|
tpath string = "/testfile/createdir"
|
2019-04-12 09:47:55 +08:00
|
|
|
|
err error
|
2019-04-10 22:16:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-14 22:41:11 +08:00
|
|
|
|
defer DelTestFiles("/testfile")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
err = Mkdir(Testpath() + tpath)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
err = Mkdir("")
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
2019-04-13 23:00:57 +08:00
|
|
|
|
err = Mkdir(Testpath() + tpath + "2/t1")
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
func TestStat(t *testing.T) {
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-15 22:52:19 +08:00
|
|
|
|
tpath1 string = "/testfile_t1.txt"
|
|
|
|
|
tpath2 string = "./testfile_t1_no.txt"
|
|
|
|
|
err error
|
|
|
|
|
fileiofo os.FileInfo
|
2019-04-10 22:16:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-04-15 22:52:19 +08:00
|
|
|
|
CreateTestFile(tpath1, "a")
|
2019-04-12 18:15:31 +08:00
|
|
|
|
defer DelTestFiles(tpath1)
|
|
|
|
|
|
2019-04-15 22:52:19 +08:00
|
|
|
|
fileiofo, err = Stat(Testpath() + tpath1)
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Assert(err, nil)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
2019-04-15 22:52:19 +08:00
|
|
|
|
gtest.Assert(fileiofo.Size(), 1)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
_, err = Stat(tpath2)
|
|
|
|
|
gtest.AssertNE(err, nil)
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMainPkgPath(t *testing.T) {
|
2019-04-12 09:47:55 +08:00
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
|
var (
|
2019-04-10 22:16:16 +08:00
|
|
|
|
reads string
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-12 09:47:55 +08:00
|
|
|
|
reads = MainPkgPath()
|
|
|
|
|
gtest.Assert(reads, "")
|
2019-04-10 22:16:16 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|