2 # SPDX-License-Identifier: GPL-2.0
4 # Runs a set of tests in a given subdirectory.
7 export logfile=/dev/stdout
8 export per_test_logging=
10 # Defaults for "settings" file fields:
11 # "timeout" how many seconds to let each test run before failing.
12 export kselftest_default_timeout=45
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.
16 if [ -z "$BASE_DIR" ]; then
17 echo "Error: BASE_DIR must be set before sourcing." >&2
21 # If Perl is unavailable, we must fall back to line-at-a-time prefixing
22 # with sed instead of unbuffered output.
25 if [ ! -x /usr/bin/perl ]; then
28 "$BASE_DIR"/kselftest/prefix.pl
34 # Make sure tests will time out if utility is available.
35 if [ -x /usr/bin/timeout ] ; then
36 /usr/bin/timeout --foreground "$kselftest_timeout" "$1"
48 BASENAME_TEST=$(basename $TEST)
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
57 if echo "$line" | grep -q '^#'; then
60 field=$(echo "$line" | cut -d= -f1)
61 value=$(echo "$line" | cut -d= -f2-)
62 eval "kselftest_$field"="$value"
66 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST"
67 echo "# $TEST_HDR_MSG"
68 if [ ! -x "$TEST" ]; then
69 echo -n "# Warning: file $TEST is "
70 if [ ! -e "$TEST" ]; then
73 echo "not executable, correct this."
75 echo "not ok $test_num $TEST_HDR_MSG"
77 cd `dirname $TEST` > /dev/null
78 ((((( tap_timeout ./$BASENAME_TEST 2>&1; echo $? >&3) |
79 tap_prefix >&4) 3>&1) |
80 (read xs; exit $xs)) 4>>"$logfile" &&
81 echo "ok $test_num $TEST_HDR_MSG") ||
83 if [ $rc -eq $skip_rc ]; then \
84 echo "ok $test_num $TEST_HDR_MSG # SKIP"
85 elif [ $rc -eq $timeout_rc ]; then \
87 echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds"
89 echo "not ok $test_num $TEST_HDR_MSG # exit=$rc"
98 DIR="${PWD#${BASE_DIR}/}"
100 total=$(echo "$@" | wc -w)
103 BASENAME_TEST=$(basename $TEST)
104 test_num=$(( test_num + 1 ))
105 if [ -n "$per_test_logging" ]; then
106 logfile="/tmp/$BASENAME_TEST"
107 cat /dev/null > "$logfile"
109 run_one "$DIR" "$TEST" "$test_num"