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