Skip to content

Commit 9c90f2e

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 5d379ce commit 9c90f2e

File tree

2 files changed

+115
-46
lines changed

2 files changed

+115
-46
lines changed

Runner/init_env

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
#!/bin/sh
2+
23
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
34
# SPDX-License-Identifier: BSD-3-Clause-Clear
45

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
6+
# Idempotency guard: only initialize ONCE per shell session
7+
[ -n "$__INIT_ENV_LOADED" ] && return
8+
__INIT_ENV_LOADED=1
9+
10+
# (Optional) Remove/comment log line below to keep CI logs clean:
11+
# echo "[INFO] init_env loaded."
12+
13+
# --- Robust root detection ---
14+
if [ -z "$ROOT_DIR" ]; then
15+
# Fast path: current working directory is root
16+
if [ -d "./utils" ] && [ -d "./suites" ]; then
17+
ROOT_DIR="$(pwd)"
18+
else
19+
# Fallback: walk up from this script's location
20+
_script_dir="$(cd "$(dirname "$0")" && pwd)"
21+
_search="$_script_dir"
22+
while [ "$_search" != "/" ]; do
23+
if [ -d "$_search/utils" ] && [ -d "$_search/suites" ]; then
24+
ROOT_DIR="$_search"
25+
break
26+
fi
27+
_search=$(dirname "$_search")
28+
done
29+
fi
30+
fi
31+
32+
if [ ! -d "$ROOT_DIR/utils" ] || [ ! -f "$ROOT_DIR/utils/functestlib.sh" ]; then
33+
echo "[ERROR] Could not detect testkit root (missing utils/ or functestlib.sh)" >&2
34+
exit 1
35+
fi
36+
37+
export ROOT_DIR
38+
export TOOLS="$ROOT_DIR/utils"
39+
export __RUNNER_SUITES_DIR="$ROOT_DIR/suites"
40+
export __RUNNER_UTILS_BIN_DIR="$ROOT_DIR/common"

Runner/run-test.sh

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,110 @@
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+
# Resolve the real path of this script
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
88

9-
#import test functions library
10-
. "${TOOLS}"/functestlib.sh
9+
# Set TOOLS path to utils under the script directory
10+
TOOLS="$SCRIPT_DIR/utils"
1111

12+
# Safely source init_env from the same directory as this script
13+
if [ -f "$SCRIPT_DIR/init_env" ]; then
14+
# shellcheck source=/dev/null
15+
. "$SCRIPT_DIR/init_env"
16+
else
17+
echo "[INFO] init_env not found at $SCRIPT_DIR/init_env — skipping."
18+
fi
1219

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##*/}
19-
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
20+
# Source functestlib.sh from utils/
21+
if [ -f "$TOOLS/functestlib.sh" ]; then
22+
# shellcheck source=/dev/null
23+
. "$TOOLS/functestlib.sh"
24+
# Export key vars so they are visible to child scripts like ./run.sh
25+
export ROOT_DIR
26+
export TOOLS
27+
export __RUNNER_SUITES_DIR
28+
export __RUNNER_UTILS_BIN_DIR
29+
else
30+
echo "[ERROR] functestlib.sh not found at $TOOLS/functestlib.sh"
31+
exit 1
32+
fi
2733

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-
}
34+
# Store results
35+
RESULTS_PASS=""
36+
RESULTS_FAIL=""
3737

38-
# Execute a test case
3938
execute_test_case() {
40-
local test_path="$1"
39+
test_path=$1
40+
test_name=$(basename "$test_path")
41+
4142
if [ -d "$test_path" ]; then
4243
run_script="$test_path/run.sh"
4344
if [ -f "$run_script" ]; then
44-
log "Executing test case: $test_path"
45-
sh "$run_script" 2>&1
45+
log "Executing test case: $test_name"
46+
if (cd "$test_path" && sh "./run.sh"); then
47+
log_pass "$test_name passed"
48+
if [ -z "$RESULTS_PASS" ]; then
49+
RESULTS_PASS="$test_name"
50+
else
51+
RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name")
52+
fi
53+
else
54+
log_fail "$test_name failed"
55+
if [ -z "$RESULTS_FAIL" ]; then
56+
RESULTS_FAIL="$test_name"
57+
else
58+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name")
59+
fi
60+
fi
4661
else
47-
log "No run.sh found in $test_path"
62+
log_error "No run.sh found in $test_path"
63+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)")
4864
fi
4965
else
50-
log "Test case directory not found: $test_path"
66+
log_error "Test case directory not found: $test_path"
67+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)")
5168
fi
5269
}
5370

54-
# Function to run a specific test case by name
5571
run_specific_test_by_name() {
56-
test_name="$1"
57-
test_path=$(find_test_case_by_name ".")
72+
test_name=$1
73+
test_path=$(find_test_case_by_name "$test_name")
5874
if [ -z "$test_path" ]; then
59-
log "Test case with name $test_name not found."
75+
log_error "Test case with name $test_name not found."
76+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)")
6077
else
6178
execute_test_case "$test_path"
6279
fi
6380
}
6481

65-
# Main script logic
82+
run_all_tests() {
83+
find "${__RUNNER_SUITES_DIR}" -maxdepth 3 -type d -name '[A-Za-z]*' | while IFS= read -r test_dir; do
84+
if [ -f "$test_dir/run.sh" ]; then
85+
execute_test_case "$test_dir"
86+
fi
87+
done
88+
}
89+
90+
print_summary() {
91+
echo
92+
log_info "========== Test Summary =========="
93+
echo "PASSED:"
94+
[ -n "$RESULTS_PASS" ] && printf "%s\n" "$RESULTS_PASS" || echo " None"
95+
echo
96+
echo "FAILED:"
97+
[ -n "$RESULTS_FAIL" ] && printf "%s\n" "$RESULTS_FAIL" || echo " None"
98+
log_info "=================================="
99+
}
100+
66101
if [ "$#" -eq 0 ]; then
67102
log "Usage: $0 [all | <testcase_name>]"
68103
exit 1
69104
fi
70105

106+
if [ "$1" = "all" ]; then
107+
run_all_tests
108+
else
109+
run_specific_test_by_name "$1"
110+
fi
71111

72-
run_specific_test_by_name "$1"
112+
print_summary

0 commit comments

Comments
 (0)