1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 18:30:08 +00:00

fix(purge): avoid counting failed dry-run removals

This commit is contained in:
tw93
2026-03-01 20:35:49 +08:00
parent 05446e0847
commit 241e6a7a34
2 changed files with 46 additions and 9 deletions

View File

@@ -1387,22 +1387,28 @@ 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 safe_remove "$item_path" true; then
if [[ "$dry_run_mode" == "1" || ! -e "$item_path" ]]; then
local current_total=$(cat "$stats_dir/purge_stats" 2> /dev/null || echo "0")
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 [[ "$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
# Update count
echo "$cleaned_count" > "$stats_dir/purge_count"

View File

@@ -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"