python:tests: Store keys as bytes rather than as lists of ints
[samba.git] / testprogs / blackbox / subunit.sh
1 #
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>
5 #
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.
10 #
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.
15 #
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
19 #
20
21 timestamp() {
22   # mark the start time. With Gnu date, you get nanoseconds from %N
23   # (here truncated to microseconds with %6N), but not on BSDs,
24   # Solaris, etc, which will apparently leave either %N or N at the end.
25   date -u +'time: %Y-%m-%d %H:%M:%S.%6NZ' | sed 's/\..*NZ$/.000000Z/'
26 }
27
28 subunit_start_test () {
29   # emit the current protocol start-marker for test $1
30   timestamp
31   printf 'test: %s\n' "$1"
32 }
33
34
35 subunit_pass_test () {
36   # emit the current protocol test passed marker for test $1
37   timestamp
38   printf 'success: %s\n' "$1"
39 }
40
41 # This is just a hack as we have some broken scripts
42 # which use "exit $failed", without initializing failed.
43 failed=0
44
45 subunit_fail_test () {
46   # emit the current protocol fail-marker for test $1, and emit stdin as
47   # the error text.
48   # we use stdin because the failure message can be arbitrarily long, and this
49   # makes it convenient to write in scripts (using <<END syntax.
50   timestamp
51   printf 'failure: %s [\n' "$1"
52   cat -
53   printf '\n]\n'
54 }
55
56
57 subunit_error_test () {
58   # emit the current protocol error-marker for test $1, and emit stdin as
59   # the error text.
60   # we use stdin because the failure message can be arbitrarily long, and this
61   # makes it convenient to write in scripts (using <<END syntax.
62   timestamp
63   printf 'error: %s [\n' "$1"
64   cat -
65   printf '\n]\n'
66 }
67
68 subunit_skip_test () {
69   # emit the current protocol skip-marker for test $1, and emit stdin as
70   # the error text.
71   # we use stdin because the failure message can be arbitrarily long, and this
72   # makes it convenient to write in scripts (using <<END syntax.
73   printf 'skip: %s [\n' "$1"
74   cat -
75   printf '\n]\n'
76 }
77
78 testit () {
79         name="$1"
80         shift
81         cmdline="$@"
82         subunit_start_test "$name"
83         output=`$cmdline 2>&1`
84         status=$?
85         if [ x$status = x0 ]; then
86                 subunit_pass_test "$name"
87         else
88                 echo "$output" | subunit_fail_test "$name"
89         fi
90         return $status
91 }
92
93 # This returns 0 if the command gave success and the grep value was found
94 # all other cases return != 0
95 testit_grep () {
96         name="$1"
97         shift
98         grep="$1"
99         shift
100         cmdline="$@"
101         subunit_start_test "$name"
102         output=`$cmdline 2>&1`
103         status=$?
104         if [ x$status != x0 ]; then
105                 printf '%s' "$output" | subunit_fail_test "$name"
106                 return $status
107         fi
108         printf '%s' "$output" | grep -q "$grep"
109         gstatus=$?
110         if [ x$gstatus = x0 ]; then
111                 subunit_pass_test "$name"
112         else
113                 printf 'GREP: "%s" not found in output:\n%s' "$grep" "$output" | subunit_fail_test "$name"
114         fi
115         return $status
116 }
117
118 testit_expect_failure () {
119         name="$1"
120         shift
121         cmdline="$@"
122         subunit_start_test "$name"
123         output=`$cmdline 2>&1`
124         status=$?
125         if [ x$status = x0 ]; then
126                 echo "$output" | subunit_fail_test "$name"
127         else
128                 subunit_pass_test "$name"
129         fi
130         return $status
131 }
132
133 # This returns 0 if the command gave a failure and the grep value was found
134 # all other cases return != 0
135 testit_expect_failure_grep () {
136         name="$1"
137         shift
138         grep="$1"
139         shift
140         cmdline="$@"
141         subunit_start_test "$name"
142         output=`$cmdline 2>&1`
143         status=$?
144         if [ x$status = x0 ]; then
145                 printf '%s' "$output" | subunit_fail_test "$name"
146                 return 1
147         fi
148         printf '%s' "$output" | grep -q "$grep"
149         gstatus=$?
150         if [ x$gstatus = x0 ]; then
151                 subunit_pass_test "$name"
152         else
153                 printf 'GREP: "%s" not found in output:\n%s' "$grep" "$output" | subunit_fail_test "$name"
154         fi
155         return $status
156 }
157
158 testok () {
159         name=`basename $1`
160         failed=$2
161
162         exit $failed
163 }
164
165 # work out the top level source directory
166 if [ -d source4 ]; then
167     SRCDIR="."
168 else
169     SRCDIR=".."
170 fi
171 export SRCDIR