Skip to content

Commit 3f1a5bc

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 3f1a5bc

File tree

2 files changed

+108
-45
lines changed

2 files changed

+108
-45
lines changed

Runner/init_env

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
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+
# Dynamically walk up to find the repo root containing both suites/ and utils/
7+
INIT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
ROOT_DIR="$INIT_DIR"
9+
10+
while [ "$ROOT_DIR" != "/" ]; do
11+
if [ -d "$ROOT_DIR/suites" ] && [ -d "$ROOT_DIR/utils" ]; then
12+
break
13+
fi
14+
ROOT_DIR=$(dirname "$ROOT_DIR")
15+
done
16+
17+
# Validate discovery
18+
if [ ! -d "$ROOT_DIR/suites" ] || [ ! -d "$ROOT_DIR/utils" ]; then
19+
echo "[ERROR] Could not detect testkit root from init_env!" >&2
20+
exit 1
21+
fi
22+
23+
export ROOT_DIR
24+
export TOOLS="$ROOT_DIR/utils"
25+
export __RUNNER_SUITES_DIR="$ROOT_DIR/suites"
26+
export __RUNNER_UTILS_BIN_DIR="$ROOT_DIR/common"
27+
28+
echo "[INFO] init_env loaded."
29+
echo "[INFO] ROOT_DIR=$ROOT_DIR"
30+
echo "[INFO] TOOLS=$TOOLS"
31+
echo "[INFO] __RUNNER_SUITES_DIR=$__RUNNER_SUITES_DIR"
32+
echo "[INFO] __RUNNER_UTILS_BIN_DIR=$__RUNNER_UTILS_BIN_DIR"

Runner/run-test.sh

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,112 @@
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="$(dirname "$(realpath "$0")")"
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() {
4039
local test_path="$1"
40+
local test_name
41+
test_name=$(basename "$test_path")
42+
4143
if [ -d "$test_path" ]; then
4244
run_script="$test_path/run.sh"
4345
if [ -f "$run_script" ]; then
44-
log "Executing test case: $test_path"
45-
sh "$run_script" 2>&1
46+
log "Executing test case: $test_name"
47+
if (cd "$test_path" && sh "./run.sh"); then
48+
log_pass "$test_name passed"
49+
if [ -z "$RESULTS_PASS" ]; then
50+
RESULTS_PASS="$test_name"
51+
else
52+
RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name")
53+
fi
54+
else
55+
log_fail "$test_name failed"
56+
if [ -z "$RESULTS_FAIL" ]; then
57+
RESULTS_FAIL="$test_name"
58+
else
59+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name")
60+
fi
61+
fi
4662
else
47-
log "No run.sh found in $test_path"
63+
log_error "No run.sh found in $test_path"
64+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)")
4865
fi
4966
else
50-
log "Test case directory not found: $test_path"
67+
log_error "Test case directory not found: $test_path"
68+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)")
5169
fi
5270
}
5371

54-
# Function to run a specific test case by name
5572
run_specific_test_by_name() {
56-
test_name="$1"
57-
test_path=$(find_test_case_by_name ".")
73+
local test_name="$1"
74+
local test_path
75+
test_path=$(find_test_case_by_name "$test_name")
5876
if [ -z "$test_path" ]; then
59-
log "Test case with name $test_name not found."
77+
log_error "Test case with name $test_name not found."
78+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)")
6079
else
6180
execute_test_case "$test_path"
6281
fi
6382
}
6483

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

108+
if [ "$1" = "all" ]; then
109+
run_all_tests
110+
else
111+
run_specific_test_by_name "$1"
112+
fi
71113

72-
run_specific_test_by_name "$1"
114+
print_summary

0 commit comments

Comments
 (0)