mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add Timestamp*Str functions for gtime; improve unit testing cases for gfile
This commit is contained in:
@ -32,8 +32,8 @@ func Copy(src string, dst string) error {
|
||||
return CopyDir(src, dst)
|
||||
}
|
||||
|
||||
// CopyFile copies the contents of the file named src to the file named
|
||||
// by dst. The file will be created if it does not already exist. If the
|
||||
// CopyFile copies the contents of the file named <src> to the file named
|
||||
// by <dst>. The file will be created if it does not exist. If the
|
||||
// destination file exists, all it's contents will be replaced by the contents
|
||||
// of the source file. The file mode will be copied from the source and
|
||||
// the copied data is synced/flushed to stable storage.
|
||||
@ -84,7 +84,7 @@ func CopyFile(src, dst string) (err error) {
|
||||
|
||||
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
|
||||
//
|
||||
// Note that, the sSource directory must exist and symlinks are ignored and skipped.
|
||||
// Note that, the Source directory must exist and symlinks are ignored and skipped.
|
||||
func CopyDir(src string, dst string) (err error) {
|
||||
if src == "" {
|
||||
return errors.New("source directory cannot be empty")
|
||||
|
||||
132
os/gfile/gfile_z_copy_test.go
Normal file
132
os/gfile/gfile_z_copy_test.go
Normal file
@ -0,0 +1,132 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
paths string = "/testfile_copyfile1.txt"
|
||||
topath string = "/testfile_copyfile2.txt"
|
||||
)
|
||||
|
||||
createTestFile(paths, "")
|
||||
defer delTestFiles(paths)
|
||||
|
||||
gtest.Assert(gfile.Copy(testpath()+paths, testpath()+topath), nil)
|
||||
defer delTestFiles(topath)
|
||||
|
||||
gtest.Assert(gfile.IsFile(testpath()+topath), true)
|
||||
gtest.AssertNE(gfile.Copy("", ""), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_CopyFile(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)
|
||||
})
|
||||
// Content replacement.
|
||||
gtest.Case(t, func() {
|
||||
src := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
dst := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
srcContent := "1"
|
||||
dstContent := "1"
|
||||
gfile.PutContents(src, srcContent)
|
||||
gfile.PutContents(dst, dstContent)
|
||||
gtest.Assert(gfile.GetContents(src), srcContent)
|
||||
gtest.Assert(gfile.GetContents(dst), dstContent)
|
||||
|
||||
err := gfile.CopyFile(src, dst)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.GetContents(src), srcContent)
|
||||
gtest.Assert(gfile.GetContents(dst), srcContent)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_CopyDir(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)
|
||||
})
|
||||
// Content replacement.
|
||||
gtest.Case(t, func() {
|
||||
src := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
||||
dst := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), gtime.TimestampNanoStr())
|
||||
srcContent := "1"
|
||||
dstContent := "1"
|
||||
gfile.PutContents(src, srcContent)
|
||||
gfile.PutContents(dst, dstContent)
|
||||
gtest.Assert(gfile.GetContents(src), srcContent)
|
||||
gtest.Assert(gfile.GetContents(dst), dstContent)
|
||||
|
||||
err := gfile.CopyDir(gfile.Dir(src), gfile.Dir(dst))
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.GetContents(src), srcContent)
|
||||
gtest.Assert(gfile.GetContents(dst), srcContent)
|
||||
})
|
||||
}
|
||||
@ -350,24 +350,6 @@ func Test_Rename(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func Test_Copy(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.Copy(testpath()+paths, testpath()+topath), nil)
|
||||
defer delTestFiles(topath)
|
||||
|
||||
gtest.Assert(gfile.IsFile(testpath()+topath), true)
|
||||
gtest.AssertNE(gfile.Copy("", ""), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DirNames(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var (
|
||||
@ -682,71 +664,3 @@ func Test_MainPkgPath(t *testing.T) {
|
||||
gtest.Assert(reads, "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_CopyFile(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 Test_CopyDir(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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -104,26 +104,50 @@ func SetTimeZone(zone string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Timestamp returns the timestamp in seconds.
|
||||
// Timestamp retrieves and returns the timestamp in seconds.
|
||||
func Timestamp() int64 {
|
||||
return Now().Timestamp()
|
||||
}
|
||||
|
||||
// TimestampMilli returns the timestamp in milliseconds.
|
||||
// TimestampMilli retrieves and returns the timestamp in milliseconds.
|
||||
func TimestampMilli() int64 {
|
||||
return Now().TimestampMilli()
|
||||
}
|
||||
|
||||
// TimestampMicro returns the timestamp in microseconds.
|
||||
// TimestampMicro retrieves and returns the timestamp in microseconds.
|
||||
func TimestampMicro() int64 {
|
||||
return Now().TimestampMicro()
|
||||
}
|
||||
|
||||
// TimestampNano returns the timestamp in nanoseconds.
|
||||
// TimestampNano retrieves and returns the timestamp in nanoseconds.
|
||||
func TimestampNano() int64 {
|
||||
return Now().TimestampNano()
|
||||
}
|
||||
|
||||
// TimestampStr is a convenience method which retrieves and returns
|
||||
// the timestamp in seconds as string.
|
||||
func TimestampStr() string {
|
||||
return Now().TimestampStr()
|
||||
}
|
||||
|
||||
// TimestampMilliStr is a convenience method which retrieves and returns
|
||||
// the timestamp in milliseconds as string.
|
||||
func TimestampMilliStr() string {
|
||||
return Now().TimestampMilliStr()
|
||||
}
|
||||
|
||||
// TimestampMicroStr is a convenience method which retrieves and returns
|
||||
// the timestamp in microseconds as string.
|
||||
func TimestampMicroStr() string {
|
||||
return Now().TimestampMicroStr()
|
||||
}
|
||||
|
||||
// TimestampNanoStr is a convenience method which retrieves and returns
|
||||
// the timestamp in nanoseconds as string.
|
||||
func TimestampNanoStr() string {
|
||||
return Now().TimestampNanoStr()
|
||||
}
|
||||
|
||||
// Second returns the timestamp in seconds.
|
||||
// Deprecated, use Timestamp instead.
|
||||
func Second() int64 {
|
||||
|
||||
@ -8,6 +8,7 @@ package gtime
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -98,6 +99,30 @@ func (t *Time) TimestampNano() int64 {
|
||||
return t.UnixNano()
|
||||
}
|
||||
|
||||
// TimestampStr is a convenience method which retrieves and returns
|
||||
// the timestamp in seconds as string.
|
||||
func (t *Time) TimestampStr() string {
|
||||
return strconv.FormatInt(t.Timestamp(), 10)
|
||||
}
|
||||
|
||||
// TimestampMilliStr is a convenience method which retrieves and returns
|
||||
// the timestamp in milliseconds as string.
|
||||
func (t *Time) TimestampMilliStr() string {
|
||||
return strconv.FormatInt(t.TimestampMilli(), 10)
|
||||
}
|
||||
|
||||
// TimestampMicroStr is a convenience method which retrieves and returns
|
||||
// the timestamp in microseconds as string.
|
||||
func (t *Time) TimestampMicroStr() string {
|
||||
return strconv.FormatInt(t.TimestampMicro(), 10)
|
||||
}
|
||||
|
||||
// TimestampNanoStr is a convenience method which retrieves and returns
|
||||
// the timestamp in nanoseconds as string.
|
||||
func (t *Time) TimestampNanoStr() string {
|
||||
return strconv.FormatInt(t.TimestampNano(), 10)
|
||||
}
|
||||
|
||||
// Second returns the second offset within the minute specified by t,
|
||||
// in the range [0, 59].
|
||||
func (t *Time) Second() int {
|
||||
|
||||
Reference in New Issue
Block a user