mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
add copyfile & copydir test
This commit is contained in:
@ -11,10 +11,6 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -25,6 +21,11 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -553,4 +554,4 @@ func CopyDir(src string, dst string) (err error) {
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
package gfile_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func TestIsDir(t *testing.T) {
|
||||
@ -677,3 +678,71 @@ func TestMainPkgPath(t *testing.T) {
|
||||
gtest.Assert(reads, "")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = "/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
createTestFile(paths, "")
|
||||
defer delTestFiles(paths)
|
||||
|
||||
gtest.Assert(gfile.CopyFile(testpath()+paths, testpath()+topath), nil)
|
||||
defer delTestFiles(topath)
|
||||
|
||||
gtest.Assert(gfile.IsFile(testpath()+topath), true)
|
||||
gtest.AssertNE(gfile.CopyFile("", ""), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCopyDir(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
dirpath1 string = "/testcopydir1"
|
||||
dirpath2 string = "/testcopydir2"
|
||||
)
|
||||
|
||||
havelist1 := []string{
|
||||
"t1.txt",
|
||||
"t2.txt",
|
||||
}
|
||||
|
||||
createDir(dirpath1)
|
||||
for _, v := range havelist1 {
|
||||
createTestFile(dirpath1+"/"+v, "")
|
||||
}
|
||||
defer delTestFiles(dirpath1)
|
||||
|
||||
yfolder := testpath() + dirpath1
|
||||
tofolder := testpath() + dirpath2
|
||||
|
||||
if gfile.IsDir(tofolder) {
|
||||
gtest.Assert(gfile.Remove(tofolder), nil)
|
||||
gtest.Assert(gfile.Remove(""), nil)
|
||||
}
|
||||
|
||||
gtest.Assert(gfile.CopyDir(yfolder, tofolder), nil)
|
||||
defer delTestFiles(tofolder)
|
||||
|
||||
// 检查复制后的旧文件夹是否真实存在
|
||||
gtest.Assert(gfile.IsDir(yfolder), true)
|
||||
|
||||
// 检查复制后的旧文件夹中的文件是否真实存在
|
||||
for _, v := range havelist1 {
|
||||
gtest.Assert(gfile.IsFile(yfolder+"/"+v), true)
|
||||
}
|
||||
|
||||
// 检查复制后的新文件夹是否真实存在
|
||||
gtest.Assert(gfile.IsDir(tofolder), true)
|
||||
|
||||
// 检查复制后的新文件夹中的文件是否真实存在
|
||||
for _, v := range havelist1 {
|
||||
gtest.Assert(gfile.IsFile(tofolder+"/"+v), true)
|
||||
}
|
||||
|
||||
gtest.Assert(gfile.Remove(tofolder), nil)
|
||||
gtest.Assert(gfile.Remove(""), nil)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user