mirror of
https://github.com/tw93/Mole.git
synced 2026-03-22 18:30:08 +00:00
trust softwareupdate for macOS status and harden tests
This commit is contained in:
@@ -351,24 +351,29 @@ check_macos_update() {
|
|||||||
# Check whitelist
|
# Check whitelist
|
||||||
if command -v is_whitelisted > /dev/null && is_whitelisted "check_macos_updates"; then return; fi
|
if command -v is_whitelisted > /dev/null && is_whitelisted "check_macos_updates"; then return; fi
|
||||||
|
|
||||||
# Fast check using system preferences
|
# Fast check using system preferences to avoid unnecessary scans.
|
||||||
local updates_available="false"
|
# We only surface a macOS update when softwareupdate itself lists a macOS
|
||||||
if [[ $(get_software_updates) == "Updates Available" ]]; then
|
# system update. If softwareupdate fails, times out, or does not list any
|
||||||
updates_available="true"
|
# macOS-labelled entries, we treat the system as up to date to avoid
|
||||||
|
# false-positive warnings.
|
||||||
|
local updates_available
|
||||||
|
updates_available="false"
|
||||||
|
|
||||||
|
if [[ $(get_software_updates) == "Updates Available" ]]; then
|
||||||
|
local sw_output
|
||||||
|
sw_output=""
|
||||||
|
local sw_status
|
||||||
|
sw_status=0
|
||||||
|
local spinner_started
|
||||||
|
spinner_started="false"
|
||||||
|
|
||||||
# Verify with softwareupdate using --no-scan to avoid triggering a fresh scan
|
|
||||||
# which can timeout. We prioritize avoiding false negatives (missing actual updates)
|
|
||||||
# over false positives, so we only clear the update flag when softwareupdate
|
|
||||||
# explicitly reports "No new software available"
|
|
||||||
local sw_output=""
|
|
||||||
local sw_status=0
|
|
||||||
local spinner_started=false
|
|
||||||
if [[ -t 1 ]]; then
|
if [[ -t 1 ]]; then
|
||||||
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Checking macOS updates..."
|
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Checking macOS updates..."
|
||||||
spinner_started=true
|
spinner_started="true"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local softwareupdate_timeout=10
|
local softwareupdate_timeout
|
||||||
|
softwareupdate_timeout=10
|
||||||
if sw_output=$(run_with_timeout "$softwareupdate_timeout" softwareupdate -l --no-scan 2> /dev/null); then
|
if sw_output=$(run_with_timeout "$softwareupdate_timeout" softwareupdate -l --no-scan 2> /dev/null); then
|
||||||
:
|
:
|
||||||
else
|
else
|
||||||
@@ -384,22 +389,18 @@ check_macos_update() {
|
|||||||
echo "[DEBUG] softwareupdate exit status: $sw_status, output lines: $(echo "$sw_output" | wc -l | tr -d ' ')" >&2
|
echo "[DEBUG] softwareupdate exit status: $sw_status, output lines: $(echo "$sw_output" | wc -l | tr -d ' ')" >&2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Prefer avoiding false negatives: if the system indicates updates are pending,
|
# Only trust softwareupdate as the source of truth. We surface a macOS
|
||||||
# only clear the flag when softwareupdate returns a list without any update entries.
|
# update *only* when softwareupdate successfully returns at least one
|
||||||
# However, macOS doesn't distinguish between system and App Store updates in the
|
# macOS-labelled entry; otherwise we prefer a false negative over a
|
||||||
# LastRecommendedUpdatesAvailable counter, so we additionally require that at least
|
# false positive.
|
||||||
# one listed update is a macOS system update before showing a macOS update warning.
|
|
||||||
if [[ $sw_status -eq 0 && -n "$sw_output" ]]; then
|
if [[ $sw_status -eq 0 && -n "$sw_output" ]]; then
|
||||||
if ! echo "$sw_output" | grep -qE '^[[:space:]]*\*'; then
|
if echo "$sw_output" | grep -qE '^[[:space:]]*\*'; then
|
||||||
# No update entries at all
|
local has_macos_update
|
||||||
updates_available="false"
|
has_macos_update="false"
|
||||||
else
|
|
||||||
# softwareupdate output may include both macOS and App Store updates.
|
|
||||||
# Treat only entries whose Label contains "macOS" as system updates.
|
|
||||||
local has_macos_update="false"
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
[[ "$line" =~ ^[[:space:]]*\* ]] || continue
|
[[ "$line" =~ ^[[:space:]]*\* ]] || continue
|
||||||
local label="$line"
|
local label
|
||||||
|
label="$line"
|
||||||
label="${label#*Label: }"
|
label="${label#*Label: }"
|
||||||
label="${label%%,*}"
|
label="${label%%,*}"
|
||||||
local lower_label
|
local lower_label
|
||||||
@@ -410,9 +411,8 @@ check_macos_update() {
|
|||||||
fi
|
fi
|
||||||
done <<< "$sw_output"
|
done <<< "$sw_output"
|
||||||
|
|
||||||
if [[ "$has_macos_update" != "true" ]]; then
|
if [[ "$has_macos_update" == "true" ]]; then
|
||||||
# Only App Store updates are pending – don't flag macOS as outdated
|
updates_available="true"
|
||||||
updates_available="false"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -227,37 +227,42 @@ fi
|
|||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
echo "6. Testing installation..."
|
echo "6. Testing installation..."
|
||||||
# Skip if Homebrew mole is installed (install.sh will refuse to overwrite)
|
# Installation script is macOS-specific; skip this test on non-macOS platforms
|
||||||
install_test_home=""
|
if [[ "$(uname -s)" != "Darwin" ]]; then
|
||||||
if command -v brew > /dev/null 2>&1 && brew list mole &> /dev/null; then
|
printf "${YELLOW}${ICON_WARNING} Installation test skipped (non-macOS)${NC}\n"
|
||||||
printf "${GREEN}${ICON_SUCCESS} Installation test skipped, Homebrew${NC}\n"
|
|
||||||
else
|
else
|
||||||
install_test_home="$(mktemp -d /tmp/mole-test-home.XXXXXX 2> /dev/null || true)"
|
# Skip if Homebrew mole is installed (install.sh will refuse to overwrite)
|
||||||
if [[ -z "$install_test_home" ]]; then
|
install_test_home=""
|
||||||
install_test_home="/tmp/mole-test-home"
|
if command -v brew > /dev/null 2>&1 && brew list mole &> /dev/null; then
|
||||||
mkdir -p "$install_test_home"
|
printf "${GREEN}${ICON_SUCCESS} Installation test skipped, Homebrew${NC}\n"
|
||||||
|
else
|
||||||
|
install_test_home="$(mktemp -d /tmp/mole-test-home.XXXXXX 2> /dev/null || true)"
|
||||||
|
if [[ -z "$install_test_home" ]]; then
|
||||||
|
install_test_home="/tmp/mole-test-home"
|
||||||
|
mkdir -p "$install_test_home"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
if [[ -z "$install_test_home" ]]; then
|
||||||
if [[ -z "$install_test_home" ]]; then
|
:
|
||||||
:
|
elif HOME="$install_test_home" \
|
||||||
elif HOME="$install_test_home" \
|
XDG_CONFIG_HOME="$install_test_home/.config" \
|
||||||
XDG_CONFIG_HOME="$install_test_home/.config" \
|
XDG_CACHE_HOME="$install_test_home/.cache" \
|
||||||
XDG_CACHE_HOME="$install_test_home/.cache" \
|
MO_NO_OPLOG=1 \
|
||||||
MO_NO_OPLOG=1 \
|
./install.sh --prefix /tmp/mole-test > /dev/null 2>&1; then
|
||||||
./install.sh --prefix /tmp/mole-test > /dev/null 2>&1; then
|
if [[ -f "/tmp/mole-test/mole" ]]; then
|
||||||
if [ -f /tmp/mole-test/mole ]; then
|
printf "${GREEN}${ICON_SUCCESS} Installation test passed${NC}\n"
|
||||||
printf "${GREEN}${ICON_SUCCESS} Installation test passed${NC}\n"
|
else
|
||||||
|
printf "${RED}${ICON_ERROR} Installation test failed${NC}\n"
|
||||||
|
((FAILED++))
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
printf "${RED}${ICON_ERROR} Installation test failed${NC}\n"
|
printf "${RED}${ICON_ERROR} Installation test failed${NC}\n"
|
||||||
((FAILED++))
|
((FAILED++))
|
||||||
fi
|
fi
|
||||||
else
|
MO_NO_OPLOG=1 safe_remove "/tmp/mole-test" true || true
|
||||||
printf "${RED}${ICON_ERROR} Installation test failed${NC}\n"
|
if [[ -n "$install_test_home" ]]; then
|
||||||
((FAILED++))
|
MO_NO_OPLOG=1 safe_remove "$install_test_home" true || true
|
||||||
fi
|
fi
|
||||||
MO_NO_OPLOG=1 safe_remove "/tmp/mole-test" true || true
|
|
||||||
if [[ -n "$install_test_home" ]]; then
|
|
||||||
MO_NO_OPLOG=1 safe_remove "$install_test_home" true || true
|
|
||||||
fi
|
fi
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
|
|||||||
@@ -607,8 +607,8 @@ echo "MACOS_UPDATE_AVAILABLE=$MACOS_UPDATE_AVAILABLE"
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[[ "$output" == *"Update available"* ]]
|
[[ "$output" == *"System up to date"* ]]
|
||||||
[[ "$output" == *"MACOS_UPDATE_AVAILABLE=true"* ]]
|
[[ "$output" == *"MACOS_UPDATE_AVAILABLE=false"* ]]
|
||||||
[[ "$output" != *"BAD_TIMEOUT:"* ]]
|
[[ "$output" != *"BAD_TIMEOUT:"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,8 +641,8 @@ echo "MACOS_UPDATE_AVAILABLE=$MACOS_UPDATE_AVAILABLE"
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[[ "$output" == *"Update available"* ]]
|
[[ "$output" == *"System up to date"* ]]
|
||||||
[[ "$output" == *"MACOS_UPDATE_AVAILABLE=true"* ]]
|
[[ "$output" == *"MACOS_UPDATE_AVAILABLE=false"* ]]
|
||||||
[[ "$output" != *"BAD_TIMEOUT:"* ]]
|
[[ "$output" != *"BAD_TIMEOUT:"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user