2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-01-02 21:29:06 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gfile_test
|
|
|
|
|
|
|
|
import (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2020-01-02 21:29:06 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Copy(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-02 21:29:06 +08:00
|
|
|
var (
|
2020-05-16 14:06:35 +08:00
|
|
|
paths = "/testfile_copyfile1.txt"
|
|
|
|
topath = "/testfile_copyfile2.txt"
|
2020-01-02 21:29:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
createTestFile(paths, "")
|
|
|
|
defer delTestFiles(paths)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Copy(testpath()+paths, testpath()+topath), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
defer delTestFiles(topath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsFile(testpath()+topath), true)
|
|
|
|
t.AssertNE(gfile.Copy("", ""), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_CopyFile(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-02 21:29:06 +08:00
|
|
|
var (
|
2020-05-16 14:06:35 +08:00
|
|
|
paths = "/testfile_copyfile1.txt"
|
|
|
|
topath = "/testfile_copyfile2.txt"
|
2020-01-02 21:29:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
createTestFile(paths, "")
|
|
|
|
defer delTestFiles(paths)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.CopyFile(testpath()+paths, testpath()+topath), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
defer delTestFiles(topath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsFile(testpath()+topath), true)
|
|
|
|
t.AssertNE(gfile.CopyFile("", ""), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
})
|
|
|
|
// Content replacement.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-28 00:41:12 +08:00
|
|
|
src := gfile.TempDir(gtime.TimestampNanoStr())
|
|
|
|
dst := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-01-02 21:29:06 +08:00
|
|
|
srcContent := "1"
|
|
|
|
dstContent := "1"
|
2020-05-16 14:06:35 +08:00
|
|
|
t.Assert(gfile.PutContents(src, srcContent), nil)
|
|
|
|
t.Assert(gfile.PutContents(dst, dstContent), nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.GetContents(src), srcContent)
|
|
|
|
t.Assert(gfile.GetContents(dst), dstContent)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
2020-05-16 14:06:35 +08:00
|
|
|
t.Assert(gfile.CopyFile(src, dst), nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.GetContents(src), srcContent)
|
|
|
|
t.Assert(gfile.GetContents(dst), srcContent)
|
2020-01-02 21:29:06 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_CopyDir(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-02 21:29:06 +08:00
|
|
|
var (
|
2020-05-16 14:06:35 +08:00
|
|
|
dirPath1 = "/test-copy-dir1"
|
|
|
|
dirPath2 = "/test-copy-dir2"
|
2020-01-02 21:29:06 +08:00
|
|
|
)
|
2020-05-16 14:06:35 +08:00
|
|
|
haveList := []string{
|
2020-01-02 21:29:06 +08:00
|
|
|
"t1.txt",
|
|
|
|
"t2.txt",
|
|
|
|
}
|
2020-05-16 14:06:35 +08:00
|
|
|
createDir(dirPath1)
|
|
|
|
for _, v := range haveList {
|
|
|
|
t.Assert(createTestFile(dirPath1+"/"+v, ""), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
}
|
2020-05-16 14:06:35 +08:00
|
|
|
defer delTestFiles(dirPath1)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
2020-05-16 14:06:35 +08:00
|
|
|
var (
|
|
|
|
yfolder = testpath() + dirPath1
|
|
|
|
tofolder = testpath() + dirPath2
|
|
|
|
)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
|
|
|
if gfile.IsDir(tofolder) {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Remove(tofolder), nil)
|
|
|
|
t.Assert(gfile.Remove(""), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.CopyDir(yfolder, tofolder), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
defer delTestFiles(tofolder)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsDir(yfolder), true)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
2020-05-16 14:06:35 +08:00
|
|
|
for _, v := range haveList {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsFile(yfolder+"/"+v), true)
|
2020-01-02 21:29:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsDir(tofolder), true)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
2020-05-16 14:06:35 +08:00
|
|
|
for _, v := range haveList {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.IsFile(tofolder+"/"+v), true)
|
2020-01-02 21:29:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Remove(tofolder), nil)
|
|
|
|
t.Assert(gfile.Remove(""), nil)
|
2020-01-02 21:29:06 +08:00
|
|
|
})
|
|
|
|
// Content replacement.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-28 00:41:12 +08:00
|
|
|
src := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
|
|
|
dst := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
2020-05-16 14:06:35 +08:00
|
|
|
defer func() {
|
|
|
|
gfile.Remove(src)
|
|
|
|
gfile.Remove(dst)
|
|
|
|
}()
|
2020-01-02 21:29:06 +08:00
|
|
|
srcContent := "1"
|
|
|
|
dstContent := "1"
|
2020-05-16 14:06:35 +08:00
|
|
|
t.Assert(gfile.PutContents(src, srcContent), nil)
|
|
|
|
t.Assert(gfile.PutContents(dst, dstContent), nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.GetContents(src), srcContent)
|
|
|
|
t.Assert(gfile.GetContents(dst), dstContent)
|
2020-01-02 21:29:06 +08:00
|
|
|
|
|
|
|
err := gfile.CopyDir(gfile.Dir(src), gfile.Dir(dst))
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gfile.GetContents(src), srcContent)
|
|
|
|
t.Assert(gfile.GetContents(dst), srcContent)
|
2020-01-02 21:29:06 +08:00
|
|
|
})
|
|
|
|
}
|