#!/bin/bash # Configuration CHECK_DIR="/logs/verifier" CHECK_FILE="$CHECK_DIR/validate_results.txt" # Global error accumulator. Sourcing scripts can check this variable # to see if any previous validation checks have failed. CHECK_FAILED=0 # Note: The results file ($CHECK_FILE) is cleared by the evaluator.py # at the start of the validation phase to allow multiple scripts (global + local) # to accumulate their results in this file. # Reports the status of a specific validation check. # # Usage: # validate [status] [--fail ] [--pass ] # # Arguments: # category : The category of the test (e.g., "functional", "requirements"). # check_name : Unique identifier for the check (e.g., "uses_sms_retriever"). # status : Optional. Exit code (0-255). If omitted, the exit status of the # immediately preceding command ($?) is used. # --fail, -f : Optional message to print to stdout if the check fails. # --pass, -p : Optional message to print to stdout if the check passes. validate() { # CRITICAL: Capture the caller's exit status immediately on the first line. local caller_status=$? local category="$1" local check_name="$2" shift 2 local status="" # Check if the next argument is a numeric status. # If it is, use it. Otherwise, use the captured caller status. if [[ "$1" =~ ^[0-9]+$ ]]; then status="$1" shift else status="$caller_status" fi local fail_msg="" local pass_msg="" # Parse named parameters for optional messages while [ "$#" -gt 0 ]; do case "$1" in --fail|-f) fail_msg="$2" shift 2 ;; --pass|-p) pass_msg="$2" shift 2 ;; *) echo "Unknown option: $1" >&2 shift ;; esac done local val="pass" if [ "$status" -ne 0 ]; then val="fail" CHECK_FAILED=1 # Mark global status as failed if [ -n "$fail_msg" ]; then echo "❌ FAILED: [$category#$check_name] $fail_msg" else echo "❌ FAILED: [$category#$check_name]" fi else if [ -n "$pass_msg" ]; then echo "✅ PASS: [$category#$check_name] $pass_msg" else echo "✅ PASS: [$category#$check_name]" fi fi # Append the result to the YAML file. # Evaluator.py will read this file to merge checks into partial score. echo "$category#$check_name: $val" >> "$CHECK_FILE" } # Finalizes the validation run. # Prints a summary banner and exits the script with the overall status code. # This function MUST be called at the very end of the validation script. validate_finalize() { echo "=====================================================" if [ $CHECK_FAILED -eq 0 ]; then echo "🎉 Validation Successful!" else echo "❌ Validation Failed with errors." fi echo "=====================================================" exit $CHECK_FAILED }