mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 11:31:46 +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:
@@ -219,10 +219,9 @@ clean_dev_jetbrains_toolbox() {
|
|||||||
[[ -d "$toolbox_root" ]] || return 0
|
[[ -d "$toolbox_root" ]] || return 0
|
||||||
|
|
||||||
local keep_previous="${MOLE_JETBRAINS_TOOLBOX_KEEP:-1}"
|
local keep_previous="${MOLE_JETBRAINS_TOOLBOX_KEEP:-1}"
|
||||||
if [[ ! "$keep_previous" =~ ^[0-9]+$ ]]; then
|
[[ "$keep_previous" =~ ^[0-9]+$ ]] || keep_previous=1
|
||||||
keep_previous=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
# Save and filter whitelist patterns for toolbox path
|
||||||
local whitelist_overridden="false"
|
local whitelist_overridden="false"
|
||||||
local -a original_whitelist=()
|
local -a original_whitelist=()
|
||||||
if [[ ${#WHITELIST_PATTERNS[@]} -gt 0 ]]; then
|
if [[ ${#WHITELIST_PATTERNS[@]} -gt 0 ]]; then
|
||||||
@@ -230,24 +229,26 @@ clean_dev_jetbrains_toolbox() {
|
|||||||
local -a filtered_whitelist=()
|
local -a filtered_whitelist=()
|
||||||
local pattern
|
local pattern
|
||||||
for pattern in "${WHITELIST_PATTERNS[@]}"; do
|
for pattern in "${WHITELIST_PATTERNS[@]}"; do
|
||||||
if [[ "$toolbox_root" == "$pattern" || "$pattern" == "$toolbox_root"* ]]; then
|
[[ "$toolbox_root" == "$pattern" || "$pattern" == "$toolbox_root"* ]] && continue
|
||||||
continue
|
|
||||||
fi
|
|
||||||
filtered_whitelist+=("$pattern")
|
filtered_whitelist+=("$pattern")
|
||||||
done
|
done
|
||||||
WHITELIST_PATTERNS=("${filtered_whitelist[@]+${filtered_whitelist[@]}}")
|
WHITELIST_PATTERNS=("${filtered_whitelist[@]+${filtered_whitelist[@]}}")
|
||||||
whitelist_overridden="true"
|
whitelist_overridden="true"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Helper to restore whitelist on exit
|
||||||
|
_restore_whitelist() {
|
||||||
|
[[ "$whitelist_overridden" == "true" ]] && WHITELIST_PATTERNS=("${original_whitelist[@]}")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
local -a product_dirs=()
|
local -a product_dirs=()
|
||||||
while IFS= read -r -d '' product_dir; do
|
while IFS= read -r -d '' product_dir; do
|
||||||
product_dirs+=("$product_dir")
|
product_dirs+=("$product_dir")
|
||||||
done < <(command find "$toolbox_root" -mindepth 1 -maxdepth 1 -type d -print0 2> /dev/null)
|
done < <(command find "$toolbox_root" -mindepth 1 -maxdepth 1 -type d -print0 2> /dev/null)
|
||||||
|
|
||||||
if [[ ${#product_dirs[@]} -eq 0 ]]; then
|
if [[ ${#product_dirs[@]} -eq 0 ]]; then
|
||||||
if [[ "$whitelist_overridden" == "true" ]]; then
|
_restore_whitelist
|
||||||
WHITELIST_PATTERNS=("${original_whitelist[@]}")
|
|
||||||
fi
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
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 < <(command find "$product_dir" -mindepth 1 -maxdepth 1 -type d -name "ch-*" -print0 2> /dev/null)
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ "$whitelist_overridden" == "true" ]]; then
|
_restore_whitelist
|
||||||
WHITELIST_PATTERNS=("${original_whitelist[@]}")
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
# Other language tool caches.
|
# Other language tool caches.
|
||||||
clean_dev_other_langs() {
|
clean_dev_other_langs() {
|
||||||
|
|||||||
@@ -110,10 +110,16 @@ clean_chrome_old_versions() {
|
|||||||
|
|
||||||
# Remove old Microsoft Edge versions while keeping Current.
|
# Remove old Microsoft Edge versions while keeping Current.
|
||||||
clean_edge_old_versions() {
|
clean_edge_old_versions() {
|
||||||
local -a app_paths=(
|
# Allow override for testing
|
||||||
"/Applications/Microsoft Edge.app"
|
local -a app_paths
|
||||||
"$HOME/Applications/Microsoft Edge.app"
|
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)
|
# Match the exact Edge process name to avoid false positives (e.g., Microsoft Teams)
|
||||||
if pgrep -x "Microsoft Edge" > /dev/null 2>&1; then
|
if pgrep -x "Microsoft Edge" > /dev/null 2>&1; then
|
||||||
|
|||||||
@@ -248,27 +248,24 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "clean_edge_old_versions removes old versions but keeps current" {
|
@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
|
set -euo pipefail
|
||||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||||
source "$PROJECT_ROOT/lib/clean/user.sh"
|
source "$PROJECT_ROOT/lib/clean/user.sh"
|
||||||
|
|
||||||
pgrep() { return 1; }
|
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; }
|
is_path_whitelisted() { return 1; }
|
||||||
get_path_size_kb() { echo "10240"; }
|
get_path_size_kb() { echo "10240"; }
|
||||||
bytes_to_human() { echo "10M"; }
|
bytes_to_human() { echo "10M"; }
|
||||||
note_activity() { :; }
|
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
|
files_cleaned=0
|
||||||
total_size_cleaned=0
|
total_size_cleaned=0
|
||||||
@@ -289,7 +286,14 @@ EOF
|
|||||||
# Use a fresh temp directory for this test
|
# Use a fresh temp directory for this test
|
||||||
TEST_HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-test8.XXXXXX")"
|
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
|
set -euo pipefail
|
||||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||||
source "$PROJECT_ROOT/lib/clean/user.sh"
|
source "$PROJECT_ROOT/lib/clean/user.sh"
|
||||||
@@ -301,17 +305,10 @@ bytes_to_human() { echo "10M"; }
|
|||||||
note_activity() { :; }
|
note_activity() { :; }
|
||||||
export -f pgrep 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
|
||||||
|
|
||||||
# Initialize counters
|
|
||||||
files_cleaned=0
|
files_cleaned=0
|
||||||
total_size_cleaned=0
|
total_size_cleaned=0
|
||||||
total_items=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
|
clean_edge_old_versions
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user