mirror of
https://github.com/tw93/Mole.git
synced 2026-03-22 21:20:09 +00:00
Protect user launch agents during clean
This commit is contained in:
@@ -412,142 +412,31 @@ EOF
|
||||
[[ "$output" != *"launchctl-called"* ]]
|
||||
}
|
||||
|
||||
@test "is_launch_item_orphaned detects orphan when program missing" {
|
||||
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/clean/apps.sh"
|
||||
|
||||
tmp_dir="$(mktemp -d)"
|
||||
tmp_plist="$tmp_dir/com.test.orphan.plist"
|
||||
|
||||
cat > "$tmp_plist" << 'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.test.orphan</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/nonexistent/app/program</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run_with_timeout() { shift; "$@"; }
|
||||
|
||||
if is_launch_item_orphaned "$tmp_plist"; then
|
||||
echo "orphan"
|
||||
fi
|
||||
|
||||
rm -rf "$tmp_dir"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"orphan"* ]]
|
||||
}
|
||||
|
||||
@test "is_launch_item_orphaned protects when program exists" {
|
||||
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/clean/apps.sh"
|
||||
|
||||
tmp_dir="$(mktemp -d)"
|
||||
tmp_plist="$tmp_dir/com.test.active.plist"
|
||||
tmp_program="$tmp_dir/program"
|
||||
touch "$tmp_program"
|
||||
|
||||
cat > "$tmp_plist" << PLIST
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.test.active</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>$tmp_program</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run_with_timeout() { shift; "$@"; }
|
||||
|
||||
if is_launch_item_orphaned "$tmp_plist"; then
|
||||
echo "orphan"
|
||||
else
|
||||
echo "not-orphan"
|
||||
fi
|
||||
|
||||
rm -rf "$tmp_dir"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"not-orphan"* ]]
|
||||
}
|
||||
|
||||
@test "is_launch_item_orphaned protects when app support active" {
|
||||
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/clean/apps.sh"
|
||||
|
||||
tmp_dir="$(mktemp -d)"
|
||||
tmp_plist="$tmp_dir/com.test.appsupport.plist"
|
||||
|
||||
mkdir -p "$HOME/Library/Application Support/TestApp"
|
||||
touch "$HOME/Library/Application Support/TestApp/recent.txt"
|
||||
|
||||
cat > "$tmp_plist" << 'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.test.appsupport</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>$HOME/Library/Application Support/TestApp/Current/app</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run_with_timeout() { shift; "$@"; }
|
||||
|
||||
if is_launch_item_orphaned "$tmp_plist"; then
|
||||
echo "orphan"
|
||||
else
|
||||
echo "not-orphan"
|
||||
fi
|
||||
|
||||
rm -rf "$tmp_dir"
|
||||
rm -rf "$HOME/Library/Application Support/TestApp"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"not-orphan"* ]]
|
||||
}
|
||||
|
||||
@test "clean_orphaned_launch_agents skips when no orphans" {
|
||||
@test "clean_orphaned_launch_agents preserves user launch agents" {
|
||||
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/clean/apps.sh"
|
||||
|
||||
mkdir -p "$HOME/Library/LaunchAgents"
|
||||
cat > "$HOME/Library/LaunchAgents/com.example.custom-task.plist" <<'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.example.custom-task</string>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
start_section_spinner() { :; }
|
||||
stop_section_spinner() { :; }
|
||||
note_activity() { :; }
|
||||
get_path_size_kb() { echo "1"; }
|
||||
run_with_timeout() { shift; "$@"; }
|
||||
|
||||
clean_orphaned_launch_agents
|
||||
|
||||
[[ -f "$HOME/Library/LaunchAgents/com.example.custom-task.plist" ]]
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
@@ -98,6 +98,29 @@ run_clean_dry_run() {
|
||||
[ -f "$HOME/Library/Caches/TestApp/cache.tmp" ]
|
||||
}
|
||||
|
||||
@test "mo clean --dry-run reports stale login item without deleting it" {
|
||||
mkdir -p "$HOME/Library/LaunchAgents"
|
||||
cat > "$HOME/Library/LaunchAgents/com.example.stale.plist" <<'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.example.stale</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/Applications/Missing.app/Contents/MacOS/Missing</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run env HOME="$HOME" MOLE_TEST_MODE=1 "$PROJECT_ROOT/mole" clean --dry-run
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Potential stale login item: com.example.stale.plist"* ]]
|
||||
[ -f "$HOME/Library/LaunchAgents/com.example.stale.plist" ]
|
||||
}
|
||||
|
||||
@test "mo clean honors whitelist entries" {
|
||||
mkdir -p "$HOME/Library/Caches/WhitelistedApp"
|
||||
echo "keep me" > "$HOME/Library/Caches/WhitelistedApp/data.tmp"
|
||||
@@ -322,4 +345,3 @@ EOF
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Time Machine backup in progress, skipping cleanup"* ]]
|
||||
}
|
||||
|
||||
|
||||
@@ -97,3 +97,65 @@ EOT3
|
||||
[[ "$output" == *"~/Library/Developer/Xcode/DerivedData"* ]]
|
||||
[[ "$output" == *"Review: mo analyze, Device backups, docker system df"* ]]
|
||||
}
|
||||
|
||||
@test "show_user_launch_agent_hint_notice reports missing app-backed target" {
|
||||
mkdir -p "$HOME/Library/LaunchAgents"
|
||||
cat > "$HOME/Library/LaunchAgents/com.example.stale.plist" <<'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.example.stale</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/Applications/Missing.app/Contents/MacOS/Missing</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOT4'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/hints.sh"
|
||||
note_activity() { :; }
|
||||
show_user_launch_agent_hint_notice
|
||||
EOT4
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Potential stale login item: com.example.stale.plist"* ]]
|
||||
[[ "$output" == *"Missing app/helper target"* ]]
|
||||
[[ "$output" == *"Review: open ~/Library/LaunchAgents"* ]]
|
||||
}
|
||||
|
||||
@test "show_user_launch_agent_hint_notice skips custom shell wrappers" {
|
||||
mkdir -p "$HOME/Library/LaunchAgents"
|
||||
cat > "$HOME/Library/LaunchAgents/com.example.custom.plist" <<'PLIST'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.example.custom</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/bin/bash</string>
|
||||
<string>-c</string>
|
||||
<string>$HOME/bin/custom-task</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
PLIST
|
||||
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOT5'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/lib/core/common.sh"
|
||||
source "$PROJECT_ROOT/lib/clean/hints.sh"
|
||||
note_activity() { :; }
|
||||
show_user_launch_agent_hint_notice
|
||||
EOT5
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ -z "$output" ]]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user