add copyfile & copydir test

This commit is contained in:
skiy
2019-07-08 11:37:02 +08:00
parent 8cfdc8f27f
commit 7c4431ceeb
3 changed files with 79 additions and 7 deletions

View File

@ -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
}
}

View File

@ -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)
})
}

2
go.mod
View File

@ -1 +1,3 @@
module github.com/gogf/gf
go 1.12