gf/os/gfile/gfile_z_scan_test.go

68 lines
2.0 KiB
Go
Raw Normal View History

// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). 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 (
"github.com/gogf/gf/debug/gdebug"
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gfile"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/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)
})
}