From 241e6a7a348a7a3db13d0726d6a3b16130ccc2b8 Mon Sep 17 00:00:00 2001 From: tw93 Date: Sun, 1 Mar 2026 20:35:49 +0800 Subject: [PATCH] fix(purge): avoid counting failed dry-run removals --- lib/clean/project.sh | 24 +++++++++++++++--------- tests/purge.bats | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/clean/project.sh b/lib/clean/project.sh index 3d768e5..d6158b4 100644 --- a/lib/clean/project.sh +++ b/lib/clean/project.sh @@ -1387,20 +1387,26 @@ clean_project_artifacts() { if [[ -t 1 ]]; then start_inline_spinner "Cleaning $project_path/$artifact_type..." fi + local removal_recorded=false if [[ -e "$item_path" ]]; then - safe_remove "$item_path" true - if [[ "$dry_run_mode" == "1" || ! -e "$item_path" ]]; then - local current_total=$(cat "$stats_dir/purge_stats" 2> /dev/null || echo "0") - echo "$((current_total + size_kb))" > "$stats_dir/purge_stats" - cleaned_count=$((cleaned_count + 1)) + if safe_remove "$item_path" true; then + if [[ "$dry_run_mode" == "1" || ! -e "$item_path" ]]; then + local current_total + current_total=$(cat "$stats_dir/purge_stats" 2> /dev/null || echo "0") + echo "$((current_total + size_kb))" > "$stats_dir/purge_stats" + cleaned_count=$((cleaned_count + 1)) + removal_recorded=true + fi fi fi if [[ -t 1 ]]; then stop_inline_spinner - if [[ "$dry_run_mode" == "1" ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} [DRY RUN] $project_path, $artifact_type${NC}, ${GREEN}$size_human${NC}" - else - echo -e "${GREEN}${ICON_SUCCESS}${NC} $project_path, $artifact_type${NC}, ${GREEN}$size_human${NC}" + if [[ "$removal_recorded" == "true" ]]; then + if [[ "$dry_run_mode" == "1" ]]; then + echo -e "${GREEN}${ICON_SUCCESS}${NC} [DRY RUN] $project_path, $artifact_type${NC}, ${GREEN}$size_human${NC}" + else + echo -e "${GREEN}${ICON_SUCCESS}${NC} $project_path, $artifact_type${NC}, ${GREEN}$size_human${NC}" + fi fi fi done diff --git a/tests/purge.bats b/tests/purge.bats index 63b0529..0137f7a 100644 --- a/tests/purge.bats +++ b/tests/purge.bats @@ -683,6 +683,37 @@ EOF [[ "$status" -eq 0 ]] || [[ "$status" -eq 2 ]] } +@test "clean_project_artifacts: dry-run does not count failed removals" { + 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/project.sh" + +mkdir -p "$HOME/.cache/mole" +echo "0" > "$HOME/.cache/mole/purge_stats" + +mkdir -p "$HOME/www/test-project/node_modules" +echo "test data" > "$HOME/www/test-project/node_modules/file.js" +touch "$HOME/www/test-project/package.json" +touch -t 202001010101 "$HOME/www/test-project/node_modules" "$HOME/www/test-project/package.json" "$HOME/www/test-project" + +PURGE_SEARCH_PATHS=("$HOME/www") +safe_remove() { return 1; } + +export MOLE_DRY_RUN=1 +clean_project_artifacts + +stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole" +echo "COUNT=$(cat "$stats_dir/purge_count" 2> /dev/null || echo missing)" +echo "SIZE=$(cat "$stats_dir/purge_stats" 2> /dev/null || echo missing)" +[[ -d "$HOME/www/test-project/node_modules" ]] +EOF + + [ "$status" -eq 0 ] + [[ "$output" == *"COUNT=0"* ]] + [[ "$output" == *"SIZE=0"* ]] +} + @test "clean_project_artifacts: scans and finds artifacts" { if ! command -v gtimeout >/dev/null 2>&1 && ! command -v timeout >/dev/null 2>&1; then skip "gtimeout/timeout not available"