github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_unit_contents_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 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/wangyougui/gf/v2/debug/gdebug" 16 "github.com/wangyougui/gf/v2/errors/gerror" 17 "github.com/wangyougui/gf/v2/os/gfile" 18 "github.com/wangyougui/gf/v2/test/gtest" 19 "github.com/wangyougui/gf/v2/text/gstr" 20 ) 21 22 func createTestFile(filename, content string) error { 23 TempDir := testpath() 24 err := os.WriteFile(TempDir+filename, []byte(content), 0666) 25 return err 26 } 27 28 func delTestFiles(filenames string) { 29 os.RemoveAll(testpath() + filenames) 30 } 31 32 func createDir(paths string) { 33 TempDir := testpath() 34 os.Mkdir(TempDir+paths, 0777) 35 } 36 37 func formatpaths(paths []string) []string { 38 for k, v := range paths { 39 paths[k] = filepath.ToSlash(v) 40 paths[k] = strings.Replace(paths[k], "./", "/", 1) 41 } 42 43 return paths 44 } 45 46 func formatpath(paths string) string { 47 paths = filepath.ToSlash(paths) 48 paths = strings.Replace(paths, "./", "/", 1) 49 return paths 50 } 51 52 func testpath() string { 53 return gstr.TrimRight(os.TempDir(), "\\/") 54 } 55 56 func Test_GetContents(t *testing.T) { 57 gtest.C(t, func(t *gtest.T) { 58 59 var ( 60 filepaths string = "/testfile_t1.txt" 61 ) 62 createTestFile(filepaths, "my name is jroam") 63 defer delTestFiles(filepaths) 64 65 t.Assert(gfile.GetContents(testpath()+filepaths), "my name is jroam") 66 t.Assert(gfile.GetContents(""), "") 67 68 }) 69 } 70 71 func Test_GetBinContents(t *testing.T) { 72 gtest.C(t, func(t *gtest.T) { 73 var ( 74 filepaths1 = "/testfile_t1.txt" 75 filepaths2 = testpath() + "/testfile_t1_no.txt" 76 readcontent []byte 77 str1 = "my name is jroam" 78 ) 79 createTestFile(filepaths1, str1) 80 defer delTestFiles(filepaths1) 81 readcontent = gfile.GetBytes(testpath() + filepaths1) 82 t.Assert(readcontent, []byte(str1)) 83 84 readcontent = gfile.GetBytes(filepaths2) 85 t.Assert(string(readcontent), "") 86 87 t.Assert(string(gfile.GetBytes(filepaths2)), "") 88 89 }) 90 } 91 92 func Test_Truncate(t *testing.T) { 93 gtest.C(t, func(t *gtest.T) { 94 var ( 95 filepaths1 = "/testfile_GetContentsyyui.txt" 96 err error 97 files *os.File 98 ) 99 createTestFile(filepaths1, "abcdefghijkmln") 100 defer delTestFiles(filepaths1) 101 err = gfile.Truncate(testpath()+filepaths1, 10) 102 t.AssertNil(err) 103 104 files, err = os.Open(testpath() + filepaths1) 105 t.AssertNil(err) 106 defer files.Close() 107 fileinfo, err2 := files.Stat() 108 t.Assert(err2, nil) 109 t.Assert(fileinfo.Size(), 10) 110 111 err = gfile.Truncate("", 10) 112 t.AssertNE(err, nil) 113 114 }) 115 } 116 117 func Test_PutContents(t *testing.T) { 118 gtest.C(t, func(t *gtest.T) { 119 var ( 120 filepaths = "/testfile_PutContents.txt" 121 err error 122 readcontent []byte 123 ) 124 createTestFile(filepaths, "a") 125 defer delTestFiles(filepaths) 126 127 err = gfile.PutContents(testpath()+filepaths, "test!") 128 t.AssertNil(err) 129 130 readcontent, err = os.ReadFile(testpath() + filepaths) 131 t.AssertNil(err) 132 t.Assert(string(readcontent), "test!") 133 134 err = gfile.PutContents("", "test!") 135 t.AssertNE(err, nil) 136 137 }) 138 } 139 140 func Test_PutContentsAppend(t *testing.T) { 141 gtest.C(t, func(t *gtest.T) { 142 var ( 143 filepaths = "/testfile_PutContents.txt" 144 err error 145 readcontent []byte 146 ) 147 148 createTestFile(filepaths, "a") 149 defer delTestFiles(filepaths) 150 err = gfile.PutContentsAppend(testpath()+filepaths, "hello") 151 t.AssertNil(err) 152 153 readcontent, err = os.ReadFile(testpath() + filepaths) 154 t.AssertNil(err) 155 t.Assert(string(readcontent), "ahello") 156 157 err = gfile.PutContentsAppend("", "hello") 158 t.AssertNE(err, nil) 159 160 }) 161 162 } 163 164 func Test_PutBinContents(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 var ( 167 filepaths = "/testfile_PutContents.txt" 168 err error 169 readcontent []byte 170 ) 171 createTestFile(filepaths, "a") 172 defer delTestFiles(filepaths) 173 174 err = gfile.PutBytes(testpath()+filepaths, []byte("test!!")) 175 t.AssertNil(err) 176 177 readcontent, err = os.ReadFile(testpath() + filepaths) 178 t.AssertNil(err) 179 t.Assert(string(readcontent), "test!!") 180 181 err = gfile.PutBytes("", []byte("test!!")) 182 t.AssertNE(err, nil) 183 184 }) 185 } 186 187 func Test_PutBinContentsAppend(t *testing.T) { 188 gtest.C(t, func(t *gtest.T) { 189 var ( 190 filepaths = "/testfile_PutContents.txt" 191 err error 192 readcontent []byte 193 ) 194 createTestFile(filepaths, "test!!") 195 defer delTestFiles(filepaths) 196 err = gfile.PutBytesAppend(testpath()+filepaths, []byte("word")) 197 t.AssertNil(err) 198 199 readcontent, err = os.ReadFile(testpath() + filepaths) 200 t.AssertNil(err) 201 t.Assert(string(readcontent), "test!!word") 202 203 err = gfile.PutBytesAppend("", []byte("word")) 204 t.AssertNE(err, nil) 205 206 }) 207 } 208 209 func Test_GetBinContentsByTwoOffsetsByPath(t *testing.T) { 210 gtest.C(t, func(t *gtest.T) { 211 var ( 212 filepaths = "/testfile_GetContents.txt" 213 readcontent []byte 214 ) 215 216 createTestFile(filepaths, "abcdefghijk") 217 defer delTestFiles(filepaths) 218 readcontent = gfile.GetBytesByTwoOffsetsByPath(testpath()+filepaths, 2, 5) 219 220 t.Assert(string(readcontent), "cde") 221 222 readcontent = gfile.GetBytesByTwoOffsetsByPath("", 2, 5) 223 t.Assert(len(readcontent), 0) 224 225 }) 226 227 } 228 229 func Test_GetNextCharOffsetByPath(t *testing.T) { 230 gtest.C(t, func(t *gtest.T) { 231 var ( 232 filepaths = "/testfile_GetContents.txt" 233 localindex int64 234 ) 235 createTestFile(filepaths, "abcdefghijk") 236 defer delTestFiles(filepaths) 237 localindex = gfile.GetNextCharOffsetByPath(testpath()+filepaths, 'd', 1) 238 t.Assert(localindex, 3) 239 240 localindex = gfile.GetNextCharOffsetByPath("", 'd', 1) 241 t.Assert(localindex, -1) 242 243 }) 244 } 245 246 func Test_GetNextCharOffset(t *testing.T) { 247 gtest.C(t, func(t *gtest.T) { 248 var ( 249 localindex int64 250 ) 251 reader := strings.NewReader("helloword") 252 253 localindex = gfile.GetNextCharOffset(reader, 'w', 1) 254 t.Assert(localindex, 5) 255 256 localindex = gfile.GetNextCharOffset(reader, 'j', 1) 257 t.Assert(localindex, -1) 258 259 }) 260 } 261 262 func Test_GetBinContentsByTwoOffsets(t *testing.T) { 263 gtest.C(t, func(t *gtest.T) { 264 var ( 265 reads []byte 266 ) 267 reader := strings.NewReader("helloword") 268 269 reads = gfile.GetBytesByTwoOffsets(reader, 1, 3) 270 t.Assert(string(reads), "el") 271 272 reads = gfile.GetBytesByTwoOffsets(reader, 10, 30) 273 t.Assert(string(reads), "") 274 275 }) 276 } 277 278 func Test_GetBinContentsTilChar(t *testing.T) { 279 gtest.C(t, func(t *gtest.T) { 280 var ( 281 reads []byte 282 indexs int64 283 ) 284 reader := strings.NewReader("helloword") 285 286 reads, _ = gfile.GetBytesTilChar(reader, 'w', 2) 287 t.Assert(string(reads), "llow") 288 289 _, indexs = gfile.GetBytesTilChar(reader, 'w', 20) 290 t.Assert(indexs, -1) 291 292 }) 293 } 294 295 func Test_GetBinContentsTilCharByPath(t *testing.T) { 296 gtest.C(t, func(t *gtest.T) { 297 var ( 298 reads []byte 299 indexs int64 300 filepaths = "/testfile_GetContents.txt" 301 ) 302 303 createTestFile(filepaths, "abcdefghijklmn") 304 defer delTestFiles(filepaths) 305 306 reads, _ = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'c', 2) 307 t.Assert(string(reads), "c") 308 309 reads, _ = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'y', 1) 310 t.Assert(string(reads), "") 311 312 _, indexs = gfile.GetBytesTilCharByPath(testpath()+filepaths, 'x', 1) 313 t.Assert(indexs, -1) 314 315 }) 316 } 317 318 func Test_Home(t *testing.T) { 319 gtest.C(t, func(t *gtest.T) { 320 var ( 321 reads string 322 err error 323 ) 324 325 reads, err = gfile.Home("a", "b") 326 t.AssertNil(err) 327 t.AssertNE(reads, "") 328 }) 329 } 330 331 func Test_NotFound(t *testing.T) { 332 gtest.C(t, func(t *gtest.T) { 333 teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/error.log" 334 callback := func(line string) error { 335 return nil 336 } 337 err := gfile.ReadLines(teatFile, callback) 338 t.AssertNE(err, nil) 339 }) 340 } 341 342 func Test_ReadLines(t *testing.T) { 343 gtest.C(t, func(t *gtest.T) { 344 var ( 345 expectList = []string{"a", "b", "c", "d", "e"} 346 getList = make([]string, 0) 347 callback = func(line string) error { 348 getList = append(getList, line) 349 return nil 350 } 351 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 352 ) 353 err := gfile.ReadLines(teatFile, callback) 354 t.AssertEQ(getList, expectList) 355 t.AssertEQ(err, nil) 356 }) 357 } 358 359 func Test_ReadLines_Error(t *testing.T) { 360 gtest.C(t, func(t *gtest.T) { 361 var ( 362 callback = func(line string) error { 363 return gerror.New("custom error") 364 } 365 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 366 ) 367 err := gfile.ReadLines(teatFile, callback) 368 t.AssertEQ(err.Error(), "custom error") 369 }) 370 } 371 372 func Test_ReadLinesBytes(t *testing.T) { 373 gtest.C(t, func(t *gtest.T) { 374 var ( 375 expectList = [][]byte{[]byte("a"), []byte("b"), []byte("c"), []byte("d"), []byte("e")} 376 getList = make([][]byte, 0) 377 callback = func(line []byte) error { 378 getList = append(getList, line) 379 return nil 380 } 381 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 382 ) 383 err := gfile.ReadLinesBytes(teatFile, callback) 384 t.AssertEQ(getList, expectList) 385 t.AssertEQ(err, nil) 386 }) 387 } 388 389 func Test_ReadLinesBytes_Error(t *testing.T) { 390 gtest.C(t, func(t *gtest.T) { 391 var ( 392 callback = func(line []byte) error { 393 return gerror.New("custom error") 394 } 395 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 396 ) 397 err := gfile.ReadLinesBytes(teatFile, callback) 398 t.AssertEQ(err.Error(), "custom error") 399 }) 400 }