mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 15:04:42 +00:00
Test extensive coverage and improvement
This commit is contained in:
98
tests/autofix.bats
Normal file
98
tests/autofix.bats
Normal file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup_file() {
|
||||
PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
|
||||
export PROJECT_ROOT
|
||||
}
|
||||
|
||||
@test "show_suggestions lists auto and manual items and exports flag" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/base.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/autofix.sh"
|
||||
|
||||
export FIREWALL_DISABLED=true
|
||||
export FILEVAULT_DISABLED=true
|
||||
export TOUCHID_NOT_CONFIGURED=true
|
||||
export ROSETTA_NOT_INSTALLED=true
|
||||
export CACHE_SIZE_GB=9
|
||||
export BREW_HAS_WARNINGS=true
|
||||
export DISK_FREE_GB=25
|
||||
|
||||
show_suggestions
|
||||
echo "AUTO_FLAG=${HAS_AUTO_FIX_SUGGESTIONS}"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Enable Firewall for better security"* ]]
|
||||
[[ "$output" == *"Enable FileVault"* ]]
|
||||
[[ "$output" == *"Enable Touch ID for sudo"* ]]
|
||||
[[ "$output" == *"Install Rosetta 2"* ]]
|
||||
[[ "$output" == *"Low disk space (25GB free)"* ]]
|
||||
[[ "$output" == *"AUTO_FLAG=true"* ]]
|
||||
}
|
||||
|
||||
@test "ask_for_auto_fix accepts Enter" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/base.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/autofix.sh"
|
||||
HAS_AUTO_FIX_SUGGESTIONS=true
|
||||
read_key() { echo "ENTER"; return 0; }
|
||||
ask_for_auto_fix
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"yes"* ]]
|
||||
}
|
||||
|
||||
@test "ask_for_auto_fix rejects other keys" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/base.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/autofix.sh"
|
||||
HAS_AUTO_FIX_SUGGESTIONS=true
|
||||
read_key() { echo "ESC"; return 0; }
|
||||
ask_for_auto_fix
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"no"* ]]
|
||||
}
|
||||
|
||||
@test "perform_auto_fix applies available actions and records summary" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/base.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/autofix.sh"
|
||||
|
||||
has_sudo_session() { return 0; }
|
||||
ensure_sudo_session() { return 0; }
|
||||
sudo() {
|
||||
case "$1" in
|
||||
defaults) return 0 ;;
|
||||
bash) return 0 ;;
|
||||
softwareupdate)
|
||||
echo "Installing Rosetta 2 stub output"
|
||||
return 0
|
||||
;;
|
||||
*) return 0 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
export FIREWALL_DISABLED=true
|
||||
export TOUCHID_NOT_CONFIGURED=true
|
||||
export ROSETTA_NOT_INSTALLED=true
|
||||
|
||||
perform_auto_fix
|
||||
echo "SUMMARY=${AUTO_FIX_SUMMARY}"
|
||||
echo "DETAILS=${AUTO_FIX_DETAILS}"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Firewall enabled"* ]]
|
||||
[[ "$output" == *"Touch ID configured"* ]]
|
||||
[[ "$output" == *"Rosetta 2 installed"* ]]
|
||||
[[ "$output" == *"SUMMARY=Auto fixes applied: 3 issue(s)"* ]]
|
||||
[[ "$output" == *"DETAILS"* ]]
|
||||
}
|
||||
@@ -1,210 +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-opt-home.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
mkdir -p "$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/Library/Application Support/com.apple.sharedfilelist"
|
||||
mkdir -p "$HOME/Library/Caches"
|
||||
mkdir -p "$HOME/Library/Saved Application State"
|
||||
}
|
||||
|
||||
@test "run_with_timeout succeeds without GNU timeout" {
|
||||
run bash --noprofile --norc -c '
|
||||
set -euo pipefail
|
||||
PATH="/usr/bin:/bin"
|
||||
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
|
||||
source "'"$PROJECT_ROOT"'/lib/core/common.sh"
|
||||
run_with_timeout 1 sleep 0.1
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "run_with_timeout enforces timeout and returns 124" {
|
||||
run bash --noprofile --norc -c '
|
||||
set -euo pipefail
|
||||
PATH="/usr/bin:/bin"
|
||||
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
|
||||
source "'"$PROJECT_ROOT"'/lib/core/common.sh"
|
||||
run_with_timeout 1 sleep 5
|
||||
'
|
||||
[ "$status" -eq 124 ]
|
||||
}
|
||||
|
||||
@test "opt_recent_items removes shared file lists" {
|
||||
local shared_dir="$HOME/Library/Application Support/com.apple.sharedfilelist"
|
||||
mkdir -p "$shared_dir"
|
||||
touch "$shared_dir/test.sfl2"
|
||||
touch "$shared_dir/recent.sfl2"
|
||||
|
||||
run env HOME="$HOME" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/optimize/tasks.sh"
|
||||
# Mock sudo and defaults to avoid system changes
|
||||
sudo() { return 0; }
|
||||
defaults() { return 0; }
|
||||
export -f sudo defaults
|
||||
opt_recent_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Recent items cleared"* ]]
|
||||
}
|
||||
|
||||
@test "opt_recent_items handles missing shared directory" {
|
||||
rm -rf "$HOME/Library/Application Support/com.apple.sharedfilelist"
|
||||
|
||||
run env HOME="$HOME" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/optimize/tasks.sh"
|
||||
sudo() { return 0; }
|
||||
defaults() { return 0; }
|
||||
export -f sudo defaults
|
||||
opt_recent_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Recent items cleared"* ]]
|
||||
}
|
||||
|
||||
@test "opt_saved_state_cleanup removes old saved states" {
|
||||
local state_dir="$HOME/Library/Saved Application State"
|
||||
mkdir -p "$state_dir/com.example.app.savedState"
|
||||
touch "$state_dir/com.example.app.savedState/data.plist"
|
||||
|
||||
# Make the file old (8+ days) - MOLE_SAVED_STATE_AGE_DAYS defaults to 7
|
||||
touch -t 202301010000 "$state_dir/com.example.app.savedState/data.plist"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
opt_saved_state_cleanup
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "opt_saved_state_cleanup handles missing state directory" {
|
||||
rm -rf "$HOME/Library/Saved Application State"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
opt_saved_state_cleanup
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"No saved states directory"* ]]
|
||||
}
|
||||
|
||||
@test "opt_cache_refresh cleans Quick Look cache" {
|
||||
mkdir -p "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache"
|
||||
touch "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache/test.db"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# Mock qlmanage and cleanup_path to avoid system calls
|
||||
qlmanage() { return 0; }
|
||||
cleanup_path() {
|
||||
local path="$1"
|
||||
local label="${2:-}"
|
||||
[[ -e "$path" ]] && rm -rf "$path" 2>/dev/null || true
|
||||
}
|
||||
export -f qlmanage cleanup_path
|
||||
opt_cache_refresh
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Finder and Safari caches updated"* ]]
|
||||
}
|
||||
|
||||
@test "opt_mail_downloads skips cleanup when size below threshold" {
|
||||
mkdir -p "$HOME/Library/Mail Downloads"
|
||||
# Create small file (below threshold of 5MB)
|
||||
echo "test" > "$HOME/Library/Mail Downloads/small.txt"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# MOLE_MAIL_DOWNLOADS_MIN_KB is readonly, defaults to 5120 KB (~5MB)
|
||||
opt_mail_downloads
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"skipping cleanup"* ]]
|
||||
[ -f "$HOME/Library/Mail Downloads/small.txt" ]
|
||||
}
|
||||
|
||||
@test "opt_mail_downloads removes old attachments" {
|
||||
mkdir -p "$HOME/Library/Mail Downloads"
|
||||
touch "$HOME/Library/Mail Downloads/old.pdf"
|
||||
# Make file old (31+ days) - MOLE_LOG_AGE_DAYS defaults to 30
|
||||
touch -t 202301010000 "$HOME/Library/Mail Downloads/old.pdf"
|
||||
|
||||
# Create large enough size to trigger cleanup (>5MB threshold)
|
||||
dd if=/dev/zero of="$HOME/Library/Mail Downloads/dummy.dat" bs=1024 count=6000 2>/dev/null
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# MOLE_MAIL_DOWNLOADS_MIN_KB and MOLE_LOG_AGE_DAYS are readonly constants
|
||||
opt_mail_downloads
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "get_path_size_kb returns zero for missing directory" {
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
size=$(get_path_size_kb "/nonexistent/path")
|
||||
echo "$size"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "0" ]
|
||||
}
|
||||
|
||||
@test "get_path_size_kb calculates directory size" {
|
||||
mkdir -p "$HOME/test_size"
|
||||
dd if=/dev/zero of="$HOME/test_size/file.dat" bs=1024 count=10 2>/dev/null
|
||||
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
size=$(get_path_size_kb "$HOME/test_size")
|
||||
echo "$size"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
# Should be >= 10 KB
|
||||
[ "$output" -ge 10 ]
|
||||
}
|
||||
487
tests/system_maintenance.bats
Normal file
487
tests/system_maintenance.bats
Normal file
@@ -0,0 +1,487 @@
|
||||
#!/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-system-clean.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
rm -rf "$HOME"
|
||||
if [[ -n "${ORIGINAL_HOME:-}" ]]; then
|
||||
export HOME="$ORIGINAL_HOME"
|
||||
fi
|
||||
}
|
||||
|
||||
@test "clean_deep_system issues safe sudo deletions" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
CALL_LOG="$HOME/system_calls.log"
|
||||
> "$CALL_LOG"
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/system.sh"
|
||||
|
||||
safe_sudo_find_delete() {
|
||||
echo "safe_sudo_find_delete:$1:$2" >> "$CALL_LOG"
|
||||
return 0
|
||||
}
|
||||
safe_sudo_remove() {
|
||||
echo "safe_sudo_remove:$1" >> "$CALL_LOG"
|
||||
return 0
|
||||
}
|
||||
log_success() { :; }
|
||||
is_sip_enabled() { return 1; }
|
||||
get_file_mtime() { echo 0; }
|
||||
get_path_size_kb() { echo 0; }
|
||||
find() { return 0; }
|
||||
|
||||
clean_deep_system
|
||||
cat "$CALL_LOG"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"/Library/Caches"* ]]
|
||||
[[ "$output" == *"/private/tmp"* ]]
|
||||
[[ "$output" == *"/private/var/log"* ]]
|
||||
}
|
||||
|
||||
@test "clean_deep_system skips /Library/Updates when SIP enabled" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
CALL_LOG="$HOME/system_calls_skip.log"
|
||||
> "$CALL_LOG"
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/system.sh"
|
||||
|
||||
safe_sudo_find_delete() { return 0; }
|
||||
safe_sudo_remove() {
|
||||
echo "REMOVE:$1" >> "$CALL_LOG"
|
||||
return 0
|
||||
}
|
||||
log_success() { :; }
|
||||
is_sip_enabled() { return 0; } # SIP enabled -> skip removal
|
||||
find() { return 0; }
|
||||
|
||||
clean_deep_system
|
||||
cat "$CALL_LOG"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != *"/Library/Updates"* ]]
|
||||
}
|
||||
|
||||
@test "clean_time_machine_failed_backups exits when tmutil has no destinations" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/system.sh"
|
||||
|
||||
tmutil() {
|
||||
if [[ "$1" == "destinationinfo" ]]; then
|
||||
echo "No destinations configured"
|
||||
return 0
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
pgrep() { return 1; }
|
||||
find() { return 0; }
|
||||
|
||||
clean_time_machine_failed_backups
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"No failed Time Machine backups found"* ]]
|
||||
}
|
||||
|
||||
@test "clean_orphaned_casks uses cached mapping when recent" {
|
||||
cache_dir="$HOME/.cache/mole"
|
||||
mkdir -p "$cache_dir"
|
||||
cat > "$cache_dir/cask_apps.cache" <<'EOF'
|
||||
fake-app|Fake.app
|
||||
EOF
|
||||
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/brew.sh"
|
||||
|
||||
touch "$HOME/.cache/mole/cask_apps.cache"
|
||||
|
||||
brew() { return 0; }
|
||||
start_inline_spinner(){ :; }
|
||||
stop_inline_spinner(){ :; }
|
||||
sudo() { return 0; }
|
||||
MOLE_SPINNER_PREFIX=""
|
||||
clean_orphaned_casks
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "clean_homebrew skips when cleaned recently" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/brew.sh"
|
||||
|
||||
mkdir -p "$HOME/.cache/mole"
|
||||
date +%s > "$HOME/.cache/mole/brew_last_cleanup"
|
||||
|
||||
brew() { return 0; }
|
||||
|
||||
clean_homebrew
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"cleaned"* ]]
|
||||
}
|
||||
|
||||
@test "clean_homebrew runs cleanup with timeout stubs" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/brew.sh"
|
||||
|
||||
mkdir -p "$HOME/.cache/mole"
|
||||
rm -f "$HOME/.cache/mole/brew_last_cleanup"
|
||||
|
||||
MO_BREW_TIMEOUT=2
|
||||
|
||||
start_inline_spinner(){ :; }
|
||||
stop_inline_spinner(){ :; }
|
||||
|
||||
brew() {
|
||||
case "$1" in
|
||||
cleanup)
|
||||
echo "Removing: package"
|
||||
return 0
|
||||
;;
|
||||
autoremove)
|
||||
echo "Uninstalling pkg"
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
clean_homebrew
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Homebrew cleanup"* ]]
|
||||
}
|
||||
|
||||
@test "check_homebrew_updates reports counts and uses cache" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/check/all.sh"
|
||||
|
||||
brew() {
|
||||
if [[ "$1" == "outdated" && "$2" == "--quiet" ]]; then
|
||||
echo "pkg1"
|
||||
echo "pkg2"
|
||||
return 0
|
||||
fi
|
||||
if [[ "$1" == "outdated" && "$2" == "--cask" ]]; then
|
||||
echo "cask1"
|
||||
return 0
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
start_inline_spinner(){ :; }
|
||||
stop_inline_spinner(){ :; }
|
||||
|
||||
check_homebrew_updates
|
||||
|
||||
# second call should read cache (no spinner)
|
||||
check_homebrew_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Homebrew"* ]]
|
||||
[[ "$output" == *"2 formula"* ]]
|
||||
}
|
||||
|
||||
@test "check_appstore_updates reports count from softwareupdate" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/check/all.sh"
|
||||
|
||||
softwareupdate() {
|
||||
echo "* Label: AppOne"
|
||||
echo "* Label: AppTwo"
|
||||
return 0
|
||||
}
|
||||
|
||||
start_inline_spinner(){ :; }
|
||||
stop_inline_spinner(){ :; }
|
||||
|
||||
check_appstore_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"App Store"* ]]
|
||||
[[ "$output" == *"2 apps"* ]]
|
||||
}
|
||||
|
||||
@test "check_macos_update warns when update available" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/check/all.sh"
|
||||
|
||||
softwareupdate() {
|
||||
echo "* Label: macOS 99"
|
||||
return 0
|
||||
}
|
||||
|
||||
start_inline_spinner(){ :; }
|
||||
stop_inline_spinner(){ :; }
|
||||
|
||||
check_macos_update
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"macOS"* ]]
|
||||
}
|
||||
|
||||
@test "run_with_timeout succeeds without GNU timeout" {
|
||||
run bash --noprofile --norc -c '
|
||||
set -euo pipefail
|
||||
PATH="/usr/bin:/bin"
|
||||
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
|
||||
source "'"$PROJECT_ROOT"'/lib/core/common.sh"
|
||||
run_with_timeout 1 sleep 0.1
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "run_with_timeout enforces timeout and returns 124" {
|
||||
run bash --noprofile --norc -c '
|
||||
set -euo pipefail
|
||||
PATH="/usr/bin:/bin"
|
||||
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
|
||||
source "'"$PROJECT_ROOT"'/lib/core/common.sh"
|
||||
run_with_timeout 1 sleep 5
|
||||
'
|
||||
[ "$status" -eq 124 ]
|
||||
}
|
||||
|
||||
@test "opt_recent_items removes shared file lists" {
|
||||
local shared_dir="$HOME/Library/Application Support/com.apple.sharedfilelist"
|
||||
mkdir -p "$shared_dir"
|
||||
touch "$shared_dir/test.sfl2"
|
||||
touch "$shared_dir/recent.sfl2"
|
||||
|
||||
run env HOME="$HOME" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/optimize/tasks.sh"
|
||||
# Mock sudo and defaults to avoid system changes
|
||||
sudo() { return 0; }
|
||||
defaults() { return 0; }
|
||||
export -f sudo defaults
|
||||
opt_recent_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Recent items cleared"* ]]
|
||||
}
|
||||
|
||||
@test "opt_recent_items handles missing shared directory" {
|
||||
rm -rf "$HOME/Library/Application Support/com.apple.sharedfilelist"
|
||||
|
||||
run env HOME="$HOME" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/optimize/tasks.sh"
|
||||
sudo() { return 0; }
|
||||
defaults() { return 0; }
|
||||
export -f sudo defaults
|
||||
opt_recent_items
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Recent items cleared"* ]]
|
||||
}
|
||||
|
||||
@test "opt_saved_state_cleanup removes old saved states" {
|
||||
local state_dir="$HOME/Library/Saved Application State"
|
||||
mkdir -p "$state_dir/com.example.app.savedState"
|
||||
touch "$state_dir/com.example.app.savedState/data.plist"
|
||||
|
||||
# Make the file old (8+ days) - MOLE_SAVED_STATE_AGE_DAYS defaults to 7
|
||||
touch -t 202301010000 "$state_dir/com.example.app.savedState/data.plist"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
opt_saved_state_cleanup
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "opt_saved_state_cleanup handles missing state directory" {
|
||||
rm -rf "$HOME/Library/Saved Application State"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
opt_saved_state_cleanup
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"No saved states directory"* ]]
|
||||
}
|
||||
|
||||
@test "opt_cache_refresh cleans Quick Look cache" {
|
||||
mkdir -p "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache"
|
||||
touch "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache/test.db"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# Mock qlmanage and cleanup_path to avoid system calls
|
||||
qlmanage() { return 0; }
|
||||
cleanup_path() {
|
||||
local path="$1"
|
||||
local label="${2:-}"
|
||||
[[ -e "$path" ]] && rm -rf "$path" 2>/dev/null || true
|
||||
}
|
||||
export -f qlmanage cleanup_path
|
||||
opt_cache_refresh
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Finder and Safari caches updated"* ]]
|
||||
}
|
||||
|
||||
@test "opt_mail_downloads skips cleanup when size below threshold" {
|
||||
mkdir -p "$HOME/Library/Mail Downloads"
|
||||
# Create small file (below threshold of 5MB)
|
||||
echo "test" > "$HOME/Library/Mail Downloads/small.txt"
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# MOLE_MAIL_DOWNLOADS_MIN_KB is readonly, defaults to 5120 KB (~5MB)
|
||||
opt_mail_downloads
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"skipping cleanup"* ]]
|
||||
[ -f "$HOME/Library/Mail Downloads/small.txt" ]
|
||||
}
|
||||
|
||||
@test "opt_mail_downloads removes old attachments" {
|
||||
mkdir -p "$HOME/Library/Mail Downloads"
|
||||
touch "$HOME/Library/Mail Downloads/old.pdf"
|
||||
# Make file old (31+ days) - MOLE_LOG_AGE_DAYS defaults to 30
|
||||
touch -t 202301010000 "$HOME/Library/Mail Downloads/old.pdf"
|
||||
|
||||
# Create large enough size to trigger cleanup (>5MB threshold)
|
||||
dd if=/dev/zero of="$HOME/Library/Mail Downloads/dummy.dat" bs=1024 count=6000 2>/dev/null
|
||||
|
||||
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/optimize/tasks.sh"
|
||||
# MOLE_MAIL_DOWNLOADS_MIN_KB and MOLE_LOG_AGE_DAYS are readonly constants
|
||||
opt_mail_downloads
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "get_path_size_kb returns zero for missing directory" {
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
size=$(get_path_size_kb "/nonexistent/path")
|
||||
echo "$size"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "0" ]
|
||||
}
|
||||
|
||||
@test "get_path_size_kb calculates directory size" {
|
||||
mkdir -p "$HOME/test_size"
|
||||
dd if=/dev/zero of="$HOME/test_size/file.dat" bs=1024 count=10 2>/dev/null
|
||||
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
size=$(get_path_size_kb "$HOME/test_size")
|
||||
echo "$size"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
# Should be >= 10 KB
|
||||
[ "$output" -ge 10 ]
|
||||
}
|
||||
|
||||
@test "opt_log_cleanup runs cleanup_path and safe_sudo_find_delete" {
|
||||
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/optimize/tasks.sh"
|
||||
|
||||
CALLS_FILE="$HOME/log_cleanup_calls"
|
||||
: > "$CALLS_FILE"
|
||||
|
||||
cleanup_path() {
|
||||
echo "cleanup:$1" >> "$CALLS_FILE"
|
||||
}
|
||||
safe_sudo_find_delete() {
|
||||
echo "safe:$1" >> "$CALLS_FILE"
|
||||
return 0
|
||||
}
|
||||
|
||||
opt_log_cleanup
|
||||
cat "$CALLS_FILE"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"cleanup:$HOME/Library/Logs/DiagnosticReports"* ]]
|
||||
[[ "$output" == *"safe:/Library/Logs/DiagnosticReports"* ]]
|
||||
}
|
||||
|
||||
@test "opt_fix_broken_configs reports fixes" {
|
||||
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/optimize/maintenance.sh"
|
||||
source "$PROJECT_ROOT/lib/optimize/tasks.sh"
|
||||
|
||||
fix_broken_preferences() {
|
||||
echo 2
|
||||
}
|
||||
fix_broken_login_items() {
|
||||
echo 1
|
||||
}
|
||||
|
||||
opt_fix_broken_configs
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Fixed 2 broken preference files"* ]]
|
||||
[[ "$output" == *"Removed 1 broken login items"* ]]
|
||||
}
|
||||
86
tests/touchid.bats
Normal file
86
tests/touchid.bats
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/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-touchid.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
rm -rf "$HOME"
|
||||
if [[ -n "${ORIGINAL_HOME:-}" ]]; then
|
||||
export HOME="$ORIGINAL_HOME"
|
||||
fi
|
||||
}
|
||||
|
||||
create_fake_sudo() {
|
||||
local dir="$1"
|
||||
mkdir -p "$dir"
|
||||
cat > "$dir/sudo" <<'SCRIPT'
|
||||
#!/usr/bin/env bash
|
||||
if [[ "$1" == "-n" || "$1" == "-v" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
exec "$@"
|
||||
SCRIPT
|
||||
chmod +x "$dir/sudo"
|
||||
}
|
||||
|
||||
@test "touchid status reflects pam file contents" {
|
||||
pam_file="$HOME/pam_test"
|
||||
cat > "$pam_file" <<'EOF'
|
||||
# comment
|
||||
auth sufficient pam_opendirectory.so
|
||||
EOF
|
||||
|
||||
run env MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" status
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"not configured"* ]]
|
||||
|
||||
cat > "$pam_file" <<'EOF'
|
||||
auth sufficient pam_tid.so
|
||||
EOF
|
||||
|
||||
run env MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" status
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"enabled"* ]]
|
||||
}
|
||||
|
||||
@test "enable_touchid inserts pam_tid line in pam file" {
|
||||
pam_file="$HOME/pam_enable"
|
||||
cat > "$pam_file" <<'EOF'
|
||||
# test pam
|
||||
auth sufficient pam_opendirectory.so
|
||||
EOF
|
||||
|
||||
fake_bin="$HOME/fake-bin"
|
||||
create_fake_sudo "$fake_bin"
|
||||
|
||||
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" enable
|
||||
[ "$status" -eq 0 ]
|
||||
grep -q "pam_tid.so" "$pam_file"
|
||||
[[ -f "${pam_file}.mole-backup" ]]
|
||||
}
|
||||
|
||||
@test "disable_touchid removes pam_tid line" {
|
||||
pam_file="$HOME/pam_disable"
|
||||
cat > "$pam_file" <<'EOF'
|
||||
auth sufficient pam_tid.so
|
||||
auth sufficient pam_opendirectory.so
|
||||
EOF
|
||||
|
||||
fake_bin="$HOME/fake-bin-disable"
|
||||
create_fake_sudo "$fake_bin"
|
||||
|
||||
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" disable
|
||||
[ "$status" -eq 0 ]
|
||||
run grep "pam_tid.so" "$pam_file"
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
@@ -1,130 +1,227 @@
|
||||
#!/usr/bin/env bats
|
||||
# shellcheck disable=SC2030,SC2031
|
||||
|
||||
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-update-manager.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
rm -rf "$HOME"
|
||||
if [[ -n "${ORIGINAL_HOME:-}" ]]; then
|
||||
export HOME="$ORIGINAL_HOME"
|
||||
fi
|
||||
|
||||
# Create a dummy cache directory for tests
|
||||
mkdir -p "${HOME}/.cache/mole"
|
||||
}
|
||||
|
||||
setup() {
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
# Default values for tests
|
||||
BREW_OUTDATED_COUNT=0
|
||||
BREW_FORMULA_OUTDATED_COUNT=0
|
||||
BREW_CASK_OUTDATED_COUNT=0
|
||||
APPSTORE_UPDATE_COUNT=0
|
||||
MACOS_UPDATE_AVAILABLE=false
|
||||
MOLE_UPDATE_AVAILABLE=false
|
||||
|
||||
# Create a temporary bin directory for mocks
|
||||
export MOCK_BIN_DIR="$BATS_TMPDIR/mole-mocks-$$"
|
||||
mkdir -p "$MOCK_BIN_DIR"
|
||||
export PATH="$MOCK_BIN_DIR:$PATH"
|
||||
}
|
||||
|
||||
# Test brew_has_outdated function
|
||||
@test "brew_has_outdated returns 1 when brew not installed" {
|
||||
# shellcheck disable=SC2329
|
||||
function brew() {
|
||||
return 127 # Command not found
|
||||
}
|
||||
export -f brew
|
||||
|
||||
run bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; brew_has_outdated"
|
||||
[ "$status" -eq 1 ]
|
||||
teardown() {
|
||||
rm -rf "$MOCK_BIN_DIR"
|
||||
}
|
||||
|
||||
@test "brew_has_outdated checks formula by default" {
|
||||
# Mock brew to simulate outdated formulas
|
||||
# shellcheck disable=SC2329
|
||||
function brew() {
|
||||
if [[ "$1" == "outdated" && "$2" != "--cask" ]]; then
|
||||
echo "package1"
|
||||
echo "package2"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
export -f brew
|
||||
|
||||
run bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; brew_has_outdated"
|
||||
[ "$status" -eq 0 ]
|
||||
read_key() {
|
||||
# Default mock: press ESC to cancel
|
||||
echo "ESC"
|
||||
return 0
|
||||
}
|
||||
|
||||
@test "brew_has_outdated checks casks when specified" {
|
||||
# Mock brew to simulate outdated casks
|
||||
function brew() {
|
||||
if [[ "$1" == "outdated" && "$2" == "--cask" ]]; then
|
||||
echo "app1"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
export -f brew
|
||||
|
||||
run bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; brew_has_outdated cask"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
# Test format_brew_update_label function
|
||||
@test "format_brew_update_label returns empty when no updates" {
|
||||
result=$(BREW_OUTDATED_COUNT=0 bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; format_brew_update_label")
|
||||
[[ -z "$result" ]]
|
||||
}
|
||||
|
||||
@test "format_brew_update_label formats with formula and cask counts" {
|
||||
result=$(BREW_OUTDATED_COUNT=5 BREW_FORMULA_OUTDATED_COUNT=3 BREW_CASK_OUTDATED_COUNT=2 bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; format_brew_update_label")
|
||||
[[ "$result" =~ "3 formula" ]]
|
||||
[[ "$result" =~ "2 cask" ]]
|
||||
}
|
||||
|
||||
@test "format_brew_update_label shows total when breakdown unavailable" {
|
||||
result=$(BREW_OUTDATED_COUNT=5 bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; format_brew_update_label")
|
||||
[[ "$result" =~ "5 updates" ]]
|
||||
}
|
||||
|
||||
# Test ask_for_updates function
|
||||
@test "ask_for_updates returns 1 when no updates available" {
|
||||
run bash -c "source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; ask_for_updates < /dev/null"
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
BREW_OUTDATED_COUNT=0
|
||||
APPSTORE_UPDATE_COUNT=0
|
||||
MACOS_UPDATE_AVAILABLE=false
|
||||
MOLE_UPDATE_AVAILABLE=false
|
||||
ask_for_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "ask_for_updates detects Homebrew updates" {
|
||||
# Mock environment with Homebrew updates
|
||||
export BREW_OUTDATED_COUNT=5
|
||||
export BREW_FORMULA_OUTDATED_COUNT=3
|
||||
export BREW_CASK_OUTDATED_COUNT=2
|
||||
@test "ask_for_updates shows updates and waits for input" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
BREW_OUTDATED_COUNT=5
|
||||
BREW_FORMULA_OUTDATED_COUNT=3
|
||||
BREW_CASK_OUTDATED_COUNT=2
|
||||
APPSTORE_UPDATE_COUNT=1
|
||||
MACOS_UPDATE_AVAILABLE=true
|
||||
MOLE_UPDATE_AVAILABLE=true
|
||||
|
||||
read_key() { echo "ESC"; return 0; }
|
||||
|
||||
ask_for_updates
|
||||
EOF
|
||||
|
||||
# Use input redirection to simulate ESC (cancel)
|
||||
run bash -c "printf '\x1b' | source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; ask_for_updates"
|
||||
# Should show updates and ask for confirmation
|
||||
[ "$status" -eq 1 ] # ESC cancels
|
||||
[[ "$output" == *"Homebrew (5 updates)"* ]]
|
||||
[[ "$output" == *"App Store (1 apps)"* ]]
|
||||
[[ "$output" == *"macOS system"* ]]
|
||||
[[ "$output" == *"Mole"* ]]
|
||||
}
|
||||
|
||||
@test "ask_for_updates detects App Store updates" {
|
||||
export APPSTORE_UPDATE_COUNT=3
|
||||
@test "ask_for_updates accepts Enter when updates exist" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
BREW_OUTDATED_COUNT=2
|
||||
BREW_FORMULA_OUTDATED_COUNT=2
|
||||
read_key() { echo "ENTER"; return 0; }
|
||||
ask_for_updates
|
||||
EOF
|
||||
|
||||
run bash -c "printf '\x1b' | source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; ask_for_updates"
|
||||
[ "$status" -eq 1 ] # ESC cancels
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"AVAILABLE UPDATES"* ]]
|
||||
[[ "$output" == *"yes"* ]]
|
||||
}
|
||||
|
||||
@test "ask_for_updates detects macOS updates" {
|
||||
export MACOS_UPDATE_AVAILABLE=true
|
||||
@test "format_brew_update_label lists formula and cask counts" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
BREW_OUTDATED_COUNT=5
|
||||
BREW_FORMULA_OUTDATED_COUNT=3
|
||||
BREW_CASK_OUTDATED_COUNT=2
|
||||
format_brew_update_label
|
||||
EOF
|
||||
|
||||
run bash -c "printf '\x1b' | source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; ask_for_updates"
|
||||
[ "$status" -eq 1 ] # ESC cancels
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"3 formula"* ]]
|
||||
[[ "$output" == *"2 cask"* ]]
|
||||
}
|
||||
|
||||
@test "ask_for_updates detects Mole updates" {
|
||||
export MOLE_UPDATE_AVAILABLE=true
|
||||
@test "perform_updates handles Homebrew success and Mole update" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
|
||||
run bash -c "printf '\x1b' | source '$PROJECT_ROOT/lib/core/common.sh'; source '$PROJECT_ROOT/lib/manage/update.sh'; ask_for_updates"
|
||||
[ "$status" -eq 1 ] # ESC cancels
|
||||
BREW_FORMULA_OUTDATED_COUNT=1
|
||||
BREW_CASK_OUTDATED_COUNT=0
|
||||
MOLE_UPDATE_AVAILABLE=true
|
||||
|
||||
FAKE_DIR="$HOME/fake-script-dir"
|
||||
mkdir -p "$FAKE_DIR"
|
||||
cat > "$FAKE_DIR/mole" <<'SCRIPT'
|
||||
#!/usr/bin/env bash
|
||||
echo "Already on latest version"
|
||||
SCRIPT
|
||||
chmod +x "$FAKE_DIR/mole"
|
||||
SCRIPT_DIR="$FAKE_DIR"
|
||||
|
||||
brew_has_outdated() { return 0; }
|
||||
start_inline_spinner() { :; }
|
||||
stop_inline_spinner() { :; }
|
||||
reset_brew_cache() { echo "BREW_CACHE_RESET"; }
|
||||
reset_mole_cache() { echo "MOLE_CACHE_RESET"; }
|
||||
has_sudo_session() { return 1; }
|
||||
ensure_sudo_session() { echo "ensure_sudo_session_called"; return 1; }
|
||||
|
||||
brew() {
|
||||
if [[ "$1" == "upgrade" ]]; then
|
||||
echo "Upgrading formula"
|
||||
return 0
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
get_appstore_update_labels() { return 0; }
|
||||
get_macos_update_labels() { return 0; }
|
||||
|
||||
perform_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Homebrew formulae updated"* ]]
|
||||
[[ "$output" == *"Already on latest version"* ]]
|
||||
[[ "$output" == *"MOLE_CACHE_RESET"* ]]
|
||||
}
|
||||
|
||||
@test "perform_updates skips brew when no outdated packages" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
|
||||
BREW_FORMULA_OUTDATED_COUNT=1
|
||||
BREW_CASK_OUTDATED_COUNT=1
|
||||
brew_has_outdated() { return 1; }
|
||||
start_inline_spinner() { :; }
|
||||
stop_inline_spinner() { :; }
|
||||
|
||||
perform_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"already up to date"* ]]
|
||||
}
|
||||
|
||||
@test "perform_updates handles App Store fallback logic" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
|
||||
APPSTORE_UPDATE_COUNT=2
|
||||
# Mock getting labels returning empty, triggering fallback
|
||||
get_appstore_update_labels() { return 0; }
|
||||
|
||||
has_sudo_session() { return 0; }
|
||||
reset_softwareupdate_cache() { :; }
|
||||
|
||||
# Mock sudo to check for -a flag (install all)
|
||||
sudo() {
|
||||
if [[ "$1" == "softwareupdate" && "$2" == "-i" && "$3" == "-a" ]]; then
|
||||
echo "Installing all updates..."
|
||||
return 0
|
||||
fi
|
||||
echo "Wrong sudo command: $*"
|
||||
return 1
|
||||
}
|
||||
|
||||
perform_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Installing all available updates"* ]]
|
||||
[[ "$output" == *"Software updates completed"* ]]
|
||||
}
|
||||
|
||||
@test "perform_updates gracefully handles sudo failure for App Store" {
|
||||
run bash --noprofile --norc <<'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/manage/update.sh"
|
||||
|
||||
APPSTORE_UPDATE_COUNT=1
|
||||
get_appstore_update_labels() { echo "Xcode"; }
|
||||
|
||||
# Simulate user declining sudo or timeout
|
||||
has_sudo_session() { return 1; }
|
||||
ensure_sudo_session() {
|
||||
echo "User declined sudo"
|
||||
return 1
|
||||
}
|
||||
|
||||
perform_updates
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"User declined sudo"* ]]
|
||||
[[ "$output" == *"update via System Settings"* ]]
|
||||
# Should not crash
|
||||
}
|
||||
Reference in New Issue
Block a user