2019-08-09 20:05:36 +08:00
|
|
|
// 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.
|
|
|
|
|
2019-07-27 11:34:04 +08:00
|
|
|
package gfile_test
|
|
|
|
|
|
|
|
import (
|
2019-08-09 20:05:36 +08:00
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2019-07-27 11:34:04 +08:00
|
|
|
"testing"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
2019-07-27 11:34:04 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-07-27 11:34:04 +08:00
|
|
|
)
|
|
|
|
|
2019-07-27 11:41:12 +08:00
|
|
|
func Test_ScanDir(t *testing.T) {
|
2020-05-15 21:51:03 +08:00
|
|
|
teatPath := gdebug.TestDataPath()
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-27 11:34:04 +08:00
|
|
|
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)
|
2019-07-27 11:34:04 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-27 11:34:04 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:51:03 +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) {
|
2020-05-15 21:51:03 +08:00
|
|
|
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)
|
2019-07-27 11:34:04 +08:00
|
|
|
})
|
|
|
|
}
|