tests: import basic TAP library from Rainbows!
[unicorn.git] / t / test-lib.sh
blob60ace97532c6d644b2a0b903175d8b0e81aaab8e
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! hackers
3 # Copyright (c) 2010 Unicorn hackers
4 . ./my-tap-lib.sh
6 set +u
7 set -e
8 RUBY="${RUBY-ruby}"
9 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
10 t_pfx=$PWD/trash/$T-$RUBY_VERSION
11 set -u
13 PATH=$PWD/bin:$PATH
14 export PATH
16 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
18 wait_for_pid () {
19 path="$1"
20 nr=30
21 while ! test -s "$path" && test $nr -gt 0
23 nr=$(($nr - 1))
24 sleep 1
25 done
28 # given a list of variable names, create temporary files and assign
29 # the pathnames to those variables
30 rtmpfiles () {
31 for id in "$@"
33 name=$id
34 _tmp=$t_pfx.$id
35 eval "$id=$_tmp"
37 case $name in
38 *fifo)
39 rm -f $_tmp
40 mkfifo $_tmp
41 T_RM_LIST="$T_RM_LIST $_tmp"
43 *socket)
44 rm -f $_tmp
45 T_RM_LIST="$T_RM_LIST $_tmp"
48 > $_tmp
49 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
51 esac
52 done
55 dbgcat () {
56 id=$1
57 eval '_file=$'$id
58 echo "==> $id <=="
59 sed -e "s/^/$id:/" < $_file
62 check_stderr () {
63 set +u
64 _r_err=${1-${r_err}}
65 set -u
66 if grep -i Error $_r_err
67 then
68 die "Errors found in $_r_err"
69 elif grep SIGKILL $_r_err
70 then
71 die "SIGKILL found in $_r_err"
75 # unicorn_setup
76 unicorn_setup () {
77 eval $(unused_listen)
78 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
79 cat > $unicorn_config <<EOF
80 listen "$listen"
81 pid "$pid"
82 stderr_path "$r_err"
83 stdout_path "$r_out"
84 EOF
87 unicorn_wait_start () {
88 # no need to play tricks with FIFOs since we got "ready_pipe" now
89 unicorn_pid=$(cat $pid)
92 rsha1 () {
93 _cmd="$(which sha1sum 2>/dev/null || :)"
94 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
95 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
97 # last resort, see comments in sha1sum.rb for reasoning
98 test -n "$_cmd" || _cmd=sha1sum.rb
99 expr "$($_cmd < random_blob)" : '\([a-f0-9]\{40\}\)'