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