xref: /freebsd/contrib/bc/tests/scripts.sh (revision 1323ec57)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30script="$0"
31
32testdir=$(dirname "${script}")
33
34pids=""
35
36# We need to figure out if we should run stuff in parallel.
37pll=1
38
39while getopts "n" opt; do
40
41	case "$opt" in
42		n) pll=0 ; shift ; set -e ;;
43		?) usage "Invalid option: $opt" ;;
44	esac
45
46done
47
48# Command-line processing.
49if [ "$#" -eq 0 ]; then
50	printf 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script"
51	exit 1
52else
53	d="$1"
54	shift
55fi
56
57if [ "$#" -gt 0 ]; then
58	run_extra_tests="$1"
59	shift
60else
61	run_extra_tests=1
62fi
63
64if [ "$#" -gt 0 ]; then
65	run_stack_tests="$1"
66	shift
67else
68	run_stack_tests=1
69fi
70
71if [ "$#" -gt 0 ]; then
72	generate="$1"
73	shift
74else
75	generate=1
76fi
77
78if [ "$#" -gt 0 ]; then
79	time_tests="$1"
80	shift
81else
82	time_tests=0
83fi
84
85if [ "$#" -gt 0 ]; then
86	exe="$1"
87	shift
88else
89	exe="$testdir/../bin/$d"
90fi
91
92scriptdir="$testdir/$d/scripts"
93
94scripts=$(cat "$scriptdir/all.txt")
95
96# Run each script test individually.
97for s in $scripts; do
98
99	f=$(basename "$s")
100
101	if [ "$pll" -ne 0 ]; then
102		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
103			"$generate" "$time_tests" "$exe" "$@" &
104		pids="$pids $!"
105	else
106		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
107			"$generate" "$time_tests" "$exe" "$@"
108	fi
109
110done
111
112if [ "$pll" -ne 0 ]; then
113
114	exit_err=0
115
116	for p in $pids; do
117
118		wait "$p"
119		err="$?"
120
121		if [ "$err" -ne 0 ]; then
122			printf 'A script failed!\n'
123			exit_err=1
124		fi
125
126	done
127
128	if [ "$exit_err" -ne 0 ]; then
129		exit 1
130	fi
131
132fi
133