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

fix(dev): normalize npm cache paths and add maven function guard

- Normalize npm cache paths with pwd -P to handle symlinks and
  trailing slashes, preventing duplicate scans of same directory
- Add declare -f check before calling clean_maven_repository for
  robustness when dynamic source fails
- Add test for trailing slash handling

Addresses: dev.sh (line 27, 48, 602)
This commit is contained in:
tw93
2026-02-21 20:22:01 +08:00
parent 0597021fd4
commit 9440d9e52a
2 changed files with 40 additions and 2 deletions

View File

@@ -44,8 +44,18 @@ clean_dev_npm() {
safe_clean "$npm_default_cache/${npm_residual_dirs[$i]}"/* "${npm_descriptions[$i]}" safe_clean "$npm_default_cache/${npm_residual_dirs[$i]}"/* "${npm_descriptions[$i]}"
done done
# Normalize paths for comparison (remove trailing slash + resolve symlinked dirs)
local npm_cache_path_normalized="${npm_cache_path%/}"
local npm_default_cache_normalized="${npm_default_cache%/}"
if [[ -d "$npm_cache_path_normalized" ]]; then
npm_cache_path_normalized=$(cd "$npm_cache_path_normalized" 2> /dev/null && pwd -P) || npm_cache_path_normalized="${npm_cache_path%/}"
fi
if [[ -d "$npm_default_cache_normalized" ]]; then
npm_default_cache_normalized=$(cd "$npm_default_cache_normalized" 2> /dev/null && pwd -P) || npm_default_cache_normalized="${npm_default_cache%/}"
fi
# Clean custom npm cache path (if different from default) # Clean custom npm cache path (if different from default)
if [[ "$npm_cache_path" != "$npm_default_cache" ]]; then if [[ "$npm_cache_path_normalized" != "$npm_default_cache_normalized" ]]; then
for i in "${!npm_residual_dirs[@]}"; do for i in "${!npm_residual_dirs[@]}"; do
safe_clean "$npm_cache_path/${npm_residual_dirs[$i]}"/* "${npm_descriptions[$i]} (custom path)" safe_clean "$npm_cache_path/${npm_residual_dirs[$i]}"/* "${npm_descriptions[$i]} (custom path)"
done done
@@ -600,7 +610,9 @@ clean_dev_jvm() {
# Source Maven cleanup module (requires bash for BASH_SOURCE) # Source Maven cleanup module (requires bash for BASH_SOURCE)
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source "$(dirname "${BASH_SOURCE[0]}")/maven.sh" 2> /dev/null || true source "$(dirname "${BASH_SOURCE[0]}")/maven.sh" 2> /dev/null || true
clean_maven_repository if declare -f clean_maven_repository > /dev/null 2>&1; then
clean_maven_repository
fi
safe_clean ~/.sbt/* "SBT cache" safe_clean ~/.sbt/* "SBT cache"
safe_clean ~/.ivy2/cache/* "Ivy cache" safe_clean ~/.ivy2/cache/* "Ivy cache"
safe_clean ~/.gradle/caches/* "Gradle cache" safe_clean ~/.gradle/caches/* "Gradle cache"

View File

@@ -133,6 +133,32 @@ EOF
[[ "$output" != *"(custom path)"* ]] [[ "$output" != *"(custom path)"* ]]
} }
@test "clean_dev_npm treats default cache path with trailing slash as same path" {
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"
start_section_spinner() { :; }
stop_section_spinner() { :; }
clean_tool_cache() { :; }
safe_clean() { echo "$2|$1"; }
note_activity() { :; }
run_with_timeout() { shift; "$@"; }
npm() {
if [[ "$1" == "config" && "$2" == "get" && "$3" == "cache" ]]; then
echo "$HOME/.npm/"
return 0
fi
return 0
}
clean_dev_npm
EOF
[ "$status" -eq 0 ]
[[ "$output" == *"npm cache directory|$HOME/.npm/_cacache/*"* ]]
[[ "$output" != *"(custom path)"* ]]
}
@test "clean_dev_docker skips when daemon not running" { @test "clean_dev_docker skips when daemon not running" {
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" MO_DEBUG=1 DRY_RUN=false bash --noprofile --norc <<'EOF' run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" MO_DEBUG=1 DRY_RUN=false bash --noprofile --norc <<'EOF'
set -euo pipefail set -euo pipefail