summaryrefslogtreecommitdiffstats
path: root/TESTS_ENVIRONMENT.in
blob: 42de49720ee928ab174c08a3647b8d507eb747bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#! /bin/bash

unset NO_COLOR

top_srcdir="@abssrcdir@"
export top_srcdir

top_srcdir_parent=`dirname ${top_srcdir}`
export top_srcdir_parent

srcdir="@abssrcdir@/test"
export srcdir

# The top build directory, derived from the path to this script.
top_builddir=`dirname $0`
export top_builddir

builddir=`pwd -P`
export builddir

test_dir="@abssrcdir@/test"
export test_dir

# Let the tests know whether bzip is supported or not.
BZIP2_SUPPORT="@BZIP2_SUPPORT@"
export BZIP2_SUPPORT

BZIP2_CMD="@BZIP2_CMD@"
export BZIP2_CMD

XZ_CMD="@XZ_CMD@"
export XZ_CMD

TSHARK_CMD="@TSHARK_CMD@"
export TSHARK_CMD

LIBARCHIVE_LIBS="@LIBARCHIVE_LIBS@"
export LIBARCHIVE_LIBS

HOME="${top_builddir}/test"
export HOME

# The full path of the test case
test_file=$1
# The base name of the test case
test_file_base=`basename $1`
# The current test number for shell based tests.
test_num=0

test_hash=""

lnav_test="${top_builddir}/src/lnav-test"
export lnav_test

lnav="${top_builddir}/src/lnav"
export lnav

LNAV_LOG_PATH="${top_builddir}/test/test.log"
export LNAV_LOG_PATH

SFTP_TEST_URL="@SFTP_TEST_URL@"
export SFTP_TEST_URL

HAVE_SQLITE3_VALUE_SUBTYPE="@HAVE_SQLITE3_VALUE_SUBTYPE@"
export HAVE_SQLITE3_VALUE_SUBTYPE

HAVE_SQLITE3_ERROR_OFFSET="@HAVE_SQLITE3_ERROR_OFFSET@"
export HAVE_SQLITE3_ERROR_OFFSET

## BEGIN Functions

LAST_TEST=""

LAST_CAP_TEST=()

has_errors=""

#
# Run a test case and capture its standard out and standard err.
#
# Usage: run_test <utility> [<argument> ...]
#
# Example:
#
#   To run rktimes and capture all of its stdio output:
#
#     run_test rktimes -V
#
run_test() {
    printf "%s \033[0;35m=============================================================\033[0m\n" $(date -Iseconds)
    LAST_TEST=("test: $@")
    echo "${LAST_TEST[@]}"
    export test_num=`expr ${test_num} \+ 1`
    "$@" > ${test_file_base}_${test_num}.tmp 2> ${test_file_base}_${test_num}.err
}

run_cap_test() {
    LAST_CAP_TEST=("test: $@")
    local full_cmd=$(echo "${LAST_CAP_TEST[@]}" | sed -e "s;${test_dir};{test_dir};g" -e "s;${top_srcdir};{top_srcdir};g")
    export test_hash=$(echo "${full_cmd}" | shasum | cut -f 1 -d ' ')
    echo "${full_cmd}" > ${test_file_base}_${test_hash}.cmd
    "$@" > ${test_file_base}_${test_hash}.out 2> ${test_file_base}_${test_hash}.err

    sed -ibak \
        -e "s;${test_dir};{test_dir};g" \
        -e "s;${builddir};{test_dir};g" \
        -e "s;${top_srcdir};{top_srcdir};g" \
        -e "s;${top_srcdir_parent};{top_srcdir_parent};g" \
        ${test_file_base}_${test_hash}.out
    echo
    printf "%s \033[0;35m=============================================================\033[0m\n" $(date -Iseconds)
    printf '\033[0;35mCommand\033[0m: %s\n' "${full_cmd}"
    printf '\033[0;32mBEGIN\033[0m %s\n' "${test_file_base}_${test_hash}.out"
    cat "${test_file_base}_${test_hash}.out"
    printf '\033[0;32mEND\033[0m   %s\n' "${test_file_base}_${test_hash}.out"
    if test -f ${srcdir}/expected/${test_file_base}_${test_hash}.out; then
       diff -w -u \
           ${srcdir}/expected/${test_file_base}_${test_hash}.out \
           ${test_file_base}_${test_hash}.out \
           > ${test_file_base}_${test_hash}.diff
       if test $? -ne 0; then
           echo OUT: "${full_cmd}"
           cat ${test_file_base}_${test_hash}.diff
           echo "FAIL! EXPECTED OUT DIFF"
           export has_errors="yes"
       fi
    else
       echo "FAIL! EXPECTED OUT MISSING -- ${srcdir}/expected/${test_file_base}_${test_hash}.out"
       export has_errors="yes"
    fi

    sed -ibak -E \
        -e "s;${test_dir};{test_dir};g" \
        -e "s;${builddir};{builddir};g" \
        -e "s;${top_srcdir};{top_srcdir};g" \
        -e 's;"errorId":".+";;g' \
        ${test_file_base}_${test_hash}.err
    printf '\033[0;31mBEGIN\033[0m %s\n' "${test_file_base}_${test_hash}.err"
    cat "${test_file_base}_${test_hash}.err"
    printf '\033[0;31mEND\033[0m   %s\n' "${test_file_base}_${test_hash}.err"
    if test -f ${srcdir}/expected/${test_file_base}_${test_hash}.err; then
       diff -w -u ${srcdir}/expected/${test_file_base}_${test_hash}.err \
           ${test_file_base}_${test_hash}.err \
           > ${test_file_base}_${test_hash}.err.diff
       if test $? -ne 0; then
           echo ERR: "${full_cmd}"
           cat ${test_file_base}_${test_hash}.err.diff
           echo "FAIL! EXPECTED ERR DIFF"
           export has_errors="yes"
       fi
    else
       echo "FAIL! EXPECTED ERR MISSING"
       export has_errors="yes"
    fi
}

#
# Check the output generated by a run_test() call.
#
# Usage: check_output <fail message> {Expected output on stdin}
#
# Example:
#
#   To check the output of 'rktimes -V' and print out 'Unable to get version?'
#   if the output doesn't match:
#
#     run_test rktimes -V
#     check_output "Unable to get version?" <<EOF
#     0.5
#     EOF
#
check_output() {
    sed -ibak \
        -e "s;${test_dir};{test_dir};g" \
        -e "s;${top_srcdir};{top_srcdir};g" \
        ${test_file_base}_${test_num}.tmp
    diff -w -u - ${test_file_base}_${test_num}.tmp > ${test_file_base}_${test_num}.diff
    if test $? -ne 0; then
        echo "${LAST_TEST[@]}"
        echo $1
        cat ${test_file_base}_${test_num}.diff
	    exit 1
    fi
}

check_output_ws() {
    diff -u - ${test_file_base}_${test_num}.tmp > ${test_file_base}_${test_num}.diff
    if test $? -ne 0; then
        echo "${LAST_TEST[@]}"
        echo $1
        cat ${test_file_base}_${test_num}.diff
	    exit 1
    fi
}

test_filename() {
    echo ${test_file_base}_${test_num}.tmp
}

test_err_filename() {
    echo ${test_file_base}_${test_num}.err
}

check_error_output() {
    sed -ibak \
        -e "s;${test_dir};{test_dir};g" \
        -e "s;${top_srcdir};{top_srcdir};g" \
        ${test_file_base}_${test_num}.err
    diff -w -u - ${test_file_base}_${test_num}.err \
        > ${test_file_base}_${test_num}.err.diff
    if test $? -ne 0; then
        echo "${LAST_TEST[@]}"
        echo $1
        cat ${test_file_base}_${test_num}.err.diff
        exit 1
    fi
}

#
# Grep for a string in the output generated by a run_test() call.
#
# Usage: grep_output_for <string> <fail message>
#
# Example:
#
#   To check the output of 'cbhey -l' for 'IDL:Foobar:1.0' and print out
#   'Unable to list supported interfaces?' if it is not found:
#
#     run_test cbhey -l
#     grep_output_for "IDL:Foobar:1.0" "Unable to list supported interface?"
#
grep_output_for() {
    if grep -q $1 ${test_file_base}_${test_num}.tmp > /dev/null 2>&1; then
	:
    else
	echo "${test_file_base}_${test_num}.tmp: $2"
	exit 1
    fi
}

on_error_log() {
  if test $? -ne 0; then
    echo $1 > /dev/stderr
    cat ${test_file_base}_${test_num}.tmp
    cat ${test_file_base}_${test_num}.err
  fi
}

on_error_fail_with() {
    if test $? -ne 0; then
	echo $1 > /dev/stderr
	cat ${test_file_base}_${test_num}.tmp
	cat ${test_file_base}_${test_num}.err
	exit 1
    fi
}

## END Functions


# Finally, run the test...

if test -x $1 && test `basename $1 .sh` == `basename $1`; then
    exec $*
else
    # Shell script
    shift
    . ${test_file}
fi

cleanup() {
  if test "${has_errors}"x = "yes"x; then
    exit 1
  fi
}

trap "cleanup" EXIT