1 # Shell function library for test cases.
3 # Note that while many of the functions in this library could benefit from
4 # using "local" to avoid possibly hammering global variables, Solaris /bin/sh
5 # doesn't support local and this library aspires to be portable to Solaris
6 # Bourne shell. Instead, all private variables are prefixed with "tap_".
8 # This file provides a TAP-compatible shell function library useful for
9 # writing test cases. It is part of C TAP Harness, which can be found at
10 # <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
12 # Written by Russ Allbery <eagle@eyrie.org>
13 # Copyright 2009-2012, 2016 Russ Allbery <eagle@eyrie.org>
14 # Copyright 2006-2008, 2013
15 # The Board of Trustees of the Leland Stanford Junior University
17 # Permission is hereby granted, free of charge, to any person obtaining a copy
18 # of this software and associated documentation files (the "Software"), to
19 # deal in the Software without restriction, including without limitation the
20 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
21 # sell copies of the Software, and to permit persons to whom the Software is
22 # furnished to do so, subject to the following conditions:
24 # The above copyright notice and this permission notice shall be included in
25 # all copies or substantial portions of the Software.
27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 # SPDX-License-Identifier: MIT
37 # Print out the number of test cases we expect to run.
46 # Prepare for lazy planning.
54 # Report the test status on exit.
56 tap_highest=`expr "$count" - 1`
57 if [ "$planned" = 0 ] ; then
58 echo "1..$tap_highest"
59 planned="$tap_highest"
61 tap_looks='# Looks like you'
62 if [ "$planned" -gt 0 ] ; then
63 if [ "$planned" -gt "$tap_highest" ] ; then
64 if [ "$planned" -gt 1 ] ; then
65 echo "$tap_looks planned $planned tests but only ran" \
68 echo "$tap_looks planned $planned test but only ran" \
71 elif [ "$planned" -lt "$tap_highest" ] ; then
72 tap_extra=`expr "$tap_highest" - "$planned"`
73 if [ "$planned" -gt 1 ] ; then
74 echo "$tap_looks planned $planned tests but ran" \
77 echo "$tap_looks planned $planned test but ran" \
80 elif [ "$failed" -gt 0 ] ; then
81 if [ "$failed" -gt 1 ] ; then
82 echo "$tap_looks failed $failed tests of $planned"
84 echo "$tap_looks failed $failed test of $planned"
86 elif [ "$planned" -gt 1 ] ; then
87 echo "# All $planned tests successful or skipped"
89 echo "# $planned test successful or skipped"
94 # Skip the entire test suite. Should be run instead of plan.
97 if [ -n "$tap_desc" ] ; then
98 echo "1..0 # skip $tap_desc"
105 # ok takes a test description and a command to run and prints success if that
106 # command is successful, false otherwise. The count starts at 1 and is
107 # updated each time ok is printed.
110 if [ -n "$tap_desc" ] ; then
111 tap_desc=" - $tap_desc"
115 echo ok "$count$tap_desc"
117 echo not ok "$count$tap_desc"
118 failed=`expr $failed + 1`
120 count=`expr $count + 1`
123 # Skip the next test. Takes the reason why the test is skipped.
125 echo "ok $count # skip $*"
126 count=`expr $count + 1`
129 # Report the same status on a whole set of tests. Takes the count of tests,
130 # the description, and then the command to run to determine the status.
133 tap_end=`expr $count + $1`
135 while [ "$tap_i" -lt "$tap_end" ] ; do
137 tap_i=`expr $tap_i + 1`
141 # Skip a whole set of tests. Takes the count and then the reason for skipping
145 tap_end=`expr $count + $1`
147 while [ "$tap_i" -lt "$tap_end" ] ; do
149 tap_i=`expr $tap_i + 1`
153 # Portable variant of printf '%s\n' "$*". In the majority of cases, this
154 # function is slower than printf, because the latter is often implemented
155 # as a builtin command. The value of the variable IFS is ignored.
157 # This macro must not be called via backticks inside double quotes, since this
158 # will result in bizarre escaping behavior and lots of extra backslashes on
166 # Run a program expected to succeed, and print ok if it does and produces the
167 # correct output. Takes the description, expected exit status, the expected
168 # output, the command to run, and then any arguments for that command.
169 # Standard output and standard error are combined when analyzing the output of
172 # If the command may contain system-specific error messages in its output,
173 # add strip_colon_error before the command to post-process its output.
181 tap_output=`"$@" 2>&1`
183 if [ $tap_status = $tap_w_status ] \
184 && [ x"$tap_output" = x"$tap_w_output" ] ; then
187 echo "# saw: ($tap_status) $tap_output"
188 echo "# not: ($tap_w_status) $tap_w_output"
193 # Strip a colon and everything after it off the output of a command, as long
194 # as that colon comes after at least one whitespace character. (This is done
195 # to avoid stripping the name of the program from the start of an error
196 # message.) This is used to remove system-specific error messages (coming
197 # from strerror, for example).
198 strip_colon_error() {
199 tap_output=`"$@" 2>&1`
201 tap_output=`puts "$tap_output" | sed 's/^\([^ ]* [^:]*\):.*/\1/'`
206 # Bail out with an error message.
208 echo 'Bail out!' "$@"
212 # Output a diagnostic on standard error, preceded by the required # mark.
217 # Search for the given file first in $C_TAP_BUILD and then in $C_TAP_SOURCE
218 # and echo the path where the file was found, or the empty string if the file
221 # This macro uses puts, so don't run it using backticks inside double quotes
222 # or bizarre quoting behavior will happen with Solaris sh.
224 if [ -n "$C_TAP_BUILD" ] && [ -f "$C_TAP_BUILD/$1" ] ; then
225 puts "$C_TAP_BUILD/$1"
226 elif [ -n "$C_TAP_SOURCE" ] && [ -f "$C_TAP_SOURCE/$1" ] ; then
227 puts "$C_TAP_SOURCE/$1"
233 # Create $C_TAP_BUILD/tmp for use by tests for storing temporary files and
234 # return the path (via standard output).
236 # This macro uses puts, so don't run it using backticks inside double quotes
237 # or bizarre quoting behavior will happen with Solaris sh.
239 if [ -z "$C_TAP_BUILD" ] ; then
242 tap_tmpdir="$C_TAP_BUILD"/tmp
244 if [ ! -d "$tap_tmpdir" ] ; then
245 mkdir "$tap_tmpdir" || bail "Error creating $tap_tmpdir"