Import C TAP Harness 1.2 as a testing harness
[openafs.git] / tests / tap / libtap.sh
1 # Shell function library for test cases.
2 #
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
6 #
7 # See LICENSE for licensing terms.
8
9 # Print out the number of test cases we expect to run.
10 plan () {
11     count=1
12     planned="$1"
13     failed=0
14     echo "1..$1"
15     trap finish 0
16 }
17
18 # Prepare for lazy planning.
19 plan_lazy () {
20     count=1
21     planned=0
22     failed=0
23     trap finish 0
24 }
25
26 # Report the test status on exit.
27 finish () {
28     local highest looks
29     highest=`expr "$count" - 1`
30     if [ "$planned" = 0 ] ; then
31         echo "1..$highest"
32         planned="$highest"
33     fi
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"
39             else
40                 echo "$looks planned $planned test but only ran $highest"
41             fi
42         elif [ "$planned" -lt "$highest" ] ; then
43             local extra
44             extra=`expr "$highest" - "$planned"`
45             if [ "$planned" -gt 1 ] ; then
46                 echo "$looks planned $planned tests but ran $extra extra"
47             else
48                 echo "$looks planned $planned test but ran $extra extra"
49             fi
50         elif [ "$failed" -gt 0 ] ; then
51             if [ "$failed" -gt 1 ] ; then
52                 echo "$looks failed $failed tests of $planned"
53             else
54                 echo "$looks failed $failed test of $planned"
55             fi
56         elif [ "$planned" -gt 1 ] ; then
57             echo "# All $planned tests successful or skipped"
58         else
59             echo "# $planned test successful or skipped"
60         fi
61     fi
62 }
63
64 # Skip the entire test suite.  Should be run instead of plan.
65 skip_all () {
66     local desc
67     desc="$1"
68     if [ -n "$desc" ] ; then
69         echo "1..0 # skip $desc"
70     else
71         echo "1..0 # skip"
72     fi
73     exit 0
74 }
75
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.
79 ok () {
80     local desc
81     desc="$1"
82     if [ -n "$desc" ] ; then
83         desc=" - $desc"
84     fi
85     shift
86     if "$@" ; then
87         echo ok $count$desc
88     else
89         echo not ok $count$desc
90         failed=`expr $failed + 1`
91     fi
92     count=`expr $count + 1`
93 }
94
95 # Skip the next test.  Takes the reason why the test is skipped.
96 skip () {
97     echo "ok $count # skip $*"
98     count=`expr $count + 1`
99 }
100
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.
103 ok_block () {
104     local end i desc
105     i=$count
106     end=`expr $count + $1`
107     shift
108     desc="$1"
109     shift
110     while [ "$i" -lt "$end" ] ; do
111         ok "$desc" "$@"
112         i=`expr $i + 1`
113     done
114 }
115
116 # Skip a whole set of tests.  Takes the count and then the reason for skipping
117 # the test.
118 skip_block () {
119     local i end
120     i=$count
121     end=`expr $count + $1`
122     shift
123     while [ "$i" -lt "$end" ] ; do
124         skip "$@"
125         i=`expr $i + 1`
126     done
127 }
128
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.
134 ok_program () {
135     local desc w_status w_output output status
136     desc="$1"
137     shift
138     w_status="$1"
139     shift
140     w_output="$1"
141     shift
142     output=`"$@" 2>&1`
143     status=$?
144     if [ "$w_status" -ne 0 ] ; then
145         output=`echo "$output" | sed 's/^\([^:]*\):.*/\1/'`
146     fi
147     if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
148         ok "$desc" true
149     else
150         echo "#  saw: ($status) $output"
151         echo "#  not: ($w_status) $w_output"
152         ok "$desc" false
153     fi
154 }
155
156 # Bail out with an error message.
157 bail () {
158     echo 'Bail out!' "$@"
159     exit 1
160 }
161
162 # Output a diagnostic on standard error, preceded by the required # mark.
163 diag () {
164     echo '#' "$@"
165 }