gf/os/gfile/gfile_z_unit_scan_test.go

95 lines
2.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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 (
"testing"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/debug/gdebug"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)
2019-07-27 11:41:12 +08:00
func Test_ScanDir(t *testing.T) {
teatPath := gdebug.TestDataPath()
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
files, err := gfile.ScanDir(teatPath, "*", false)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertIN(teatPath+gfile.Separator+"dir1", files)
t.AssertIN(teatPath+gfile.Separator+"dir2", files)
t.AssertNE(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files)
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
files, err := gfile.ScanDir(teatPath, "*", true)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertIN(teatPath+gfile.Separator+"dir1", files)
t.AssertIN(teatPath+gfile.Separator+"dir2", files)
t.AssertIN(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files)
t.AssertIN(teatPath+gfile.Separator+"dir2"+gfile.Separator+"file2", files)
2019-07-27 11:41:12 +08:00
})
}
func Test_ScanDirFunc(t *testing.T) {
teatPath := gdebug.TestDataPath()
gtest.C(t, func(t *gtest.T) {
files, err := gfile.ScanDirFunc(teatPath, "*", true, func(path string) string {
if gfile.Name(path) != "file1" {
return ""
}
return path
})
t.Assert(err, nil)
t.Assert(len(files), 1)
t.Assert(gfile.Name(files[0]), "file1")
})
}
2019-07-27 11:41:12 +08:00
func Test_ScanDirFile(t *testing.T) {
teatPath := gdebug.TestDataPath()
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-27 11:41:12 +08:00
files, err := gfile.ScanDirFile(teatPath, "*", false)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(len(files), 0)
2019-07-27 11:41:12 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-27 11:41:12 +08:00
files, err := gfile.ScanDirFile(teatPath, "*", true)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertNI(teatPath+gfile.Separator+"dir1", files)
t.AssertNI(teatPath+gfile.Separator+"dir2", files)
t.AssertIN(teatPath+gfile.Separator+"dir1"+gfile.Separator+"file1", files)
t.AssertIN(teatPath+gfile.Separator+"dir2"+gfile.Separator+"file2", files)
})
}
func Test_ScanDirFileFunc(t *testing.T) {
teatPath := gdebug.TestDataPath()
gtest.C(t, func(t *gtest.T) {
array := garray.New()
files, err := gfile.ScanDirFileFunc(teatPath, "*", false, func(path string) string {
array.Append(1)
return path
})
t.Assert(err, nil)
t.Assert(len(files), 0)
t.Assert(array.Len(), 0)
})
gtest.C(t, func(t *gtest.T) {
array := garray.New()
files, err := gfile.ScanDirFileFunc(teatPath, "*", true, func(path string) string {
array.Append(1)
if gfile.Basename(path) == "file1" {
return path
}
return ""
})
t.Assert(err, nil)
t.Assert(len(files), 1)
t.Assert(array.Len(), 3)
})
}