From f7dff3b7982a9d72ddfca7938aaba236d66c22ad Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sat, 15 Nov 2025 13:40:43 +0800 Subject: [PATCH] Test case update --- bin/clean.sh | 21 ++++++------ lib/optimize_health.sh | 6 +++- mole | 2 +- scripts/build-analyze.sh | 4 ++- tests/analyze.bats | 71 ---------------------------------------- tests/common.bats | 15 +++++++-- tests/whitelist.bats | 46 -------------------------- 7 files changed, 32 insertions(+), 133 deletions(-) delete mode 100644 tests/analyze.bats diff --git a/bin/clean.sh b/bin/clean.sh index b3e32d6..660d131 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -15,7 +15,7 @@ source "$SCRIPT_DIR/../lib/common.sh" # Configuration SYSTEM_CLEAN=false DRY_RUN=false -IS_M_SERIES=$([ "$(uname -m)" = "arm64" ] && echo "true" || echo "false") +IS_M_SERIES=$([[ "$(uname -m)" == "arm64" ]] && echo "true" || echo "false") # Constants readonly MAX_PARALLEL_JOBS=15 # Maximum parallel background jobs @@ -404,9 +404,9 @@ clean_ds_store_tree() { ) # Limit depth for HOME to avoid slow scans - local max_depth="" + local -a max_depth=() if [[ "$target" == "$HOME" ]]; then - max_depth="-maxdepth 5" + max_depth=(-maxdepth 5) fi # Find .DS_Store files with exclusions and depth limit @@ -423,7 +423,7 @@ clean_ds_store_tree() { if [[ $file_count -ge 500 ]]; then break fi - done < <(find "$target" $max_depth "${exclude_paths[@]}" -type f -name '.DS_Store' -print0 2> /dev/null) + done < <(find "$target" "${max_depth[@]}" "${exclude_paths[@]}" -type f -name '.DS_Store' -print0 2> /dev/null) if [[ "$spinner_active" == "true" ]]; then stop_inline_spinner @@ -1243,19 +1243,18 @@ perform_cleanup() { # Define resource types to scan local -a resource_types=( - "~/Library/Caches|Caches|com.*:org.*:net.*:io.*" - "~/Library/Logs|Logs|com.*:org.*:net.*:io.*" - "~/Library/Saved Application State|States|*.savedState" - "~/Library/WebKit|WebKit|com.*:org.*:net.*:io.*" - "~/Library/HTTPStorages|HTTP|com.*:org.*:net.*:io.*" - "~/Library/Cookies|Cookies|*.binarycookies" + "$HOME/Library/Caches|Caches|com.*:org.*:net.*:io.*" + "$HOME/Library/Logs|Logs|com.*:org.*:net.*:io.*" + "$HOME/Library/Saved Application State|States|*.savedState" + "$HOME/Library/WebKit|WebKit|com.*:org.*:net.*:io.*" + "$HOME/Library/HTTPStorages|HTTP|com.*:org.*:net.*:io.*" + "$HOME/Library/Cookies|Cookies|*.binarycookies" ) orphaned_count=0 for resource_type in "${resource_types[@]}"; do IFS='|' read -r base_path label patterns <<< "$resource_type" - base_path="${base_path/#\~/$HOME}" [[ -d "$base_path" ]] || continue diff --git a/lib/optimize_health.sh b/lib/optimize_health.sh index d16e66c..25c5012 100755 --- a/lib/optimize_health.sh +++ b/lib/optimize_health.sh @@ -101,7 +101,11 @@ check_startup_items() { ) for dir in "${dirs[@]}"; do - [[ -d "$dir" ]] && count=$((count + $(ls -1 "$dir" 2> /dev/null | wc -l))) + if [[ -d "$dir" ]]; then + local dir_count + dir_count=$(find "$dir" -maxdepth 1 -type f -name "*.plist" 2> /dev/null | wc -l) + count=$((count + dir_count)) + fi done if [[ $count -gt 5 ]]; then diff --git a/mole b/mole index 352f966..96d13b8 100755 --- a/mole +++ b/mole @@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/lib/common.sh" # Version info -VERSION="1.8.3" +VERSION="1.9.0" MOLE_TAGLINE="can dig deep to clean your Mac." # Get latest version from remote repository diff --git a/scripts/build-analyze.sh b/scripts/build-analyze.sh index c1b191e..24aafad 100755 --- a/scripts/build-analyze.sh +++ b/scripts/build-analyze.sh @@ -28,6 +28,8 @@ echo "" echo "✓ Build complete!" echo "" file bin/analyze-go -ls -lh bin/analyze-go | awk '{print "Size:", $5}' +size_bytes=$(stat -f%z bin/analyze-go 2> /dev/null || echo 0) +size_mb=$((size_bytes / 1024 / 1024)) +printf "Size: %d MB (%d bytes)\n" "$size_mb" "$size_bytes" echo "" echo "Binary supports: arm64 (Apple Silicon) + x86_64 (Intel)" diff --git a/tests/analyze.bats b/tests/analyze.bats deleted file mode 100644 index 872980f..0000000 --- a/tests/analyze.bats +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env bats - -setup_file() { - PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)" - export PROJECT_ROOT - - ORIGINAL_HOME="${HOME:-}" - export ORIGINAL_HOME - - HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-analyze-home.XXXXXX")" - export HOME -} - -teardown_file() { - rm -rf "$HOME" - if [[ -n "${ORIGINAL_HOME:-}" ]]; then - export HOME="$ORIGINAL_HOME" - fi -} - -setup() { - export TERM="dumb" - rm -rf "${HOME:?}"/* - mkdir -p "$HOME" -} - -@test "scan_directories lists largest folders first" { - run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' -set -euo pipefail -source "$PROJECT_ROOT/bin/analyze.sh" - -root="$HOME/analyze-root" -mkdir -p "$root/Small" "$root/Large" -printf 'tiny' > "$root/Small/file.txt" -dd if=/dev/zero of="$root/Large/big.dat" bs=1024 count=200 >/dev/null 2>&1 - -output_file="$HOME/directories.txt" -scan_directories "$root" "$output_file" 1 - -head -n1 "$output_file" -EOF - - [ "$status" -eq 0 ] - [[ "$output" == *"Large"* ]] -} - -@test "aggregate_by_directory sums child sizes per parent" { - run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' -set -euo pipefail -source "$PROJECT_ROOT/bin/analyze.sh" - -root="$HOME/group" -mkdir -p "$root/a" "$root/b" - -input_file="$HOME/files.txt" -cat > "$input_file" < /dev/null; then + kill "$pid" 2> /dev/null || true + wait "$pid" 2> /dev/null || true + echo "timeout" + else + wait "$pid" 2> /dev/null || true + fi + ) [[ "$result" == "done" ]] } diff --git a/tests/whitelist.bats b/tests/whitelist.bats index f5f2d01..f6beac3 100644 --- a/tests/whitelist.bats +++ b/tests/whitelist.bats @@ -97,49 +97,3 @@ setup() { fi [ "$status" -ne 0 ] } - -@test "whitelist rejects paths with spaces" { - # Paths with spaces should be rejected by the new stricter validation - mkdir -p "$HOME/.config/mole" - echo "$HOME/.cache/invalid path" > "$WHITELIST_PATH" - - # Load whitelist - path with space should be rejected - warnings=$(HOME="$HOME" bash --noprofile --norc -c " - source '$PROJECT_ROOT/bin/clean.sh' - load_whitelist_file 2>&1 | grep -c 'Invalid path format' || echo 0 - ") - [ "$warnings" -ge 1 ] -} - -@test "whitelist rejects paths with consecutive slashes" { - mkdir -p "$HOME/.config/mole" - echo "$HOME/.cache//invalid" > "$WHITELIST_PATH" - - warnings=$(HOME="$HOME" bash --noprofile --norc -c " - source '$PROJECT_ROOT/bin/clean.sh' - load_whitelist_file 2>&1 | grep -c 'Consecutive slashes' || echo 0 - ") - [ "$warnings" -ge 1 ] -} - -@test "whitelist rejects system protected paths" { - mkdir -p "$HOME/.config/mole" - echo "/System/Library/test" > "$WHITELIST_PATH" - - warnings=$(HOME="$HOME" bash --noprofile --norc -c " - source '$PROJECT_ROOT/bin/clean.sh' - load_whitelist_file 2>&1 | grep -c 'Protected system path' || echo 0 - ") - [ "$warnings" -ge 1 ] -} - -@test "whitelist accepts valid paths with wildcards at end" { - mkdir -p "$HOME/.config/mole" - echo "$HOME/.cache/test/*" > "$WHITELIST_PATH" - - # Should be accepted without warnings - HOME="$HOME" bash --noprofile --norc -c " - source '$PROJECT_ROOT/bin/clean.sh' - load_whitelist_file - " 2>&1 | grep -q "Invalid path format" && exit 1 || exit 0 -}