Skip to content

Commit b8f2a3f

Browse files
committed
fix(clean): clarify Docker whitelist and harden Bun cache cleanup
1 parent fb58ed4 commit b8f2a3f

File tree

3 files changed

+155
-13
lines changed

3 files changed

+155
-13
lines changed

lib/clean/dev.sh

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,39 @@ clean_dev_npm() {
8787
# pnpm not installed or not usable, just clean the default store directory
8888
safe_clean "$pnpm_default_store"/* "pnpm store"
8989
fi
90-
note_activity
91-
safe_clean ~/.tnpm/_cacache/* "tnpm cache directory"
92-
safe_clean ~/.tnpm/_logs/* "tnpm logs"
93-
safe_clean ~/.yarn/cache/* "Yarn cache"
94-
95-
# Bun cache - check if bun is installed and usable
90+
local bun_default_cache="$HOME/.bun/install/cache"
91+
local bun_cache_path="$bun_default_cache"
9692
if command -v bun > /dev/null 2>&1 && bun --version > /dev/null 2>&1; then
9793
clean_tool_cache "bun cache" bun pm cache rm
98-
local bun_cache_path
94+
9995
start_section_spinner "Checking bun cache path..."
10096
bun_cache_path=$(run_with_timeout 2 bun pm cache 2> /dev/null) || bun_cache_path=""
10197
stop_section_spinner
102-
# If custom cache path, also clean orphaned default location
103-
if [[ -n "$bun_cache_path" && "$bun_cache_path" != "$HOME/.bun/install/cache" ]]; then
104-
safe_clean ~/.bun/install/cache/* "Orphaned bun cache"
98+
99+
if [[ -z "$bun_cache_path" || "$bun_cache_path" != /* ]]; then
100+
bun_cache_path="$bun_default_cache"
101+
fi
102+
103+
local bun_cache_path_normalized="${bun_cache_path%/}"
104+
local bun_default_cache_normalized="${bun_default_cache%/}"
105+
if [[ -d "$bun_cache_path_normalized" ]]; then
106+
bun_cache_path_normalized=$(cd "$bun_cache_path_normalized" 2> /dev/null && pwd -P) || bun_cache_path_normalized="${bun_cache_path%/}"
107+
fi
108+
if [[ -d "$bun_default_cache_normalized" ]]; then
109+
bun_default_cache_normalized=$(cd "$bun_default_cache_normalized" 2> /dev/null && pwd -P) || bun_default_cache_normalized="${bun_default_cache%/}"
110+
fi
111+
112+
if [[ "$bun_cache_path_normalized" != "$bun_default_cache_normalized" ]]; then
113+
safe_clean "$bun_default_cache"/* "Orphaned bun cache"
105114
fi
106115
else
107-
# bun not installed, just clean the default cache directory
108-
safe_clean ~/.bun/install/cache/* "bun cache"
116+
safe_clean "$bun_default_cache"/* "Bun cache"
109117
fi
118+
110119
note_activity
120+
safe_clean ~/.tnpm/_cacache/* "tnpm cache directory"
121+
safe_clean ~/.tnpm/_logs/* "tnpm logs"
122+
safe_clean ~/.yarn/cache/* "Yarn cache"
111123
}
112124
# Python/pip ecosystem caches.
113125
clean_dev_python() {
@@ -258,6 +270,7 @@ clean_dev_docker() {
258270
is_path_whitelisted "$HOME/Library/Containers/com.docker.docker" ||
259271
is_path_whitelisted "$HOME/Library/Group Containers/group.com.docker"; then
260272
echo -e " ${GRAY}${ICON_WARNING}${NC} Docker unused data · skipped (whitelisted)"
273+
echo -e " ${GRAY}${ICON_REVIEW}${NC} ${GRAY}Review: mo clean --whitelist, protect Docker Desktop data to disable this prune${NC}"
261274
debug_log "Docker cleanup skipped: Docker paths found in whitelist"
262275
else
263276
clean_tool_cache "Docker unused data" docker system prune -af --volumes
@@ -270,6 +283,7 @@ clean_dev_docker() {
270283
else
271284
note_activity
272285
echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} Docker unused data · would clean"
286+
echo -e " ${GRAY}${ICON_REVIEW}${NC} ${GRAY}Review: mo clean --whitelist, protect Docker Desktop data to disable this prune${NC}"
273287
fi
274288
fi
275289
safe_clean ~/.docker/buildx/cache/* "Docker BuildX cache"

lib/manage/whitelist.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Firefox browser cache|$HOME/Library/Caches/Firefox/*|browser_cache
142142
Brave browser cache|$HOME/Library/Caches/BraveSoftware/Brave-Browser/*|browser_cache
143143
Surge proxy cache|$HOME/Library/Caches/com.nssurge.surge-mac/*|network_tools
144144
Surge configuration and data|$HOME/Library/Application Support/com.nssurge.surge-mac/*|network_tools
145-
Docker Desktop image cache|$HOME/Library/Containers/com.docker.docker/Data/*|container_cache
145+
Docker Desktop data (skip Docker unused data prune)|$HOME/Library/Containers/com.docker.docker/Data/*|container_cache
146146
Podman container cache|$HOME/.local/share/containers/cache/*|container_cache
147147
Font cache|$HOME/Library/Caches/com.apple.FontRegistry/*|system_cache
148148
Spotlight metadata cache|$HOME/Library/Caches/com.apple.spotlight/*|system_cache

tests/clean_dev_caches.bats

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,134 @@ EOF
159159
[[ "$output" != *"(custom path)"* ]]
160160
}
161161

162+
@test "clean_dev_npm cleans default bun cache when bun is unavailable" {
163+
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
164+
set -euo pipefail
165+
source "$PROJECT_ROOT/lib/core/common.sh"
166+
source "$PROJECT_ROOT/lib/clean/dev.sh"
167+
start_section_spinner() { :; }
168+
stop_section_spinner() { :; }
169+
clean_tool_cache() { echo "$1|$*"; }
170+
safe_clean() { echo "$2|$1"; }
171+
note_activity() { :; }
172+
run_with_timeout() { shift; "$@"; }
173+
npm() { return 0; }
174+
bun() { return 1; }
175+
export -f npm bun
176+
clean_dev_npm
177+
EOF
178+
179+
[ "$status" -eq 0 ]
180+
[[ "$output" == *"Bun cache|$HOME/.bun/install/cache/*"* ]]
181+
[[ "$output" != *"bun cache|bun cache bun pm cache rm"* ]]
182+
[[ "$output" != *"Orphaned bun cache"* ]]
183+
}
184+
185+
@test "clean_dev_npm uses bun cache command for default bun cache path" {
186+
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
187+
set -euo pipefail
188+
source "$PROJECT_ROOT/lib/core/common.sh"
189+
source "$PROJECT_ROOT/lib/clean/dev.sh"
190+
start_section_spinner() { :; }
191+
stop_section_spinner() { :; }
192+
clean_tool_cache() { echo "$1|$*"; }
193+
safe_clean() { echo "$2|$1"; }
194+
note_activity() { :; }
195+
run_with_timeout() { shift; "$@"; }
196+
npm() { return 0; }
197+
bun() {
198+
if [[ "$1" == "--version" ]]; then
199+
echo "1.2.0"
200+
return 0
201+
fi
202+
if [[ "$1" == "pm" && "$2" == "cache" && "${3:-}" == "rm" ]]; then
203+
return 0
204+
fi
205+
if [[ "$1" == "pm" && "$2" == "cache" ]]; then
206+
echo "$HOME/.bun/install/cache"
207+
return 0
208+
fi
209+
return 0
210+
}
211+
export -f npm bun
212+
clean_dev_npm
213+
EOF
214+
215+
[ "$status" -eq 0 ]
216+
[[ "$output" == *"bun cache|bun cache bun pm cache rm"* ]]
217+
[[ "$output" != *"Orphaned bun cache"* ]]
218+
}
219+
220+
@test "clean_dev_npm cleans orphaned default bun cache when custom path is configured" {
221+
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
222+
set -euo pipefail
223+
source "$PROJECT_ROOT/lib/core/common.sh"
224+
source "$PROJECT_ROOT/lib/clean/dev.sh"
225+
start_section_spinner() { :; }
226+
stop_section_spinner() { :; }
227+
clean_tool_cache() { echo "$1|$*"; }
228+
safe_clean() { echo "$2|$1"; }
229+
note_activity() { :; }
230+
run_with_timeout() { shift; "$@"; }
231+
npm() { return 0; }
232+
bun() {
233+
if [[ "$1" == "--version" ]]; then
234+
echo "1.2.0"
235+
return 0
236+
fi
237+
if [[ "$1" == "pm" && "$2" == "cache" && "${3:-}" == "rm" ]]; then
238+
return 0
239+
fi
240+
if [[ "$1" == "pm" && "$2" == "cache" ]]; then
241+
echo "/tmp/mole-bun-cache"
242+
return 0
243+
fi
244+
return 0
245+
}
246+
export -f npm bun
247+
clean_dev_npm
248+
EOF
249+
250+
[ "$status" -eq 0 ]
251+
[[ "$output" == *"bun cache|bun cache bun pm cache rm"* ]]
252+
[[ "$output" == *"Orphaned bun cache|$HOME/.bun/install/cache/*"* ]]
253+
}
254+
255+
@test "clean_dev_npm treats default bun cache path with trailing slash as same path" {
256+
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
257+
set -euo pipefail
258+
source "$PROJECT_ROOT/lib/core/common.sh"
259+
source "$PROJECT_ROOT/lib/clean/dev.sh"
260+
start_section_spinner() { :; }
261+
stop_section_spinner() { :; }
262+
clean_tool_cache() { echo "$1|$*"; }
263+
safe_clean() { echo "$2|$1"; }
264+
note_activity() { :; }
265+
run_with_timeout() { shift; "$@"; }
266+
npm() { return 0; }
267+
bun() {
268+
if [[ "$1" == "--version" ]]; then
269+
echo "1.2.0"
270+
return 0
271+
fi
272+
if [[ "$1" == "pm" && "$2" == "cache" && "${3:-}" == "rm" ]]; then
273+
return 0
274+
fi
275+
if [[ "$1" == "pm" && "$2" == "cache" ]]; then
276+
echo "$HOME/.bun/install/cache/"
277+
return 0
278+
fi
279+
return 0
280+
}
281+
export -f npm bun
282+
clean_dev_npm
283+
EOF
284+
285+
[ "$status" -eq 0 ]
286+
[[ "$output" == *"bun cache|bun cache bun pm cache rm"* ]]
287+
[[ "$output" != *"Orphaned bun cache"* ]]
288+
}
289+
162290
@test "clean_dev_docker skips when daemon not running" {
163291
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" DRY_RUN=false bash --noprofile --norc <<'EOF'
164292
set -euo pipefail

0 commit comments

Comments
 (0)