From 5c20db60af5a31f7738a7998e9e39bba077d589e Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sat, 6 Dec 2025 00:18:57 +0800 Subject: [PATCH] Optimize automated testing --- .shellcheckrc | 2 +- lib/core/common.sh | 58 +++++++++++++++++++++++++---------- mole | 2 +- tests/optimization_tasks.bats | 2 ++ 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/.shellcheckrc b/.shellcheckrc index 92118f3..a7842b2 100644 --- a/.shellcheckrc +++ b/.shellcheckrc @@ -4,4 +4,4 @@ disable=SC2155 # Declare and assign separately disable=SC2034 # Unused variables disable=SC2059 # Don't use variables in printf format disable=SC1091 # Not following sourced files - +disable=SC2038 # Use find -print0 | xargs -0 pattern diff --git a/lib/core/common.sh b/lib/core/common.sh index f566dc7..4d99f7e 100755 --- a/lib/core/common.sh +++ b/lib/core/common.sh @@ -106,7 +106,10 @@ get_file_size() { # Get file modification time (epoch seconds) using BSD stat get_file_mtime() { local file="$1" - [[ -z "$file" ]] && { echo "0"; return; } + [[ -z "$file" ]] && { + echo "0" + return + } local result result=$($STAT_BSD -f%m "$file" 2> /dev/null) echo "${result:-0}" @@ -562,23 +565,41 @@ run_with_timeout() { "$@" & local cmd_pid=$! - # More efficient wait: use wait with timeout in subshell - ( - sleep "$duration" & - 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=$! + # Create temp marker to track timeout + local timeout_marker + timeout_marker=$(mktemp 2> /dev/null || echo "/tmp/mole-timeout-$$") + # 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 - local exit_code=$? - kill "$watcher_pid" 2> /dev/null || true - wait "$watcher_pid" 2> /dev/null || true - return $exit_code + exit_code=$? + set -e + + # 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 @@ -1172,7 +1193,10 @@ clean_tool_cache() { # Returns: size in KB, or 0 if path doesn't exist or error occurs get_path_size_kb() { local path="$1" - [[ -z "$path" || ! -e "$path" ]] && { echo "0"; return; } + [[ -z "$path" || ! -e "$path" ]] && { + echo "0" + return + } local result result=$(command du -sk "$path" 2> /dev/null | awk '{print $1}') echo "${result:-0}" diff --git a/mole b/mole index 4d72605..ebaeecd 100755 --- a/mole +++ b/mole @@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/lib/core/common.sh" # Version info -VERSION="1.11.30" +VERSION="1.11.31" MOLE_TAGLINE="can dig deep to clean your Mac." # Check if Touch ID is already configured diff --git a/tests/optimization_tasks.bats b/tests/optimization_tasks.bats index 4ee90a0..b1a6a2d 100644 --- a/tests/optimization_tasks.bats +++ b/tests/optimization_tasks.bats @@ -32,6 +32,7 @@ setup() { run bash --noprofile --norc -c ' set -euo pipefail PATH="/usr/bin:/bin" + unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN source "'"$PROJECT_ROOT"'/lib/core/common.sh" run_with_timeout 1 sleep 0.1 ' @@ -42,6 +43,7 @@ setup() { run bash --noprofile --norc -c ' set -euo pipefail PATH="/usr/bin:/bin" + unset MO_TIMEOUT_INITIALIZED MO_TIMEOUT_BIN source "'"$PROJECT_ROOT"'/lib/core/common.sh" run_with_timeout 1 sleep 5 '