| 1 |
#! /usr/bin/env bash |
| 2 |
# |
| 3 |
# Usage: btest-diff [options] <filename> |
| 4 |
# |
| 5 |
# These environment variables are set by btest: |
| 6 |
# TEST_MODE={TEST|UPDATE|UPDATE_INTERACTIVE} |
| 7 |
# TEST_BASELINE |
| 8 |
# TEST_DIAGNOSTICS |
| 9 |
# TEST_NAME |
| 10 |
# |
| 11 |
# A test can optionally set these environment variables: |
| 12 |
# TEST_DIFF_CANONIFIER |
| 13 |
# TEST_DIFF_BRIEF |
| 14 |
# TEST_DIFF_FILE_MAX_LINES |
| 15 |
# |
| 16 |
# This script has the following exit codes: |
| 17 |
# |
| 18 |
# When TEST_MODE is TEST: |
| 19 |
# 0 - Comparison succeeded, files are the same |
| 20 |
# 1 - Problems with input file/args or running TEST_DIFF_CANONIFIER, or file contents differ |
| 21 |
# 2 - Other diffing trouble (inherited from diff) |
| 22 |
# 100 - No baseline to compare to available |
| 23 |
# |
| 24 |
# When TEST_MODE is UPDATE: |
| 25 |
# 0 - Baseline updated |
| 26 |
# 1 - Problems with input file/args or running TEST_DIFF_CANONIFIER |
| 27 |
# |
| 28 |
# When TEST_MODE is UPDATE_INTERACTIVE: |
| 29 |
# 0 - Baseline updated, or nothing to update |
| 30 |
# 1 - Problems with input file/args or running TEST_DIFF_CANONIFIER, or user skips a deviating baseline |
| 31 |
# 200 - User asks to abort after a deviating baseline |
| 32 |
# |
| 33 |
# Otherwise: exits with 1 |
| 34 |
|
| 35 |
# It's okay to check $? explicitly: |
| 36 |
# shellcheck disable=SC2181 |
| 37 |
|
| 38 |
# Maximum number of lines to show from mismatching input file by default. |
| 39 |
MAX_LINES=100 |
| 40 |
|
| 41 |
# Header line we tuck onto new baselines generated by |
| 42 |
# btest-diff. Serves both as a warning and as an indicator that the |
| 43 |
# baseline has been run through the TEST_DIFF_CANONIFIER (if any). |
| 44 |
HEADER="### BTest baseline data generated by btest-diff. Do not edit. Use \"btest -U/-u\" to update. Requires BTest >= 0.63." |
| 45 |
|
| 46 |
# btest-diff supports a binary mode to simplify the handling of files |
| 47 |
# that are better treated as binary blobs rather than text files. In |
| 48 |
# binary mode, we treat the input file as-is, meaning: |
| 49 |
# |
| 50 |
# - only check whether input and baseline are identical |
| 51 |
# - don't prepend our btest header line when updating baseline |
| 52 |
# - don't canonify when updating baseline |
| 53 |
# |
| 54 |
BINARY_MODE= |
| 55 |
|
| 56 |
is_binary_mode() { |
| 57 |
test -n "$BINARY_MODE" |
| 58 |
} |
| 59 |
|
| 60 |
# Predicate, succeeds if the given baseline is canonicalized. |
| 61 |
is_canon_baseline() { |
| 62 |
local input="$1" |
| 63 |
|
| 64 |
# The baseline is canonicalized when we find our header in it. To |
| 65 |
# allow for some wiggle room in updating the wording in the header |
| 66 |
# in the future, we don't fix the exact string, and end after the |
| 67 |
# "Do not edit." sentence. |
| 68 |
local header |
| 69 |
header=$(echo "$HEADER" | sed -E 's/Do not edit\..*/Do not edit./') |
| 70 |
|
| 71 |
if head -n 1 "$input" | grep -q -F "$header" 2>/dev/null; then |
| 72 |
return 0 |
| 73 |
fi |
| 74 |
|
| 75 |
return 1 |
| 76 |
} |
| 77 |
|
| 78 |
# Prints the requested baseline to standard out if it is canonicalized |
| 79 |
# or we're using binary mode. Otherwise fails and prints nothing. |
| 80 |
get_baseline() { |
| 81 |
local input="$1" |
| 82 |
|
| 83 |
if is_binary_mode; then |
| 84 |
cat "$input" |
| 85 |
return 0 |
| 86 |
fi |
| 87 |
|
| 88 |
! is_canon_baseline "$input" && return 1 |
| 89 |
tail -n +2 "$input" |
| 90 |
} |
| 91 |
|
| 92 |
# Updates the given baseline to the given filename inside the *first* |
| 93 |
# baseline directory. Prepends our header if we're not in binary mode. |
| 94 |
update_baseline() { |
| 95 |
local input="$1" |
| 96 |
local output="${baseline_dirs[0]}/$2" |
| 97 |
|
| 98 |
if ! is_binary_mode; then |
| 99 |
echo "$HEADER" >"$output" |
| 100 |
cat "$input" >>"$output" |
| 101 |
else |
| 102 |
cat "$input" >"$output" |
| 103 |
fi |
| 104 |
} |
| 105 |
|
| 106 |
# ---- Main program ---------------------------------------------------- |
| 107 |
|
| 108 |
while [ "$1" != "" ]; do |
| 109 |
case "$1" in |
| 110 |
"--binary") |
| 111 |
BINARY_MODE=1 |
| 112 |
shift |
| 113 |
;; |
| 114 |
*) |
| 115 |
break |
| 116 |
;; |
| 117 |
esac |
| 118 |
done |
| 119 |
|
| 120 |
if [ -n "$TEST_DIFF_FILE_MAX_LINES" ]; then |
| 121 |
MAX_LINES=$TEST_DIFF_FILE_MAX_LINES |
| 122 |
fi |
| 123 |
|
| 124 |
if [ "$TEST_DIAGNOSTICS" = "" ]; then |
| 125 |
TEST_DIAGNOSTICS=/dev/stdout |
| 126 |
fi |
| 127 |
|
| 128 |
if [ "$#" -lt 1 ]; then |
| 129 |
echo "btest-diff: wrong number of arguments" >$TEST_DIAGNOSTICS |
| 130 |
exit 1 |
| 131 |
fi |
| 132 |
|
| 133 |
# Split string with baseline directories into array. |
| 134 |
IFS=':' read -ra baseline_dirs <<<"$TEST_BASELINE" |
| 135 |
|
| 136 |
input="$1" |
| 137 |
# shellcheck disable=SC2001 |
| 138 |
canon=$(echo "$input" | sed 's#/#.#g') |
| 139 |
shift |
| 140 |
|
| 141 |
if [ ! -f "$input" ]; then |
| 142 |
echo "btest-diff: input $input does not exist." >$TEST_DIAGNOSTICS |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
|
| 146 |
tmpfiles="" |
| 147 |
delete_tmps() { |
| 148 |
# shellcheck disable=SC2086 |
| 149 |
rm -f $tmpfiles 2>/dev/null |
| 150 |
} |
| 151 |
|
| 152 |
trap delete_tmps 0 |
| 153 |
|
| 154 |
# First available baseline across directories. |
| 155 |
baseline="" |
| 156 |
for dir in "${baseline_dirs[@]}"; do |
| 157 |
test -f "$dir/$canon" && baseline="$dir/$canon" && break |
| 158 |
done |
| 159 |
|
| 160 |
result=2 |
| 161 |
|
| 162 |
rm -f $TEST_DIAGNOSTICS 2>/dev/null |
| 163 |
|
| 164 |
echo "== File ===============================" >>$TEST_DIAGNOSTICS |
| 165 |
|
| 166 |
if [ -z "$baseline" ]; then |
| 167 |
cat "$input" >>$TEST_DIAGNOSTICS |
| 168 |
elif [ -n "$TEST_DIFF_BRIEF" ]; then |
| 169 |
echo "<Content not shown>" >>$TEST_DIAGNOSTICS |
| 170 |
else |
| 171 |
if [ "$(wc -l "$input" | awk '{print $1}')" -le "$MAX_LINES" ]; then |
| 172 |
cat "$input" >>$TEST_DIAGNOSTICS |
| 173 |
else |
| 174 |
head -n "$MAX_LINES" "$input" >>$TEST_DIAGNOSTICS |
| 175 |
echo "[... File too long, truncated ...]" >>$TEST_DIAGNOSTICS |
| 176 |
fi |
| 177 |
fi |
| 178 |
|
| 179 |
# If no canonifier is defined, just copy. Simplifies code layout. |
| 180 |
# In binary mode, always just copy. |
| 181 |
if [ -z "$TEST_DIFF_CANONIFIER" ] || is_binary_mode; then |
| 182 |
TEST_DIFF_CANONIFIER="cat" |
| 183 |
fi |
| 184 |
|
| 185 |
canon_output=/tmp/test-diff.$$.$canon.tmp |
| 186 |
tmpfiles="$tmpfiles $canon_output" |
| 187 |
error=0 |
| 188 |
|
| 189 |
# Canonicalize the new test output. |
| 190 |
# shellcheck disable=SC2094 |
| 191 |
eval "$TEST_DIFF_CANONIFIER" "$input" <"$input" >"$canon_output" |
| 192 |
if [ $? -ne 0 ]; then |
| 193 |
echo "== Error ==============================" >>$TEST_DIAGNOSTICS |
| 194 |
echo "btest-diff: TEST_DIFF_CANONIFIER failed on file '$input'" >>$TEST_DIAGNOSTICS |
| 195 |
error=1 |
| 196 |
result=1 |
| 197 |
fi |
| 198 |
|
| 199 |
if [ -n "$baseline" ]; then |
| 200 |
canon_baseline=/tmp/test-diff.$$.$canon.baseline.tmp |
| 201 |
tmpfiles="$tmpfiles $canon_baseline" |
| 202 |
|
| 203 |
# Prepare the baseline. When created by a recent btest-diff, we |
| 204 |
# don't need to re-canonicalize, otherwise we do. |
| 205 |
if ! get_baseline "$baseline" >"$canon_baseline"; then |
| 206 |
# It's an older uncanonicalized baseline, so canonicalize |
| 207 |
# it now prior to comparison. Future updates via btest |
| 208 |
# -U/-u will then store it canonicalized. |
| 209 |
# shellcheck disable=SC2094 |
| 210 |
eval "$TEST_DIFF_CANONIFIER" "$baseline" <"$baseline" >"$canon_baseline" |
| 211 |
if [ $? -ne 0 ]; then |
| 212 |
echo "== Error ==============================" >>$TEST_DIAGNOSTICS |
| 213 |
echo "btest-diff: TEST_DIFF_CANONIFIER failed on file '$baseline'" >>$TEST_DIAGNOSTICS |
| 214 |
error=1 |
| 215 |
result=1 |
| 216 |
fi |
| 217 |
fi |
| 218 |
|
| 219 |
if [ $error -eq 0 ]; then |
| 220 |
echo "== Diff ===============================" >>$TEST_DIAGNOSTICS |
| 221 |
if is_binary_mode; then |
| 222 |
diff -s "$@" "$canon_baseline" "$canon_output" >>$TEST_DIAGNOSTICS |
| 223 |
else |
| 224 |
diff -au "$@" "$canon_baseline" "$canon_output" >>$TEST_DIAGNOSTICS |
| 225 |
fi |
| 226 |
result=$? |
| 227 |
fi |
| 228 |
elif [ "$TEST_MODE" = "TEST" ]; then |
| 229 |
echo "== Error ==============================" >>$TEST_DIAGNOSTICS |
| 230 |
echo "test-diff: no baseline found." >>$TEST_DIAGNOSTICS |
| 231 |
result=100 |
| 232 |
fi |
| 233 |
|
| 234 |
echo "=======================================" >>$TEST_DIAGNOSTICS |
| 235 |
|
| 236 |
if [ "$TEST_MODE" = "TEST" ]; then |
| 237 |
exit $result |
| 238 |
|
| 239 |
elif [ "$TEST_MODE" = "UPDATE_INTERACTIVE" ]; then |
| 240 |
|
| 241 |
# We had a problem running the canonifier |
| 242 |
if [ "$error" != 0 ]; then |
| 243 |
exit 1 |
| 244 |
fi |
| 245 |
|
| 246 |
# There's no change to the baseline, so skip user interaction |
| 247 |
if [ "$result" = 0 ]; then |
| 248 |
exit 0 |
| 249 |
fi |
| 250 |
|
| 251 |
btest-ask-update |
| 252 |
rc=$? |
| 253 |
|
| 254 |
echo -n "$TEST_NAME ..." >/dev/tty |
| 255 |
|
| 256 |
if [ $rc = 0 ]; then |
| 257 |
update_baseline "$canon_output" "$canon" |
| 258 |
exit 0 |
| 259 |
fi |
| 260 |
|
| 261 |
exit $rc |
| 262 |
|
| 263 |
elif [ "$TEST_MODE" = "UPDATE" ]; then |
| 264 |
|
| 265 |
# We had a problem running the canonifier |
| 266 |
if [ "$error" != 0 ]; then |
| 267 |
exit 1 |
| 268 |
fi |
| 269 |
|
| 270 |
update_baseline "$canon_output" "$canon" |
| 271 |
exit 0 |
| 272 |
fi |
| 273 |
|
| 274 |
echo "test-diff: unknown test mode $TEST_MODE" >$TEST_DIAGNOSTICS |
| 275 |
exit 1 |