tests: DRY require tests for Rev/Revactor
[rainbows.git] / t / test-lib.sh
blob54d1b2de47c1150f938caa4c43a57c4526f55502
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! developers
4 # pipefail is non-POSIX, but useful in ksh/bash
6 set +e
7 set -o pipefail 2>/dev/null
9 if test $? -eq 0
10 then
11 set -o pipefail
12 else
13 echo >&2 "WARNING: your shell does not understand pipefail"
16 set -e
18 T=$(basename $0)
19 if test -z "$model"
20 then
21 case $T in
22 t1???-thread-pool-*.sh) model=ThreadPool ;;
23 t2???-thread-spawn-*.sh) model=ThreadSpawn ;;
24 t3???-revactor-*.sh) model=Revactor ;;
25 t4???-rev-*.sh) model=Rev ;;
26 *) model=any ;;
27 esac
30 set -u
31 ruby="${ruby-ruby}"
33 # ensure a sane environment
34 TZ=UTC LC_ALL=C LANG=C
35 export LANG LC_ALL TZ
36 unset CDPATH
38 die () {
39 echo >&2 "$@"
40 exit 1
43 _TEST_RM_LIST=""
44 trap 'rm -f $_TEST_RM_LIST' 0
45 PATH=$PWD/bin:$PATH
46 export PATH
48 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
50 wait_for_pid () {
51 path="$1"
52 nr=30
53 while ! test -s "$path" && test $nr -gt 0
55 nr=$(($nr - 1))
56 sleep 1
57 done
60 require_check () {
61 lib=$1
62 const=$2
63 if ! $ruby -r$lib -e "puts $const" >/dev/null 2>&1
64 then
65 echo >&2 "skipping $T since we don't have $lib"
66 exit 0
70 # given a list of variable names, create temporary files and assign
71 # the pathnames to those variables
72 rtmpfiles () {
73 for id in "$@"
75 _tmp=$(mktemp -t rainbows.$$.$id.XXXXXXXX)
76 eval "$id=$_tmp"
77 _TEST_RM_LIST="$_TEST_RM_LIST $_tmp"
78 done
81 dbgcat () {
82 id=$1
83 eval '_file=$'$id
84 echo "==> $id <=="
85 sed -e "s/^/$id:/" < $_file
88 case $model in
89 Rev) require_check rev Rev::VERSION ;;
90 Revactor) require_check revactor Revactor::VERSION ;;
91 esac