diff --git a/tests/zip_images_resize_recursive.bats b/tests/zip_images_resize_recursive.bats index 3e3e73e..3a18100 100644 --- a/tests/zip_images_resize_recursive.bats +++ b/tests/zip_images_resize_recursive.bats @@ -53,6 +53,29 @@ teardown() { assert_equal "$(grep -c 'Resized' <<<"$output")" "2" } +@test "processes ZIPs in descending size order" { + # Create ZIPs of different sizes by varying image dimensions + local small medium large + small="$TEST_WORKDIR/small.zip" + medium="$TEST_WORKDIR/medium.zip" + large="$TEST_WORKDIR/large.zip" + + convert -size 50x50 xc:gray "$TEST_WORKDIR/s.jpg" + convert -size 100x100 xc:gray "$TEST_WORKDIR/m.jpg" + convert -size 200x200 xc:gray "$TEST_WORKDIR/l.jpg" + (cd "$TEST_WORKDIR" && zip -q small.zip s.jpg) + (cd "$TEST_WORKDIR" && zip -q medium.zip m.jpg) + (cd "$TEST_WORKDIR" && zip -q large.zip l.jpg) + + run "$SCRIPT" "$TEST_WORKDIR" + assert_success + + # Extract the order ZIPs were processed from the output + local order + order=$(grep 'Processing file' <<<"$output" | grep -o '[a-z]*.zip' | tr '\n' ' ') + assert_equal "$order" "large.zip medium.zip small.zip " +} + @test "no temp files left in /dev/shm after recursive run" { make_zip "$TEST_WORKDIR/a.zip" "$FIXTURES/unprocessed.jpg" make_zip "$TEST_WORKDIR/b.zip" "$FIXTURES/processed.jpg" diff --git a/zip_images_resize_recursive.sh b/zip_images_resize_recursive.sh index 51f7d1e..820b6f1 100755 --- a/zip_images_resize_recursive.sh +++ b/zip_images_resize_recursive.sh @@ -10,8 +10,9 @@ fi echo "Resizing images in ZIPs recursively in $SEARCH_DIR" -find "$SEARCH_DIR" -iname '*.zip' -print0 | while IFS= read -r -d '' f -do - "$SCRIPT_DIR/zip_images_resize.sh" "$f" -done +find "$SEARCH_DIR" -iname '*.zip' -printf '%s\t%p\0' | \ + sort -z -t$'\t' -k1,1rn | \ + while IFS=$'\t' read -r -d '' _size f; do + "$SCRIPT_DIR/zip_images_resize.sh" "$f" + done