#!/usr/bin/env bash set -e # ==== 1. Check ESP-IDF environment ==== if [[ -z "$IDF_PATH" ]]; then echo "❌ ESP-IDF environment not found. Please source the export.sh file first:" echo "'. $HOME/esp/esp-idf/export.sh' or wherever the ESP-IDF is installed" exit 1 fi # ==== 2. Valid Modes and Defaults ==== valid_test_tags=("core" "tilt" "notify" "all" "relay_chn" "nvs" "run_limit" "batch" "inertia" "direction" "auto" "sensitivity" "counter" "interrupt") valid_test_profiles=("run_limit" "tilt" "nvs" "nvs_custom" "multi" "full_single" "full_multi") arg_tag="all" # Default to 'all' if no tag specified arg_profile="full_multi" # Default to 'full_multi' if no profile specified arg_clean=false arg_log=false arg_dry_run=false print_help() { echo "Usage: $0 -t [OPTIONS]" echo "" echo "This script builds and runs tests for the relay_chn component using QEMU." echo "" echo "Arguments:" echo " -t, --tag [relay_chn|core|tilt|notify|nvs|run_limit|batch|inertia|direction|auto|sensitivity|counter|interrupt|all] Specify which test tag to run." echo "" echo " If no tag is specified, it defaults to 'all'." echo "" echo " -p, --profile [run_limit|tilt|nvs|nvs_custom|multi|full_single|full_multi] Specify which test tag to run." echo "" echo " If no profile is specified, it defaults to 'full_multi'." echo "" echo "Options:" echo " -f, --file Specify a custom sdkconfig file to use for the build." echo " Defaults to 'sdkconfig.defaults' if not provided." echo " -c, --clean Perform a 'fullclean' before building the tests." echo " -l, --log Log the test output to a timestamped file." echo " -n, --dry-run Build the project without running qemu." echo " -h, --help Show this help message and exit." } help() { print_help exit 0 } usage() { print_help exit 1 } # ==== 3. Argument Parsing ==== while [[ $# -gt 0 ]]; do case $1 in --tag|-t) arg_tag="$2" shift 2 ;; --profile|-p) arg_profile="$2" shift 2 ;; --clean|-c) arg_clean=true shift ;; --log|-l) arg_log=true shift ;; --dry-run|-n) arg_dry_run=true shift ;; --help|-h) help ;; *) usage ;; esac done # ==== 4. Validity Check ==== if [[ ! " ${valid_test_tags[*]} " =~ " $arg_tag " ]]; then echo "❌ Invalid mode: '$arg_tag'" usage fi if [[ ! " ${valid_test_profiles[*]} " =~ " $arg_profile " ]]; then echo "❌ Invalid profile: '$arg_profile'" usage fi # ==== 5. Resolve Paths and Switch to Working Directory ==== script_dir=$(dirname "$(readlink -f "$0")") project_root=$(dirname "$script_dir") test_apps_dir="${project_root}/test_apps" if [[ -z "$test_apps_dir" || ! -d "$test_apps_dir" ]]; then echo "❌ 'test_apps' directory not found within the project root: '$project_root'" echo " Please ensure the script is in a 'scripts' directory and 'test_apps' is a sibling." exit 1 fi echo "⏳ Current time is: $(date +"%Y-%m-%d %H:%M:%S")" echo "🧪 Test mode: $arg_tag | Profile: $arg_profile" echo "🧹 Clean: $arg_clean | 📄 Log: $arg_log" echo "📂 Changing to working directory: $test_apps_dir" cd "$test_apps_dir" || exit 1 # ==== 6. Clean if requested ==== if $arg_clean; then echo "🧹 Doing Fullclean..." idf.py fullclean rm sdkconfig fi # ==== 7. Building and Running Tests ==== # In some locales, we can get errors like: "Error: unknown opcode or format name 'wsr.IBREAKA1'" # The 'LC_ALL=C' env variable is set to ensure consistent locale settings. LC_ALL=C \ RELAY_CHN_UNITY_TEST_GROUP_TAG="$arg_tag" \ idf.py @profiles/"${arg_profile}" reconfigure build echo "🚀 Running test with QEMU..." if $arg_log; then TIMESTAMP=$(date +"%Y%m%d_%H%M%S") LOGFILE="test_log_${arg_profile}_${arg_tag}_$TIMESTAMP.txt" if $arg_dry_run; then echo "🔍 Dry run mode: Logging to $LOGFILE but not executing." | tee "$LOGFILE" echo "Command: idf.py qemu" | tee "$LOGFILE" else echo "📜 Logging test output to: $LOGFILE" idf.py qemu --qemu-extra-args "-no-reboot" | tee "$LOGFILE" fi else if $arg_dry_run; then echo "🔍 Dry run mode: Not executing idf.py qemu." echo "Command: idf.py qemu" else echo "🚀 Running idf.py qemu..." idf.py qemu --qemu-extra-args "-no-reboot" fi fi