1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 16:45:07 +00:00

fix(clean): guard empty Xcode DeviceSupport arrays

This commit is contained in:
tw93
2026-03-07 23:07:38 +08:00
parent b12308f3ad
commit 50efe51565
2 changed files with 55 additions and 35 deletions

View File

@@ -359,47 +359,49 @@ clean_xcode_device_support() {
version_dirs+=("$entry") version_dirs+=("$entry")
done < <(command find "$ds_dir" -mindepth 1 -maxdepth 1 -print0 2> /dev/null) done < <(command find "$ds_dir" -mindepth 1 -maxdepth 1 -print0 2> /dev/null)
# Sort by modification time (most recent first) if [[ ${#version_dirs[@]} -gt 0 ]]; then
local -a sorted_dirs=() # Sort by modification time (most recent first)
while IFS= read -r line; do local -a sorted_dirs=()
sorted_dirs+=("${line#* }") while IFS= read -r line; do
done < <( sorted_dirs+=("${line#* }")
for entry in "${version_dirs[@]}"; do done < <(
printf '%s %s\n' "$(stat -f%m "$entry" 2> /dev/null || echo 0)" "$entry" for entry in "${version_dirs[@]}"; do
done | sort -rn printf '%s %s\n' "$(stat -f%m "$entry" 2> /dev/null || echo 0)" "$entry"
) done | sort -rn
)
# Get stale versions (everything after keep_count) # Get stale versions (everything after keep_count)
local -a stale_dirs=("${sorted_dirs[@]:$keep_count}") local -a stale_dirs=("${sorted_dirs[@]:$keep_count}")
if [[ ${#stale_dirs[@]} -gt 0 ]]; then if [[ ${#stale_dirs[@]} -gt 0 ]]; then
# Calculate total size of stale versions # Calculate total size of stale versions
local stale_size_kb=0 entry_size_kb local stale_size_kb=0 entry_size_kb
for stale_entry in "${stale_dirs[@]}"; do
entry_size_kb=$(get_path_size_kb "$stale_entry" 2> /dev/null || echo 0)
stale_size_kb=$((stale_size_kb + entry_size_kb))
done
local stale_size_human
stale_size_human=$(bytes_to_human "$((stale_size_kb * 1024))")
if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} ${display_name} · would remove ${#stale_dirs[@]} old versions (${stale_size_human}), keeping ${keep_count} most recent"
note_activity
else
# Remove old versions
local removed_count=0
for stale_entry in "${stale_dirs[@]}"; do for stale_entry in "${stale_dirs[@]}"; do
if should_protect_path "$stale_entry" || is_path_whitelisted "$stale_entry"; then entry_size_kb=$(get_path_size_kb "$stale_entry" 2> /dev/null || echo 0)
continue stale_size_kb=$((stale_size_kb + entry_size_kb))
fi
if safe_remove "$stale_entry"; then
removed_count=$((removed_count + 1))
fi
done done
local stale_size_human
stale_size_human=$(bytes_to_human "$((stale_size_kb * 1024))")
if [[ $removed_count -gt 0 ]]; then if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${GREEN}${ICON_SUCCESS}${NC} ${display_name} · removed ${removed_count} old versions, ${stale_size_human}" echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} ${display_name} · would remove ${#stale_dirs[@]} old versions (${stale_size_human}), keeping ${keep_count} most recent"
note_activity note_activity
else
# Remove old versions
local removed_count=0
for stale_entry in "${stale_dirs[@]}"; do
if should_protect_path "$stale_entry" || is_path_whitelisted "$stale_entry"; then
continue
fi
if safe_remove "$stale_entry"; then
removed_count=$((removed_count + 1))
fi
done
if [[ $removed_count -gt 0 ]]; then
echo -e " ${GREEN}${ICON_SUCCESS}${NC} ${display_name} · removed ${removed_count} old versions, ${stale_size_human}"
note_activity
fi
fi fi
fi fi
fi fi

View File

@@ -136,6 +136,24 @@ EOF
[[ "$output" != *"NDK versions"* ]] [[ "$output" != *"NDK versions"* ]]
} }
@test "clean_xcode_device_support handles empty directories under nounset" {
local ds_dir="$HOME/EmptyDeviceSupport"
mkdir -p "$ds_dir"
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
set -euo pipefail
source "$PROJECT_ROOT/lib/core/common.sh"
source "$PROJECT_ROOT/lib/clean/dev.sh"
note_activity() { :; }
safe_clean() { :; }
clean_xcode_device_support "$HOME/EmptyDeviceSupport" "iOS DeviceSupport"
echo "survived"
EOF
[ "$status" -eq 0 ]
[[ "$output" == *"survived"* ]]
}
@test "clean_xcode_documentation_cache keeps newest DeveloperDocumentation index" { @test "clean_xcode_documentation_cache keeps newest DeveloperDocumentation index" {
local doc_root="$HOME/DocumentationCache" local doc_root="$HOME/DocumentationCache"
mkdir -p "$doc_root" mkdir -p "$doc_root"