Skip to content

Commit 3f14413

Browse files
committed
init_env, run-test.sh: improve testkit root detection and integration logic
- Updated init_env to dynamically resolve the root directory using presence of utils/ and suites/ - Enhanced run-test.sh to be fully POSIX-compliant and standalone-friendly - Ensures both scripts work without requiring a .git repo or fixed working directory - Improved robustness for embedded/CI environments Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 377a8b4 commit 3f14413

File tree

2 files changed

+113
-47
lines changed

2 files changed

+113
-47
lines changed

Runner/init_env

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
#!/bin/sh
2+
23
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
34
# SPDX-License-Identifier: BSD-3-Clause-Clear
4-
5-
# Source this file to setup the test suite environment
6-
BASEDIR=$(pwd)
7-
export BASEDIR
8-
TOOLS=$(pwd)/utils
9-
export TOOLS
10-
SUITES=$(pwd)/suites
11-
export SUITES
5+
6+
# Prevent multiple loads per shell
7+
[ -n "$__INIT_ENV_LOADED" ] && return
8+
__INIT_ENV_LOADED=1
9+
10+
# shellcheck disable=SC2164
11+
INIT_DIR="$(cd "$(dirname "$0")" && pwd)"
12+
ROOT_DIR="$INIT_DIR"
13+
14+
while [ "$ROOT_DIR" != "/" ]; do
15+
if [ -d "$ROOT_DIR/suites" ] && [ -d "$ROOT_DIR/utils" ]; then
16+
break
17+
fi
18+
ROOT_DIR=$(dirname "$ROOT_DIR")
19+
done
20+
21+
if [ ! -d "$ROOT_DIR/suites" ] || [ ! -d "$ROOT_DIR/utils" ]; then
22+
echo "[ERROR] Could not detect testkit root from init_env!" >&2
23+
exit 1
24+
fi
25+
26+
export ROOT_DIR
27+
export TOOLS="$ROOT_DIR/utils"
28+
export __RUNNER_SUITES_DIR="$ROOT_DIR/suites"
29+
export __RUNNER_UTILS_BIN_DIR="$ROOT_DIR/common"
30+
31+
# Show log only if not suppressed
32+
if [ -z "$NO_INIT_LOG" ]; then
33+
if command -v log_info >/dev/null 2>&1; then
34+
log_info "init_env loaded."
35+
log_info "ROOT_DIR=$ROOT_DIR"
36+
log_info "TOOLS=$TOOLS"
37+
log_info "__RUNNER_SUITES_DIR=$__RUNNER_SUITES_DIR"
38+
log_info "__RUNNER_UTILS_BIN_DIR=$__RUNNER_UTILS_BIN_DIR"
39+
else
40+
echo "[INFO] init_env loaded."
41+
echo "[INFO] ROOT_DIR=$ROOT_DIR"
42+
echo "[INFO] TOOLS=$TOOLS"
43+
echo "[INFO] __RUNNER_SUITES_DIR=$__RUNNER_SUITES_DIR"
44+
echo "[INFO] __RUNNER_UTILS_BIN_DIR=$__RUNNER_UTILS_BIN_DIR"
45+
fi
46+
fi

Runner/run-test.sh

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,101 @@
33
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

6-
# Import test suite definitions
7-
. "${PWD}"/init_env
6+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
7+
TOOLS="$SCRIPT_DIR/utils"
88

9-
#import test functions library
10-
. "${TOOLS}"/functestlib.sh
11-
12-
13-
# Find test case path by name
14-
find_test_case_by_name() {
15-
# Check if the file is a directory
16-
if [ -d "$1" ]; then
17-
# Get the directory name
18-
dir_name_in_dir=${1##*/}
9+
# Source init_env
10+
if [ -f "$SCRIPT_DIR/init_env" ]; then
11+
# shellcheck source=/dev/null
12+
. "$SCRIPT_DIR/init_env"
13+
else
14+
echo "[INFO] init_env not found at $SCRIPT_DIR/init_env — skipping."
15+
fi
1916

20-
# Check if the directory name matches the user input
21-
if [ "${dir_name_in_dir}" = "$test_name" ]; then
22-
# Get the absolute path of the directory
23-
abs_path=$(readlink -f "$1")
24-
echo "$abs_path"
25-
fi
26-
fi
17+
# Source functestlib
18+
if [ -f "$TOOLS/functestlib.sh" ]; then
19+
# shellcheck source=/dev/null
20+
. "$TOOLS/functestlib.sh"
21+
export ROOT_DIR TOOLS __RUNNER_SUITES_DIR __RUNNER_UTILS_BIN_DIR
22+
else
23+
echo "[ERROR] functestlib.sh not found at $TOOLS/functestlib.sh"
24+
exit 1
25+
fi
2726

28-
# Recursively search for the directory in the subdirectory
29-
for file in "$1"/*; do
30-
# Check if the file is a directory
31-
if [ -d "$file" ]; then
32-
# Recursively search for the directory in the subdirectory
33-
find_test_case_by_name "$file"
34-
fi
35-
done
36-
}
27+
RESULTS_PASS=""
28+
RESULTS_FAIL=""
3729

38-
# Execute a test case
3930
execute_test_case() {
40-
local test_path="$1"
31+
test_path="$1"
32+
test_name=$(basename "$test_path")
33+
4134
if [ -d "$test_path" ]; then
4235
run_script="$test_path/run.sh"
4336
if [ -f "$run_script" ]; then
44-
log "Executing test case: $test_path"
45-
sh "$run_script" 2>&1
37+
log "Executing test case: $test_name"
38+
if (cd "$test_path" && NO_INIT_LOG=1 sh "./run.sh"); then
39+
log_pass "$test_name passed"
40+
if [ -z "$RESULTS_PASS" ]; then
41+
RESULTS_PASS="$test_name"
42+
else
43+
RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name")
44+
fi
45+
else
46+
log_fail "$test_name failed"
47+
if [ -z "$RESULTS_FAIL" ]; then
48+
RESULTS_FAIL="$test_name"
49+
else
50+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name")
51+
fi
52+
fi
4653
else
47-
log "No run.sh found in $test_path"
54+
log_error "No run.sh found in $test_path"
55+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)")
4856
fi
4957
else
50-
log "Test case directory not found: $test_path"
58+
log_error "Test case directory not found: $test_path"
59+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)")
5160
fi
5261
}
5362

54-
# Function to run a specific test case by name
5563
run_specific_test_by_name() {
5664
test_name="$1"
57-
test_path=$(find_test_case_by_name ".")
65+
test_path=$(find_test_case_by_name "$test_name")
5866
if [ -z "$test_path" ]; then
59-
log "Test case with name $test_name not found."
67+
log_error "Test case with name $test_name not found."
68+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)")
6069
else
6170
execute_test_case "$test_path"
6271
fi
6372
}
6473

65-
# Main script logic
74+
run_all_tests() {
75+
find "${__RUNNER_SUITES_DIR}" -type d -name '[A-Za-z]*' -maxdepth 3 | while IFS= read -r test_dir; do
76+
[ -f "$test_dir/run.sh" ] && execute_test_case "$test_dir"
77+
done
78+
}
79+
80+
print_summary() {
81+
echo
82+
log_info "========== Test Summary =========="
83+
echo "PASSED:"
84+
[ -n "$RESULTS_PASS" ] && printf "%s\n" "$RESULTS_PASS" || echo " None"
85+
echo
86+
echo "FAILED:"
87+
[ -n "$RESULTS_FAIL" ] && printf "%s\n" "$RESULTS_FAIL" || echo " None"
88+
log_info "=================================="
89+
}
90+
91+
# Main
6692
if [ "$#" -eq 0 ]; then
6793
log "Usage: $0 [all | <testcase_name>]"
6894
exit 1
6995
fi
7096

97+
if [ "$1" = "all" ]; then
98+
run_all_tests
99+
else
100+
run_specific_test_by_name "$1"
101+
fi
71102

72-
run_specific_test_by_name "$1"
103+
print_summary

0 commit comments

Comments
 (0)