|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 5 | + |
| 6 | +# Resolve the real path of this script |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 8 | + |
| 9 | +# Disable wrapper-level capture; each test will capture in its own folder |
| 10 | +export RUN_STDOUT_ENABLE=0 |
| 11 | +unset RUN_STDOUT_TAG RUN_STDOUT_FILE |
| 12 | + |
| 13 | +# Safely source init_env from the same directory as this script |
| 14 | +# init_env will set TOOLS, ROOT_DIR, __RUNNER_SUITES_DIR, etc. |
| 15 | +if [ -f "$SCRIPT_DIR/init_env" ]; then |
| 16 | + # shellcheck source=/dev/null |
| 17 | + . "$SCRIPT_DIR/init_env" |
| 18 | +else |
| 19 | + echo "[ERROR] init_env not found at $SCRIPT_DIR/init_env" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Verify that init_env set up the environment correctly |
| 24 | +if [ -z "$TOOLS" ] || [ ! -f "$TOOLS/functestlib.sh" ]; then |
| 25 | + echo "[ERROR] functestlib.sh not found at $TOOLS/functestlib.sh" |
| 26 | + echo "[ERROR] init_env may not have set up the environment correctly" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Export key vars so they are visible to child scripts like ./run.sh |
| 31 | +export ROOT_DIR |
| 32 | +export TOOLS |
| 33 | +export __RUNNER_SUITES_DIR |
| 34 | +export __RUNNER_UTILS_BIN_DIR |
| 35 | + |
| 36 | +# Set host-tools specific suites directory |
| 37 | +HOST_TOOLS_DIR="$SCRIPT_DIR" |
| 38 | +export HOST_TOOLS_DIR |
| 39 | + |
| 40 | +# Store results |
| 41 | +RESULTS_PASS="" |
| 42 | +RESULTS_FAIL="" |
| 43 | +RESULTS_SKIP="" |
| 44 | + |
| 45 | +execute_test_case() { |
| 46 | + test_path=$1 |
| 47 | + shift |
| 48 | + |
| 49 | + test_name=$(basename "$test_path") |
| 50 | + |
| 51 | + if [ -d "$test_path" ]; then |
| 52 | + run_script="$test_path/run.sh" |
| 53 | + if [ -f "$run_script" ]; then |
| 54 | + log "Executing test case: $test_name" |
| 55 | + ( |
| 56 | + cd "$test_path" || exit 2 |
| 57 | + # Enable per-test capture in the test folder with a clear tag |
| 58 | + RUN_STDOUT_ENABLE=1 RUN_STDOUT_TAG="$test_name" sh "./run.sh" "$@" |
| 59 | + ) |
| 60 | + res_file="$test_path/$test_name.res" |
| 61 | + if [ -f "$res_file" ]; then |
| 62 | + if grep -q "SKIP" "$res_file"; then |
| 63 | + log_skip "$test_name skipped" |
| 64 | + if [ -z "$RESULTS_SKIP" ]; then |
| 65 | + RESULTS_SKIP="$test_name" |
| 66 | + else |
| 67 | + RESULTS_SKIP=$(printf "%s\n%s" "$RESULTS_SKIP" "$test_name") |
| 68 | + fi |
| 69 | + elif grep -q "PASS" "$res_file"; then |
| 70 | + log_pass "$test_name passed" |
| 71 | + if [ -z "$RESULTS_PASS" ]; then |
| 72 | + RESULTS_PASS="$test_name" |
| 73 | + else |
| 74 | + RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name") |
| 75 | + fi |
| 76 | + elif grep -q "FAIL" "$res_file"; then |
| 77 | + log_fail "$test_name failed" |
| 78 | + if [ -z "$RESULTS_FAIL" ]; then |
| 79 | + RESULTS_FAIL="$test_name" |
| 80 | + else |
| 81 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name") |
| 82 | + fi |
| 83 | + else |
| 84 | + log_fail "$test_name: unknown result in .res file" |
| 85 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (unknown result)") |
| 86 | + fi |
| 87 | + else |
| 88 | + log_fail "$test_name: .res file not found" |
| 89 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (.res not found)") |
| 90 | + fi |
| 91 | + else |
| 92 | + log_error "No run.sh found in $test_path" |
| 93 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)") |
| 94 | + fi |
| 95 | + else |
| 96 | + log_error "Test case directory not found: $test_path" |
| 97 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)") |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +run_specific_test_by_name() { |
| 102 | + test_name=$1 |
| 103 | + shift |
| 104 | + test_path=$(find_test_case_by_name "$test_name") |
| 105 | + if [ -z "$test_path" ]; then |
| 106 | + log_error "Test case with name $test_name not found." |
| 107 | + RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)") |
| 108 | + else |
| 109 | + execute_test_case "$test_path" "$@" |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +run_all_tests() { |
| 114 | + # Search for tests in host-tools directory (not Runner/suites) |
| 115 | + find "${HOST_TOOLS_DIR}" -maxdepth 2 -type d -name '[A-Za-z]*' | while IFS= read -r test_dir; do |
| 116 | + # Skip the host-tools directory itself |
| 117 | + if [ "$test_dir" = "$HOST_TOOLS_DIR" ]; then |
| 118 | + continue |
| 119 | + fi |
| 120 | + if [ -f "$test_dir/run.sh" ]; then |
| 121 | + execute_test_case "$test_dir" |
| 122 | + fi |
| 123 | + done |
| 124 | +} |
| 125 | + |
| 126 | +print_summary() { |
| 127 | + echo |
| 128 | + log_info "========== Test Summary ==========" |
| 129 | + echo "PASSED:" |
| 130 | + [ -n "$RESULTS_PASS" ] && printf "%s\n" "$RESULTS_PASS" || echo " None" |
| 131 | + echo |
| 132 | + echo "FAILED:" |
| 133 | + [ -n "$RESULTS_FAIL" ] && printf "%s\n" "$RESULTS_FAIL" || echo " None" |
| 134 | + echo |
| 135 | + echo "SKIPPED:" |
| 136 | + [ -n "$RESULTS_SKIP" ] && printf "%s\n" "$RESULTS_SKIP" || echo " None" |
| 137 | + log_info "==================================" |
| 138 | +} |
| 139 | + |
| 140 | +print_usage() { |
| 141 | + cat >&2 <<EOF |
| 142 | +Usage: |
| 143 | + "${0##*/}" all |
| 144 | + "${0##*/}" <testcase_name> [arg1 arg2 ...] |
| 145 | +
|
| 146 | +Notes: |
| 147 | + - Extra args are forwarded only when a single <testcase_name> is specified. |
| 148 | + - 'all' runs every test and does not accept additional args. |
| 149 | + - Each test captures stdout/stderr next to its .res file as: |
| 150 | + <testname>_stdout_<timestamp>.log |
| 151 | +EOF |
| 152 | +} |
| 153 | + |
| 154 | +if [ "$#" -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 155 | + print_usage |
| 156 | + if [ "$#" -eq 0 ]; then |
| 157 | + log_error "No arguments provided" |
| 158 | + exit 1 |
| 159 | + else |
| 160 | + exit 0 |
| 161 | + fi |
| 162 | +fi |
| 163 | + |
| 164 | +if [ "$1" = "all" ]; then |
| 165 | + run_all_tests |
| 166 | +else |
| 167 | + test_case_name="$1" |
| 168 | + shift |
| 169 | + run_specific_test_by_name "$test_case_name" "$@" |
| 170 | +fi |
| 171 | + |
| 172 | +print_summary |
0 commit comments