3 # SPDX-License-Identifier: GPL-3.0-or-later
5 # Copyright 2010 Patrick LeBoutillier <patrick.leboutillier@gmail.com>
22 tap-functions: A TAP-producing BASH library
26 plan_skip_all [REASON]
32 is RESULT EXPECTED [NAME]
33 isnt RESULT EXPECTED [NAME]
34 like RESULT PATTERN [NAME]
35 unlike RESULT PATTERN [NAME]
40 skip [CONDITION] [REASON] [NB_TESTS=1]
42 skip $feature_not_present "feature not present" 2 || {
48 Specify TODO mode by setting $TODO:
49 TODO="not implemented yet"
50 ok $result "some not implemented test"
64 is $USER $me "I am myself"
65 like $HOME $me "My home is mine"
66 like "`id`" $me "My id matches myself"
70 # Same thing using okx shortcut
73 [[ "`id -u`" != "0" ]]
75 skip $i_am_not_root "Must be root" || {
79 TODO="figure out how to become root..."
80 okx [ "$HOME" == "/root" ]
88 while getopts ":sx" opt
; do
94 shift $
(( OPTIND
- 1 ))
95 # Don't allow uninitialized variables if requested
96 [[ -n "$set_u" ]] && set -u
99 # Used to call _cleanup on shell exit
104 (( _plan_set
!= 0 )) && "You tried to plan twice!"
114 local reason
=${1:-''}
116 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
118 _print_plan
0 "Skip $reason"
130 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
131 (( tests
== 0 )) && _die
"You said to run 0 tests! You've got to run something."
134 _expected_tests
=$tests
143 local directive
=${2:-''}
146 [[ -n "$directive" ]] && echo -n " # $directive"
162 # This is the workhorse method that actually
163 # prints the tests result.
168 (( _plan_set
== 0 )) && _die
"You tried to run a test without a plan! Gotta have a plan."
170 _executed_tests
=$
(( $_executed_tests + 1 ))
172 if [[ -n "$name" ]] ; then
173 if _matches
"$name" "^[0-9]+$" ; then
174 diag
" You named your test '$name'. You shouldn't use numbers for your test names."
175 diag
" Very confusing."
179 if (( result
!= 0 )) ; then
181 _failed_tests
=$
(( _failed_tests
+ 1 ))
183 echo -n "ok $_executed_tests"
185 if [[ -n "$name" ]] ; then
186 local ename
=${name//\#/\\#}
190 if [[ -n "$TODO" ]] ; then
191 echo -n " # TODO $TODO" ;
192 if (( result
!= 0 )) ; then
193 _failed_tests
=$
(( _failed_tests
- 1 ))
198 if (( result
!= 0 )) ; then
199 local file='tap-functions'
204 local bt
=$
(caller
$i)
205 while _matches
"$bt" "tap-functions$" ; do
210 eval $
(caller
$i |
(read line func
file ; echo "backtrace=\"$file:$func() at line $line.\""))
213 [[ -n "$TODO" ]] && t
="(TODO) "
215 if [[ -n "$name" ]] ; then
216 diag
" Failed ${t}test '$name'"
217 diag
" in $backtrace"
219 diag
" Failed ${t}test in $backtrace"
231 diag
"Output of '$command':"
232 "$@" |
while read line
; do
235 ok
${PIPESTATUS[0]} "$command"
241 local expected
=${2:?}
243 if [[ "$result" == "$expected" ]] ; then
251 # Thanks to Aaron Kangas for the patch to allow regexp matching
253 _bash_major_version
=${BASH_VERSION%%.*}
258 if [[ -z "$result" ||
-z "$pattern" ]] ; then
261 if (( _bash_major_version
>= 3 )) ; then
262 [[ "$result" =~
"$pattern" ]]
264 echo "$result" |
egrep -q "$pattern"
272 local expected
=${2:?}
274 diag
" got: '$result'"
275 diag
" expected: '$expected'"
281 local expected
=${2:?}
284 _equals
"$result" "$expected"
288 (( r
!= 0 )) && _is_diag
"$result" "$expected"
295 local expected
=${2:?}
298 _equals
"$result" "$expected"
302 (( r
!= 0 )) && _is_diag
"$result" "$expected"
312 _matches
"$result" "$pattern"
316 (( r
!= 0 )) && diag
" '$result' doesn't match '$pattern'"
326 _matches
"$result" "$pattern"
330 (( r
!= 0 )) && diag
" '$result' matches '$pattern'"
336 local condition
=${1:?}
337 local reason
=${2:-''}
340 if (( condition
== 0 )) ; then
342 for (( i
=0 ; i
<$n ; i
++ )) ; do
343 _executed_tests
=$
(( _executed_tests
+ 1 ))
344 echo "ok $_executed_tests # skip: $reason"
356 if [[ -n "$msg" ]] ; then
365 local reason
=${1:-'<unspecified error>'}
374 local reason
=${1:-''}
376 echo "Bail out! $reason" >&2
384 if (( _plan_set
== 0 )) ; then
385 diag
"Looks like your test died before it could output anything."
389 if (( _test_died
!= 0 )) ; then
390 diag
"Looks like your test died just after $_executed_tests."
394 if (( _skip_all
== 0 && _no_plan
!= 0 )) ; then
395 _print_plan
$_executed_tests
399 if (( _no_plan
== 0 && _expected_tests
< _executed_tests
)) ; then
400 s
= ; (( _expected_tests
> 1 )) && s
=s
401 local extra
=$
(( _executed_tests
- _expected_tests
))
402 diag
"Looks like you planned $_expected_tests test$s but ran $extra extra."
406 if (( _no_plan
== 0 && _expected_tests
> _executed_tests
)) ; then
407 s
= ; (( _expected_tests
> 1 )) && s
=s
408 diag
"Looks like you planned $_expected_tests test$s but only ran $_executed_tests."
411 if (( _failed_tests
> 0 )) ; then
412 s
= ; (( _failed_tests
> 1 )) && s
=s
413 diag
"Looks like you failed $_failed_tests test$s of $_executed_tests."
421 if (( _no_plan
!= 0 || _plan_set
== 0 )) ; then
422 return $_failed_tests
425 if (( _expected_tests
< _executed_tests
)) ; then
426 return $
(( _executed_tests
- _expected_tests
))
429 return $
(( _failed_tests
+ ( _expected_tests
- _executed_tests
)))
435 if [[ -z "$rc" ]] ; then
442 (( alt_rc
!= 0 )) && rc
=$alt_rc
This page took 0.038808 seconds and 4 git commands to generate.