xref: /linux/tools/testing/selftests/kselftest/runner.sh (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
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 logfile=/dev/stdout
7export per_test_logging=
8
9# There isn't a shell-agnostic way to find the path of a sourced file,
10# so we must rely on BASE_DIR being set to find other tools.
11if [ -z "$BASE_DIR" ]; then
12	echo "Error: BASE_DIR must be set before sourcing." >&2
13	exit 1
14fi
15
16# If Perl is unavailable, we must fall back to line-at-a-time prefixing
17# with sed instead of unbuffered output.
18tap_prefix()
19{
20	if [ ! -x /usr/bin/perl ]; then
21		sed -e 's/^/# /'
22	else
23		"$BASE_DIR"/kselftest/prefix.pl
24	fi
25}
26
27run_one()
28{
29	DIR="$1"
30	TEST="$2"
31	NUM="$3"
32
33	BASENAME_TEST=$(basename $TEST)
34
35	TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST"
36	echo "# $TEST_HDR_MSG"
37	if [ ! -x "$TEST" ]; then
38		echo -n "# Warning: file $TEST is "
39		if [ ! -e "$TEST" ]; then
40			echo "missing!"
41		else
42			echo "not executable, correct this."
43		fi
44		echo "not ok $test_num $TEST_HDR_MSG"
45	else
46		cd `dirname $TEST` > /dev/null
47		(((((./$BASENAME_TEST 2>&1; echo $? >&3) |
48			tap_prefix >&4) 3>&1) |
49			(read xs; exit $xs)) 4>>"$logfile" &&
50		echo "ok $test_num $TEST_HDR_MSG") ||
51		(if [ $? -eq $skip_rc ]; then	\
52			echo "not ok $test_num $TEST_HDR_MSG # SKIP"
53		else
54			echo "not ok $test_num $TEST_HDR_MSG"
55		fi)
56		cd - >/dev/null
57	fi
58}
59
60run_many()
61{
62	echo "TAP version 13"
63	DIR=$(basename "$PWD")
64	test_num=0
65	total=$(echo "$@" | wc -w)
66	echo "1..$total"
67	for TEST in "$@"; do
68		BASENAME_TEST=$(basename $TEST)
69		test_num=$(( test_num + 1 ))
70		if [ -n "$per_test_logging" ]; then
71			logfile="/tmp/$BASENAME_TEST"
72			cat /dev/null > "$logfile"
73		fi
74		run_one "$DIR" "$TEST" "$test_num"
75	done
76}
77