1#!/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 --foreground "$kselftest_timeout" \ 37 /usr/bin/timeout "$kselftest_timeout" $1 38 else 39 $1 40 fi 41} 42 43run_one() 44{ 45 DIR="$1" 46 TEST="$2" 47 NUM="$3" 48 49 BASENAME_TEST=$(basename $TEST) 50 51 # Reset any "settings"-file variables. 52 export kselftest_timeout="$kselftest_default_timeout" 53 # Load per-test-directory kselftest "settings" file. 54 settings="$BASE_DIR/$DIR/settings" 55 if [ -r "$settings" ] ; then 56 while read line ; do 57 # Skip comments. 58 if echo "$line" | grep -q '^#'; then 59 continue 60 fi 61 field=$(echo "$line" | cut -d= -f1) 62 value=$(echo "$line" | cut -d= -f2-) 63 eval "kselftest_$field"="$value" 64 done < "$settings" 65 fi 66 67 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST" 68 echo "# $TEST_HDR_MSG" 69 if [ ! -e "$TEST" ]; then 70 echo "# Warning: file $TEST is missing!" 71 echo "not ok $test_num $TEST_HDR_MSG" 72 else 73 cmd="./$BASENAME_TEST" 74 if [ ! -x "$TEST" ]; then 75 echo "# Warning: file $TEST is not executable" 76 77 if [ $(head -n 1 "$TEST" | cut -c -2) = "#!" ] 78 then 79 interpreter=$(head -n 1 "$TEST" | cut -c 3-) 80 cmd="$interpreter ./$BASENAME_TEST" 81 else 82 echo "not ok $test_num $TEST_HDR_MSG" 83 return 84 fi 85 fi 86 cd `dirname $TEST` > /dev/null 87 ((((( tap_timeout "$cmd" 2>&1; echo $? >&3) | 88 tap_prefix >&4) 3>&1) | 89 (read xs; exit $xs)) 4>>"$logfile" && 90 echo "ok $test_num $TEST_HDR_MSG") || 91 (rc=$?; \ 92 if [ $rc -eq $skip_rc ]; then \ 93 echo "ok $test_num $TEST_HDR_MSG # SKIP" 94 elif [ $rc -eq $timeout_rc ]; then \ 95 echo "#" 96 echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds" 97 else 98 echo "not ok $test_num $TEST_HDR_MSG # exit=$rc" 99 fi) 100 cd - >/dev/null 101 fi 102} 103 104run_many() 105{ 106 echo "TAP version 13" 107 DIR="${PWD#${BASE_DIR}/}" 108 test_num=0 109 total=$(echo "$@" | wc -w) 110 echo "1..$total" 111 for TEST in "$@"; do 112 BASENAME_TEST=$(basename $TEST) 113 test_num=$(( test_num + 1 )) 114 if [ -n "$per_test_logging" ]; then 115 logfile="/tmp/$BASENAME_TEST" 116 cat /dev/null > "$logfile" 117 fi 118 run_one "$DIR" "$TEST" "$test_num" 119 done 120} 121