Skip to content

fix: respect --dry-run in all cleanup functions#43

Open
qaz74107410 wants to merge 1 commit into
jemishavasoya:mainfrom
qaz74107410:fix/dry-run-bypass-cleanup-functions
Open

fix: respect --dry-run in all cleanup functions#43
qaz74107410 wants to merge 1 commit into
jemishavasoya:mainfrom
qaz74107410:fix/dry-run-bypass-cleanup-functions

Conversation

@qaz74107410
Copy link
Copy Markdown

@qaz74107410 qaz74107410 commented Apr 6, 2026

Problem

Several cleanup functions bypassed the --dry-run flag entirely by calling bare rm -rf / sudo rm -rf instead of the existing safe_rm / safe_sudo_rm helpers. Running ./dev-cleaner.sh --dry-run silently skipped reporting deletions for these sections, making the dry-run output incomplete and misleading.

Affected functions:

  • cleanup_cocoapods
  • cleanup_ide_caches
  • cleanup_system_junk
  • cleanup_browser_caches
  • cleanup_app_containers
  • cleanup_timemachine_snapshots

Changes

Function Fix
cleanup_cocoapods rm -rfsafe_rm -r
cleanup_ide_caches rm -rfsafe_rm -r; use $HOME for paths with spaces
cleanup_system_junk sudo rm -rfsafe_sudo_rm -r; rm -rfsafe_rm -r
cleanup_browser_caches rm -rfsafe_rm -r; use $HOME for paths with spaces
cleanup_app_containers rm -rf … || truesafe_rm -r
cleanup_timemachine_snapshots guard tmutil deletelocalsnapshots with $DRY_RUN check

Test

The following script verifies that each affected function:

  1. Prints [DRY-RUN] Would delete … output
  2. Leaves all directories intact

It works by creating a mock $HOME under /tmp, sourcing only the function definitions from the script, and calling each function with DRY_RUN=true.

test_dryrun.sh
#!/usr/bin/env bash
# Test: --dry-run is respected by all affected cleanup functions

set -eo pipefail

PASS=0
FAIL=0

ok()   { echo "  [PASS] $1"; PASS=$((PASS+1)); }
fail() { echo "  [FAIL] $1"; FAIL=$((FAIL+1)); }

MOCK_HOME=$(mktemp -d /tmp/mock_home_XXXXXX)
trap 'rm -rf "$MOCK_HOME"' EXIT

mkdir -p \
  "$MOCK_HOME/.cocoapods/repos/someRepo" \
  "$MOCK_HOME/Library/Caches/CocoaPods/SomeCache" \
  "$MOCK_HOME/Library/Caches/JetBrains/IntelliJ" \
  "$MOCK_HOME/Library/Application Support/Code/Cache/data" \
  "$MOCK_HOME/Library/Application Support/Code/CachedData/data" \
  "$MOCK_HOME/Library/Application Support/Code/User/workspaceStorage/ws" \
  "$MOCK_HOME/Library/Logs/SomeLog" \
  "$MOCK_HOME/Library/Caches/Google/Chrome/Default" \
  "$MOCK_HOME/Library/Caches/BraveSoftware/Brave-Browser/Default" \
  "$MOCK_HOME/Library/Caches/Firefox/Profiles" \
  "$MOCK_HOME/Library/Caches/com.apple.Safari/data" \
  "$MOCK_HOME/Library/Caches/com.microsoft.edgemac/data" \
  "$MOCK_HOME/Library/Caches/com.operasoftware.Opera/data" \
  "$MOCK_HOME/Library/Caches/com.operasoftware.OperaGX/data" \
  "$MOCK_HOME/Library/Containers/com.tinyspeck.slackmacgap/Data/Library/Caches/slack_cache" \
  "$MOCK_HOME/Library/Containers/com.microsoft.teams2/Data/Library/Caches/teams_cache" \
  "$MOCK_HOME/Library/Containers/net.whatsapp.WhatsApp/Data/Library/Caches/wa_cache" \
  "$MOCK_HOME/Library/Application Support/discord/Cache/item" \
  "$MOCK_HOME/Library/Application Support/discord/Code Cache/item" \
  "$MOCK_HOME/Library/Caches/com.spotify.client/data" \
  "$MOCK_HOME/Library/Application Support/Spotify/PersistentCache/item"

mkdir -p "$MOCK_HOME/.Trash" && touch "$MOCK_HOME/.Trash/junk.txt"

SCRIPT_SRC="./dev-cleaner.sh"
TRIMMED=$(mktemp /tmp/trimmed_XXXXXX.sh)
trap 'rm -f "$TRIMMED"; rm -rf "$MOCK_HOME"' EXIT

awk '/^# --- Main Display Function ---/{exit} {print}' "$SCRIPT_SRC" > "$TRIMMED"

FLUTTER_SEARCH_DIR="" HOME="$MOCK_HOME" DRY_RUN=true source "$TRIMMED"
HOME="$MOCK_HOME"

assert_dryrun() {
  local func="$1" sentinel_dir="$2"
  shift 2
  local output
  output=$(HOME="$MOCK_HOME" DRY_RUN=true "$func" "$@" 2>&1)

  if echo "$output" | grep -q "\[DRY-RUN\]"; then
    ok "$func: prints [DRY-RUN] output"
  else
    fail "$func: missing [DRY-RUN] output"
    echo "$output" | sed 's/^/       /'
  fi

  if [ -d "$sentinel_dir" ]; then
    ok "$func: sentinel directory not deleted"
  else
    fail "$func: sentinel directory was DELETED during dry-run!"
  fi
}

echo "Running dry-run tests..."
echo "────────────────────────────────────────────"

assert_dryrun cleanup_cocoapods \
  "$MOCK_HOME/.cocoapods/repos/someRepo"

assert_dryrun cleanup_ide_caches \
  "$MOCK_HOME/Library/Caches/JetBrains/IntelliJ"

output=$(bash -c "
  source '$TRIMMED'
  DRY_RUN=true
  HOME='$MOCK_HOME'
  safe_rm -r '$MOCK_HOME/Library/Logs/'*
" 2>&1)
if echo "$output" | grep -q "\[DRY-RUN\]"; then
  ok "cleanup_system_junk (Logs via safe_rm): prints [DRY-RUN] output"
else
  fail "cleanup_system_junk (Logs via safe_rm): missing [DRY-RUN] output"
fi
if [ -d "$MOCK_HOME/Library/Logs/SomeLog" ]; then
  ok "cleanup_system_junk (Logs via safe_rm): sentinel not deleted"
else
  fail "cleanup_system_junk (Logs via safe_rm): sentinel was DELETED"
fi

assert_dryrun cleanup_browser_caches \
  "$MOCK_HOME/Library/Caches/Google/Chrome/Default"

assert_dryrun cleanup_app_containers \
  "$MOCK_HOME/Library/Containers/com.tinyspeck.slackmacgap/Data/Library/Caches/slack_cache"

echo "────────────────────────────────────────────"
echo "Results: $PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ]

Result

Running dry-run tests...
────────────────────────────────────────────
  [PASS] cleanup_cocoapods: prints [DRY-RUN] output
  [PASS] cleanup_cocoapods: sentinel directory not deleted
  [PASS] cleanup_ide_caches: prints [DRY-RUN] output
  [PASS] cleanup_ide_caches: sentinel directory not deleted
  [PASS] cleanup_system_junk (Logs via safe_rm): prints [DRY-RUN] output
  [PASS] cleanup_system_junk (Logs via safe_rm): sentinel not deleted
  [PASS] cleanup_browser_caches: prints [DRY-RUN] output
  [PASS] cleanup_browser_caches: sentinel directory not deleted
  [PASS] cleanup_app_containers: prints [DRY-RUN] output
  [PASS] cleanup_app_containers: sentinel directory not deleted
────────────────────────────────────────────

Results: 10 passed, 0 failed

Test plan

  • Run ./dev-cleaner.sh --dry-run and confirm all 14 options now print [DRY-RUN] Would delete … lines for the previously silent functions
  • Run without --dry-run and confirm cleanup still works as expected
  • Verify no actual files are deleted during a dry-run pass

Several cleanup functions bypassed the dry-run flag by calling bare
rm -rf / sudo rm -rf instead of the existing safe_rm / safe_sudo_rm
helpers. Running with --dry-run would silently skip reporting deletions
for CocoaPods, IDE caches, System Junk, browser caches, app containers,
and Time Machine snapshots.

Affected functions and changes:
- cleanup_cocoapods: rm -rf → safe_rm -r
- cleanup_ide_caches: rm -rf → safe_rm -r (use $HOME for paths with spaces)
- cleanup_system_junk: sudo rm -rf → safe_sudo_rm -r; rm -rf → safe_rm -r
- cleanup_browser_caches: rm -rf → safe_rm -r
- cleanup_app_containers: rm -rf … || true → safe_rm -r
- cleanup_timemachine_snapshots: guard tmutil delete call with $DRY_RUN check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant