mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 13:16:47 +00:00
feat: enhance clean logic
1. Add recursive empty directory cleanup for Application Support and Caches. 2. Add support for cleaning old Edge Updater versions.
This commit is contained in:
@@ -103,6 +103,39 @@ is_path_whitelisted() {
|
||||
[[ "$1" == *"128.0.0.0"* ]] && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
@test "clean_edge_updater_old_versions keeps latest version" {
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" DRY_RUN=true 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
|
||||
|
||||
UPDATER_DIR="$HOME/Library/Application Support/Microsoft/EdgeUpdater/apps/msedge-stable"
|
||||
mkdir -p "$UPDATER_DIR"/{117.0.2045.60,118.0.2088.46,119.0.2108.9}
|
||||
|
||||
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
|
||||
|
||||
files_cleaned=0
|
||||
total_size_cleaned=0
|
||||
total_items=0
|
||||
|
||||
clean_edge_updater_old_versions
|
||||
|
||||
echo "Cleaned: $files_cleaned items"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Edge updater old versions"* ]]
|
||||
[[ "$output" == *"dry"* ]]
|
||||
[[ "$output" == *"Cleaned: 2 items"* ]]
|
||||
}
|
||||
get_path_size_kb() { echo "10240"; }
|
||||
bytes_to_human() { echo "10M"; }
|
||||
note_activity() { :; }
|
||||
|
||||
@@ -69,34 +69,6 @@ EOF
|
||||
[[ "$output" != *"Maven repository cache"* ]]
|
||||
}
|
||||
|
||||
@test "mo clean respects MO_BREW_TIMEOUT environment variable" {
|
||||
if ! command -v brew > /dev/null 2>&1; then
|
||||
skip "Homebrew not installed"
|
||||
fi
|
||||
|
||||
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/brew.sh"
|
||||
|
||||
MO_BREW_TIMEOUT=5
|
||||
CALL_LOG="$HOME/timeout.log"
|
||||
|
||||
run_with_timeout() {
|
||||
echo "$1" >> "$CALL_LOG"
|
||||
shift
|
||||
"$@"
|
||||
}
|
||||
|
||||
brew() { return 0; }
|
||||
|
||||
clean_homebrew
|
||||
cat "$CALL_LOG"
|
||||
EOF
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"5"* ]]
|
||||
}
|
||||
|
||||
@test "FINDER_METADATA_SENTINEL in whitelist protects .DS_Store files" {
|
||||
mkdir -p "$HOME/Documents"
|
||||
touch "$HOME/Documents/.DS_Store"
|
||||
@@ -277,3 +249,92 @@ EOF
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Time Machine backup in progress, skipping cleanup"* ]]
|
||||
}
|
||||
|
||||
@test "clean_empty_library_items removes nested empty directories in Application Support" {
|
||||
# Create nested empty directory structure
|
||||
mkdir -p "$HOME/Library/Application Support/UninstalledApp1/SubDir/DeepDir"
|
||||
mkdir -p "$HOME/Library/Application Support/UninstalledApp2/Cache"
|
||||
mkdir -p "$HOME/Library/Application Support/ActiveApp/Data"
|
||||
mkdir -p "$HOME/Library/Caches/EmptyCache/SubCache"
|
||||
|
||||
# Create a file in ActiveApp to make it non-empty
|
||||
touch "$HOME/Library/Application Support/ActiveApp/Data/config.json"
|
||||
|
||||
# Create top-level empty directory in Library
|
||||
mkdir -p "$HOME/Library/EmptyTopLevel"
|
||||
|
||||
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/user.sh"
|
||||
|
||||
# Mock dependencies
|
||||
is_path_whitelisted() { return 1; }
|
||||
is_critical_system_component() { return 1; }
|
||||
bytes_to_human() { echo "$1"; }
|
||||
note_activity() { :; }
|
||||
safe_clean() {
|
||||
# Actually remove the directories for testing
|
||||
for path in "$@"; do
|
||||
if [ "$path" != "${@: -1}" ]; then # Skip the description (last arg)
|
||||
rm -rf "$path" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
clean_empty_library_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Empty nested dirs should be removed
|
||||
[ ! -d "$HOME/Library/Application Support/UninstalledApp1" ]
|
||||
[ ! -d "$HOME/Library/Application Support/UninstalledApp2" ]
|
||||
[ ! -d "$HOME/Library/Caches/EmptyCache" ]
|
||||
[ ! -d "$HOME/Library/EmptyTopLevel" ]
|
||||
|
||||
# Non-empty directory should remain
|
||||
[ -d "$HOME/Library/Application Support/ActiveApp" ]
|
||||
[ -f "$HOME/Library/Application Support/ActiveApp/Data/config.json" ]
|
||||
}
|
||||
|
||||
@test "clean_empty_library_items respects whitelist for empty directories" {
|
||||
mkdir -p "$HOME/Library/Application Support/ProtectedEmptyApp"
|
||||
mkdir -p "$HOME/Library/Application Support/UnprotectedEmptyApp"
|
||||
mkdir -p "$HOME/.config/mole"
|
||||
|
||||
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/user.sh"
|
||||
|
||||
# Mock dependencies
|
||||
is_critical_system_component() { return 1; }
|
||||
bytes_to_human() { echo "$1"; }
|
||||
note_activity() { :; }
|
||||
|
||||
# Mock whitelist to protect ProtectedEmptyApp
|
||||
is_path_whitelisted() {
|
||||
[[ "$1" == *"ProtectedEmptyApp"* ]]
|
||||
}
|
||||
|
||||
safe_clean() {
|
||||
# Actually remove the directories for testing
|
||||
for path in "$@"; do
|
||||
if [ "$path" != "${@: -1}" ]; then # Skip the description (last arg)
|
||||
rm -rf "$path" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
clean_empty_library_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Whitelisted directory should remain even if empty
|
||||
[ -d "$HOME/Library/Application Support/ProtectedEmptyApp" ]
|
||||
|
||||
# Non-whitelisted directory should be removed
|
||||
[ ! -d "$HOME/Library/Application Support/UnprotectedEmptyApp" ]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user