Files
mharb 3f5796efa2 v0.4
Signed-off-by: mharb <mharb@noreply.localhost>
2026-10-13 21:27:01 +00:00

131 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run all .NET tests for the repository with prerequisites checks
set -euo pipefail
echo "Running ModPulse tests"
# Prefer repository-local dotnet install if present
if [ -x "./.dotnet10/dotnet" ]; then
DOTNET_BIN="./.dotnet10/dotnet"
elif command -v dotnet &> /dev/null; then
DOTNET_BIN="dotnet"
else
echo "Error: dotnet SDK not found. Install .NET 8+ from https://dotnet.microsoft.com/download"
exit 2
fi
# Opt out of telemetry and reduce network chatter
export DOTNET_CLI_TELEMETRY_OPTOUT=1
DOTNET_VERSION=$($DOTNET_BIN --version)
DOTNET_MAJOR=$(echo "$DOTNET_VERSION" | cut -d. -f1)
if [ "$DOTNET_MAJOR" -lt 8 ]; then
echo "Error: .NET 8.0 or later is required. Found: $DOTNET_VERSION"
exit 3
fi
echo "dotnet $DOTNET_VERSION found at $DOTNET_BIN. Restoring, building, and running tests..."
# Adaptive parallelism: leave 2 cores free on 8+ core hosts, else 2; env override via DOTNET_MAX_CPU_COUNT or BUILD_PARALLELISM
CORES=$(nproc 2>/dev/null || echo 2)
if [ -n "${BUILD_PARALLELISM:-}" ]; then
: # use provided
elif [ "$CORES" -le 2 ]; then
BUILD_PARALLELISM=1
elif [ "$CORES" -gt 4 ]; then
BUILD_PARALLELISM=$((CORES - 2))
else
BUILD_PARALLELISM=2
fi
# Respect DOTNET_MAX_CPU_COUNT if set
if [ -n "${DOTNET_MAX_CPU_COUNT:-}" ] && [ "$DOTNET_MAX_CPU_COUNT" -lt "$BUILD_PARALLELISM" ]; then
BUILD_PARALLELISM=$DOTNET_MAX_CPU_COUNT
fi
# Optionally disable analyzers for faster local iterations
if [ "${DISABLE_ANALYZERS:-true}" = "true" ]; then
ANALYZER_FLAG="/p:RunAnalyzersDuringBuild=false"
else
ANALYZER_FLAG=""
fi
# Optionally disable symbol generation to reduce disk IO and build time
if [ "${DISABLE_SYMBOLS:-true}" = "true" ]; then
SYMBOLS_FLAG="/p:DebugSymbols=false /p:DebugType=None"
else
SYMBOLS_FLAG=""
fi
MSBUILD_MT_FLAG="-m:${BUILD_PARALLELISM}"
# Restore solution (single threaded to avoid contention)
$DOTNET_BIN restore ModPulse.sln
# Build full solution (includes Simulator) with reduced outputs
$DOTNET_BIN build ModPulse.sln -c Release $MSBUILD_MT_FLAG $ANALYZER_FLAG $SYMBOLS_FLAG --no-restore --nologo
# Configure simulator ports (choose free high ports to avoid collisions)
find_free_port() {
if command -v python3 >/dev/null 2>&1; then
python3 - <<'PY'
import socket
s=socket.socket()
s.bind(('',0))
p=s.getsockname()[1]
s.close()
print(p)
PY
else
# Fallback: use common default ports if python is not available
echo 10502
fi
}
SIMULATOR_PORT=$(find_free_port)
SIMULATOR_API_PORT=$(find_free_port)
if [ -z "${SIMULATOR_PORT}" ] || [ -z "${SIMULATOR_API_PORT}" ]; then
echo "Error: could not find free ports for simulator"
exit 4
fi
export SIMULATOR_PORT
export SIMULATOR_API_PORT
# Make integration/UAT part of test run by default; allow override via env
export RUN_INTEGRATION_TESTS="${RUN_INTEGRATION_TESTS:-true}"
export RUN_UAT="${RUN_UAT:-true}"
# Start simulator on configured REST and Modbus ports (run built DLL to respect ASPNETCORE_URLS)
ASPNETCORE_URLS="http://localhost:${SIMULATOR_API_PORT}" Simulator__Port=${SIMULATOR_PORT} $DOTNET_BIN src/ModPulse.Simulator/bin/Release/net10.0/ModPulse.Simulator.dll &
SIM_PID=$!
echo "Started simulator (PID: $SIM_PID). Waiting for health..."
# Wait for health endpoint (shorter timeout)
for i in {1..30}; do
if curl -fsS http://localhost:${SIMULATOR_API_PORT}/health >/dev/null 2>&1; then
echo "Simulator healthy"
break
fi
sleep 1
done
# Run tests with no-build (solution already built); tests are gated by RUN_INTEGRATION_TESTS and RUN_UAT env vars
TEST_PROJECT="tests/ModPulse.Tests.csproj"
$DOTNET_BIN test "$TEST_PROJECT" -c Release --no-build --no-restore --nologo --logger trx --verbosity minimal || TEST_FAILED=1
# Stop simulator
if [ -n "${SIM_PID-}" ]; then
echo "Stopping simulator $SIM_PID"
kill $SIM_PID 2>/dev/null || true
wait $SIM_PID 2>/dev/null || true
fi
if [ -n "${TEST_FAILED-}" ]; then
echo "Tests failed"
exit 1
fi
echo "All tests completed."