mirror of
https://github.com/tw93/Mole.git
synced 2026-03-22 15:00: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:
@@ -13,6 +13,11 @@ list_login_items() {
|
||||
return
|
||||
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
|
||||
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
|
||||
|
||||
@@ -123,8 +123,11 @@ scan_installed_apps() {
|
||||
done
|
||||
# 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 "")
|
||||
echo "$running_apps" | tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' > "$scan_tmp_dir/running.txt"
|
||||
# Skip AppleScript during tests to avoid permission dialogs
|
||||
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
|
||||
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
|
||||
|
||||
@@ -11,7 +11,13 @@ clean_user_essentials() {
|
||||
if ! is_path_whitelisted "$HOME/.Trash"; then
|
||||
local trash_count
|
||||
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
|
||||
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 |
|
||||
@@ -22,10 +28,18 @@ clean_user_essentials() {
|
||||
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"
|
||||
elif [[ $trash_count -gt 0 ]]; then
|
||||
if run_with_timeout 5 osascript -e 'tell application "Finder" to empty trash' > /dev/null 2>&1; then
|
||||
echo -e " ${GREEN}${ICON_SUCCESS}${NC} Trash · emptied, $trash_count items"
|
||||
note_activity
|
||||
local emptied_via_finder=false
|
||||
# Skip AppleScript during tests to avoid permission dialogs
|
||||
if [[ "${MOLE_TEST_MODE:-0}" == "1" || "${MOLE_TEST_NO_AUTH:-0}" == "1" ]]; then
|
||||
debug_log "Skipping Finder AppleScript in test mode"
|
||||
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"
|
||||
local cleaned_count=0
|
||||
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).
|
||||
local sys_idle_assets_dir="/Library/Application Support/com.apple.idleassetsd/Customer"
|
||||
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
|
||||
# Skip sudo operations during tests to avoid password prompts
|
||||
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
|
||||
|
||||
# Clean old aerial wallpaper videos (can be large, safe to remove).
|
||||
|
||||
@@ -303,8 +303,10 @@ log_system_info() {
|
||||
fi
|
||||
echo "Shell: ${SHELL:-unknown}, ${TERM:-unknown}"
|
||||
|
||||
# Check sudo status non-interactively
|
||||
if sudo -n true 2> /dev/null; then
|
||||
# Check sudo status non-interactively (skip in test mode)
|
||||
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"
|
||||
else
|
||||
echo "Sudo Access: Required"
|
||||
|
||||
@@ -186,26 +186,29 @@ remove_login_item() {
|
||||
|
||||
# Remove from Login Items using index-based deletion (handles broken items)
|
||||
if [[ -n "$clean_name" ]]; then
|
||||
# Escape double quotes and backslashes for AppleScript
|
||||
local escaped_name="${clean_name//\\/\\\\}"
|
||||
escaped_name="${escaped_name//\"/\\\"}"
|
||||
# Skip AppleScript during tests to avoid permission dialogs
|
||||
if [[ "${MOLE_TEST_MODE:-0}" != "1" && "${MOLE_TEST_NO_AUTH:-0}" != "1" ]]; then
|
||||
# Escape double quotes and backslashes for AppleScript
|
||||
local escaped_name="${clean_name//\\/\\\\}"
|
||||
escaped_name="${escaped_name//\"/\\\"}"
|
||||
|
||||
osascript <<- EOF > /dev/null 2>&1 || true
|
||||
tell application "System Events"
|
||||
try
|
||||
set itemCount to count of login items
|
||||
-- Delete in reverse order to avoid index shifting
|
||||
repeat with i from itemCount to 1 by -1
|
||||
try
|
||||
set itemName to name of login item i
|
||||
if itemName is "$escaped_name" then
|
||||
delete login item i
|
||||
end if
|
||||
end try
|
||||
end repeat
|
||||
end try
|
||||
end tell
|
||||
EOF
|
||||
osascript <<- EOF > /dev/null 2>&1 || true
|
||||
tell application "System Events"
|
||||
try
|
||||
set itemCount to count of login items
|
||||
-- Delete in reverse order to avoid index shifting
|
||||
repeat with i from itemCount to 1 by -1
|
||||
try
|
||||
set itemName to name of login item i
|
||||
if itemName is "$escaped_name" then
|
||||
delete login item i
|
||||
end if
|
||||
end try
|
||||
end repeat
|
||||
end try
|
||||
end tell
|
||||
EOF
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,10 @@ setup_file() {
|
||||
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-brew-uninstall-home.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-app-caches.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-apps-module.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-browser-cleanup.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-home.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-clean-extras.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-system-clean.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ setup_file() {
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-user-core.XXXXXX")"
|
||||
export HOME
|
||||
|
||||
# Prevent AppleScript permission dialogs during tests
|
||||
MOLE_TEST_MODE=1
|
||||
export MOLE_TEST_MODE
|
||||
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user