1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 23:40:09 +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

@@ -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

View File

@@ -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).