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

This commit is contained in:
Christian Winther 2024-01-26 21:22:43 +00:00
parent c4f984b205
commit 8bdb0ca77b
2 changed files with 30 additions and 1 deletions

View file

@ -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)

View file

@ -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
}