gf/os/gfile/gfile_z_copy_test.go

132 lines
3.3 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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 (
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"testing"
)
func Test_Copy(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
2020-05-16 14:06:35 +08:00
paths = "/testfile_copyfile1.txt"
topath = "/testfile_copyfile2.txt"
)
createTestFile(paths, "")
defer delTestFiles(paths)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Copy(testpath()+paths, testpath()+topath), nil)
defer delTestFiles(topath)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsFile(testpath()+topath), true)
t.AssertNE(gfile.Copy("", ""), nil)
})
}
func Test_CopyFile(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
2020-05-16 14:06:35 +08:00
paths = "/testfile_copyfile1.txt"
topath = "/testfile_copyfile2.txt"
)
createTestFile(paths, "")
defer delTestFiles(paths)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.CopyFile(testpath()+paths, testpath()+topath), nil)
defer delTestFiles(topath)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsFile(testpath()+topath), true)
t.AssertNE(gfile.CopyFile("", ""), nil)
})
// Content replacement.
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
src := gfile.TempDir(gtime.TimestampNanoStr())
dst := gfile.TempDir(gtime.TimestampNanoStr())
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-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)
})
}
func Test_CopyDir(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
2020-05-16 14:06:35 +08:00
dirPath1 = "/test-copy-dir1"
dirPath2 = "/test-copy-dir2"
)
2020-05-16 14:06:35 +08:00
haveList := []string{
"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-05-16 14:06:35 +08:00
defer delTestFiles(dirPath1)
2020-05-16 14:06:35 +08:00
var (
yfolder = testpath() + dirPath1
tofolder = testpath() + dirPath2
)
if gfile.IsDir(tofolder) {
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Remove(tofolder), nil)
t.Assert(gfile.Remove(""), nil)
}
2020-03-19 22:56:12 +08:00
t.Assert(gfile.CopyDir(yfolder, tofolder), nil)
defer delTestFiles(tofolder)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.IsDir(yfolder), true)
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-03-19 22:56:12 +08:00
t.Assert(gfile.IsDir(tofolder), true)
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-03-19 22:56:12 +08:00
t.Assert(gfile.Remove(tofolder), nil)
t.Assert(gfile.Remove(""), nil)
})
// Content replacement.
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
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)
}()
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)
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)
})
}