From 8bdb0ca77bb7c3aabbf1d696c29147f2f08954f0 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 26 Jan 2024 21:22:43 +0000 Subject: [PATCH] fix directory-is-empty and add tests to avoid regressions --- docker/shared/root/docker/helpers.sh | 2 +- tests/bats/helpers.bats | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docker/shared/root/docker/helpers.sh b/docker/shared/root/docker/helpers.sh index 1589e0780..51e955682 100644 --- a/docker/shared/root/docker/helpers.sh +++ b/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) diff --git a/tests/bats/helpers.bats b/tests/bats/helpers.bats index ea4c7b518..6ba65c2c3 100644 --- a/tests/bats/helpers.bats +++ b/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 +}