blob: 8de745753952a4424d67f094149a432f8836f7cc [file] [log] [blame]
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +00001#!/bin/sh
2
3ME_="${0##*/}"
4
5warn_() { printf >&2 '%s\n' "$*"; }
6fail_() { warn_ "$ME_: failed test: $*"; exit 1; }
7skip_() { warn_ "$ME_: skipped test: $*"; exit 77; }
8framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; }
Mike Frysingere689e042011-02-28 19:57:24 -05009framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; }
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000010
11check_prog()
12{
Maxin B. John066a7332013-03-18 11:35:06 +010013 type "$@" > /dev/null 2>&1 ||
Mike Frysingere689e042011-02-28 19:57:24 -050014 framework_skip_ "$* is not available"
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000015}
16
Dmitry V. Levin8f546642015-03-17 17:07:57 +000017dump_log_and_fail_with()
18{
19 cat < "$LOG"
20 fail_ "$*"
21}
22
23run_prog()
24{
25 if [ $# -eq 0 ]; then
26 set -- "./${ME_%.test}"
27 fi
28 args="$*"
29 "$@" || {
30 if [ $? -eq 77 ]; then
31 skip_ "$args exited with code 77"
32 else
33 fail_ "$args failed"
34 fi
35 }
36}
37
38
39run_prog_skip_if_failed()
40{
41 args="$*"
42 "$@" || framework_skip_ "$args failed"
43}
44
45run_strace()
46{
47 > "$LOG" || fail_ "failed to write $LOG"
48 args="$*"
49 $STRACE -o "$LOG" "$@" ||
50 dump_log_and_fail_with "$STRACE $args failed"
51}
52
53run_strace_merge()
54{
55 rm -f -- "$LOG".[0-9]*
56 run_strace -ff -tt "$@"
57 "$srcdir"/../strace-log-merge "$LOG" > "$LOG" ||
58 dump_log_and_fail_with 'strace-log-merge failed'
59 rm -f -- "$LOG".[0-9]*
60}
61
Dmitry V. Levin79c5c5d2015-04-06 23:40:13 +000062check_gawk()
63{
64 check_prog gawk
65 check_prog grep
66
67 local program="$1"; shift
68 if grep '^@include[[:space:]]' < "$program" > /dev/null; then
69 gawk '@include "/dev/null"' < /dev/null ||
70 framework_skip_ 'gawk does not support @include'
71 fi
72}
73
Dmitry V. Levin8f546642015-03-17 17:07:57 +000074# Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
Dmitry V. Levina69854a2015-09-19 00:01:35 +000075# Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk.
76# If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE.
Dmitry V. Levin8f546642015-03-17 17:07:57 +000077match_awk()
78{
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +000079 local output program error
Dmitry V. Levin8f546642015-03-17 17:07:57 +000080 if [ $# -eq 0 ]; then
81 output="$LOG"
82 else
83 output="$1"; shift
84 fi
85 if [ $# -eq 0 ]; then
86 program="$srcdir/${ME_%.test}.awk"
87 else
88 program="$1"; shift
89 fi
90 if [ $# -eq 0 ]; then
91 error="$STRACE $args output mismatch"
92 else
93 error="$1"; shift
94 fi
Dmitry V. Levin8f546642015-03-17 17:07:57 +000095
Dmitry V. Levin79c5c5d2015-04-06 23:40:13 +000096 check_gawk "$program"
Dmitry V. Levin8f546642015-03-17 17:07:57 +000097
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +000098 AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || {
Dmitry V. Levin8f546642015-03-17 17:07:57 +000099 cat < "$output"
100 fail_ "$error"
101 }
102}
103
104# Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
105# Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
106# If it does, dump the difference and fail with ERROR_MESSAGE.
107match_diff()
108{
109 local output expected error
110 if [ $# -eq 0 ]; then
111 output="$LOG"
112 else
113 output="$1"; shift
114 fi
115 if [ $# -eq 0 ]; then
116 expected="$srcdir/${ME_%.test}.expected"
117 else
118 expected="$1"; shift
119 fi
120 if [ $# -eq 0 ]; then
121 error="$STRACE $args output mismatch"
122 else
123 error="$1"; shift
124 fi
125
126 check_prog diff
127
128 diff -- "$expected" "$output" ||
129 fail_ "$error"
130}
131
132# Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
133# Check whether all patterns listed in FILE_WITH_PATTERNS
134# match FILE_TO_CHECK using egrep.
135# If at least one of these patterns does not match,
136# dump both files and fail with ERROR_MESSAGE.
137match_grep()
138{
Dmitry V. Levin3b731942015-09-19 00:11:07 +0000139 local output patterns error pattern failed=
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000140 if [ $# -eq 0 ]; then
141 output="$LOG"
142 else
143 output="$1"; shift
144 fi
145 if [ $# -eq 0 ]; then
146 patterns="$srcdir/${ME_%.test}.expected"
147 else
148 patterns="$1"; shift
149 fi
150 if [ $# -eq 0 ]; then
151 error="$STRACE $args output mismatch"
152 else
153 error="$1"; shift
154 fi
155
156 check_prog wc
157 check_prog grep
158
Dmitry V. Levin3b731942015-09-19 00:11:07 +0000159 while read -r pattern; do
160 LC_ALL=C grep -E -x -e "$pattern" < "$output" > /dev/null || {
161 test -n "$failed" || {
162 echo 'Failed patterns of expected output:'
163 failed=1
164 }
165 printf '%s\n' "$pattern"
166 }
167 done < "$patterns"
168 test -z "$failed" || {
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000169 echo 'Actual output:'
170 cat < "$output"
171 fail_ "$error"
172 }
173}
174
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +0000175check_prog cat
176check_prog rm
177
178LOG="$ME_.tmp"
179rm -f "$LOG"
180
Dmitry V. Levinf60347d2013-06-18 15:28:47 +0000181: "${STRACE:=../strace}"
182: "${TIMEOUT_DURATION:=60}"
Dmitry V. Levin1e0a2802013-06-18 20:51:49 +0000183: "${SLEEP_A_BIT:=sleep 1}"