]> pd.if.org Git - zpackage/blobdiff - t/tap.sh
fix tap diagnostic
[zpackage] / t / tap.sh
index b82f203f5f7628cbdaca6d46d46974a2431ba826..e3a345784092fb12475e02d4bdd2b4395ae76cd4 100755 (executable)
--- a/t/tap.sh
+++ b/t/tap.sh
@@ -4,29 +4,47 @@ tn=0
 planned=0
 
 tryrun() {
-       note="$@"
+       if [ "$1" = "-v" ]; then
+               allout=1
+               shift
+       else
+               allout=0
+       fi
+
        program=$1
        shift
-       $program "$@"
-       if [ $? -ne 0 ]; then
+
+       $program "$@" > test.out 2>&1
+       tap_rv=$?
+       tn=$((tn + 1))
+
+       if [ $tap_rv -ne 0 ]; then
                printf 'not ';
        fi
-       tn=$((tn + 1))
        printf 'ok %d - %s %s\n' $tn $program "$*"
+
+       if [ $allout -eq 1 ] || [ $tap_rv -ne 0 ]; then
+               if test -s test.out; then
+                       cat test.out | diagstdin
+               fi
+               rm test.out
+       fi
+
+       return $tap_rv
 }
 
 require() {
-       note="$@"
-       program=$1
-       shift
-       $program "$@"
-       if [ $? -ne 0 ]; then
-               printf "bail out! fail[$?]: $@\n"
-               exit 255;
+       tryrun "$@"
+       rv=$?
+       if [ $rv -ne 0 ]; then
+               bailout "exit status $rv";
        fi
-       tn=$((tn + 1))
-       printf 'ok %d - %s %s\n' $tn $program "$*"
+}
 
+bailout() {
+       diag "$@"
+       printf "bail out!\n";
+       exit 255;
 }
 
 shownote() {
@@ -39,6 +57,14 @@ diag() {
        printf '# %s\n' "$@"
 }
 
+diagfile() {
+       sed -e 's/^/# /' $1
+}
+
+diagstdin() {
+       sed -e 's/^/# /'
+}
+
 okexit() {
        exitwith 0 "$*"
 }
@@ -48,7 +74,7 @@ failsok() {
        note=
        if [ $rv -eq 0 ]; then
                printf 'not ';
-               note=$(printf 'got "%d" expected "%d"' "$rv" "0")
+               note=$(printf 'got "%d" expected non-zero' "$rv")
        fi
        tn=$((tn + 1))
        printf 'ok %d - %s\n' $tn "$*"
@@ -60,19 +86,20 @@ exitwith() {
        note=
        if [ $rv -ne $1 ]; then
                printf 'not ';
-               note=$(printf 'got "%d" expected "%d"' "$rv" "$1")
+               note=$(printf 'got "%d" expected "%d"' "$rv" "$1")
        fi
        shift
        tn=$((tn + 1))
        printf 'ok %d - %s\n' $tn "$*"
        shownote
+       return $rv
 }
 
 okstreq() {
        note=
        if [ "$1" != "$2" ]; then
                printf 'not ';
-               note=$(printf 'got "%s" expected "%s"' "$1" "$2")
+               note=$(printf 'got "%s"\n# expected "%s"' "$1" "$2")
        fi
 
        shift;shift;
@@ -91,3 +118,33 @@ finish() {
                printf '1..%d\n' $tn
        fi
 }
+
+skip() {
+       tap_skipping=$1
+       shift
+       while [ $tap_skipping -gt 0 ]; do
+               tn=$((tn + 1))
+               tap_skipping=$((tap_skipping - 1))
+               printf 'ok %d - # skip %s\n' $tn "$*"
+               #ok '# skip ' "$*"
+       done
+}
+
+ok() {
+       rv=$1
+       shift
+       if [ $rv = 1 ]; then
+               printf 'not '
+       fi
+       tn=$((tn + 1))
+       printf 'ok %d - %s\n' $tn "$*"
+       return $rv
+}
+
+pass() {
+       ok 0 "$@"
+}
+
+fail() {
+       ok 1 "$@"
+}