mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve package gfile
This commit is contained in:
@ -2,11 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := `/Users/john/Workspace/Go/GOPATH/pkg/mod/github.com/nats-io/nats-server/v2@v2.1.4`
|
||||
d := `/Users/john/Workspace/Go/GOPATH/src/github.com/nats-io/nats-server/v2`
|
||||
fmt.Println(gfile.Copy(s, d))
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println(gtime.TimestampNanoStr())
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,19 +134,22 @@ func Exists(path string) bool {
|
||||
}
|
||||
|
||||
// IsDir checks whether given <path> a directory.
|
||||
// Note that it returns false if the <path> does not exist.
|
||||
func IsDir(path string) bool {
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return false
|
||||
}
|
||||
return s.IsDir()
|
||||
}
|
||||
|
||||
// Pwd returns absolute path of current working directory.
|
||||
// Note that it returns an empty string if retrieving current
|
||||
// working directory failed.
|
||||
func Pwd() string {
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
@ -158,10 +161,11 @@ func Chdir(dir string) error {
|
||||
}
|
||||
|
||||
// IsFile checks whether given <path> a file, which means it's not a directory.
|
||||
// Note that it returns false if the <path> does not exist.
|
||||
func IsFile(path string) bool {
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return false
|
||||
}
|
||||
return !s.IsDir()
|
||||
}
|
||||
@ -231,7 +235,6 @@ func Glob(pattern string, onlyNames ...bool) ([]string, error) {
|
||||
// Remove deletes all file/directory with <path> parameter.
|
||||
// If parameter <path> is directory, it deletes it recursively.
|
||||
func Remove(path string) error {
|
||||
//intlog.Print(`Remove:`, path)
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
|
||||
@ -45,6 +45,10 @@ func CopyFile(src, dst string) (err error) {
|
||||
if dst == "" {
|
||||
return errors.New("destination file cannot be empty")
|
||||
}
|
||||
// If src and dst are the same path, it does nothing.
|
||||
if src == dst {
|
||||
return nil
|
||||
}
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return
|
||||
@ -88,6 +92,10 @@ func CopyDir(src string, dst string) (err error) {
|
||||
if dst == "" {
|
||||
return errors.New("destination directory cannot be empty")
|
||||
}
|
||||
// If src and dst are the same path, it does nothing.
|
||||
if src == dst {
|
||||
return nil
|
||||
}
|
||||
src = filepath.Clean(src)
|
||||
dst = filepath.Clean(dst)
|
||||
si, err := os.Stat(src)
|
||||
|
||||
@ -19,25 +19,21 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
// 创建测试文件
|
||||
func createTestFile(filename, content string) error {
|
||||
TempDir := testpath()
|
||||
err := ioutil.WriteFile(TempDir+filename, []byte(content), 0666)
|
||||
return err
|
||||
}
|
||||
|
||||
// 测试完删除文件或目录
|
||||
func delTestFiles(filenames string) {
|
||||
os.RemoveAll(testpath() + filenames)
|
||||
}
|
||||
|
||||
// 创建目录
|
||||
func createDir(paths string) {
|
||||
TempDir := testpath()
|
||||
os.Mkdir(TempDir+paths, 0777)
|
||||
}
|
||||
|
||||
// 统一格式化文件目录为"/"
|
||||
func formatpaths(paths []string) []string {
|
||||
for k, v := range paths {
|
||||
paths[k] = filepath.ToSlash(v)
|
||||
@ -47,14 +43,12 @@ func formatpaths(paths []string) []string {
|
||||
return paths
|
||||
}
|
||||
|
||||
// 统一格式化文件目录为"/"
|
||||
func formatpath(paths string) string {
|
||||
paths = filepath.ToSlash(paths)
|
||||
paths = strings.Replace(paths, "./", "/", 1)
|
||||
return paths
|
||||
}
|
||||
|
||||
// 指定返回要测试的目录
|
||||
func testpath() string {
|
||||
return gstr.TrimRight(os.TempDir(), "\\/")
|
||||
}
|
||||
@ -77,10 +71,10 @@ func Test_GetContents(t *testing.T) {
|
||||
func Test_GetBinContents(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths1 string = "/testfile_t1.txt" // 文件存在时
|
||||
filepaths2 string = testpath() + "/testfile_t1_no.txt" // 文件不存在时
|
||||
filepaths1 = "/testfile_t1.txt"
|
||||
filepaths2 = testpath() + "/testfile_t1_no.txt"
|
||||
readcontent []byte
|
||||
str1 string = "my name is jroam"
|
||||
str1 = "my name is jroam"
|
||||
)
|
||||
createTestFile(filepaths1, str1)
|
||||
defer delTestFiles(filepaths1)
|
||||
@ -95,11 +89,10 @@ func Test_GetBinContents(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// 截断文件为指定的大小
|
||||
func Test_Truncate(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths1 string = "/testfile_GetContentsyyui.txt" //文件存在时
|
||||
filepaths1 = "/testfile_GetContentsyyui.txt"
|
||||
err error
|
||||
files *os.File
|
||||
)
|
||||
@ -108,7 +101,6 @@ func Test_Truncate(t *testing.T) {
|
||||
err = gfile.Truncate(testpath()+filepaths1, 10)
|
||||
t.Assert(err, nil)
|
||||
|
||||
//=========================检查修改文后的大小,是否与期望一致
|
||||
files, err = os.Open(testpath() + filepaths1)
|
||||
defer files.Close()
|
||||
t.Assert(err, nil)
|
||||
@ -116,7 +108,6 @@ func Test_Truncate(t *testing.T) {
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(fileinfo.Size(), 10)
|
||||
|
||||
//====测试当为空时,是否报错
|
||||
err = gfile.Truncate("", 10)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
@ -126,7 +117,7 @@ func Test_Truncate(t *testing.T) {
|
||||
func Test_PutContents(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
filepaths = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
@ -136,7 +127,6 @@ func Test_PutContents(t *testing.T) {
|
||||
err = gfile.PutContents(testpath()+filepaths, "test!")
|
||||
t.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(readcontent), "test!")
|
||||
@ -150,7 +140,7 @@ func Test_PutContents(t *testing.T) {
|
||||
func Test_PutContentsAppend(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
filepaths = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
@ -160,7 +150,6 @@ func Test_PutContentsAppend(t *testing.T) {
|
||||
err = gfile.PutContentsAppend(testpath()+filepaths, "hello")
|
||||
t.Assert(err, nil)
|
||||
|
||||
//==================判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(readcontent), "ahello")
|
||||
@ -175,7 +164,7 @@ func Test_PutContentsAppend(t *testing.T) {
|
||||
func Test_PutBinContents(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_PutContents.txt"
|
||||
filepaths = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
@ -185,7 +174,6 @@ func Test_PutBinContents(t *testing.T) {
|
||||
err = gfile.PutBytes(testpath()+filepaths, []byte("test!!"))
|
||||
t.Assert(err, nil)
|
||||
|
||||
// 判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(readcontent), "test!!")
|
||||
@ -199,7 +187,7 @@ func Test_PutBinContents(t *testing.T) {
|
||||
func Test_PutBinContentsAppend(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_PutContents.txt" //原文件内容: yy
|
||||
filepaths = "/testfile_PutContents.txt"
|
||||
err error
|
||||
readcontent []byte
|
||||
)
|
||||
@ -208,7 +196,6 @@ func Test_PutBinContentsAppend(t *testing.T) {
|
||||
err = gfile.PutBytesAppend(testpath()+filepaths, []byte("word"))
|
||||
t.Assert(err, nil)
|
||||
|
||||
// 判断是否真正写入
|
||||
readcontent, err = ioutil.ReadFile(testpath() + filepaths)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(readcontent), "test!!word")
|
||||
@ -222,7 +209,7 @@ func Test_PutBinContentsAppend(t *testing.T) {
|
||||
func Test_GetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
|
||||
filepaths = "/testfile_GetContents.txt"
|
||||
readcontent []byte
|
||||
)
|
||||
|
||||
@ -242,7 +229,7 @@ func Test_GetBinContentsByTwoOffsetsByPath(t *testing.T) {
|
||||
func Test_GetNextCharOffsetByPath(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
filepaths string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
|
||||
filepaths = "/testfile_GetContents.txt"
|
||||
localindex int64
|
||||
)
|
||||
createTestFile(filepaths, "abcdefghijk")
|
||||
@ -310,7 +297,7 @@ func Test_GetBinContentsTilCharByPath(t *testing.T) {
|
||||
var (
|
||||
reads []byte
|
||||
indexs int64
|
||||
filepaths string = "/testfile_GetContents.txt"
|
||||
filepaths = "/testfile_GetContents.txt"
|
||||
)
|
||||
|
||||
createTestFile(filepaths, "abcdefghijklmn")
|
||||
@ -338,6 +325,5 @@ func Test_Home(t *testing.T) {
|
||||
reads, err = gfile.Home()
|
||||
t.Assert(err, nil)
|
||||
t.AssertNE(reads, "")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,8 +16,8 @@ import (
|
||||
func Test_Copy(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = "/testfile_copyfile2.txt"
|
||||
paths = "/testfile_copyfile1.txt"
|
||||
topath = "/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
createTestFile(paths, "")
|
||||
@ -34,8 +34,8 @@ func Test_Copy(t *testing.T) {
|
||||
func Test_CopyFile(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = "/testfile_copyfile2.txt"
|
||||
paths = "/testfile_copyfile1.txt"
|
||||
topath = "/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
createTestFile(paths, "")
|
||||
@ -53,13 +53,12 @@ func Test_CopyFile(t *testing.T) {
|
||||
dst := gfile.TempDir(gtime.TimestampNanoStr())
|
||||
srcContent := "1"
|
||||
dstContent := "1"
|
||||
gfile.PutContents(src, srcContent)
|
||||
gfile.PutContents(dst, dstContent)
|
||||
t.Assert(gfile.PutContents(src, srcContent), nil)
|
||||
t.Assert(gfile.PutContents(dst, dstContent), nil)
|
||||
t.Assert(gfile.GetContents(src), srcContent)
|
||||
t.Assert(gfile.GetContents(dst), dstContent)
|
||||
|
||||
err := gfile.CopyFile(src, dst)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(gfile.CopyFile(src, dst), nil)
|
||||
t.Assert(gfile.GetContents(src), srcContent)
|
||||
t.Assert(gfile.GetContents(dst), srcContent)
|
||||
})
|
||||
@ -68,23 +67,23 @@ func Test_CopyFile(t *testing.T) {
|
||||
func Test_CopyDir(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
dirpath1 string = "/testcopydir1"
|
||||
dirpath2 string = "/testcopydir2"
|
||||
dirPath1 = "/test-copy-dir1"
|
||||
dirPath2 = "/test-copy-dir2"
|
||||
)
|
||||
|
||||
havelist1 := []string{
|
||||
haveList := []string{
|
||||
"t1.txt",
|
||||
"t2.txt",
|
||||
}
|
||||
|
||||
createDir(dirpath1)
|
||||
for _, v := range havelist1 {
|
||||
createTestFile(dirpath1+"/"+v, "")
|
||||
createDir(dirPath1)
|
||||
for _, v := range haveList {
|
||||
t.Assert(createTestFile(dirPath1+"/"+v, ""), nil)
|
||||
}
|
||||
defer delTestFiles(dirpath1)
|
||||
defer delTestFiles(dirPath1)
|
||||
|
||||
yfolder := testpath() + dirpath1
|
||||
tofolder := testpath() + dirpath2
|
||||
var (
|
||||
yfolder = testpath() + dirPath1
|
||||
tofolder = testpath() + dirPath2
|
||||
)
|
||||
|
||||
if gfile.IsDir(tofolder) {
|
||||
t.Assert(gfile.Remove(tofolder), nil)
|
||||
@ -94,19 +93,15 @@ func Test_CopyDir(t *testing.T) {
|
||||
t.Assert(gfile.CopyDir(yfolder, tofolder), nil)
|
||||
defer delTestFiles(tofolder)
|
||||
|
||||
// 检查复制后的旧文件夹是否真实存在
|
||||
t.Assert(gfile.IsDir(yfolder), true)
|
||||
|
||||
// 检查复制后的旧文件夹中的文件是否真实存在
|
||||
for _, v := range havelist1 {
|
||||
for _, v := range haveList {
|
||||
t.Assert(gfile.IsFile(yfolder+"/"+v), true)
|
||||
}
|
||||
|
||||
// 检查复制后的新文件夹是否真实存在
|
||||
t.Assert(gfile.IsDir(tofolder), true)
|
||||
|
||||
// 检查复制后的新文件夹中的文件是否真实存在
|
||||
for _, v := range havelist1 {
|
||||
for _, v := range haveList {
|
||||
t.Assert(gfile.IsFile(tofolder+"/"+v), true)
|
||||
}
|
||||
|
||||
@ -117,10 +112,14 @@ func Test_CopyDir(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
src := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
||||
dst := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
||||
defer func() {
|
||||
gfile.Remove(src)
|
||||
gfile.Remove(dst)
|
||||
}()
|
||||
srcContent := "1"
|
||||
dstContent := "1"
|
||||
gfile.PutContents(src, srcContent)
|
||||
gfile.PutContents(dst, dstContent)
|
||||
t.Assert(gfile.PutContents(src, srcContent), nil)
|
||||
t.Assert(gfile.PutContents(dst, dstContent), nil)
|
||||
t.Assert(gfile.GetContents(src), srcContent)
|
||||
t.Assert(gfile.GetContents(dst), dstContent)
|
||||
|
||||
|
||||
@ -610,7 +610,11 @@ func Test_ExtName(t *testing.T) {
|
||||
|
||||
func Test_TempDir(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gfile.TempDir(), "/tmp")
|
||||
if gfile.Separator != "/" || !gfile.Exists("/tmp") {
|
||||
t.Assert(gfile.TempDir(), os.TempDir())
|
||||
} else {
|
||||
t.Assert(gfile.TempDir(), "/tmp")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user