blob: e84d901f85672fdc7df7fe9e947f3068f398d1bc [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Runs a set of tests in a given subdirectory.
5export skip_rc=4
6export timeout_rc=124
7export logfile=/dev/stdout
8export per_test_logging=
9
10# Defaults for "settings" file fields:
11# "timeout" how many seconds to let each test run before failing.
12export kselftest_default_timeout=45
13
14# There isn't a shell-agnostic way to find the path of a sourced file,
15# so we must rely on BASE_DIR being set to find other tools.
16if [ -z "$BASE_DIR" ]; then
17 echo "Error: BASE_DIR must be set before sourcing." >&2
18 exit 1
19fi
20
21# If Perl is unavailable, we must fall back to line-at-a-time prefixing
22# with sed instead of unbuffered output.
23tap_prefix()
24{
25 if [ ! -x /usr/bin/perl ]; then
26 sed -e 's/^/# /'
27 else
28 "$BASE_DIR"/kselftest/prefix.pl
29 fi
30}
31
32tap_timeout()
33{
34 # Make sure tests will time out if utility is available.
35 if [ -x /usr/bin/timeout ] ; then
36 /usr/bin/timeout "$kselftest_timeout" "$1"
37 else
38 "$1"
39 fi
40}
41
42run_one()
43{
44 DIR="$1"
45 TEST="$2"
46 NUM="$3"
47
48 BASENAME_TEST=$(basename $TEST)
49
50 # Reset any "settings"-file variables.
51 export kselftest_timeout="$kselftest_default_timeout"
52 # Load per-test-directory kselftest "settings" file.
53 settings="$BASE_DIR/$DIR/settings"
54 if [ -r "$settings" ] ; then
55 while read line ; do
56 field=$(echo "$line" | cut -d= -f1)
57 value=$(echo "$line" | cut -d= -f2-)
58 eval "kselftest_$field"="$value"
59 done < "$settings"
60 fi
61
62 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST"
63 echo "# $TEST_HDR_MSG"
64 if [ ! -x "$TEST" ]; then
65 echo -n "# Warning: file $TEST is "
66 if [ ! -e "$TEST" ]; then
67 echo "missing!"
68 else
69 echo "not executable, correct this."
70 fi
71 echo "not ok $test_num $TEST_HDR_MSG"
72 else
73 cd `dirname $TEST` > /dev/null
74 ((((( tap_timeout ./$BASENAME_TEST 2>&1; echo $? >&3) |
75 tap_prefix >&4) 3>&1) |
76 (read xs; exit $xs)) 4>>"$logfile" &&
77 echo "ok $test_num $TEST_HDR_MSG") ||
78 (rc=$?; \
79 if [ $rc -eq $skip_rc ]; then \
80 echo "not ok $test_num $TEST_HDR_MSG # SKIP"
81 elif [ $rc -eq $timeout_rc ]; then \
82 echo "#"
83 echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT"
84 else
85 echo "not ok $test_num $TEST_HDR_MSG # exit=$rc"
86 fi)
87 cd - >/dev/null
88 fi
89}
90
91run_many()
92{
93 echo "TAP version 13"
94 DIR="${PWD#${BASE_DIR}/}"
95 test_num=0
96 total=$(echo "$@" | wc -w)
97 echo "1..$total"
98 for TEST in "$@"; do
99 BASENAME_TEST=$(basename $TEST)
100 test_num=$(( test_num + 1 ))
101 if [ -n "$per_test_logging" ]; then
102 logfile="/tmp/$BASENAME_TEST"
103 cat /dev/null > "$logfile"
104 fi
105 run_one "$DIR" "$TEST" "$test_num"
106 done
107}