blob: cfdf2180aad8a6720b2d8e45ad6de5168cce6e4d (
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
|
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
test_begin() {
# Count tests to allow the test harness to double-check if all were
# included correctly.
ctr=0
[ -z "$RV" ] && RV="../rv/rv"
[ -z "$RVGEN" ] && RVGEN="python3 ../rvgen"
[ -z "$GOLDEN_DIR" ] && GOLDEN_DIR="tests/golden"
[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
}
failure() {
fail=1
if [ $# -gt 0 ]; then
failbuf+="$1"
failbuf+=$'\n'
fi
}
report() {
local desc="$1"
if [ "$fail" -eq 0 ]; then
echo "ok $ctr - $desc"
else
# Add output and exit code as comments in case of failure
echo "not ok $ctr - $desc"
echo -n "$failbuf"
echo "$result" | col -b | while read -r line; do echo "# $line"; done
printf "#\n# exit code %s\n" "$exitcode"
fi
}
_check() {
local command=$2
local expected_exitcode=${3:-0}
local expected_output=$4
local unexpected_output=$5
local all_lines_pattern=$6
local patterns="$expected_output $unexpected_output $all_lines_pattern"
local bgpid pid
eval "$TIMEOUT" "$command" &> check_output.$$ &
bgpid=$!
if grep -q "\$pid" <<< "$patterns"; then
for _ in {1..30}; do
pid=$(pgrep -f "${command%%[|;&>]*}" | tail -n1)
[ -n "$pid" ] && break
sleep 0.1
done
fi
wait $bgpid
exitcode=$?
result=$(tr -d '\0' < check_output.$$)
rm -f check_output.$$
failbuf=''
fail=0
# Suppress any other error if a needed pid is empty
if [ -z "$pid" ] && grep -q "\$pid" <<< "$patterns"; then
result=''
failure "# Empty pid for $command"
return 1
fi
expected_output="${expected_output//\$pid/$pid}"
unexpected_output="${unexpected_output//\$pid/$pid}"
all_lines_pattern="${all_lines_pattern//\$pid/$pid}"
# Test if the results matches if requested
if [ -n "$expected_output" ] && ! grep -qe "$expected_output" <<< "$result"; then
failure "# Output match failed: \"$expected_output\""
fi
if [ -n "$unexpected_output" ] && grep -qe "$unexpected_output" <<< "$result"; then
failure "# Output non-match failed: \"$unexpected_output\""
fi
if [ -n "$all_lines_pattern" ] && grep -vqe "$all_lines_pattern" <<< "$result"; then
failure "# All-lines pattern failed: \"$all_lines_pattern\""
fi
if [ $exitcode -ne "$expected_exitcode" ]; then
failure "# Expected exit code $expected_exitcode"
fi
}
check() {
# Simple check: run the command with given arguments and test exit code.
# If TEST_COUNT is set, run the test. Otherwise, just count.
ctr=$((ctr + 1))
if [ -n "$TEST_COUNT" ]; then
_check "$@"
report "$1"
fi
}
check_if_exists() {
# Conditional check that skips if a file or folder doesn't exist
local desc=$1
local command=$2
local file=$3
local expected_output=$4
local unexpected_output=$5
local all_lines_pattern=$6
ctr=$((ctr + 1))
if [ -n "$TEST_COUNT" ]; then
if [ ! -e "$file" ]; then
echo "ok $ctr - $desc # SKIP file not found: $file"
else
_check "$desc" "$command" 0 "$expected_output" \
"$unexpected_output" "$all_lines_pattern"
report "$desc"
fi
fi
}
check_and_compare_folder() {
# Run command, compare generated folder to golden, and cleanup
local desc=$1
local command=$2
local generated_dir=$3
local expected_output=$4
local unexpected_output=$5
local golden_dir="$GOLDEN_DIR/$generated_dir"
ctr=$((ctr + 1))
if [ -n "$TEST_COUNT" ]; then
rm -rf "$generated_dir"
_check "$desc" "$command" 0 "$expected_output" "$unexpected_output"
if [ "$fail" -eq 0 ] && [ ! -d "$generated_dir" ]; then
failure "# Generated directory not found: $generated_dir"
fi
if [ "$fail" -ne 0 ]; then
:
elif ! diff -r "$generated_dir" "$golden_dir" &> /dev/null; then
failure "# Directories differ:"
failbuf+=$(diff -r "$generated_dir" "$golden_dir" 2>&1 | sed 's/^/# /')
failbuf+=$'\n'
fi
report "$1"
rm -rf "$generated_dir"
fi
}
set_timeout() {
TIMEOUT="timeout -v -k 30s $1"
}
set_expected_timeout() {
TIMEOUT="timeout --preserve-status -k 30s $1"
}
unset_timeout() {
unset TIMEOUT
}
test_end() {
# If running without TEST_COUNT, tests are not actually run, just
# counted. In that case, re-run the test with the correct count.
[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash "$0" || true
}
# Avoid any environmental discrepancies
export LC_ALL=C
unset_timeout
|