github.com/sandwich-go/boost@v1.3.29/xos/dir_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package xos
     5  
     6  import (
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"io/fs"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  func TestDir(t *testing.T) {
    15  	Convey("test dir", t, func() {
    16  		var dest = filepath.Join(os.TempDir(), "test_perf")
    17  		_ = os.RemoveAll(dest)
    18  
    19  		So(Exists(dest), ShouldBeFalse)
    20  
    21  		So(Mkdir(dest), ShouldBeNil)
    22  		So(Exists(dest), ShouldBeTrue)
    23  
    24  		So(os.RemoveAll(dest), ShouldBeNil)
    25  
    26  		So(MkdirAll(filepath.Join(dest, "a.go")), ShouldBeNil)
    27  		So(Exists(dest), ShouldBeTrue)
    28  
    29  		So(os.RemoveAll(dest), ShouldBeNil)
    30  
    31  		So(IsEmpty(dest), ShouldBeTrue)
    32  		So(Mkdir(dest), ShouldBeNil)
    33  		So(Exists(dest), ShouldBeTrue)
    34  		So(IsEmpty(dest), ShouldBeTrue)
    35  
    36  		var destFile = filepath.Join(dest, "a.go")
    37  		So(FilePutContents(destFile, nil), ShouldBeNil)
    38  		So(IsEmpty(destFile), ShouldBeTrue)
    39  
    40  		// 不存在的子目录,不会报错
    41  		So(RemoveSubDirsUnderDir(filepath.Join(dest, "sub"), nil), ShouldBeNil)
    42  
    43  		var destDir1, destDir2 = filepath.Join(dest, "a"), filepath.Join(dest, "b")
    44  		So(Mkdir(destDir1), ShouldBeNil)
    45  		So(Mkdir(destDir2), ShouldBeNil)
    46  		So(FilePutContents(filepath.Join(destDir1, "a.go"), nil), ShouldBeNil)
    47  		So(FilePutContents(filepath.Join(destDir1, "b.go"), nil), ShouldBeNil)
    48  
    49  		So(RemoveSubDirsUnderDir(dest, func(dir string) bool { return dir == destDir1 }), ShouldBeNil)
    50  		So(Exists(destDir1), ShouldBeFalse)
    51  
    52  		So(Mkdir(destDir1), ShouldBeNil)
    53  		So(FilePutContents(filepath.Join(destDir1, "a.go"), nil), ShouldBeNil)
    54  		So(FilePutContents(filepath.Join(destDir1, "b.go"), nil), ShouldBeNil)
    55  		So(RemoveEmptyDirs(destDir1), ShouldNotBeNil)
    56  		So(Exists(destDir1), ShouldBeTrue)
    57  		So(RemoveDirs(destDir1), ShouldBeNil)
    58  		So(Exists(destDir1), ShouldBeTrue)
    59  		So(IsEmpty(destDir1), ShouldBeTrue)
    60  
    61  		So(FilePutContents(filepath.Join(destDir1, "a.go"), nil), ShouldBeNil)
    62  		So(FilePutContents(filepath.Join(destDir1, "b.go"), nil), ShouldBeNil)
    63  		RemoveFilesUnderDir(destDir1, func(f string) bool { return f == filepath.Join(destDir1, "a.go") })
    64  		var fileList []string
    65  		So(filepath.Walk(destDir1, FileWalkFunc(&fileList, ".txt")), ShouldBeNil)
    66  		So(len(fileList), ShouldBeZeroValue)
    67  		So(filepath.Walk(destDir1, FileWalkFunc(&fileList, ".go")), ShouldBeNil)
    68  		So(len(fileList), ShouldEqual, 1)
    69  		So(fileList[0], ShouldEqual, filepath.Join(destDir1, "b.go"))
    70  		fileList = nil
    71  		So(filepath.Walk(destDir1, FileWalkFuncWithIncludeFilter(&fileList, func(f string) bool { return f == filepath.Join(destDir1, "a.go") }, ".go")), ShouldBeNil)
    72  		So(len(fileList), ShouldBeZeroValue)
    73  		fileList = nil
    74  		So(filepath.Walk(destDir1, FileWalkFuncWithIncludeFilter(&fileList, func(f string) bool { return f == filepath.Join(destDir1, "b.go") }, ".go")), ShouldBeNil)
    75  		So(len(fileList), ShouldEqual, 1)
    76  		So(fileList[0], ShouldEqual, filepath.Join(destDir1, "b.go"))
    77  		fileList = nil
    78  		So(filepath.Walk(destDir1, FileWalkFuncWithExcludeFilter(&fileList, func(f string) bool { return f == filepath.Join(destDir1, "b.go") }, ".go")), ShouldBeNil)
    79  		So(len(fileList), ShouldBeZeroValue)
    80  		fileList = nil
    81  		So(filepath.Walk(destDir1, FileWalkFuncWithExcludeFilter(&fileList, func(f string) bool { return f == filepath.Join(destDir1, "a.go") }, ".go")), ShouldBeNil)
    82  		So(len(fileList), ShouldEqual, 1)
    83  		So(fileList[0], ShouldEqual, filepath.Join(destDir1, "b.go"))
    84  		fileList = nil
    85  
    86  		So(FilePathWalkFollowLink(destDir1, func(path string, info fs.FileInfo, err error) error {
    87  			if !info.IsDir() {
    88  				fileList = append(fileList, path)
    89  			}
    90  			return err
    91  		}), ShouldBeNil)
    92  		So(len(fileList), ShouldEqual, 1)
    93  		So(fileList[0], ShouldEqual, filepath.Join(destDir1, "b.go"))
    94  
    95  		So(FilePutContents(filepath.Join(destDir1, "c.txt"), nil), ShouldBeNil)
    96  
    97  		fileNames, err := ReadDir(destDir1)
    98  		So(err, ShouldBeNil)
    99  		So(len(fileNames), ShouldEqual, 2)
   100  
   101  		fileNames, err = ReadDirWithExt(destDir1, ".go")
   102  		So(err, ShouldBeNil)
   103  		So(len(fileNames), ShouldEqual, 1)
   104  		So(fileNames[0], ShouldEqual, "b.go")
   105  
   106  		So(IsDirWriteable(destDir1), ShouldBeNil)
   107  		So(TouchDirAll(filepath.Join(destDir1, "c.txt")), ShouldNotBeNil)
   108  		So(TouchDirAll(destDir1), ShouldBeNil)
   109  		So(CreateDirAll(destDir1), ShouldNotBeNil)
   110  
   111  		So(os.RemoveAll(dest), ShouldBeNil)
   112  	})
   113  	Convey("IsEmpty", t, func() {
   114  		temDir := os.TempDir()
   115  		temDirTesting, err := os.MkdirTemp(temDir, "issue")
   116  		So(err, ShouldBeNil)
   117  		So(IsEmpty(temDirTesting), ShouldBeTrue)
   118  		filename := ".config"
   119  		err = os.WriteFile(filepath.Join(temDirTesting, filename), []byte(filename), 0666)
   120  		So(err, ShouldBeNil)
   121  		So(IsEmpty(temDirTesting), ShouldBeFalse)
   122  	})
   123  }