File size: 3,149 Bytes
89995d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #!/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 <category> <check_name> [status] [--fail <fail_message>] [--pass <pass_message>]
#
# 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
}
|