tests: "wc -l" portability for *BSDs
[unicorn.git] / t / test-lib.sh
blob28d6a88655397888b1845a8d94da249de239e627
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! hackers
3 # Copyright (c) 2010 Unicorn hackers
4 . ./my-tap-lib.sh
6 set +u
8 # sometimes we rely on http_proxy to avoid wasting bandwidth with Isolate
9 # and multiple Ruby versions
10 NO_PROXY=${UNICORN_TEST_ADDR-127.0.0.1}
11 export NO_PROXY
13 set -e
14 RUBY="${RUBY-ruby}"
15 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
16 RUBY_ENGINE=${RUBY_ENGINE-$($RUBY -e 'puts((RUBY_ENGINE rescue "ruby"))')}
17 t_pfx=$PWD/trash/$T-$RUBY_ENGINE-$RUBY_VERSION
18 set -u
20 PATH=$PWD/bin:$PATH
21 export PATH
23 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
25 wait_for_pid () {
26 path="$1"
27 nr=30
28 while ! test -s "$path" && test $nr -gt 0
30 nr=$(($nr - 1))
31 sleep 1
32 done
35 # "unix_time" is not in POSIX, but in GNU, and FreeBSD 9.0 (possibly earlier)
36 unix_time () {
37 $RUBY -e 'puts Time.now.to_i'
40 # "wc -l" outputs leading whitespace on *BSDs, filter it out for portability
41 count_lines () {
42 wc -l | tr -d '[:space:]'
45 # "wc -c" outputs leading whitespace on *BSDs, filter it out for portability
46 count_bytes () {
47 wc -c | tr -d '[:space:]'
50 # given a list of variable names, create temporary files and assign
51 # the pathnames to those variables
52 rtmpfiles () {
53 for id in "$@"
55 name=$id
57 case $name in
58 *fifo)
59 _tmp=$t_pfx.$id
60 eval "$id=$_tmp"
61 rm -f $_tmp
62 mkfifo $_tmp
63 T_RM_LIST="$T_RM_LIST $_tmp"
65 *socket)
66 _tmp="$(mktemp -t $id.$$.XXXXXXXX)"
67 if test $(printf "$_tmp" |count_bytes) -gt 108
68 then
69 echo >&2 "$_tmp too long, tests may fail"
70 echo >&2 "Try to set TMPDIR to a shorter path"
72 eval "$id=$_tmp"
73 rm -f $_tmp
74 T_RM_LIST="$T_RM_LIST $_tmp"
77 _tmp=$t_pfx.$id
78 eval "$id=$_tmp"
79 > $_tmp
80 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
82 esac
83 done
86 dbgcat () {
87 id=$1
88 eval '_file=$'$id
89 echo "==> $id <=="
90 sed -e "s/^/$id:/" < $_file
93 check_stderr () {
94 set +u
95 _r_err=${1-${r_err}}
96 set -u
97 if grep -v $T $_r_err | grep -i Error
98 then
99 die "Errors found in $_r_err"
100 elif grep SIGKILL $_r_err
101 then
102 die "SIGKILL found in $_r_err"
106 # unicorn_setup
107 unicorn_setup () {
108 eval $(unused_listen)
109 port=$(expr $listen : '[^:]*:\([0-9]\+\)')
110 host=$(expr $listen : '\([^:]*\):[0-9]\+')
112 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
113 cat > $unicorn_config <<EOF
114 listen "$listen"
115 pid "$pid"
116 stderr_path "$r_err"
117 stdout_path "$r_out"
121 unicorn_wait_start () {
122 # no need to play tricks with FIFOs since we got "ready_pipe" now
123 unicorn_pid=$(cat $pid)
126 rsha1 () {
127 sha1sum.rb