Skip to content

Commit

Permalink
A shellcheck pass on btest-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ckreibich committed Oct 7, 2020
1 parent 1b3d01b commit f220c54
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions btest-diff
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# TEST_DIFF_CANONIFIER
# TEST_DIFF_BRIEF
# TEST_DIFF_FILE_MAX_LINES
#
# It's okay to check $? explicitly:
# shellcheck disable=SC2181

# Maximum number of lines to show from mismatching input file by default.
MAX_LINES=100
Expand All @@ -22,10 +25,10 @@ MAX_LINES=100
HEADER="### btest baseline data generated by btest-diff. Do not edit. Use btest -U/-u to update."

# Predicate, succeeds if the given baseline is canonicalized.
function is_canon_baseline {
is_canon_baseline() {
local input="$1"

if head -1 "$input" | grep -q -F "$HEADER" 2>/dev/null; then
if head -n 1 "$input" | grep -q -F "$HEADER" 2>/dev/null; then
return 0
fi

Expand All @@ -34,7 +37,7 @@ function is_canon_baseline {

# Prints the requested baseline to standard out if it is
# canonicalized. Otherwise, fails.
function get_canon_baseline {
get_canon_baseline() {
local input="$1"

! is_canon_baseline "$input" && return 1
Expand All @@ -43,7 +46,7 @@ function get_canon_baseline {

# Updates the given baseline to the given destination, prepending our
# header.
function update_canon_baseline {
update_canon_baseline() {
local input="$1"
local output="$2"

Expand All @@ -64,17 +67,18 @@ if [ "$#" -lt 1 ]; then
exit 1
fi

input=$1
canon=`echo $input | sed 's#/#.#g'`
input="$1"
# shellcheck disable=SC2001
canon=$(echo "$input" | sed 's#/#.#g')
shift

if [ ! -f $input ]; then
if [ ! -f "$input" ]; then
echo "btest-diff: input $input does not exist." >$TEST_DIAGNOSTICS
exit 1
fi

tmpfiles=""
function delete_tmps {
delete_tmps() {
rm -f $tmpfiles 2>/dev/null
}

Expand All @@ -86,26 +90,26 @@ rm -f $TEST_DIAGNOSTICS 2>/dev/null

echo "== File ===============================" >>$TEST_DIAGNOSTICS

if [ ! -e $TEST_BASELINE/$canon ]; then
cat $input >>$TEST_DIAGNOSTICS
if [ ! -e "$TEST_BASELINE/$canon" ]; then
cat "$input" >>$TEST_DIAGNOSTICS
elif [ -n "$TEST_DIFF_BRIEF" ]; then
echo "<Content not shown>" >>$TEST_DIAGNOSTICS
else
if [ `wc -l $input | awk '{print $1}'` -le $MAX_LINES ]; then
cat $input >>$TEST_DIAGNOSTICS
if [ "$(wc -l "$input" | awk '{print $1}')" -le "$MAX_LINES" ]; then
cat "$input" >>$TEST_DIAGNOSTICS
else
head -$MAX_LINES $input >>$TEST_DIAGNOSTICS
head -n "$MAX_LINES" "$input" >>$TEST_DIAGNOSTICS
echo "[... File too long, truncated ...]" >>$TEST_DIAGNOSTICS
fi
fi

if [ -e $TEST_BASELINE/$canon ]; then
if [ -e "$TEST_BASELINE/$canon" ]; then
error=0

# If no canonifier is defined, make it a runnable
# no-op. Simplifies code layout.
if [ -z "$TEST_DIFF_CANONIFIER" ]; then
TEST_DIFF_CANONIFIER=cat
TEST_DIFF_CANONIFIER="cat"
fi

canon_baseline=/tmp/test-diff.$$.$canon.baseline.tmp
Expand All @@ -114,11 +118,11 @@ if [ -e $TEST_BASELINE/$canon ]; then

# Prepare the baseline. When created by a recent btest-diff, we
# don't need to re-canonicalize, otherwise we do.
if ! get_canon_baseline $TEST_BASELINE/$canon >$canon_baseline; then
if ! get_canon_baseline "$TEST_BASELINE/$canon" >"$canon_baseline"; then
# It's an older uncanonicalized baseline, so canonicalize
# it now prior to comparison. Future updates via btest
# -U/-u will then store it canonicalized.
eval $TEST_DIFF_CANONIFIER <$TEST_BASELINE/$canon >$canon_baseline
eval "$TEST_DIFF_CANONIFIER" <"$TEST_BASELINE/$canon" >"$canon_baseline"
if [ $? -ne 0 ]; then
error=1
echo "== Error ==============================" >>$TEST_DIAGNOSTICS
Expand All @@ -128,7 +132,8 @@ if [ -e $TEST_BASELINE/$canon ]; then
fi

# Canonicalize the new test output
eval $TEST_DIFF_CANONIFIER $input <$input >$canon_output
# shellcheck disable=SC2094
eval "$TEST_DIFF_CANONIFIER" "$input" <"$input" >"$canon_output"
if [ $? -ne 0 ]; then
error=1
echo "== Error ==============================" >>$TEST_DIAGNOSTICS
Expand All @@ -138,7 +143,7 @@ if [ -e $TEST_BASELINE/$canon ]; then

if [ $error -eq 0 ]; then
echo "== Diff ===============================" >>$TEST_DIAGNOSTICS
diff -au $@ $canon_baseline $canon_output >>$TEST_DIAGNOSTICS
diff -au "$@" "$canon_baseline" "$canon_output" >>$TEST_DIAGNOSTICS
result=$?
fi
else
Expand All @@ -149,12 +154,12 @@ fi

echo "=======================================" >>$TEST_DIAGNOSTICS

if [ "$TEST_MODE" == "TEST" ]; then
if [ "$TEST_MODE" = "TEST" ]; then
exit $result

elif [ "$TEST_MODE" == "UPDATE_INTERACTIVE" ]; then
elif [ "$TEST_MODE" = "UPDATE_INTERACTIVE" ]; then

if [ "$result" == 0 ]; then
if [ "$result" = 0 ]; then
exit 0
fi

Expand All @@ -167,16 +172,16 @@ elif [ "$TEST_MODE" == "UPDATE_INTERACTIVE" ]; then
# new baseline. Otherwise, just use the input.
update="${canon_output:-$input}"

if [ $rc == 0 ]; then
update_canon_baseline $update $TEST_BASELINE/$canon
if [ $rc = 0 ]; then
update_canon_baseline "$update" "$TEST_BASELINE/$canon"
exit 0
fi

exit $rc

elif [ "$TEST_MODE" == "UPDATE" ]; then
elif [ "$TEST_MODE" = "UPDATE" ]; then
update="${canon_output:-$input}"
update_canon_baseline $update $TEST_BASELINE/$canon
update_canon_baseline "$update" "$TEST_BASELINE/$canon"
exit 0
fi

Expand Down

0 comments on commit f220c54

Please sign in to comment.