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

Optimize automated testing

This commit is contained in:
Tw93
2025-12-06 00:18:57 +08:00
parent 3561f088c4
commit 5c20db60af
4 changed files with 45 additions and 19 deletions

View File

@@ -4,4 +4,4 @@ disable=SC2155 # Declare and assign separately
disable=SC2034 # Unused variables disable=SC2034 # Unused variables
disable=SC2059 # Don't use variables in printf format disable=SC2059 # Don't use variables in printf format
disable=SC1091 # Not following sourced files disable=SC1091 # Not following sourced files
disable=SC2038 # Use find -print0 | xargs -0 pattern

View File

@@ -106,7 +106,10 @@ get_file_size() {
# Get file modification time (epoch seconds) using BSD stat # Get file modification time (epoch seconds) using BSD stat
get_file_mtime() { get_file_mtime() {
local file="$1" local file="$1"
[[ -z "$file" ]] && { echo "0"; return; } [[ -z "$file" ]] && {
echo "0"
return
}
local result local result
result=$($STAT_BSD -f%m "$file" 2> /dev/null) result=$($STAT_BSD -f%m "$file" 2> /dev/null)
echo "${result:-0}" echo "${result:-0}"
@@ -562,23 +565,41 @@ run_with_timeout() {
"$@" & "$@" &
local cmd_pid=$! local cmd_pid=$!
# More efficient wait: use wait with timeout in subshell # Create temp marker to track timeout
( local timeout_marker
sleep "$duration" & timeout_marker=$(mktemp 2> /dev/null || echo "/tmp/mole-timeout-$$")
local timer_pid=$!
wait "$cmd_pid" 2> /dev/null && kill "$timer_pid" 2> /dev/null && exit 0
kill -TERM "$cmd_pid" 2> /dev/null || true
sleep 0.5
kill -KILL "$cmd_pid" 2> /dev/null || true
exit 124
) &
local watcher_pid=$!
# Killer: sleep then kill command if still running
(
sleep "$duration"
if kill -0 "$cmd_pid" 2> /dev/null; then
echo "1" > "$timeout_marker" 2> /dev/null || true
kill -TERM "$cmd_pid" 2> /dev/null || true
sleep 0.5
kill -KILL "$cmd_pid" 2> /dev/null || true
fi
) &
local killer_pid=$!
# Wait for command to finish (disable errexit temporarily to prevent exit on wait failure)
local exit_code
set +e
wait "$cmd_pid" 2> /dev/null wait "$cmd_pid" 2> /dev/null
local exit_code=$? exit_code=$?
kill "$watcher_pid" 2> /dev/null || true set -e
wait "$watcher_pid" 2> /dev/null || true
return $exit_code # Kill the killer if command finished early
kill "$killer_pid" 2> /dev/null || true
wait "$killer_pid" 2> /dev/null || true
# Check if timeout occurred
if [[ -f "$timeout_marker" ]] && [[ "$(cat "$timeout_marker" 2> /dev/null)" == "1" ]]; then
rm -f "$timeout_marker" 2> /dev/null || true
return 124
fi
rm -f "$timeout_marker" 2> /dev/null || true
return "$exit_code"
} }
# Menu display helper # Menu display helper
@@ -1172,7 +1193,10 @@ clean_tool_cache() {
# Returns: size in KB, or 0 if path doesn't exist or error occurs # Returns: size in KB, or 0 if path doesn't exist or error occurs
get_path_size_kb() { get_path_size_kb() {
local path="$1" local path="$1"
[[ -z "$path" || ! -e "$path" ]] && { echo "0"; return; } [[ -z "$path" || ! -e "$path" ]] && {
echo "0"
return
}
local result local result
result=$(command du -sk "$path" 2> /dev/null | awk '{print $1}') result=$(command du -sk "$path" 2> /dev/null | awk '{print $1}')
echo "${result:-0}" echo "${result:-0}"

2
mole
View File

@@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/core/common.sh" source "$SCRIPT_DIR/lib/core/common.sh"
# Version info # Version info
VERSION="1.11.30" VERSION="1.11.31"
MOLE_TAGLINE="can dig deep to clean your Mac." MOLE_TAGLINE="can dig deep to clean your Mac."
# Check if Touch ID is already configured # Check if Touch ID is already configured

View File

@@ -32,6 +32,7 @@ setup() {
run bash --noprofile --norc -c ' run bash --noprofile --norc -c '
set -euo pipefail set -euo pipefail
PATH="/usr/bin:/bin" PATH="/usr/bin:/bin"
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
source "'"$PROJECT_ROOT"'/lib/core/common.sh" source "'"$PROJECT_ROOT"'/lib/core/common.sh"
run_with_timeout 1 sleep 0.1 run_with_timeout 1 sleep 0.1
' '
@@ -42,6 +43,7 @@ setup() {
run bash --noprofile --norc -c ' run bash --noprofile --norc -c '
set -euo pipefail set -euo pipefail
PATH="/usr/bin:/bin" PATH="/usr/bin:/bin"
unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN
source "'"$PROJECT_ROOT"'/lib/core/common.sh" source "'"$PROJECT_ROOT"'/lib/core/common.sh"
run_with_timeout 1 sleep 5 run_with_timeout 1 sleep 5
' '