3 # Copyright 2010 Patrick LeBoutillier <patrick.leboutillier@gmail.com>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 tap-functions: A TAP-producing BASH library
37 plan_skip_all [REASON]
43 is RESULT EXPECTED [NAME]
44 isnt RESULT EXPECTED [NAME]
45 like RESULT PATTERN [NAME]
46 unlike RESULT PATTERN [NAME]
51 skip [CONDITION] [REASON] [NB_TESTS=1]
53 skip $feature_not_present "feature not present" 2 || {
59 Specify TODO mode by setting $TODO:
60 TODO="not implemented yet"
61 ok $result "some not implemented test"
75 is $USER $me "I am myself"
76 like $HOME $me "My home is mine"
77 like "`id`" $me "My id matches myself"
81 # Same thing using okx shortcut
84 [[ "`id -u`" != "0" ]]
86 skip $i_am_not_root "Must be root" || {
90 TODO="figure out how to become root..."
91 okx [ "$HOME" == "/root" ]
99 while getopts ":sx" opt
; do
105 shift $
(( OPTIND
- 1 ))
106 # Don't allow uninitialized variables if requested
107 [[ -n "$set_u" ]] && set -u
110 # Used to call _cleanup on shell exit
115 (( _plan_set
!= 0 )) && "You tried to plan twice!"
125 local reason
=${1:-''}
127 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
129 _print_plan
0 "Skip $reason"
141 (( _plan_set
!= 0 )) && _die
"You tried to plan twice!"
142 (( tests
== 0 )) && _die
"You said to run 0 tests! You've got to run something."
145 _expected_tests
=$tests
154 local directive
=${2:-''}
157 [[ -n "$directive" ]] && echo -n " # $directive"
173 # This is the workhorse method that actually
174 # prints the tests result.
179 (( _plan_set
== 0 )) && _die
"You tried to run a test without a plan! Gotta have a plan."
181 _executed_tests
=$
(( $_executed_tests + 1 ))
183 if [[ -n "$name" ]] ; then
184 if _matches
"$name" "^[0-9]+$" ; then
185 diag
" You named your test '$name'. You shouldn't use numbers for your test names."
186 diag
" Very confusing."
190 if (( result
!= 0 )) ; then
192 _failed_tests
=$
(( _failed_tests
+ 1 ))
194 echo -n "ok $_executed_tests"
196 if [[ -n "$name" ]] ; then
197 local ename
=${name//\#/\\#}
201 if [[ -n "$TODO" ]] ; then
202 echo -n " # TODO $TODO" ;
203 if (( result
!= 0 )) ; then
204 _failed_tests
=$
(( _failed_tests
- 1 ))
209 if (( result
!= 0 )) ; then
210 local file='tap-functions'
215 local bt
=$
(caller
$i)
216 while _matches
"$bt" "tap-functions$" ; do
221 eval $
(caller
$i |
(read line func
file ; echo "backtrace=\"$file:$func() at line $line.\""))
224 [[ -n "$TODO" ]] && t
="(TODO) "
226 if [[ -n "$name" ]] ; then
227 diag
" Failed ${t}test '$name'"
228 diag
" in $backtrace"
230 diag
" Failed ${t}test in $backtrace"
242 diag
"Output of '$command':"
243 "$@" |
while read line
; do
246 ok
${PIPESTATUS[0]} "$command"
252 local expected
=${2:?}
254 if [[ "$result" == "$expected" ]] ; then
262 # Thanks to Aaron Kangas for the patch to allow regexp matching
264 _bash_major_version
=${BASH_VERSION%%.*}
269 if [[ -z "$result" ||
-z "$pattern" ]] ; then
272 if (( _bash_major_version
>= 3 )) ; then
273 [[ "$result" =~
"$pattern" ]]
275 echo "$result" |
egrep -q "$pattern"
283 local expected
=${2:?}
285 diag
" got: '$result'"
286 diag
" expected: '$expected'"
292 local expected
=${2:?}
295 _equals
"$result" "$expected"
299 (( r
!= 0 )) && _is_diag
"$result" "$expected"
306 local expected
=${2:?}
309 _equals
"$result" "$expected"
313 (( r
!= 0 )) && _is_diag
"$result" "$expected"
323 _matches
"$result" "$pattern"
327 (( r
!= 0 )) && diag
" '$result' doesn't match '$pattern'"
337 _matches
"$result" "$pattern"
341 (( r
!= 0 )) && diag
" '$result' matches '$pattern'"
347 local condition
=${1:?}
348 local reason
=${2:-''}
351 if (( condition
== 0 )) ; then
353 for (( i
=0 ; i
<$n ; i
++ )) ; do
354 _executed_tests
=$
(( _executed_tests
+ 1 ))
355 echo "ok $_executed_tests # skip: $reason"
367 if [[ -n "$msg" ]] ; then
376 local reason
=${1:-'<unspecified error>'}
385 local reason
=${1:-''}
387 echo "Bail out! $reason" >&2
395 if (( _plan_set
== 0 )) ; then
396 diag
"Looks like your test died before it could output anything."
400 if (( _test_died
!= 0 )) ; then
401 diag
"Looks like your test died just after $_executed_tests."
405 if (( _skip_all
== 0 && _no_plan
!= 0 )) ; then
406 _print_plan
$_executed_tests
410 if (( _no_plan
== 0 && _expected_tests
< _executed_tests
)) ; then
411 s
= ; (( _expected_tests
> 1 )) && s
=s
412 local extra
=$
(( _executed_tests
- _expected_tests
))
413 diag
"Looks like you planned $_expected_tests test$s but ran $extra extra."
417 if (( _no_plan
== 0 && _expected_tests
> _executed_tests
)) ; then
418 s
= ; (( _expected_tests
> 1 )) && s
=s
419 diag
"Looks like you planned $_expected_tests test$s but only ran $_executed_tests."
422 if (( _failed_tests
> 0 )) ; then
423 s
= ; (( _failed_tests
> 1 )) && s
=s
424 diag
"Looks like you failed $_failed_tests test$s of $_executed_tests."
432 if (( _no_plan
!= 0 || _plan_set
== 0 )) ; then
433 return $_failed_tests
436 if (( _expected_tests
< _executed_tests
)) ; then
437 return $
(( _executed_tests
- _expected_tests
))
440 return $
(( _failed_tests
+ ( _expected_tests
- _executed_tests
)))
446 if [[ -z "$rc" ]] ; then
453 (( alt_rc
!= 0 )) && rc
=$alt_rc
This page took 0.037563 seconds and 4 git commands to generate.