1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 20:15:07 +00:00

fix(tests): prevent permission dialogs during test runs

Add MOLE_TEST_MODE=1 checks to skip AppleScript osascript calls
and sudo operations that trigger system permission dialogs during tests:

- lib/check/all.sh: Skip login items listing
- lib/clean/apps.sh: Skip running app detection
- lib/clean/user.sh: Skip Finder trash operations and sudo test calls
- lib/core/log.sh: Skip sudo status check in debug log
- lib/uninstall/batch.sh: Skip login item removal

Also add MOLE_TEST_MODE=1 export to all test files that load
these modules to ensure consistent test isolation.
This commit is contained in:
Tw93
2026-03-16 20:43:35 +08:00
parent 0cc4862f9c
commit ea4cd9d0e3
13 changed files with 91 additions and 29 deletions

View File

@@ -13,6 +13,11 @@ list_login_items() {
return return
fi fi
# Skip AppleScript during tests to avoid permission dialogs
if [[ "${MOLE_TEST_MODE:-0}" == "1" || "${MOLE_TEST_NO_AUTH:-0}" == "1" ]]; then
return
fi
local raw_items local raw_items
raw_items=$(osascript -e 'tell application "System Events" to get the name of every login item' 2> /dev/null || echo "") raw_items=$(osascript -e 'tell application "System Events" to get the name of every login item' 2> /dev/null || echo "")
[[ -z "$raw_items" || "$raw_items" == "missing value" ]] && return [[ -z "$raw_items" || "$raw_items" == "missing value" ]] && return

View File

@@ -123,8 +123,11 @@ scan_installed_apps() {
done done
# Collect running apps and LaunchAgents to avoid false orphan cleanup. # Collect running apps and LaunchAgents to avoid false orphan cleanup.
( (
local running_apps=$(run_with_timeout 5 osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2> /dev/null || echo "") # Skip AppleScript during tests to avoid permission dialogs
echo "$running_apps" | tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' > "$scan_tmp_dir/running.txt" if [[ "${MOLE_TEST_MODE:-0}" != "1" && "${MOLE_TEST_NO_AUTH:-0}" != "1" ]]; then
local running_apps=$(run_with_timeout 5 osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2> /dev/null || echo "")
echo "$running_apps" | tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' > "$scan_tmp_dir/running.txt"
fi
# Fallback: lsappinfo is more reliable than osascript # Fallback: lsappinfo is more reliable than osascript
if command -v lsappinfo > /dev/null 2>&1; then if command -v lsappinfo > /dev/null 2>&1; then
run_with_timeout 3 lsappinfo list 2> /dev/null | grep -o '"CFBundleIdentifier"="[^"]*"' | cut -d'"' -f4 >> "$scan_tmp_dir/running.txt" 2> /dev/null || true run_with_timeout 3 lsappinfo list 2> /dev/null | grep -o '"CFBundleIdentifier"="[^"]*"' | cut -d'"' -f4 >> "$scan_tmp_dir/running.txt" 2> /dev/null || true

View File

@@ -11,7 +11,13 @@ clean_user_essentials() {
if ! is_path_whitelisted "$HOME/.Trash"; then if ! is_path_whitelisted "$HOME/.Trash"; then
local trash_count local trash_count
local trash_count_status=0 local trash_count_status=0
trash_count=$(run_with_timeout 3 osascript -e 'tell application "Finder" to count items in trash' 2> /dev/null) || trash_count_status=$? # Skip AppleScript during tests to avoid permission dialogs
if [[ "${MOLE_TEST_MODE:-0}" == "1" || "${MOLE_TEST_NO_AUTH:-0}" == "1" ]]; then
trash_count=$(command find "$HOME/.Trash" -mindepth 1 -maxdepth 1 -exec printf '.' ';' 2> /dev/null |
wc -c | awk '{print $1}' || echo "0")
else
trash_count=$(run_with_timeout 3 osascript -e 'tell application "Finder" to count items in trash' 2> /dev/null) || trash_count_status=$?
fi
if [[ $trash_count_status -eq 124 ]]; then if [[ $trash_count_status -eq 124 ]]; then
debug_log "Finder trash count timed out, using direct .Trash scan" debug_log "Finder trash count timed out, using direct .Trash scan"
trash_count=$(command find "$HOME/.Trash" -mindepth 1 -maxdepth 1 -exec printf '.' ';' 2> /dev/null | trash_count=$(command find "$HOME/.Trash" -mindepth 1 -maxdepth 1 -exec printf '.' ';' 2> /dev/null |
@@ -22,10 +28,18 @@ clean_user_essentials() {
if [[ "$DRY_RUN" == "true" ]]; then if [[ "$DRY_RUN" == "true" ]]; then
[[ $trash_count -gt 0 ]] && echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} Trash · would empty, $trash_count items" || echo -e " ${GREEN}${ICON_SUCCESS}${NC} Trash · already empty" [[ $trash_count -gt 0 ]] && echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} Trash · would empty, $trash_count items" || echo -e " ${GREEN}${ICON_SUCCESS}${NC} Trash · already empty"
elif [[ $trash_count -gt 0 ]]; then elif [[ $trash_count -gt 0 ]]; then
if run_with_timeout 5 osascript -e 'tell application "Finder" to empty trash' > /dev/null 2>&1; then local emptied_via_finder=false
echo -e " ${GREEN}${ICON_SUCCESS}${NC} Trash · emptied, $trash_count items" # Skip AppleScript during tests to avoid permission dialogs
note_activity if [[ "${MOLE_TEST_MODE:-0}" == "1" || "${MOLE_TEST_NO_AUTH:-0}" == "1" ]]; then
debug_log "Skipping Finder AppleScript in test mode"
else else
if run_with_timeout 5 osascript -e 'tell application "Finder" to empty trash' > /dev/null 2>&1; then
emptied_via_finder=true
echo -e " ${GREEN}${ICON_SUCCESS}${NC} Trash · emptied, $trash_count items"
note_activity
fi
fi
if [[ "$emptied_via_finder" != "true" ]]; then
debug_log "Finder trash empty failed or timed out, falling back to direct deletion" debug_log "Finder trash empty failed or timed out, falling back to direct deletion"
local cleaned_count=0 local cleaned_count=0
while IFS= read -r -d '' item; do while IFS= read -r -d '' item; do
@@ -452,8 +466,11 @@ clean_support_app_data() {
# Clean system-level idle/aerial screensaver videos (macOS re-downloads as needed). # Clean system-level idle/aerial screensaver videos (macOS re-downloads as needed).
local sys_idle_assets_dir="/Library/Application Support/com.apple.idleassetsd/Customer" local sys_idle_assets_dir="/Library/Application Support/com.apple.idleassetsd/Customer"
if sudo test -d "$sys_idle_assets_dir" 2> /dev/null; then # Skip sudo operations during tests to avoid password prompts
safe_sudo_find_delete "$sys_idle_assets_dir" "*" "$support_age_days" "f" || true if [[ "${MOLE_TEST_MODE:-0}" != "1" && "${MOLE_TEST_NO_AUTH:-0}" != "1" ]]; then
if sudo test -d "$sys_idle_assets_dir" 2> /dev/null; then
safe_sudo_find_delete "$sys_idle_assets_dir" "*" "$support_age_days" "f" || true
fi
fi fi
# Clean old aerial wallpaper videos (can be large, safe to remove). # Clean old aerial wallpaper videos (can be large, safe to remove).

View File

@@ -303,8 +303,10 @@ log_system_info() {
fi fi
echo "Shell: ${SHELL:-unknown}, ${TERM:-unknown}" echo "Shell: ${SHELL:-unknown}, ${TERM:-unknown}"
# Check sudo status non-interactively # Check sudo status non-interactively (skip in test mode)
if sudo -n true 2> /dev/null; then if [[ "${MOLE_TEST_MODE:-0}" == "1" || "${MOLE_TEST_NO_AUTH:-0}" == "1" ]]; then
echo "Sudo Access: Skipped (test mode)"
elif sudo -n true 2> /dev/null; then
echo "Sudo Access: Active" echo "Sudo Access: Active"
else else
echo "Sudo Access: Required" echo "Sudo Access: Required"

View File

@@ -186,26 +186,29 @@ remove_login_item() {
# Remove from Login Items using index-based deletion (handles broken items) # Remove from Login Items using index-based deletion (handles broken items)
if [[ -n "$clean_name" ]]; then if [[ -n "$clean_name" ]]; then
# Escape double quotes and backslashes for AppleScript # Skip AppleScript during tests to avoid permission dialogs
local escaped_name="${clean_name//\\/\\\\}" if [[ "${MOLE_TEST_MODE:-0}" != "1" && "${MOLE_TEST_NO_AUTH:-0}" != "1" ]]; then
escaped_name="${escaped_name//\"/\\\"}" # Escape double quotes and backslashes for AppleScript
local escaped_name="${clean_name//\\/\\\\}"
escaped_name="${escaped_name//\"/\\\"}"
osascript <<- EOF > /dev/null 2>&1 || true osascript <<- EOF > /dev/null 2>&1 || true
tell application "System Events" tell application "System Events"
try try
set itemCount to count of login items set itemCount to count of login items
-- Delete in reverse order to avoid index shifting -- Delete in reverse order to avoid index shifting
repeat with i from itemCount to 1 by -1 repeat with i from itemCount to 1 by -1
try try
set itemName to name of login item i set itemName to name of login item i
if itemName is "$escaped_name" then if itemName is "$escaped_name" then
delete login item i delete login item i
end if end if
end try end try
end repeat end repeat
end try end try
end tell end tell
EOF EOF
fi
fi fi
} }

View File

@@ -9,6 +9,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-brew-uninstall-home.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-brew-uninstall-home.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
} }
teardown_file() { teardown_file() {

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-app-caches.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-app-caches.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-apps-module.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-apps-module.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-browser-cleanup.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-browser-cleanup.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-home.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-home.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-extras.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-extras.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-system-clean.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-system-clean.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }

View File

@@ -10,6 +10,10 @@ setup_file() {
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-user-core.XXXXXX")" HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-user-core.XXXXXX")"
export HOME export HOME
# Prevent AppleScript permission dialogs during tests
MOLE_TEST_MODE=1
export MOLE_TEST_MODE
mkdir -p "$HOME" mkdir -p "$HOME"
} }