1 # Shell function library for test cases.
3 # Written by Russ Allbery <rra@stanford.edu>
4 # Copyright 2009, 2010 Russ Allbery <rra@stanford.edu>
5 # Copyright 2006, 2007, 2008 Board of Trustees, Leland Stanford Jr. University
7 # See LICENSE for licensing terms.
9 # Print out the number of test cases we expect to run.
18 # Prepare for lazy planning.
26 # Report the test status on exit.
29 highest
=`expr "$count" - 1`
30 if [ "$planned" = 0 ] ; then
34 looks
='# Looks like you'
35 if [ "$planned" -gt 0 ] ; then
36 if [ "$planned" -gt "$highest" ] ; then
37 if [ "$planned" -gt 1 ] ; then
38 echo "$looks planned $planned tests but only ran $highest"
40 echo "$looks planned $planned test but only ran $highest"
42 elif [ "$planned" -lt "$highest" ] ; then
44 extra
=`expr "$highest" - "$planned"`
45 if [ "$planned" -gt 1 ] ; then
46 echo "$looks planned $planned tests but ran $extra extra"
48 echo "$looks planned $planned test but ran $extra extra"
50 elif [ "$failed" -gt 0 ] ; then
51 if [ "$failed" -gt 1 ] ; then
52 echo "$looks failed $failed tests of $planned"
54 echo "$looks failed $failed test of $planned"
56 elif [ "$planned" -gt 1 ] ; then
57 echo "# All $planned tests successful or skipped"
59 echo "# $planned test successful or skipped"
64 # Skip the entire test suite. Should be run instead of plan.
68 if [ -n "$desc" ] ; then
69 echo "1..0 # skip $desc"
76 # ok takes a test description and a command to run and prints success if that
77 # command is successful, false otherwise. The count starts at 1 and is
78 # updated each time ok is printed.
82 if [ -n "$desc" ] ; then
89 echo not ok
$count$desc
90 failed
=`expr $failed + 1`
92 count
=`expr $count + 1`
95 # Skip the next test. Takes the reason why the test is skipped.
97 echo "ok $count # skip $*"
98 count
=`expr $count + 1`
101 # Report the same status on a whole set of tests. Takes the count of tests,
102 # the description, and then the command to run to determine the status.
106 end
=`expr $count + $1`
110 while [ "$i" -lt "$end" ] ; do
116 # Skip a whole set of tests. Takes the count and then the reason for skipping
121 end
=`expr $count + $1`
123 while [ "$i" -lt "$end" ] ; do
129 # Run a program expected to succeed, and print ok if it does and produces the
130 # correct output. Takes the description, expected exit status, the expected
131 # output, the command to run, and then any arguments for that command. Strip
132 # a colon and everything after it off the output if the expected status is
133 # non-zero, since this is probably a system-specific error message.
135 local desc w_status w_output output status
144 if [ "$w_status" -ne 0 ] ; then
145 output
=`echo "$output" | sed 's/^\([^:]*\):.*/\1/'`
147 if [ $status = $w_status ] && [ x
"$output" = x
"$w_output" ] ; then
150 echo "# saw: ($status) $output"
151 echo "# not: ($w_status) $w_output"
156 # Bail out with an error message.
158 echo 'Bail out!' "$@"
162 # Output a diagnostic on standard error, preceded by the required # mark.