1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 12:41:46 +00:00

fix: replace pgrep with tmutil status for more accurate Time Machine backup detection and add new tests for this logic.

This commit is contained in:
Tw93
2025-12-28 20:10:16 +08:00
parent 4a626f9337
commit ff6f288c9b
2 changed files with 96 additions and 2 deletions

View File

@@ -213,8 +213,8 @@ clean_time_machine_failed_backups() {
return 0
fi
# Skip if backup is running
if pgrep -x "backupd" > /dev/null 2>&1; then
# Skip if backup is running (check actual Running status, not just daemon existence)
if tmutil status 2>/dev/null | grep -q "Running = 1"; then
if [[ "$spinner_active" == "true" ]]; then
stop_section_spinner
fi

View File

@@ -166,3 +166,97 @@ EOF
# Verify old file was actually removed
[ ! -f "$HOME/Library/Mail Downloads/old.pdf" ]
}
@test "clean_time_machine_failed_backups detects running backup correctly" {
# Skip test if tmutil is not available
if ! command -v tmutil > /dev/null 2>&1; then
skip "tmutil not available"
fi
# Create a mock tmutil executable
local mock_bin="$HOME/bin"
mkdir -p "$mock_bin"
cat > "$mock_bin/tmutil" << 'MOCK_TMUTIL'
#!/bin/bash
if [[ "$1" == "status" ]]; then
cat << 'TMUTIL_OUTPUT'
Backup session status:
{
ClientID = "com.apple.backupd";
Running = 0;
}
TMUTIL_OUTPUT
elif [[ "$1" == "destinationinfo" ]]; then
cat << 'DEST_OUTPUT'
====================================================
Name : TestBackup
Kind : Local
Mount Point : /Volumes/TestBackup
ID : 12345678-1234-1234-1234-123456789012
====================================================
DEST_OUTPUT
fi
MOCK_TMUTIL
chmod +x "$mock_bin/tmutil"
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" PATH="$mock_bin:$PATH" bash --noprofile --norc << 'EOF'
set -euo pipefail
source "$PROJECT_ROOT/lib/core/common.sh"
source "$PROJECT_ROOT/lib/clean/system.sh"
# Run the function - should NOT skip cleanup when Running = 0
clean_time_machine_failed_backups
EOF
[ "$status" -eq 0 ]
# Should NOT output the "backup in progress" message
[[ "$output" != *"Time Machine backup in progress, skipping cleanup"* ]]
}
@test "clean_time_machine_failed_backups skips when backup is actually running" {
# Skip test if tmutil is not available
if ! command -v tmutil > /dev/null 2>&1; then
skip "tmutil not available"
fi
# Create a mock tmutil executable
local mock_bin="$HOME/bin"
mkdir -p "$mock_bin"
cat > "$mock_bin/tmutil" << 'MOCK_TMUTIL'
#!/bin/bash
if [[ "$1" == "status" ]]; then
cat << 'TMUTIL_OUTPUT'
Backup session status:
{
ClientID = "com.apple.backupd";
Running = 1;
}
TMUTIL_OUTPUT
elif [[ "$1" == "destinationinfo" ]]; then
cat << 'DEST_OUTPUT'
====================================================
Name : TestBackup
Kind : Local
Mount Point : /Volumes/TestBackup
ID : 12345678-1234-1234-1234-123456789012
====================================================
DEST_OUTPUT
fi
MOCK_TMUTIL
chmod +x "$mock_bin/tmutil"
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" PATH="$mock_bin:$PATH" bash --noprofile --norc << 'EOF'
set -euo pipefail
source "$PROJECT_ROOT/lib/core/common.sh"
source "$PROJECT_ROOT/lib/clean/system.sh"
# Run the function - should skip cleanup when Running = 1
clean_time_machine_failed_backups
EOF
[ "$status" -eq 0 ]
# Should output the "backup in progress" message
[[ "$output" == *"Time Machine backup in progress, skipping cleanup"* ]]
}