github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_unit_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/wangyougui/gf. 6 7 package gfile_test 8 9 import ( 10 "os" 11 "testing" 12 13 "github.com/wangyougui/gf/v2/os/gfile" 14 "github.com/wangyougui/gf/v2/os/gtime" 15 "github.com/wangyougui/gf/v2/test/gtest" 16 "github.com/wangyougui/gf/v2/util/guid" 17 ) 18 19 func Test_Copy(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 var ( 22 paths = "/testfile_copyfile1.txt" 23 topath = "/testfile_copyfile2.txt" 24 ) 25 26 createTestFile(paths, "") 27 defer delTestFiles(paths) 28 29 t.Assert(gfile.Copy(testpath()+paths, testpath()+topath), nil) 30 defer delTestFiles(topath) 31 32 t.Assert(gfile.IsFile(testpath()+topath), true) 33 t.AssertNE(gfile.Copy(paths, ""), nil) 34 t.AssertNE(gfile.Copy("", topath), nil) 35 }) 36 } 37 38 func Test_Copy_File_To_Dir(t *testing.T) { 39 gtest.C(t, func(t *gtest.T) { 40 var ( 41 src = gtest.DataPath("dir1", "file1") 42 dst = gfile.Temp(guid.S(), "dir2") 43 ) 44 err := gfile.Mkdir(dst) 45 t.AssertNil(err) 46 defer gfile.Remove(dst) 47 48 err = gfile.Copy(src, dst) 49 t.AssertNil(err) 50 51 expectPath := gfile.Join(dst, "file1") 52 t.Assert(gfile.GetContents(expectPath), gfile.GetContents(src)) 53 }) 54 } 55 56 func Test_Copy_Dir_To_File(t *testing.T) { 57 gtest.C(t, func(t *gtest.T) { 58 var ( 59 src = gtest.DataPath("dir1") 60 dst = gfile.Temp(guid.S(), "file2") 61 ) 62 f, err := gfile.Create(dst) 63 t.AssertNil(err) 64 defer f.Close() 65 defer gfile.Remove(dst) 66 67 err = gfile.Copy(src, dst) 68 t.AssertNE(err, nil) 69 }) 70 } 71 72 func Test_CopyFile(t *testing.T) { 73 gtest.C(t, func(t *gtest.T) { 74 var ( 75 paths = "/testfile_copyfile1.txt" 76 topath = "/testfile_copyfile2.txt" 77 ) 78 79 createTestFile(paths, "") 80 defer delTestFiles(paths) 81 82 t.Assert(gfile.CopyFile(testpath()+paths, testpath()+topath), nil) 83 defer delTestFiles(topath) 84 85 t.Assert(gfile.IsFile(testpath()+topath), true) 86 t.AssertNE(gfile.CopyFile(paths, ""), nil) 87 t.AssertNE(gfile.CopyFile("", topath), nil) 88 }) 89 // Content replacement. 90 gtest.C(t, func(t *gtest.T) { 91 src := gfile.Temp(gtime.TimestampNanoStr()) 92 dst := gfile.Temp(gtime.TimestampNanoStr()) 93 srcContent := "1" 94 dstContent := "1" 95 t.Assert(gfile.PutContents(src, srcContent), nil) 96 t.Assert(gfile.PutContents(dst, dstContent), nil) 97 t.Assert(gfile.GetContents(src), srcContent) 98 t.Assert(gfile.GetContents(dst), dstContent) 99 100 t.Assert(gfile.CopyFile(src, dst), nil) 101 t.Assert(gfile.GetContents(src), srcContent) 102 t.Assert(gfile.GetContents(dst), srcContent) 103 }) 104 // Set mode 105 gtest.C(t, func(t *gtest.T) { 106 var ( 107 src = "/testfile_copyfile1.txt" 108 dst = "/testfile_copyfile2.txt" 109 dstMode = os.FileMode(0600) 110 ) 111 t.AssertNil(createTestFile(src, "")) 112 defer delTestFiles(src) 113 114 t.Assert(gfile.CopyFile(testpath()+src, testpath()+dst, gfile.CopyOption{Mode: dstMode}), nil) 115 defer delTestFiles(dst) 116 117 dstStat, err := gfile.Stat(testpath() + dst) 118 t.AssertNil(err) 119 t.Assert(dstStat.Mode().Perm(), dstMode) 120 }) 121 // Preserve src file's mode 122 gtest.C(t, func(t *gtest.T) { 123 var ( 124 src = "/testfile_copyfile1.txt" 125 dst = "/testfile_copyfile2.txt" 126 ) 127 t.AssertNil(createTestFile(src, "")) 128 defer delTestFiles(src) 129 130 t.Assert(gfile.CopyFile(testpath()+src, testpath()+dst, gfile.CopyOption{PreserveMode: true}), nil) 131 defer delTestFiles(dst) 132 133 srcStat, err := gfile.Stat(testpath() + src) 134 t.AssertNil(err) 135 dstStat, err := gfile.Stat(testpath() + dst) 136 t.AssertNil(err) 137 t.Assert(srcStat.Mode().Perm(), dstStat.Mode().Perm()) 138 }) 139 } 140 141 func Test_CopyDir(t *testing.T) { 142 gtest.C(t, func(t *gtest.T) { 143 var ( 144 dirPath1 = "/test-copy-dir1" 145 dirPath2 = "/test-copy-dir2" 146 ) 147 haveList := []string{ 148 "t1.txt", 149 "t2.txt", 150 } 151 createDir(dirPath1) 152 for _, v := range haveList { 153 t.Assert(createTestFile(dirPath1+"/"+v, ""), nil) 154 } 155 defer delTestFiles(dirPath1) 156 157 var ( 158 yfolder = testpath() + dirPath1 159 tofolder = testpath() + dirPath2 160 ) 161 162 if gfile.IsDir(tofolder) { 163 t.Assert(gfile.Remove(tofolder), nil) 164 t.Assert(gfile.Remove(""), nil) 165 } 166 167 t.Assert(gfile.CopyDir(yfolder, tofolder), nil) 168 defer delTestFiles(tofolder) 169 170 t.Assert(gfile.IsDir(yfolder), true) 171 172 for _, v := range haveList { 173 t.Assert(gfile.IsFile(yfolder+"/"+v), true) 174 } 175 176 t.Assert(gfile.IsDir(tofolder), true) 177 178 for _, v := range haveList { 179 t.Assert(gfile.IsFile(tofolder+"/"+v), true) 180 } 181 182 t.Assert(gfile.Remove(tofolder), nil) 183 t.Assert(gfile.Remove(""), nil) 184 }) 185 // Content replacement. 186 gtest.C(t, func(t *gtest.T) { 187 src := gfile.Temp(gtime.TimestampNanoStr(), gtime.TimestampNanoStr()) 188 dst := gfile.Temp(gtime.TimestampNanoStr(), gtime.TimestampNanoStr()) 189 defer func() { 190 gfile.Remove(src) 191 gfile.Remove(dst) 192 }() 193 srcContent := "1" 194 dstContent := "1" 195 t.Assert(gfile.PutContents(src, srcContent), nil) 196 t.Assert(gfile.PutContents(dst, dstContent), nil) 197 t.Assert(gfile.GetContents(src), srcContent) 198 t.Assert(gfile.GetContents(dst), dstContent) 199 200 err := gfile.CopyDir(gfile.Dir(src), gfile.Dir(dst)) 201 t.AssertNil(err) 202 t.Assert(gfile.GetContents(src), srcContent) 203 t.Assert(gfile.GetContents(dst), srcContent) 204 205 t.AssertNE(gfile.CopyDir(gfile.Dir(src), ""), nil) 206 t.AssertNE(gfile.CopyDir("", gfile.Dir(dst)), nil) 207 }) 208 }