github.com/gogf/gf@v1.16.9/os/gfile/gfile_z_copy_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gfile_test 8 9 import ( 10 "github.com/gogf/gf/os/gfile" 11 "github.com/gogf/gf/os/gtime" 12 "github.com/gogf/gf/test/gtest" 13 "testing" 14 ) 15 16 func Test_Copy(t *testing.T) { 17 gtest.C(t, func(t *gtest.T) { 18 var ( 19 paths = "/testfile_copyfile1.txt" 20 topath = "/testfile_copyfile2.txt" 21 ) 22 23 createTestFile(paths, "") 24 defer delTestFiles(paths) 25 26 t.Assert(gfile.Copy(testpath()+paths, testpath()+topath), nil) 27 defer delTestFiles(topath) 28 29 t.Assert(gfile.IsFile(testpath()+topath), true) 30 t.AssertNE(gfile.Copy("", ""), nil) 31 }) 32 } 33 34 func Test_CopyFile(t *testing.T) { 35 gtest.C(t, func(t *gtest.T) { 36 var ( 37 paths = "/testfile_copyfile1.txt" 38 topath = "/testfile_copyfile2.txt" 39 ) 40 41 createTestFile(paths, "") 42 defer delTestFiles(paths) 43 44 t.Assert(gfile.CopyFile(testpath()+paths, testpath()+topath), nil) 45 defer delTestFiles(topath) 46 47 t.Assert(gfile.IsFile(testpath()+topath), true) 48 t.AssertNE(gfile.CopyFile("", ""), nil) 49 }) 50 // Content replacement. 51 gtest.C(t, func(t *gtest.T) { 52 src := gfile.TempDir(gtime.TimestampNanoStr()) 53 dst := gfile.TempDir(gtime.TimestampNanoStr()) 54 srcContent := "1" 55 dstContent := "1" 56 t.Assert(gfile.PutContents(src, srcContent), nil) 57 t.Assert(gfile.PutContents(dst, dstContent), nil) 58 t.Assert(gfile.GetContents(src), srcContent) 59 t.Assert(gfile.GetContents(dst), dstContent) 60 61 t.Assert(gfile.CopyFile(src, dst), nil) 62 t.Assert(gfile.GetContents(src), srcContent) 63 t.Assert(gfile.GetContents(dst), srcContent) 64 }) 65 } 66 67 func Test_CopyDir(t *testing.T) { 68 gtest.C(t, func(t *gtest.T) { 69 var ( 70 dirPath1 = "/test-copy-dir1" 71 dirPath2 = "/test-copy-dir2" 72 ) 73 haveList := []string{ 74 "t1.txt", 75 "t2.txt", 76 } 77 createDir(dirPath1) 78 for _, v := range haveList { 79 t.Assert(createTestFile(dirPath1+"/"+v, ""), nil) 80 } 81 defer delTestFiles(dirPath1) 82 83 var ( 84 yfolder = testpath() + dirPath1 85 tofolder = testpath() + dirPath2 86 ) 87 88 if gfile.IsDir(tofolder) { 89 t.Assert(gfile.Remove(tofolder), nil) 90 t.Assert(gfile.Remove(""), nil) 91 } 92 93 t.Assert(gfile.CopyDir(yfolder, tofolder), nil) 94 defer delTestFiles(tofolder) 95 96 t.Assert(gfile.IsDir(yfolder), true) 97 98 for _, v := range haveList { 99 t.Assert(gfile.IsFile(yfolder+"/"+v), true) 100 } 101 102 t.Assert(gfile.IsDir(tofolder), true) 103 104 for _, v := range haveList { 105 t.Assert(gfile.IsFile(tofolder+"/"+v), true) 106 } 107 108 t.Assert(gfile.Remove(tofolder), nil) 109 t.Assert(gfile.Remove(""), nil) 110 }) 111 // Content replacement. 112 gtest.C(t, func(t *gtest.T) { 113 src := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr()) 114 dst := gfile.TempDir(gtime.TimestampNanoStr(), gtime.TimestampNanoStr()) 115 defer func() { 116 gfile.Remove(src) 117 gfile.Remove(dst) 118 }() 119 srcContent := "1" 120 dstContent := "1" 121 t.Assert(gfile.PutContents(src, srcContent), nil) 122 t.Assert(gfile.PutContents(dst, dstContent), nil) 123 t.Assert(gfile.GetContents(src), srcContent) 124 t.Assert(gfile.GetContents(dst), dstContent) 125 126 err := gfile.CopyDir(gfile.Dir(src), gfile.Dir(dst)) 127 t.Assert(err, nil) 128 t.Assert(gfile.GetContents(src), srcContent) 129 t.Assert(gfile.GetContents(dst), srcContent) 130 }) 131 }