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

feat: overhaul quality checks and expand test suite for clean and optimize features

This commit is contained in:
Tw93
2025-12-31 18:13:37 +08:00
parent 1e8ff30fa1
commit 592f02e6e2
45 changed files with 1506 additions and 910 deletions

View File

@@ -1,17 +1,11 @@
#!/usr/bin/env bats
# Timeout functionality tests
# Tests for lib/core/timeout.sh
setup() {
PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
export PROJECT_ROOT
export MO_DEBUG=0 # Disable debug output for cleaner tests
export MO_DEBUG=0
}
# =================================================================
# Basic Timeout Functionality
# =================================================================
@test "run_with_timeout: command completes before timeout" {
result=$(bash -c "
set -euo pipefail
@@ -48,10 +42,6 @@ setup() {
[[ "$result" == "no_timeout" ]]
}
# =================================================================
# Exit Code Handling
# =================================================================
@test "run_with_timeout: preserves command exit code on success" {
bash -c "
set -euo pipefail
@@ -65,7 +55,7 @@ setup() {
@test "run_with_timeout: preserves command exit code on failure" {
set +e
bash -c "
set +e # Don't exit on error
set +e
source '$PROJECT_ROOT/lib/core/timeout.sh'
run_with_timeout 5 false
exit \$?
@@ -76,8 +66,6 @@ setup() {
}
@test "run_with_timeout: returns 124 on timeout (if using gtimeout)" {
# This test only passes if gtimeout/timeout is available
# Skip if using shell fallback (can't guarantee exit code 124 in all cases)
if ! command -v gtimeout >/dev/null 2>&1 && ! command -v timeout >/dev/null 2>&1; then
skip "gtimeout/timeout not available"
fi
@@ -94,12 +82,7 @@ setup() {
[[ $exit_code -eq 124 ]]
}
# =================================================================
# Timeout Behavior
# =================================================================
@test "run_with_timeout: kills long-running command" {
# Command should be killed after 2 seconds
start_time=$(date +%s)
set +e
bash -c "
@@ -111,13 +94,10 @@ setup() {
end_time=$(date +%s)
duration=$((end_time - start_time))
# Should complete in ~2 seconds, not 30
# Allow some margin (up to 5 seconds for slow systems)
[[ $duration -lt 10 ]]
}
@test "run_with_timeout: handles fast-completing commands" {
# Fast command should complete immediately
start_time=$(date +%s)
bash -c "
set -euo pipefail
@@ -127,14 +107,9 @@ setup() {
end_time=$(date +%s)
duration=$((end_time - start_time))
# Should complete in ~0 seconds
[[ $duration -lt 3 ]]
}
# =================================================================
# Pipefail Compatibility
# =================================================================
@test "run_with_timeout: works in pipefail mode" {
result=$(bash -c "
set -euo pipefail
@@ -154,10 +129,6 @@ setup() {
[[ "$result" == "survived" ]]
}
# =================================================================
# Command Arguments
# =================================================================
@test "run_with_timeout: handles commands with arguments" {
result=$(bash -c "
set -euo pipefail
@@ -176,10 +147,6 @@ setup() {
[[ "$result" == "hello world" ]]
}
# =================================================================
# Debug Logging
# =================================================================
@test "run_with_timeout: debug logging when MO_DEBUG=1" {
output=$(bash -c "
set -euo pipefail
@@ -187,29 +154,20 @@ setup() {
source '$PROJECT_ROOT/lib/core/timeout.sh'
run_with_timeout 5 echo 'test' 2>&1
")
# Should contain debug output
[[ "$output" =~ TIMEOUT ]]
}
@test "run_with_timeout: no debug logging when MO_DEBUG=0" {
# When MO_DEBUG=0, no debug messages should appear during function execution
# (Initialization messages may appear if module is loaded for first time)
output=$(bash -c "
set -euo pipefail
export MO_DEBUG=0
unset MO_TIMEOUT_INITIALIZED # Force re-initialization
unset MO_TIMEOUT_INITIALIZED
source '$PROJECT_ROOT/lib/core/timeout.sh'
# Capture only the function call output, not initialization
run_with_timeout 5 echo 'test'
" 2>/dev/null) # Discard stderr (initialization messages)
# Should only have command output
" 2>/dev/null)
[[ "$output" == "test" ]]
}
# =================================================================
# Module Loading
# =================================================================
@test "timeout.sh: prevents multiple sourcing" {
result=$(bash -c "
set -euo pipefail