1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 09:46:44 +00:00

fix: improve cleanup code and fix Edge test isolation

- Refactor JetBrains Toolbox cleanup with _restore_whitelist helper
- Add MOLE_EDGE_APP_PATHS env var for test isolation
- Fix Edge old versions tests to avoid scanning system Edge installation
This commit is contained in:
tw93
2026-01-26 11:09:38 +08:00
parent 61723e8d86
commit b8c25e4056
3 changed files with 38 additions and 36 deletions

View File

@@ -219,10 +219,9 @@ clean_dev_jetbrains_toolbox() {
[[ -d "$toolbox_root" ]] || return 0
local keep_previous="${MOLE_JETBRAINS_TOOLBOX_KEEP:-1}"
if [[ ! "$keep_previous" =~ ^[0-9]+$ ]]; then
keep_previous=1
fi
[[ "$keep_previous" =~ ^[0-9]+$ ]] || keep_previous=1
# Save and filter whitelist patterns for toolbox path
local whitelist_overridden="false"
local -a original_whitelist=()
if [[ ${#WHITELIST_PATTERNS[@]} -gt 0 ]]; then
@@ -230,24 +229,26 @@ clean_dev_jetbrains_toolbox() {
local -a filtered_whitelist=()
local pattern
for pattern in "${WHITELIST_PATTERNS[@]}"; do
if [[ "$toolbox_root" == "$pattern" || "$pattern" == "$toolbox_root"* ]]; then
continue
fi
[[ "$toolbox_root" == "$pattern" || "$pattern" == "$toolbox_root"* ]] && continue
filtered_whitelist+=("$pattern")
done
WHITELIST_PATTERNS=("${filtered_whitelist[@]+${filtered_whitelist[@]}}")
whitelist_overridden="true"
fi
# Helper to restore whitelist on exit
_restore_whitelist() {
[[ "$whitelist_overridden" == "true" ]] && WHITELIST_PATTERNS=("${original_whitelist[@]}")
return 0
}
local -a product_dirs=()
while IFS= read -r -d '' product_dir; do
product_dirs+=("$product_dir")
done < <(command find "$toolbox_root" -mindepth 1 -maxdepth 1 -type d -print0 2> /dev/null)
if [[ ${#product_dirs[@]} -eq 0 ]]; then
if [[ "$whitelist_overridden" == "true" ]]; then
WHITELIST_PATTERNS=("${original_whitelist[@]}")
fi
_restore_whitelist
return 0
fi
@@ -315,9 +316,7 @@ clean_dev_jetbrains_toolbox() {
done < <(command find "$product_dir" -mindepth 1 -maxdepth 1 -type d -name "ch-*" -print0 2> /dev/null)
done
if [[ "$whitelist_overridden" == "true" ]]; then
WHITELIST_PATTERNS=("${original_whitelist[@]}")
fi
_restore_whitelist
}
# Other language tool caches.
clean_dev_other_langs() {

View File

@@ -110,10 +110,16 @@ clean_chrome_old_versions() {
# Remove old Microsoft Edge versions while keeping Current.
clean_edge_old_versions() {
local -a app_paths=(
"/Applications/Microsoft Edge.app"
"$HOME/Applications/Microsoft Edge.app"
)
# Allow override for testing
local -a app_paths
if [[ -n "${MOLE_EDGE_APP_PATHS:-}" ]]; then
IFS=':' read -ra app_paths <<< "$MOLE_EDGE_APP_PATHS"
else
app_paths=(
"/Applications/Microsoft Edge.app"
"$HOME/Applications/Microsoft Edge.app"
)
fi
# Match the exact Edge process name to avoid false positives (e.g., Microsoft Teams)
if pgrep -x "Microsoft Edge" > /dev/null 2>&1; then

View File

@@ -248,27 +248,24 @@ EOF
}
@test "clean_edge_old_versions removes old versions but keeps current" {
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" DRY_RUN=true bash --noprofile --norc <<'EOF'
# Create mock Edge directory structure
local EDGE_APP="$HOME/Applications/Microsoft Edge.app"
local VERSIONS_DIR="$EDGE_APP/Contents/Frameworks/Microsoft Edge Framework.framework/Versions"
mkdir -p "$VERSIONS_DIR"/{120.0.0.0,121.0.0.0,122.0.0.0}
ln -s "122.0.0.0" "$VERSIONS_DIR/Current"
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" DRY_RUN=true \
MOLE_EDGE_APP_PATHS="$EDGE_APP" bash --noprofile --norc <<'EOF'
set -euo pipefail
source "$PROJECT_ROOT/lib/core/common.sh"
source "$PROJECT_ROOT/lib/clean/user.sh"
pgrep() { return 1; }
export -f pgrep
# Create mock Edge directory structure
EDGE_APP="$HOME/Applications/Microsoft Edge.app"
VERSIONS_DIR="$EDGE_APP/Contents/Frameworks/Microsoft Edge Framework.framework/Versions"
mkdir -p "$VERSIONS_DIR"/{120.0.0.0,121.0.0.0,122.0.0.0}
# Create Current symlink pointing to 122.0.0.0
ln -s "122.0.0.0" "$VERSIONS_DIR/Current"
is_path_whitelisted() { return 1; }
get_path_size_kb() { echo "10240"; }
bytes_to_human() { echo "10M"; }
note_activity() { :; }
export -f is_path_whitelisted get_path_size_kb bytes_to_human note_activity
export -f pgrep is_path_whitelisted get_path_size_kb bytes_to_human note_activity
files_cleaned=0
total_size_cleaned=0
@@ -289,7 +286,14 @@ EOF
# Use a fresh temp directory for this test
TEST_HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-test8.XXXXXX")"
run env HOME="$TEST_HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
# Create Edge with only current version
local EDGE_APP="$TEST_HOME/Applications/Microsoft Edge.app"
local VERSIONS_DIR="$EDGE_APP/Contents/Frameworks/Microsoft Edge Framework.framework/Versions"
mkdir -p "$VERSIONS_DIR/122.0.0.0"
ln -s "122.0.0.0" "$VERSIONS_DIR/Current"
run env HOME="$TEST_HOME" PROJECT_ROOT="$PROJECT_ROOT" \
MOLE_EDGE_APP_PATHS="$EDGE_APP" bash --noprofile --norc <<'EOF'
set -euo pipefail
source "$PROJECT_ROOT/lib/core/common.sh"
source "$PROJECT_ROOT/lib/clean/user.sh"
@@ -301,17 +305,10 @@ bytes_to_human() { echo "10M"; }
note_activity() { :; }
export -f pgrep is_path_whitelisted get_path_size_kb bytes_to_human note_activity
# Initialize counters
files_cleaned=0
total_size_cleaned=0
total_items=0
# Create Edge with only current version
EDGE_APP="$HOME/Applications/Microsoft Edge.app"
VERSIONS_DIR="$EDGE_APP/Contents/Frameworks/Microsoft Edge Framework.framework/Versions"
mkdir -p "$VERSIONS_DIR/122.0.0.0"
ln -s "122.0.0.0" "$VERSIONS_DIR/Current"
clean_edge_old_versions
EOF