#!/usr/bin/env bash # Runs the headless suites in a few parallel Godot processes, inside a fixed time budget. # # The budget is a hard limit on the whole run. At the deadline, running suites # are stopped and the rest don't start; those are listed as NOT RUN. Only the # user changes BUDGET_SECS. Suites run longest first (by their last time), which # fills the workers best; one that was never timed runs first. # # A suite fails on a failed check, a SCRIPT ERROR or a Godot crash. # # Usage: tools/run_tests.sh [name-filter] e.g. tools/run_tests.sh inspector # tools/run_tests.sh --sweep release sweep: every suite, 5x the budget # Exit codes: 0 all passed, 1 a suite failed, 2 over budget, nothing failed. # Failing suites print their first 8 error lines; passing output is discarded. set -u readonly BUDGET_SECS=15 # The release sweep (before dev goes into main) gets 5 times the budget: a rule of the user's. readonly SWEEP_MULTIPLIER=5 limit_secs=$BUDGET_SECS if [[ "${1:-}" == "--sweep" ]]; then limit_secs=$(( BUDGET_SECS * SWEEP_MULTIPLIER )) shift (( $# )) && { echo "--sweep runs every suite: no filter"; exit 1; } fi cd "$(dirname "$0")/.." || exit 1 GODOT="${GODOT:-$HOME/apps/Godot_v4.7.2-stable_linux.x86_64}" TIMES=.godot/test_times # git-ignored: " " CLASSES=.godot/global_script_class_cache.cfg start_ms=$(date +%s%3N) now_ms() { echo $(( $(date +%s%3N) - start_ms )); } # 1 s is kept back for stopping suites and printing the report. left_ms() { echo $(( limit_secs * 1000 - 1000 - $(now_ms) )); } # Runs a command at low priority; it is killed at the deadline. in_budget() { local left; left=$(left_ms) (( left < 100 )) && return 124 nice -n 10 timeout -s KILL "$(( left / 1000 )).$(printf %03d $(( left % 1000 )))" "$@" } OUT="$(mktemp -d)" trap 'rm -rf "$OUT"' EXIT # Register class_name scripts, only when a script was added or changed its list. # Compares names, not dates, so ordinary edits don't cost an import. declared="$(grep -rhoE --include='*.gd' --exclude-dir=.godot --exclude-dir=.claude '^class_name +[A-Za-z0-9_]+' . | awk '{print $2}' | sort)" known="$(grep -oE '"class": &"[^"]+"' "$CLASSES" 2>/dev/null | cut -d'"' -f4 | sort)" if [[ "$declared" != "$known" ]]; then in_budget "$GODOT" --headless --path . --import >/dev/null 2>&1 fi shopt -s nullglob declare -A last_ms while read -r ms n; do last_ms[$n]=$ms; done < <(cat "$TIMES" 2>/dev/null) mapfile -t suites < <(for t in tests/test_*"${1:-}"*.gd; do n="$(basename "$t" .gd)"; echo "${last_ms[$n]:-99999} $n" done | sort -rn | cut -d' ' -f2) (( ${#suites[@]} )) || { echo "No suite matches '${1:-}'"; exit 1; } # A few Godot workers share the list (tests/run_suites.gd). --fixed-fps makes # timers and animations use simulated time, not real waiting. mkdir "$OUT/claims" SUITES="$(IFS=,; echo "${suites[*]}")" export SUITES CLAIMS="$OUT/claims" jobs_wanted=${JOBS:-6} (( jobs_wanted > ${#suites[@]} )) && jobs_wanted=${#suites[@]} for (( w = 0; w < jobs_wanted; w++ )); do in_budget "$GODOT" --headless --fixed-fps 60 --path . -s res://tests/run_suites.gd -- --sample >"$OUT/worker$w" 2>&1 & done wait # Cut the workers' output into one log per suite; "DONE" gives its time. awk -v out="$OUT" ' /^SUITE test_/ { suite = $2; log_file = out "/" suite ".log"; printf "" >log_file; next } /^DONE test_/ { print $3 >(out "/" $2 ".ms"); print $4 >(out "/" $2 ".failures"); suite = ""; next } suite != "" { print >log_file } ' "$OUT"/worker* fail=0 not_run=() stopped=() declare -A first_err for n in "${suites[@]}"; do log="$OUT/$n.log" crashed=0 if [[ -e "$OUT/$n.ms" ]]; then last_ms[$n]=$(<"$OUT/$n.ms") if [[ $(<"$OUT/$n.failures") == 0 ]] && ! grep -q "SCRIPT ERROR" "$log"; then echo "ok $n" continue fi elif [[ -e "$log" ]] && grep -q "handle_crash" "$log"; then crashed=1 else not_run+=("$n") # Started but stopped at the deadline: it may be what ate the budget. [[ -e "$log" ]] && stopped+=("$n") continue fi fail=1 e="$(grep -m1 -E "SCRIPT ERROR|^ *FAIL [^:]" "$log")" # A shared compile error fails many suites the same way: report it once. if (( ! crashed )) && [[ "$e" == *"SCRIPT ERROR"* ]]; then first_err["$e"]+="$n " else echo "FAIL $n" (( crashed )) && echo " Godot crashed" grep -E "FAIL|SCRIPT ERROR|ERROR|^ +\[[0-9]+\] .*res://" "$log" | head -8 fi done for e in "${!first_err[@]}"; do echo "FAIL ${first_err[$e]}" echo " $e" done for n in "${!last_ms[@]}"; do [[ -e "tests/$n.gd" ]] && echo "${last_ms[$n]} $n" done | sort -n >"$TIMES" took="$(awk -v ms="$(now_ms)" 'BEGIN { printf "%.1f", ms / 1000 }')s of ${limit_secs}s" if (( ${#not_run[@]} )); then echo "NOT RUN (${#not_run[@]}): ${not_run[*]}" (( ${#stopped[@]} )) && echo "Stopped while running: ${stopped[*]}" echo "Slowest: $(sort -rn "$TIMES" | head -5 | awk '{ printf "%s %.1fs ", $2, $1 / 1000 }')" fi if (( fail )); then echo "SOME SUITES FAILED ($took)"; exit 1; fi if (( ${#not_run[@]} )); then echo "OVER BUDGET: $(( ${#suites[@]} - ${#not_run[@]} )) of ${#suites[@]} suites passed ($took)"; exit 2 fi echo "ALL ${#suites[@]} SUITES PASSED ($took)"