diff options
| -rwxr-xr-x | bin/photoalbum | 6 | ||||
| -rwxr-xr-x | src/photoalbum.sh | 6 | ||||
| -rwxr-xr-x | tests/cli.sh | 84 | ||||
| -rwxr-xr-x | tests/helpers.sh | 23 |
4 files changed, 115 insertions, 4 deletions
diff --git a/bin/photoalbum b/bin/photoalbum index 8f7f1bd..7079465 100755 --- a/bin/photoalbum +++ b/bin/photoalbum @@ -1960,11 +1960,11 @@ cleanup_generation_staging_dir() { } clear_generation_staging_traps() { - trap - EXIT INT TERM + trap - EXIT INT TERM HUP PIPE } ignore_generation_staging_interrupts() { - trap '' INT TERM + trap '' INT TERM HUP PIPE } replace_dist_with_staging() { @@ -2037,6 +2037,8 @@ generate_staged() { trap cleanup_generation_staging_dir EXIT trap 'cleanup_generation_staging_dir; exit 130' INT trap 'cleanup_generation_staging_dir; exit 143' TERM + trap 'cleanup_generation_staging_dir; exit 129' HUP + trap 'cleanup_generation_staging_dir; exit 141' PIPE if ! prepare_generation_staging_dir "$final_dist" "$staging_dir"; then cleanup_generation_staging_dir diff --git a/src/photoalbum.sh b/src/photoalbum.sh index f616af7..9f9cc7d 100755 --- a/src/photoalbum.sh +++ b/src/photoalbum.sh @@ -1960,11 +1960,11 @@ cleanup_generation_staging_dir() { } clear_generation_staging_traps() { - trap - EXIT INT TERM + trap - EXIT INT TERM HUP PIPE } ignore_generation_staging_interrupts() { - trap '' INT TERM + trap '' INT TERM HUP PIPE } replace_dist_with_staging() { @@ -2037,6 +2037,8 @@ generate_staged() { trap cleanup_generation_staging_dir EXIT trap 'cleanup_generation_staging_dir; exit 130' INT trap 'cleanup_generation_staging_dir; exit 143' TERM + trap 'cleanup_generation_staging_dir; exit 129' HUP + trap 'cleanup_generation_staging_dir; exit 141' PIPE if ! prepare_generation_staging_dir "$final_dist" "$staging_dir"; then cleanup_generation_staging_dir diff --git a/tests/cli.sh b/tests/cli.sh index 367cd1a..8056da5 100755 --- a/tests/cli.sh +++ b/tests/cli.sh @@ -2248,6 +2248,87 @@ test_generate_imagemagick_timeout_preserves_dist() { test::teardown } +test_generate_sighup_cleans_staging_dir() { + local config_file + local fake_bin + local output_file + local release_file + local started_file + local -i release_pid=0 + local -i status=0 + local -i waited=0 + local -i generate_pid=0 + + test::setup + fake_bin="$TEST_TMPDIR/bin" + config_file="$TEST_TMPDIR/photoalbum.conf" + output_file="$TEST_TMPDIR/generate.out" + release_file="$TEST_TMPDIR/release-magick" + started_file="$TEST_TMPDIR/magick-started" + + test::install_fake_imagemagick "$fake_bin" + PATH="$fake_bin:$PATH" \ + test::generate_fixture_images "$TEST_TMPDIR/incoming" + test::install_blocking_imagemagick "$fake_bin" + mkdir -p "$TEST_TMPDIR/dist" + printf 'old dist\n' > "$TEST_TMPDIR/dist/index.html" + test::write_album_config \ + "$config_file" "$TEST_TMPDIR/incoming" "$TEST_TMPDIR/dist" \ + 'SIGHUP album' 40 + + ( + cd "$TEST_TMPDIR" + PATH="$fake_bin:$PATH" \ + TEST_BLOCKING_MAGICK_STARTED="$started_file" \ + TEST_BLOCKING_MAGICK_RELEASE="$release_file" \ + "$TEST_PHOTOALBUM" --generate + ) > "$output_file" 2>&1 & + generate_pid=$! + + while [ ! -f "$started_file" ] && kill -0 "$generate_pid" 2>/dev/null; do + if (( waited >= 100 )); then + touch "$release_file" + wait "$generate_pid" || true + echo 'FAIL: timed out waiting for fake ImageMagick to start' >&2 + cat "$output_file" >&2 + exit 1 + fi + (( ++waited )) + sleep 0.05 + done + + if [ ! -f "$started_file" ]; then + touch "$release_file" + wait "$generate_pid" || true + echo 'FAIL: generate exited before fake ImageMagick started' >&2 + cat "$output_file" >&2 + exit 1 + fi + + ( sleep 5; touch "$release_file" ) & + release_pid=$! + + kill -HUP "$generate_pid" + set +e + wait "$generate_pid" + status=$? + set -e + + touch "$release_file" + kill "$release_pid" 2>/dev/null || true + wait "$release_pid" 2>/dev/null || true + + if (( status != 129 )); then + echo "FAIL: expected SIGHUP exit status 129, got $status" >&2 + cat "$output_file" >&2 + exit 1 + fi + + test "$(<"$TEST_TMPDIR/dist/index.html")" = 'old dist' + test::assert_no_staging_dirs "$TEST_TMPDIR" + test::teardown +} + test_generate_tar_timeout_preserves_dist() { local config_file local fake_bin @@ -2952,6 +3033,9 @@ main() { '--generate ImageMagick timeout preserves final dist' \ test_generate_imagemagick_timeout_preserves_dist test::run_case \ + '--generate SIGHUP cleans staging directory' \ + test_generate_sighup_cleans_staging_dir + test::run_case \ '--generate tar timeout preserves final dist' \ test_generate_tar_timeout_preserves_dist test::run_case \ diff --git a/tests/helpers.sh b/tests/helpers.sh index bf6000e..6306004 100755 --- a/tests/helpers.sh +++ b/tests/helpers.sh @@ -303,6 +303,29 @@ MAGICK cp "$bin_dir/magick" "$bin_dir/convert" } +test::install_blocking_imagemagick() { + local -r bin_dir="$1"; shift + + mkdir -p "$bin_dir" + + cat > "$bin_dir/magick" <<'MAGICK' +#!/usr/bin/env bash +set -euo pipefail + +if [ "${1:-}" = identify ]; then + exit 0 +fi + +printf 'started\n' > "${TEST_BLOCKING_MAGICK_STARTED:?}" +while [ ! -e "${TEST_BLOCKING_MAGICK_RELEASE:?}" ]; do + sleep 0.1 +done +exit "${TEST_BLOCKING_MAGICK_EXIT:-42}" +MAGICK + chmod 0755 "$bin_dir/magick" + cp "$bin_dir/magick" "$bin_dir/convert" +} + test::install_failing_generation_tools() { local -r bin_dir="$1"; shift local name |
