mirror of
https://github.com/mudler/luet.git
synced 2025-08-31 23:02:16 +00:00
Add DirectoryIsEmpty
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -82,6 +83,20 @@ func ListDir(dir string) ([]string, error) {
|
|||||||
return content, err
|
return content, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DirectoryIsEmpty Checks wether the directory is empty or not
|
||||||
|
func DirectoryIsEmpty(dir string) (bool, error) {
|
||||||
|
f, err := os.Open(dir)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if _, err = f.Readdirnames(1); err == io.EOF {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Touch creates an empty file
|
// Touch creates an empty file
|
||||||
func Touch(f string) error {
|
func Touch(f string) error {
|
||||||
_, err := os.Stat(f)
|
_, err := os.Stat(f)
|
||||||
|
@@ -33,6 +33,23 @@ var _ = Describe("Helpers", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Context("DirectoryIsEmpty", func() {
|
||||||
|
It("Detects empty directory", func() {
|
||||||
|
testDir, err := ioutil.TempDir(os.TempDir(), "test")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
Expect(DirectoryIsEmpty(testDir)).To(BeTrue())
|
||||||
|
})
|
||||||
|
It("Detects directory with files", func() {
|
||||||
|
testDir, err := ioutil.TempDir(os.TempDir(), "test")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
err = Touch(filepath.Join(testDir, "foo"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(DirectoryIsEmpty(testDir)).To(BeFalse())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Context("Orders dir and files correctly", func() {
|
Context("Orders dir and files correctly", func() {
|
||||||
It("puts files first and folders at end", func() {
|
It("puts files first and folders at end", func() {
|
||||||
testDir, err := ioutil.TempDir(os.TempDir(), "test")
|
testDir, err := ioutil.TempDir(os.TempDir(), "test")
|
||||||
|
Reference in New Issue
Block a user