66499d817ed79d0719d2fa636870ab6bd78a9165
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"
24 tap-functions: A TAP-producing BASH library
28 plan_skip_all [REASON]
34 is RESULT EXPECTED [NAME]
35 isnt RESULT EXPECTED [NAME]
36 like RESULT PATTERN [NAME]
37 unlike RESULT PATTERN [NAME]
42 skip [CONDITION] [REASON] [NB_TESTS=1]
44 skip $feature_not_present "feature not present" 2 || {
50 Specify TODO mode by setting $TODO:
51 TODO="not implemented yet"
52 ok $result "some not implemented test"
67 is $USER $me "I am myself"
68 like $HOME $me "My home is mine"
69 like "`id`" $me "My id matches myself"
73 # Same thing using okx shortcut
76 [[ "`id -u`" != "0" ]]
78 skip $i_am_not_root "Must be root" || {
82 TODO="figure out how to become root..."
83 okx [ "$HOME" == "/root" ]
91 while getopts ":sx" opt
; do
97 shift $
(( OPTIND
- 1 ))
98 # Don't allow uninitialized variables if requested
99 [[ -n "$set_u" ]] && set -u
102 # Used to call _cleanup on shell exit
107 (( _plan_set
!= 0 )) && "You tried to plan twice!"
111 _last_time
=$
("${TIME_SCRIPT}")
118 local reason
=${1:-''}
120 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
122 _print_plan
0 "Skip $reason"
126 _last_time
=$
("${TIME_SCRIPT}")
135 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
136 (( tests
== 0 )) && _die
"You said to run 0 tests! You've got to run something."
139 _expected_tests
=$tests
141 _last_time
=$
("${TIME_SCRIPT}")
149 local directive
=${2:-''}
152 [[ -n "$directive" ]] && echo -n " # $directive"
168 # This is the workhorse method that actually
169 # prints the tests result.
174 (( _plan_set
== 0 )) && _die
"You tried to run a test without a plan! Gotta have a plan."
176 _executed_tests
=$
(( $_executed_tests + 1 ))
178 if [[ -n "$name" ]] ; then
179 if _matches
"$name" "^[0-9]+$" ; then
180 diag
" You named your test '$name'. You shouldn't use numbers for your test names."
181 diag
" Very confusing."
185 if (( result
!= 0 )) ; then
187 _failed_tests
=$
(( _failed_tests
+ 1 ))
189 echo -n "ok $_executed_tests"
191 if [[ -n "$name" ]] ; then
192 local ename
=${name//\#/\\#}
196 if [[ -n "$TODO" ]] ; then
197 echo -n " # TODO $TODO" ;
198 if (( result
!= 0 )) ; then
199 _failed_tests
=$
(( _failed_tests
- 1 ))
205 if (( result
!= 0 )) ; then
206 local file='tap-functions'
211 local bt
=$
(caller
$i)
212 while _matches
"$bt" "tap-functions$" ; do
217 eval $
(caller
$i |
(read line func
file ; echo "backtrace=\"$file:$func() at line $line.\""))
220 [[ -n "$TODO" ]] && t
="(TODO) "
222 if [[ -n "$name" ]] ; then
223 diag
" Failed ${t}test '$name'"
224 diag
" in $backtrace"
226 diag
" Failed ${t}test in $backtrace"
238 diag
"Output of '$command':"
239 "$@" |
while read line
; do
242 ok
${PIPESTATUS[0]} "$command"
249 local expected
=${2:?}
251 if [[ "$result" == "$expected" ]] ; then
259 # Thanks to Aaron Kangas for the patch to allow regexp matching
261 _bash_major_version
=${BASH_VERSION%%.*}
266 if [[ -z "$result" ||
-z "$pattern" ]] ; then
269 if (( _bash_major_version
>= 3 )) ; then
270 [[ "$result" =~
"$pattern" ]]
272 echo "$result" |
egrep -q "$pattern"
280 local expected
=${2:?}
282 diag
" got: '$result'"
283 diag
" expected: '$expected'"
289 local expected
=${2:?}
292 _equals
"$result" "$expected"
296 (( r
!= 0 )) && _is_diag
"$result" "$expected"
303 local expected
=${2:?}
306 _equals
"$result" "$expected"
310 (( r
!= 0 )) && _is_diag
"$result" "$expected"
320 _matches
"$result" "$pattern"
324 (( r
!= 0 )) && diag
" '$result' doesn't match '$pattern'"
334 _matches
"$result" "$pattern"
338 (( r
!= 0 )) && diag
" '$result' matches '$pattern'"
344 local condition
=${1:?}
345 local reason
=${2:-''}
348 if (( condition
== 0 )) ; then
350 for (( i
=0 ; i
<$n ; i
++ )) ; do
351 _executed_tests
=$
(( _executed_tests
+ 1 ))
352 echo "ok $_executed_tests # skip: $reason"
365 if [ "${_auto_timing}" -eq "1" ] ; then
366 new_time
=$
("${TIME_SCRIPT}")
367 duration
=$
(awk "BEGIN { printf(\"%f\n\", ($new_time - $_last_time)*1000) }")
369 echo " duration_ms: ${duration}"
372 _last_time
=$
("${TIME_SCRIPT}")
380 if [[ "${val}" != "0" ]] ; then
391 if [[ -n "$msg" ]] ; then
400 local reason
=${1:-'<unspecified error>'}
409 local reason
=${1:-''}
411 echo "Bail out! $reason" >&2
419 if (( _plan_set
== 0 )) ; then
420 diag
"Looks like your test died before it could output anything."
424 if (( _test_died
!= 0 )) ; then
425 diag
"Looks like your test died just after $_executed_tests."
429 if (( _skip_all
== 0 && _no_plan
!= 0 )) ; then
430 _print_plan
$_executed_tests
434 if (( _no_plan
== 0 && _expected_tests
< _executed_tests
)) ; then
435 s
= ; (( _expected_tests
> 1 )) && s
=s
436 local extra
=$
(( _executed_tests
- _expected_tests
))
437 diag
"Looks like you planned $_expected_tests test$s but ran $extra extra."
441 if (( _no_plan
== 0 && _expected_tests
> _executed_tests
)) ; then
442 s
= ; (( _expected_tests
> 1 )) && s
=s
443 diag
"Looks like you planned $_expected_tests test$s but only ran $_executed_tests."
446 if (( _failed_tests
> 0 )) ; then
447 s
= ; (( _failed_tests
> 1 )) && s
=s
448 diag
"Looks like you failed $_failed_tests test$s of $_executed_tests."
456 if (( _no_plan
!= 0 || _plan_set
== 0 )) ; then
457 return $_failed_tests
460 if (( _expected_tests
< _executed_tests
)) ; then
461 return $
(( _executed_tests
- _expected_tests
))
464 return $
(( _failed_tests
+ ( _expected_tests
- _executed_tests
)))
470 if [[ -z "$rc" ]] ; then
477 (( alt_rc
!= 0 )) && rc
=$alt_rc
This page took 0.038544 seconds and 4 git commands to generate.