3 # Copyright 2010 Patrick LeBoutillier <patrick.leboutillier@gmail.com>
5 # SPDX-License-Identifier: GPL-3.0-or-later
17 _auto_timing
="${TAP_AUTOTIME:-1}"
20 TIME_SCRIPT
="$(realpath -e -- "$
(dirname "${BASH_SOURCE[0]}")")/clock"
21 print
"${TIME_SCRIPT}"
25 tap-functions: A TAP-producing BASH library
29 plan_skip_all [REASON]
35 is RESULT EXPECTED [NAME]
36 isnt RESULT EXPECTED [NAME]
37 like RESULT PATTERN [NAME]
38 unlike RESULT PATTERN [NAME]
43 skip [CONDITION] [REASON] [NB_TESTS=1]
45 skip $feature_not_present "feature not present" 2 || {
51 Specify TODO mode by setting $TODO:
52 TODO="not implemented yet"
53 ok $result "some not implemented test"
68 is $USER $me "I am myself"
69 like $HOME $me "My home is mine"
70 like "`id`" $me "My id matches myself"
74 # Same thing using okx shortcut
77 [[ "`id -u`" != "0" ]]
79 skip $i_am_not_root "Must be root" || {
83 TODO="figure out how to become root..."
84 okx [ "$HOME" == "/root" ]
92 while getopts ":sx" opt
; do
98 shift $
(( OPTIND
- 1 ))
99 # Don't allow uninitialized variables if requested
100 [[ -n "$set_u" ]] && set -u
103 # Used to call _cleanup on shell exit
108 (( _plan_set
!= 0 )) && "You tried to plan twice!"
112 _last_time
=$
("${TIME_SCRIPT}")
119 local reason
=${1:-''}
121 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
123 _print_plan
0 "Skip $reason"
127 _last_time
=$
("${TIME_SCRIPT}")
136 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
137 (( tests
== 0 )) && _die
"You said to run 0 tests! You've got to run something."
140 _expected_tests
=$tests
142 _last_time
=$
("${TIME_SCRIPT}")
150 local directive
=${2:-''}
153 [[ -n "$directive" ]] && echo -n " # $directive"
169 # This is the workhorse method that actually
170 # prints the tests result.
175 (( _plan_set
== 0 )) && _die
"You tried to run a test without a plan! Gotta have a plan."
177 _executed_tests
=$
(( $_executed_tests + 1 ))
179 if [[ -n "$name" ]] ; then
180 if _matches
"$name" "^[0-9]+$" ; then
181 diag
" You named your test '$name'. You shouldn't use numbers for your test names."
182 diag
" Very confusing."
186 if (( result
!= 0 )) ; then
188 _failed_tests
=$
(( _failed_tests
+ 1 ))
190 echo -n "ok $_executed_tests"
192 if [[ -n "$name" ]] ; then
193 local ename
=${name//\#/\\#}
197 if [[ -n "$TODO" ]] ; then
198 echo -n " # TODO $TODO" ;
199 if (( result
!= 0 )) ; then
200 _failed_tests
=$
(( _failed_tests
- 1 ))
206 if (( result
!= 0 )) ; then
207 local file='tap-functions'
212 local bt
=$
(caller
$i)
213 while _matches
"$bt" "tap-functions$" ; do
218 eval $
(caller
$i |
(read line func
file ; echo "backtrace=\"$file:$func() at line $line.\""))
221 [[ -n "$TODO" ]] && t
="(TODO) "
223 if [[ -n "$name" ]] ; then
224 diag
" Failed ${t}test '$name'"
225 diag
" in $backtrace"
227 diag
" Failed ${t}test in $backtrace"
239 diag
"Output of '$command':"
240 "$@" |
while read line
; do
243 ok
${PIPESTATUS[0]} "$command"
250 local expected
=${2:?}
252 if [[ "$result" == "$expected" ]] ; then
260 # Thanks to Aaron Kangas for the patch to allow regexp matching
262 _bash_major_version
=${BASH_VERSION%%.*}
267 if [[ -z "$result" ||
-z "$pattern" ]] ; then
270 if (( _bash_major_version
>= 3 )) ; then
271 [[ "$result" =~
"$pattern" ]]
273 echo "$result" |
egrep -q "$pattern"
281 local expected
=${2:?}
283 diag
" got: '$result'"
284 diag
" expected: '$expected'"
290 local expected
=${2:?}
293 _equals
"$result" "$expected"
297 (( r
!= 0 )) && _is_diag
"$result" "$expected"
304 local expected
=${2:?}
307 _equals
"$result" "$expected"
311 (( r
!= 0 )) && _is_diag
"$result" "$expected"
321 _matches
"$result" "$pattern"
325 (( r
!= 0 )) && diag
" '$result' doesn't match '$pattern'"
335 _matches
"$result" "$pattern"
339 (( r
!= 0 )) && diag
" '$result' matches '$pattern'"
345 local condition
=${1:?}
346 local reason
=${2:-''}
349 if (( condition
== 0 )) ; then
351 for (( i
=0 ; i
<$n ; i
++ )) ; do
352 _executed_tests
=$
(( _executed_tests
+ 1 ))
353 echo "ok $_executed_tests # skip: $reason"
366 if [ "${_auto_timing}" -eq "1" ] ; then
367 new_time
=$
("${TIME_SCRIPT}")
368 duration
=$
(awk "BEGIN { printf(\"%f\n\", ($new_time - $_last_time)*1000) }")
370 echo " duration_ms: ${duration}"
373 _last_time
=$
("${TIME_SCRIPT}")
381 if [[ "${val}" != "0" ]] ; then
392 if [[ -n "$msg" ]] ; then
401 local reason
=${1:-'<unspecified error>'}
410 local reason
=${1:-''}
412 echo "Bail out! $reason" >&2
420 if (( _plan_set
== 0 )) ; then
421 diag
"Looks like your test died before it could output anything."
425 if (( _test_died
!= 0 )) ; then
426 diag
"Looks like your test died just after $_executed_tests."
430 if (( _skip_all
== 0 && _no_plan
!= 0 )) ; then
431 _print_plan
$_executed_tests
435 if (( _no_plan
== 0 && _expected_tests
< _executed_tests
)) ; then
436 s
= ; (( _expected_tests
> 1 )) && s
=s
437 local extra
=$
(( _executed_tests
- _expected_tests
))
438 diag
"Looks like you planned $_expected_tests test$s but ran $extra extra."
442 if (( _no_plan
== 0 && _expected_tests
> _executed_tests
)) ; then
443 s
= ; (( _expected_tests
> 1 )) && s
=s
444 diag
"Looks like you planned $_expected_tests test$s but only ran $_executed_tests."
447 if (( _failed_tests
> 0 )) ; then
448 s
= ; (( _failed_tests
> 1 )) && s
=s
449 diag
"Looks like you failed $_failed_tests test$s of $_executed_tests."
457 if (( _no_plan
!= 0 || _plan_set
== 0 )) ; then
458 return $_failed_tests
461 if (( _expected_tests
< _executed_tests
)) ; then
462 return $
(( _executed_tests
- _expected_tests
))
465 return $
(( _failed_tests
+ ( _expected_tests
- _executed_tests
)))
471 if [[ -z "$rc" ]] ; then
478 (( alt_rc
!= 0 )) && rc
=$alt_rc
This page took 0.041353 seconds and 4 git commands to generate.