github.com/gogf/gf@v1.16.9/os/gfile/gfile_z_scan_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/container/garray" 11 "github.com/gogf/gf/debug/gdebug" 12 "testing" 13 14 "github.com/gogf/gf/os/gfile" 15 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 func Test_ScanDir(t *testing.T) { 20 teatPath := gdebug.TestDataPath() 21 gtest.C(t, func(t *gtest.T) { 22 files, err := gfile.ScanDir(teatPath, "*", false) 23 t.Assert(err, nil) 24 t.AssertIN(teatPath+gfile.Separator+"dir1", files) 25 t.AssertIN(teatPath+gfile.Separator+"dir2", files) 26 t.AssertNE(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files) 27 }) 28 gtest.C(t, func(t *gtest.T) { 29 files, err := gfile.ScanDir(teatPath, "*", true) 30 t.Assert(err, nil) 31 t.AssertIN(teatPath+gfile.Separator+"dir1", files) 32 t.AssertIN(teatPath+gfile.Separator+"dir2", files) 33 t.AssertIN(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files) 34 t.AssertIN(teatPath+gfile.Separator+"dir2"+gfile.Separator+"file2", files) 35 }) 36 } 37 38 func Test_ScanDirFunc(t *testing.T) { 39 teatPath := gdebug.TestDataPath() 40 gtest.C(t, func(t *gtest.T) { 41 files, err := gfile.ScanDirFunc(teatPath, "*", true, func(path string) string { 42 if gfile.Name(path) != "file1" { 43 return "" 44 } 45 return path 46 }) 47 t.Assert(err, nil) 48 t.Assert(len(files), 1) 49 t.Assert(gfile.Name(files[0]), "file1") 50 }) 51 } 52 53 func Test_ScanDirFile(t *testing.T) { 54 teatPath := gdebug.TestDataPath() 55 gtest.C(t, func(t *gtest.T) { 56 files, err := gfile.ScanDirFile(teatPath, "*", false) 57 t.Assert(err, nil) 58 t.Assert(len(files), 0) 59 }) 60 gtest.C(t, func(t *gtest.T) { 61 files, err := gfile.ScanDirFile(teatPath, "*", true) 62 t.Assert(err, nil) 63 t.AssertNI(teatPath+gfile.Separator+"dir1", files) 64 t.AssertNI(teatPath+gfile.Separator+"dir2", files) 65 t.AssertIN(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files) 66 t.AssertIN(teatPath+gfile.Separator+"dir2"+gfile.Separator+"file2", files) 67 }) 68 } 69 70 func Test_ScanDirFileFunc(t *testing.T) { 71 teatPath := gdebug.TestDataPath() 72 gtest.C(t, func(t *gtest.T) { 73 array := garray.New() 74 files, err := gfile.ScanDirFileFunc(teatPath, "*", false, func(path string) string { 75 array.Append(1) 76 return path 77 }) 78 t.Assert(err, nil) 79 t.Assert(len(files), 0) 80 t.Assert(array.Len(), 0) 81 }) 82 gtest.C(t, func(t *gtest.T) { 83 array := garray.New() 84 files, err := gfile.ScanDirFileFunc(teatPath, "*", true, func(path string) string { 85 array.Append(1) 86 if gfile.Basename(path) == "file1" { 87 return path 88 } 89 return "" 90 }) 91 t.Assert(err, nil) 92 t.Assert(len(files), 1) 93 t.Assert(array.Len(), 3) 94 }) 95 }