1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3test_begin() {
4	# Count tests to allow the test harness to double-check if all were
5	# included correctly.
6	ctr=0
7	[ -z "$RTLA" ] && RTLA="./rtla"
8	[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
9}
10
11check() {
12	# Simple check: run rtla with given arguments and test exit code.
13	# If TEST_COUNT is set, run the test. Otherwise, just count.
14	ctr=$(($ctr + 1))
15	if [ -n "$TEST_COUNT" ]
16	then
17		# Run rtla; in case of failure, include its output as comment
18		# in the test results.
19		result=$(stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
20		if [ $exitcode -eq 0 ]
21		then
22			echo "ok $ctr - $1"
23		else
24			echo "not ok $ctr - $1"
25			# Add rtla output and exit code as comments in case of failure
26			echo "$result" | col -b | while read line; do echo "# $line"; done
27			printf "#\n# exit code %s\n" $exitcode
28		fi
29	fi
30}
31
32set_timeout() {
33	TIMEOUT="timeout -v -k 15s $1"
34}
35
36unset_timeout() {
37	unset TIMEOUT
38}
39
40test_end() {
41	# If running without TEST_COUNT, tests are not actually run, just
42	# counted. In that case, re-run the test with the correct count.
43	[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
44}
45
46# Avoid any environmental discrepancies
47export LC_ALL=C
48unset_timeout
49