xref: /freebsd/contrib/bc/tests/scripts.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2025 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
34. "$testdir/../scripts/functions.sh"
35
36# Just print the usage and exit with an error. This can receive a message to
37# print.
38# @param 1  A message to print.
39usage() {
40	if [ $# -eq 1 ]; then
41		printf '%s\n\n' "$1"
42	fi
43	printf 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [generate_tests] [exec args...]\n' "$script"
44	exit 1
45}
46
47pids=""
48
49# We need to figure out if we should run stuff in parallel.
50pll=1
51
52while getopts "n" opt; do
53
54	case "$opt" in
55		n) pll=0 ; set -e ;;
56		?) usage "Invalid option: $opt" ;;
57	esac
58
59done
60shift $(($OPTIND - 1))
61
62# Command-line processing.
63if [ "$#" -eq 0 ]; then
64	usage "Need at least 1 argument"
65else
66	d="$1"
67	shift
68	check_d_arg "$d"
69fi
70
71if [ "$#" -gt 0 ]; then
72	run_extra_tests="$1"
73	shift
74	check_bool_arg "$run_extra_tests"
75else
76	run_extra_tests=1
77	check_bool_arg "$run_extra_tests"
78fi
79
80if [ "$#" -gt 0 ]; then
81	run_stack_tests="$1"
82	shift
83	check_bool_arg "$run_stack_tests"
84else
85	run_stack_tests=1
86	check_bool_arg "$run_stack_tests"
87fi
88
89if [ "$#" -gt 0 ]; then
90	generate="$1"
91	shift
92	check_bool_arg "$generate"
93else
94	generate=1
95	check_bool_arg "$generate"
96fi
97
98if [ "$#" -gt 0 ]; then
99	exe="$1"
100	shift
101	check_exec_arg "$exe"
102else
103	exe="$testdir/../bin/$d"
104	check_exec_arg "$exe"
105fi
106
107scriptdir="$testdir/$d/scripts"
108
109scripts=$(cat "$scriptdir/all.txt")
110
111# Run each script test individually.
112for s in $scripts; do
113
114	f=$(basename "$s")
115
116	if [ "$pll" -ne 0 ]; then
117		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
118			"$generate" "$exe" "$@" &
119		pids="$pids $!"
120	else
121		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
122			"$generate" "$exe" "$@"
123	fi
124
125done
126
127if [ "$pll" -ne 0 ]; then
128
129	exit_err=0
130
131	for p in $pids; do
132
133		wait "$p"
134		err="$?"
135
136		if [ "$err" -ne 0 ]; then
137			printf 'A script failed!\n'
138			exit_err=1
139		fi
140
141	done
142
143	if [ "$exit_err" -ne 0 ]; then
144		exit 1
145	fi
146
147fi
148