github.com/paketo-buildpacks/packit@v1.3.2-0.20211206231111-86b75c657449/fs/is_empty_dir_test.go (about)

     1  package fs_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/paketo-buildpacks/packit/fs"
     9  	"github.com/sclevine/spec"
    10  
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  func testIsEmptyDir(t *testing.T, context spec.G, it spec.S) {
    15  	var (
    16  		Expect = NewWithT(t).Expect
    17  
    18  		path string
    19  	)
    20  
    21  	it.Before(func() {
    22  		var err error
    23  		path, err = os.MkdirTemp("", "dir")
    24  		Expect(err).NotTo(HaveOccurred())
    25  	})
    26  
    27  	it.After(func() {
    28  		Expect(os.RemoveAll(path)).To(Succeed())
    29  	})
    30  
    31  	context("when the directory is empty", func() {
    32  		it("returns true", func() {
    33  			Expect(fs.IsEmptyDir(path)).To(BeTrue())
    34  		})
    35  	})
    36  
    37  	context("when the directory is not empty", func() {
    38  		it.Before(func() {
    39  			Expect(os.WriteFile(filepath.Join(path, "some-file"), []byte{}, 0644)).To(Succeed())
    40  		})
    41  
    42  		it("returns false", func() {
    43  			Expect(fs.IsEmptyDir(path)).To(BeFalse())
    44  		})
    45  	})
    46  
    47  	context("when the directory does not exist", func() {
    48  		it.Before(func() {
    49  			Expect(os.RemoveAll(path)).To(Succeed())
    50  		})
    51  
    52  		it("returns false", func() {
    53  			Expect(fs.IsEmptyDir(path)).To(BeFalse())
    54  		})
    55  	})
    56  
    57  	context("when the directory cannot be read", func() {
    58  		it.Before(func() {
    59  			Expect(os.Chmod(path, 0000)).To(Succeed())
    60  		})
    61  
    62  		it.After(func() {
    63  			Expect(os.Chmod(path, os.ModePerm)).To(Succeed())
    64  		})
    65  
    66  		it("returns false", func() {
    67  			Expect(fs.IsEmptyDir(path)).To(BeFalse())
    68  		})
    69  	})
    70  }