auth4: Fix CID 1034877 Resource leak
[Samba.git] / testprogs / blackbox / subunit.sh
blob0c27775d07c298d428895b07db1ef500543d0638
2 # subunit.sh: shell functions to report test status via the subunit protocol.
3 # Copyright (C) 2006 Robert Collins <robertc@robertcollins.net>
4 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 timestamp()
23 # mark the start time. With Gnu date, you get nanoseconds from %N
24 # (here truncated to microseconds with %6N), but not on BSDs,
25 # Solaris, etc, which will apparently leave either %N or N at the end.
26 date -u +'time: %Y-%m-%d %H:%M:%S.%6NZ' | sed 's/\..*NZ$/.000000Z/'
29 subunit_start_test()
31 # emit the current protocol start-marker for test $1
32 timestamp
33 printf 'test: %s\n' "$1"
36 subunit_pass_test()
38 # emit the current protocol test passed marker for test $1
39 timestamp
40 printf 'success: %s\n' "$1"
43 # This is just a hack as we have some broken scripts
44 # which use "exit $failed", without initializing failed.
45 failed=0
47 subunit_fail_test()
49 # emit the current protocol fail-marker for test $1, and emit stdin as
50 # the error text.
51 # we use stdin because the failure message can be arbitrarily long, and this
52 # makes it convenient to write in scripts (using <<END syntax.
53 timestamp
54 printf 'failure: %s [\n' "$1"
55 cat -
56 printf '\n]\n'
59 subunit_error_test()
61 # emit the current protocol error-marker for test $1, and emit stdin as
62 # the error text.
63 # we use stdin because the failure message can be arbitrarily long, and this
64 # makes it convenient to write in scripts (using <<END syntax.
65 timestamp
66 printf 'error: %s [\n' "$1"
67 cat -
68 printf '\n]\n'
71 subunit_skip_test()
73 # emit the current protocol skip-marker for test $1, and emit stdin as
74 # the error text.
75 # we use stdin because the failure message can be arbitrarily long, and this
76 # makes it convenient to write in scripts (using <<END syntax.
77 printf 'skip: %s [\n' "$1"
78 cat -
79 printf '\n]\n'
82 testit()
84 name="$1"
85 shift
86 cmdline="$*"
87 subunit_start_test "$name"
88 output=$($cmdline 2>&1)
89 status=$?
90 if [ ${status} -eq 0 ]; then
91 subunit_pass_test "$name"
92 else
93 echo "$output" | subunit_fail_test "$name"
95 return $status
98 # This returns 0 if the command gave success and the grep value was found
99 # all other cases return != 0
100 testit_grep()
102 name="$1"
103 shift
104 grep="$1"
105 shift
106 cmdline="$*"
107 subunit_start_test "$name"
108 output=$($cmdline 2>&1)
109 status=$?
110 if [ ${status} -ne 0 ]; then
111 printf '%s' "$output" | subunit_fail_test "$name"
112 return $status
114 printf '%s' "$output" | grep -q "$grep"
115 gstatus=$?
116 if [ ${gstatus} -eq 0 ]; then
117 subunit_pass_test "$name"
118 else
119 printf 'GREP: "%s" not found in output:\n%s' "$grep" "$output" | subunit_fail_test "$name"
121 return $gstatus
124 # This returns 0 if the command gave success and the grep value was found
125 # num times all other cases return != 0
126 testit_grep_count()
128 name="$1"
129 shift
130 grep="$1"
131 shift
132 num="$1"
133 shift
134 cmdline="$*"
135 subunit_start_test "$name"
136 output=$($cmdline 2>&1)
137 status=$?
138 if [ ${status} -ne 0 ]; then
139 printf '%s' "$output" | subunit_fail_test "$name"
140 return $status
142 found=$(printf '%s' "$output" | grep -c "$grep")
143 if [ "${found}" -eq "$num" ]; then
144 subunit_pass_test "$name"
145 else
146 printf 'GREP: "%s" found "%d" times, expected "%d" in output:\n%s'\
147 "$grep" "$found" "$num" "$output" |
148 subunit_fail_test "$name"
149 return 1
151 return 0
154 testit_expect_failure()
156 name="$1"
157 shift
158 cmdline="$*"
159 subunit_start_test "$name"
160 output=$($cmdline 2>&1)
161 status=$?
162 if [ ${status} = 0 ]; then
163 echo "$output" | subunit_fail_test "$name"
164 return 1
165 else
166 subunit_pass_test "$name"
167 return 0
171 # This returns 0 if the command gave a failure and the grep value was found
172 # all other cases return != 0
173 testit_expect_failure_grep()
175 name="$1"
176 shift
177 grep="$1"
178 shift
179 cmdline="$*"
180 subunit_start_test "$name"
181 output=$($cmdline 2>&1)
182 status=$?
183 if [ ${status} -eq 0 ]; then
184 printf '%s' "$output" | subunit_fail_test "$name"
185 return 1
187 printf '%s' "$output" | grep -q "$grep"
188 gstatus=$?
189 if [ ${gstatus} -eq 0 ]; then
190 subunit_pass_test "$name"
191 else
192 printf 'GREP: "%s" not found in output:\n%s' "$grep" "$output" | subunit_fail_test "$name"
193 return 1
195 return 0
198 testok()
200 name=$(basename $1)
201 failed=$2
203 exit $failed
206 # work out the top level source directory
207 if [ -d source4 ]; then
208 SRCDIR="."
209 else
210 SRCDIR=".."
212 export SRCDIR