diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh new file mode 100755 index 0000000..cc39ffb --- /dev/null +++ b/scripts/run_tests.sh @@ -0,0 +1,156 @@ +#!/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" "listener" "all" "relay_chn") +arg_tag="all" # Default to 'all' if no tag specified +arg_clean=false +arg_log=false +arg_dry_run=false +arg_sdkconfig_file="" +flag_file=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|listener|all] Specify which test tag to run." + echo "" + echo " If no tag is specified, it defaults to 'all'." + 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 + ;; + --file|-f) + arg_sdkconfig_file="$2" + flag_file=true + 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 + +# ==== 5. Resolve Paths and Switch to Working Directory ==== +script_dir=$(dirname "$(readlink -f "$0")") +project_root=$(dirname "$script_dir") + +echo "๐Ÿ” Searching for 'test_apps' directory in '$project_root'..." +test_apps_dir=$(find "$project_root" -type d -name "test_apps" | head -n 1) + +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 "โœ… Found 'test_apps' at: $test_apps_dir" + +if $flag_file; then + if [[ -z "$arg_sdkconfig_file" || ! -f "$arg_sdkconfig_file" ]]; then + echo "โŒ Invalid or missing file: '$arg_sdkconfig_file'" + usage + fi + # Resolve to an absolute path to work correctly after changing directory + arg_sdkconfig_file=$(readlink -f "$arg_sdkconfig_file") +else + echo "โš ๏ธ No SDK configuration file provided. Using default sdkconfig." + arg_sdkconfig_file="$test_apps_dir/sdkconfig.defaults" +fi + +echo "๐Ÿงช Test mode: $arg_tag" +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 \ +SDKCONFIG_DEFAULTS="$arg_sdkconfig_file" \ +RELAY_CHN_UNITY_TEST_GROUP_TAG="$arg_tag" \ +idf.py reconfigure build + +echo "๐Ÿš€ Running test with QEMU..." + +if $arg_log; then + TIMESTAMP=$(date +"%Y%m%d_%H%M%S") + LOGFILE="test_log_${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