Skip to content

Commit

Permalink
Improve logging on empty script dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahoney committed Jan 8, 2021
1 parent 4bd29d9 commit 83aa62c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions docker-lifecycle-listener.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ run_if_possible() {
fi
}

is_empty_dir() {
local dir="$1"
[ -z "$(ls -A "$dir")" ]
}

run_on() {
local command="$1"
local script_dir="$2"
Expand All @@ -99,9 +104,13 @@ run_on() {

local dir; dir="$script_dir/on_$command/"
if [ -d "$dir" ]; then
for file in "$dir"/*; do
run_if_possible "$file"
done
if ! is_empty_dir "$dir"; then
for file in "$dir"/*; do
run_if_possible "$file"
done
else
log "$dir is empty"
fi
else
log "$dir does not exist"
fi
Expand Down
13 changes: 13 additions & 0 deletions test/docker-lifecycle-listener.bats
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,16 @@ teardown() {

test "$status" -ne 0
}

@test "is_empty_dir succeeds if dir is empty" {
local empty_dir; empty_dir=$(mktemp -d)
is_empty_dir "$empty_dir"
}

@test "is_empty_dir fails if dir is empty" {
local non_empty_dir; non_empty_dir=$(mktemp -d)
touch "$non_empty_dir/something"

run is_empty_dir "$non_empty_dir"
test "$status" -ne 0
}

0 comments on commit 83aa62c

Please sign in to comment.