Skip to content

Commit 4d1afbf

Browse files
authored
Merge pull request #1231 from LuisMIguelFurlanettoSousa/fix/clean-item-count
fix(clean): report the cleaned item count instead of the raw target count
2 parents 073a7af + 2039244 commit 4d1afbf

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

bin/clean.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,13 @@ safe_clean() {
977977
size_human=$(bytes_to_human "$((total_size_kb * 1024))")
978978

979979
# Multi-target cleanups report the item count as part of the detail
980-
# column, keeping the label clean: "npm logs · 2 items, 2KB".
980+
# column, keeping the label clean: "npm logs · 2 items, 2KB". Use the
981+
# actually-cleaned count (total_count), not the raw target count, so it
982+
# stays consistent with the reported size after protected, whitelisted,
983+
# missing, and deduplicated targets have been dropped.
981984
local count_note=""
982-
if [[ ${#targets[@]} -gt 1 ]]; then
983-
count_note="${#targets[@]} items, "
985+
if [[ $total_count -gt 1 ]]; then
986+
count_note="$total_count items, "
984987
fi
985988

986989
if [[ "$DRY_RUN" == "true" ]]; then

tests/clean_core.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,45 @@ run_clean_dry_run() {
7474
"$PROJECT_ROOT/mole" clean --dry-run
7575
}
7676

77+
@test "safe_clean item count reflects cleaned items, not raw target count" {
78+
local base="$HOME/safe_clean_count"
79+
mkdir -p "$base"
80+
printf 'xxxx' > "$base/a"
81+
printf 'xxxx' > "$base/b"
82+
printf 'xxxx' > "$base/keep"
83+
84+
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" MOLE_TEST_MODE=1 bash --noprofile --norc <<EOF
85+
set -euo pipefail
86+
source "\$PROJECT_ROOT/lib/core/common.sh"
87+
source "\$PROJECT_ROOT/bin/clean.sh"
88+
DRY_RUN=false
89+
files_cleaned=0
90+
total_size_cleaned=0
91+
total_items=0
92+
start_section_spinner() { :; }
93+
stop_section_spinner() { :; }
94+
start_inline_spinner() { :; }
95+
stop_inline_spinner() { :; }
96+
note_activity() { :; }
97+
# One of the three targets is whitelisted, so only two are actually cleaned.
98+
is_path_whitelisted() { [[ "\$1" == "$base/keep" ]]; }
99+
safe_remove() { /bin/rm -rf "\$1"; return 0; }
100+
safe_clean "$base/a" "$base/b" "$base/keep" "Test cache"
101+
EOF
102+
103+
[ "$status" -eq 0 ] || return 1
104+
# Two items were removed, so the detail column must say "2 items", not "3".
105+
# Every assertion ends with || return 1: bare [[ ]] failures mid-test can be
106+
# swallowed and let the test pass vacuously (same shape as #886).
107+
[[ "$output" == *"2 items"* ]] || return 1
108+
[[ "$output" != *"3 items"* ]] || return 1
109+
[[ ! -e "$base/a" ]] || return 1
110+
[[ ! -e "$base/b" ]] || return 1
111+
[[ -e "$base/keep" ]] || return 1
112+
113+
rm -rf "$base"
114+
}
115+
77116
@test "mo clean --dry-run skips system cleanup in non-interactive mode" {
78117
set_mock_sudo_uncached
79118
run_clean_dry_run

0 commit comments

Comments
 (0)