Browse Source

fix directory-is-empty and add tests to avoid regressions

Christian Winther 1 năm trước cách đây
mục cha
commit
8bdb0ca77b
2 tập tin đã thay đổi với 30 bổ sung1 xóa
  1. 1 1
      docker/shared/root/docker/helpers.sh
  2. 29 0
      tests/bats/helpers.bats

+ 1 - 1
docker/shared/root/docker/helpers.sh

@@ -324,7 +324,7 @@ function file-exists()
 # @exitcode 1 If $1 does *NOT* contain files
 function directory-is-empty()
 {
-    path-exists "${1}" && [[ -z "$(ls -A "${1}")" ]]
+    ! path-exists "${1}" || [[ -z "$(ls -A "${1}")" ]]
 }
 
 # @description Ensures a directory exists (via mkdir)

+ 29 - 0
tests/bats/helpers.bats

@@ -5,6 +5,12 @@ setup() {
     load "$ROOT/docker/shared/root/docker/helpers.sh"
 }
 
+teardown() {
+    if [[ -e test_dir ]]; then
+        rm -rf test_dir
+    fi
+}
+
 @test "test [is-true]" {
     is-true "1"
     is-true "true"
@@ -72,3 +78,26 @@ setup() {
 
     return 1
 }
+
+@test "test [directory-is-empty] - non existing" {
+    directory-is-empty test_dir
+}
+
+@test "test [directory-is-empty] - actually empty" {
+    mkdir -p test_dir
+
+    directory-is-empty test_dir
+}
+
+@test "test [directory-is-empty] - not empty (directory)" {
+    mkdir -p test_dir/sub-dir
+
+    ! directory-is-empty test_dir
+}
+
+@test "test [directory-is-empty] - not empty (file)" {
+    mkdir -p test_dir/
+    touch test_dir/hello-world.txt
+
+    ! directory-is-empty test_dir
+}